Highcharts 数据导入 JSON - 格式化
Highcharts data import JSON - formatting
我正在尝试获取一些数据以使用 Highcharts 渲染散点图。这是数据示例。
{ "results": [ [ "2013-04-14 10:00", -1 ],
[ "2013-04-14 11:48", 0 ],
[ "2013-04-14 14:45", -1 ],
[ "2013-04-14 18:02", -1 ],
[ "2013-04-14 18:45", -1 ],
[ "2013-04-14 19:57", -1 ],
[ "2013-04-14 22:19", 0 ],
[ "2013-04-14 23:00", 0 ],
[ "2013-04-14 23:00", -1 ],
[ "2013-04-14 23:00", 0 ],
[ "2013-04-14 23:00", 0 ],
[ "2013-04-14 23:00", 0 ],
[ "2013-04-14 23:00", 0 ],
[ "2013-04-14 23:00", 0 ],
[ "2013-04-14 23:00", 0 ],
[ "2013-04-15 00:18", 0 ],
[ "2013-04-15 00:21", 0 ],
[ "2013-04-15 00:22", 0 ],
[ "2013-04-15 00:24", 1 ],
[ "2013-04-15 04:15", -1 ],
[ "2013-04-15 09:02", -1 ]
]
}
这是js:
$(document).ready(function() {
var options = {
chart: {
renderTo: 'container',
type: 'scatter',
zoomType: 'xy'
},
title: {
text: 'Sentiment Report by Publish Date'
},
subtitle: {
text: 'Written by Kat Chilton for Oxyme'
},
xAxis: {
title: {
text: 'Publish Date'
},
type: 'datetime'
},
yAxis: {
min: 0,
title: {
text: 'Sentiment',
align: 'high'
},
labels: {
overflow: 'justify'
}
},
plotOptions: {
scatter: {
marker: {
radius: 5,
states: {
hover: {
enabled: true,
lineColor: 'rgb(0,0,0)'
}
}
},
states: {
hover: {
marker: {
enabled: false
}
}
},
allowPointSelect: true,
dashStyle: 'dot',
}
},
legend: {
layout: 'vertical',
align: 'left',
verticalAlign: 'top',
y: 0,
floating: true,
borderWidth: 1,
backgroundColor: ((Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF'),
shadow: true
},
credits: {
enabled: false
},
series: [{}]
};
$.getJSON('sample.json', function(data) {
options.series[0].data = data;
var chart = new Highcharts.Chart(options);
});
我在页面上没有得到任何分数,但图表确实显示了标题,但没有。我认为这是数据格式的问题,但我似乎无法找到原因。有人可以帮忙吗?
您需要从 data
获取 results
属性,像这样
options.series[0].data = data.results;
我正在尝试获取一些数据以使用 Highcharts 渲染散点图。这是数据示例。
{ "results": [ [ "2013-04-14 10:00", -1 ],
[ "2013-04-14 11:48", 0 ],
[ "2013-04-14 14:45", -1 ],
[ "2013-04-14 18:02", -1 ],
[ "2013-04-14 18:45", -1 ],
[ "2013-04-14 19:57", -1 ],
[ "2013-04-14 22:19", 0 ],
[ "2013-04-14 23:00", 0 ],
[ "2013-04-14 23:00", -1 ],
[ "2013-04-14 23:00", 0 ],
[ "2013-04-14 23:00", 0 ],
[ "2013-04-14 23:00", 0 ],
[ "2013-04-14 23:00", 0 ],
[ "2013-04-14 23:00", 0 ],
[ "2013-04-14 23:00", 0 ],
[ "2013-04-15 00:18", 0 ],
[ "2013-04-15 00:21", 0 ],
[ "2013-04-15 00:22", 0 ],
[ "2013-04-15 00:24", 1 ],
[ "2013-04-15 04:15", -1 ],
[ "2013-04-15 09:02", -1 ]
]
}
这是js:
$(document).ready(function() {
var options = {
chart: {
renderTo: 'container',
type: 'scatter',
zoomType: 'xy'
},
title: {
text: 'Sentiment Report by Publish Date'
},
subtitle: {
text: 'Written by Kat Chilton for Oxyme'
},
xAxis: {
title: {
text: 'Publish Date'
},
type: 'datetime'
},
yAxis: {
min: 0,
title: {
text: 'Sentiment',
align: 'high'
},
labels: {
overflow: 'justify'
}
},
plotOptions: {
scatter: {
marker: {
radius: 5,
states: {
hover: {
enabled: true,
lineColor: 'rgb(0,0,0)'
}
}
},
states: {
hover: {
marker: {
enabled: false
}
}
},
allowPointSelect: true,
dashStyle: 'dot',
}
},
legend: {
layout: 'vertical',
align: 'left',
verticalAlign: 'top',
y: 0,
floating: true,
borderWidth: 1,
backgroundColor: ((Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF'),
shadow: true
},
credits: {
enabled: false
},
series: [{}]
};
$.getJSON('sample.json', function(data) {
options.series[0].data = data;
var chart = new Highcharts.Chart(options);
});
我在页面上没有得到任何分数,但图表确实显示了标题,但没有。我认为这是数据格式的问题,但我似乎无法找到原因。有人可以帮忙吗?
您需要从 data
获取 results
属性,像这样
options.series[0].data = data.results;