[英]Parse JSON to Google Charts
我正在尋找一種將我的 json 解析為簡單 js 折線圖的方法。 我嘗試了 ChartJS 但沒有成功,然后我嘗試了 Google Charts。
問題是我有一個很長的 JSON 來解析並且無法找到過濾它並解析到谷歌圖表的方法。
我想過濾 JSON 並僅獲取一個節點的值和日期(例如:在下面的 JSON 中獲取ABEV3
的值)。
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"
}
}
編碼:
<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>
responseJSON
代替responseText
。ABEV3
的值,可以使用jsonData.ABEV3
檢索ABEV3
的值。google.visualization.Line
修改為google.visualization.LineChart
。當以上幾點反映到您的腳本時,它變成如下。
請修改drawChart()
如下。
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",
可以返回您問題中的 JSON 數據。 請注意這一點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.