Search code examples
javascriptreactjsreact-chartjs

React-chartjs ignoring options


I have got this code using react-chartjs. It displays the graph but the options get ignored everytime no matter what I put there. Is there another way you are supposed to add the options into the component?

import React, { PropTypes } from 'react'
var LineChart = require("react-chartjs").Line;

var data = {
  labels: ['M', 'T', 'W', 'T', 'F', 'S', 'S'],
  datasets: [{
    label: 'apples',
    data: [12, 19, 3, 17, 6, 3, 7]
  }]
};

var options = {
  lineTension : 0,
  fill : false,
  showLines : false 
};

const Graph = () => (
  <span>
    <h1>Heart Graph</h1>
    <LineChart  data={data} options={options} width="600" height="250"/>
  </span>
)

export default Graph

Solution

  • You're using a "dataset structure" so check out the docs on options for Line Chart Datasets.

    They suggest you should send the "options" along with your data object - in this case to the Components data prop.

    var data = {
        labels: ["January", "February", "March", "April", "May", "June", "July"],
        datasets: [
            {
                label: "My First dataset",
                data: ['data1', 'data2'],
                fill: false,
                lineTension: 0.1,
                ...
    

    Alternatively, you can provide a more complex option object with Axis settings to the option prop.

    options: {
        scales: {
            xAxes: [{
                type: 'linear',
                position: 'bottom'
            }]
        }
    }