Reputation: 324
I'm looking for a way to parse my json to a simple js line chart. I tried ChartJS but had no success, than I tried Google Charts.
The problem is that I have a long JSON to parse and couldn't figure out a way to filter it and parse to Google Chart.
I'd like to filter the JSON and get the values and dates for only one node (for example: get the values for ABEV3
in the JSON below).
The JSON:
{
"RAIL3": {
"2021-02-12": "20.18",
"2021-02-11": "19.81",
"2021-02-10": "20.13",
"2021-02-09": "20.85",
"2021-02-08": "21.35",
"2021-02-05": "21.35",
"2021-02-04": "21.50",
"2021-02-03": "21.70",
"2021-02-02": "20.77",
"2021-02-01": "21.10",
"2021-01-29": "20.30",
"2021-01-28": "20.92",
"2021-01-27": "20.74",
"2021-01-26": "20.67",
"2021-01-22": "20.85",
"2021-01-21": "20.89",
"2021-01-20": "21.03",
"2021-01-19": "21.10",
"2021-01-18": "20.92",
"2021-01-15": "21.21",
"2021-01-14": "20.74"
},
"ABEV3": {
"2021-02-12": "14.79",
"2021-02-11": "14.95",
"2021-02-10": "15.01",
"2021-02-09": "14.96",
"2021-02-08": "14.94",
"2021-02-05": "15.52",
"2021-02-04": "15.60",
"2021-02-03": "15.68",
"2021-02-02": "15.61",
"2021-02-01": "15.60",
"2021-01-29": "15.11",
"2021-01-28": "15.54",
"2021-01-27": "15.39",
"2021-01-26": "15.40",
"2021-01-22": "15.15",
"2021-01-21": "15.61",
"2021-01-20": "16.02",
"2021-01-19": "16.27",
"2021-01-18": "16.22",
"2021-01-15": "15.95",
"2021-01-14": "16.26"
},
"BBAS3": {
"2021-02-12": "33.75",
"2021-02-11": "33.94",
"2021-02-10": "33.81",
"2021-02-09": "34.28",
"2021-02-08": "33.87",
"2021-02-05": "33.96",
"2021-02-04": "34.19",
"2021-02-03": "34.33",
"2021-02-02": "34.06",
"2021-02-01": "34.29",
"2021-01-29": "33.86",
"2021-01-28": "34.54",
"2021-01-27": "33.59",
"2021-01-26": "32.79",
"2021-01-22": "33.69",
"2021-01-21": "34.18",
"2021-01-20": "34.53",
"2021-01-19": "35.32",
"2021-01-18": "35.93",
"2021-01-15": "36.30",
"2021-01-14": "37.46"
}
}
The code:
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
// Load the Visualization API and the piechart package.
google.charts.load('current', {'packages':['corechart']});
// Set a callback to run when the Google Visualization API is loaded.
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var jsonData = $.ajax({
url: "jsonfile.json",
dataType: "json",
async: false
}).responseText;
// Create our data table out of JSON data loaded from server.
var data = new google.visualization.DataTable(jsonData);
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.Line(document.getElementById('chart_div'));
chart.draw(data, {width: 400, height: 400});
}
</script>
<div id="chart_div" style="width: 100%; height: 100%"></div>
Upvotes: 1
Views: 952
Reputation: 201388
responseJSON
can be used instead of responseText
.ABEV3
, you can retrieve the values of ABEV3
with jsonData.ABEV3
.google.visualization.Line
to google.visualization.LineChart
.When above points are reflected to your script, it becomes as follows.
Please modify drawChart()
as follows.
function drawChart() {
var jsonData = $.ajax({
url: "jsonfile.json",
dataType: "json",
async: false
}).responseJSON;
var key = "ABEV3"; // Please set the key you want to use.
var ar = Object.entries(jsonData[key]).map(([a, b]) => [a, Number(b)]);
var data = new google.visualization.DataTable();
data.addColumn('string', 'date');
data.addColumn('number', 'value');
data.addRows(ar);
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, {width: 400, height: 400});
}
url: "jsonfile.json",
can return the JSON data in your question. Please be careful this.Upvotes: 1