I'm trying to pass an array as data from a json file:
[
{
"label": "label 1",
"color": "#FFF000",
"data": [
{"x": "Day 1", "y": 69},
{"x": "Day 2", "y": 71},
{"x": "Day 3", "y": 45},
{"x": "Day 4", "y": 72},
]
}, {
"label": "Overall Score",
"color": "#444001",
"datapoints": [
{"x": "Day 1", "y": 48},
{"x": "Day 2", "y": 57},
{"x": "Day 3", "y": 50},
{"x": "Day 4", "y": 80, "tooltip": "Good Job!"}
]
} ]
With Chartjs and according to the using array values in chart.js data and label field my component code is as follows:
import React, {Component} from 'react';
import {Line} from 'react-chartjs-2';
var topdata = this.props.graphData[0].map((graph, i) => {
var color_top_data = graph.color;
var label_top_data = graph.label;
graph.datapoints.map((row, i) => {
var day_top_data = row.x;
var value_top_data = row.y;
});
});
class LineChart extends Component {
render() {
return (
<div>
<Line
data={{
labels: day_top_data,
datasets: [
{
data: value_top_data,
backgroundColor: color_top_data,
fill: false,
lineTension: 0.4,
}]
}} />
</div>
);
}
}
export default LineChart;
But in react it fails to compile cause color_top_data, day_top_data, value_top_data
are not defined. Can someone help with this?