javascript - Highcharts 散点图 - 将数据库中的系列添加为不同的颜色
问题描述
我正在尝试从具有两个系列的数据集构建一个 highcharts 散点图。第一个被称为“inperson”,应该用蓝色绘制,第二个系列被称为“other”,需要用红色绘制。我似乎无法让第二个系列出现在我的图表上。
这是我的代码:
var options = {
chart: {
renderTo: 'container',
type: 'scatter',
plotBorderWidth: 1,
zoomType: 'xy'
},
legend: {
enabled: false
},
tooltip: {
formatter: function () {
return "<br/><br/> " + "Date: " + this.x + "<br/><br/> " + "Yr: "+ this.y + "<br/><br/>" + contact_notes[this.point.index];
}},
xAxis: {
gridLineWidth: 0,
type: 'datetime',
dateTimeLabelFormats: {
day: '%m/%d' //ex- 01 Jan 2016
}
},
yAxis: {
gridLineWidth: 0,
},
plotOptions: {
column: {
dataLabels: {
enabled: true
}
}
},
series: [{}],
};
vardata={
"chart_data": {
"contact_date": [
"01/01",
"02/06",
"04/03",
"08/09",
"10/11",
"12/27"
],
"other": [
2019.0,
2017.0,
2018.0,
2017.0,
2018.0,
2017.0
],
"inperson": [
2019.0,
2018.0,
2017.0,
NaN,
2019.0,
2017.0,
],
"contact_notes": [
"AA",
"BB",
"CC",
"DD",
"EE",
"FF",
]
}
};
options.xAxis.categories = data['chart_data']['contact_date'];
options.series[0].data = data['chart_data']['in-person'];
options.series[0].name = 'in-person'
options.series[0].color = '#3232ff';
var contact_notes = data['chart_data']['contact_notes'];
var chart = new Highcharts.Chart(options);
chart.addSeries({name:'other', color:'#b20000', data: data['chart_data']['other']}, false);
chart.redraw();
Highcharts.chart('container', options);
最后几行(addSeries 和 redraw)似乎不起作用。关于如何修复的任何建议?
谢谢!
解决方案
代码的最后一行(Highcharts.chart
构造函数方法) - 将图表重置为不添加系列的状态。
这是没有调用构造函数的工作JSFiddle 演示。
PS顺便说一句,您的代码中有两个拼写错误:vardata
应该options.series[0].data = data['chart_data']['in-person'];
访问['inperson']
推荐阅读
- javascript - 模拟按键进入没有 jQuery
- javascript - 如何选择然后对 MongoDB 文档进行排序,然后找到下一个要挖掘的文档?
- node.js - JS 问题:我怎样才能做到这一点,以便当我在字符串中检测到一个单词时,我会检查独立的单词,比如 Hello
- wordpress - Wordpress 如何将页面链接到博客内容
- python - 在 python 中通过 LXML 和 Pandas 将 CSV 转换为 XML
- python - 如何比较 django 中的 html 输入值?
- python-3.x - 你能用 ttk.Button() 实现 OSX-Native Tab+Space Navigation 吗?
- node.js - 我可以在我的项目和特定库之间使用不同的节点版本吗?
- swift - 如何使用 RxSwift、RxCocoa 使用 SkeltonView
- azure - 是否可以在 Azure 服务总线中同步接收订阅中的消息