javascript - 以下代码有什么问题?为什么图表不显示
问题描述
我正在尝试使用外部 JSON 文件中的 canvasjs 制作折线图。JSON 文件由 Date、high、open、low、vol 和 price 组成。图表中需要显示的是日期、最高价、开盘价和最低价。
这就是我所做的:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<script>
window.onload = function () {
var dataPoints1 = [];
var dataPoints2 = [];
var dataPoints3 = [];
var chart = new CanvasJS.Chart("chartContainer", {
title:{
text: "Data"
},
axisX:{
title:"Date"
},
axisY:[{
title: "Open",
lineColor: "#C24642",
tickColor: "#C24642",
labelFontColor: "#C24642",
titleFontColor: "#C24642"
},
{
title: "High",
lineColor: "#369EAD",
tickColor: "#369EAD",
labelFontColor: "#369EAD",
titleFontColor: "#369EAD"
}],
axisY2: {
title: "Low",
lineColor: "#7F6084",
tickColor: "#7F6084",
labelFontColor: "#7F6084",
titleFontColor: "#7F6084"
},
toolTip: {
shared: true
},
legend: {
cursor: "pointer",
itemclick: toggleDataSeries
},
data: [{
type: "line",
name: "High",
color: "#369EAD",
showInLegend: true,
axisYIndex: 1,
dataPoints: dataPoints1
},
{
type: "line",
name: "Open",
color: "#C24642",
axisYIndex: 0,
showInLegend: true,
dataPoints: dataPoints2
},
{
type: "line",
name: "Low",
color: "#7F6084",
axisYType: "secondary",
showInLegend: true,
dataPoints: dataPoints3
}]
});
$.getJSON("q_data.json", callback);
function callback(data) {
for (var i = 0; i < data.length; i++) {
dataPoints1.push({
x: data[i].Date,
y: data[i].open
});
dataPoints2.push({
x: data[i].Date,
y: data[i].high
});
dataPoints3.push({
x: data[i].Date,
y: data[i].low
});
}
chart.render();
}
function toggleDataSeries(e) {
if (typeof (e.dataSeries.visible) === "undefined" || e.dataSeries.visible) {
e.dataSeries.visible = false;
} else {
e.dataSeries.visible = true;
}
e.chart.render();
}
}
</script>
</head>
<body>
<div id="chartContainer" style="height: 370px; max-width: 920px; margin: 0px auto;"></div>
<script src="https://canvasjs.com/assets/script/jquery-1.11.1.min.js></script>
<script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
</body>
</html>
我希望它显示绘制的折线图,但它只显示图表的 y 轴、x 轴和标题。不显示错误消息。
解决方案
CanvasJS 仅支持x 值中的数字和日期时间,而 x 值是您共享的示例 JSON 中的字符串。在解析 JSON 时将其更改为 date-object 应该可以正常工作。
var dataPoints1 = [];
var dataPoints2 = [];
var dataPoints3 = [];
var chart = new CanvasJS.Chart("chartContainer", {
title:{
text: "Data"
},
axisX:{
title:"Date"
},
axisY:[{
title: "Open",
lineColor: "#C24642",
tickColor: "#C24642",
labelFontColor: "#C24642",
titleFontColor: "#C24642"
},{
title: "High",
lineColor: "#369EAD",
tickColor: "#369EAD",
labelFontColor: "#369EAD",
titleFontColor: "#369EAD"
}],
axisY2: {
title: "Low",
lineColor: "#7F6084",
tickColor: "#7F6084",
labelFontColor: "#7F6084",
titleFontColor: "#7F6084"
},
toolTip: {
shared: true
},
legend: {
cursor: "pointer",
itemclick: toggleDataSeries
},
data: [{
type: "line",
name: "High",
color: "#369EAD",
showInLegend: true,
axisYIndex: 1,
dataPoints: dataPoints1
},{
type: "line",
name: "Open",
color: "#C24642",
axisYIndex: 0,
showInLegend: true,
dataPoints: dataPoints2
},{
type: "line",
name: "Low",
color: "#7F6084",
axisYType: "secondary",
showInLegend: true,
dataPoints: dataPoints3
}]
});
$.getJSON("https://api.myjson.com/bins/1gfuo7", callback);
function callback(data) {
for (var i = 0; i < data.length; i++) {
dataPoints1.push({
x: new Date(data[i].Date),
y: data[i].open
});
dataPoints2.push({
x: new Date(data[i].Date),
y: data[i].high
});
dataPoints3.push({
x: new Date(data[i].Date),
y: data[i].low
});
}
chart.render();
}
function toggleDataSeries(e) {
if (typeof (e.dataSeries.visible) === "undefined" || e.dataSeries.visible) {
e.dataSeries.visible = false;
} else {
e.dataSeries.visible = true;
}
e.chart.render();
}
<div id="chartContainer" style="height: 250px; max-width: 920px; margin: 0px auto;"></div>
<script src="https://canvasjs.com/assets/script/jquery-1.11.1.min.js"></script>
<script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
推荐阅读
- java - 不能比较两个arraylist对象
- azure - Azure 中的用户分析
- reactjs - 在 React 中单击按钮时重定向
- excel - 在Odoo 13中导入考勤模块的excel文件的考勤数据丢失了,如何解决?
- angular - 角度反应形式:如何在对话框中显示验证错误?
- python - 是否可以在使用 excel 打开之前格式化 .tsv 文件中的单个单元格?(通过python?)
- javascript - 使用 ActiveX 的 Outlook 约会
- javascript - 如何从 mountebank 响应块中的另一个 js 文件调用函数
- amazon-s3 - 在 AWS S3 的子目录中创建目录是错误的
- node.js - 如何检查我的 Node.js create-react-app 是否在 localhost 上运行?