django - Highcharts 范围选择器按钮和输入过滤器不起作用
问题描述
我在 Django 中使用 Highcharts 创建了一个图表,但范围选择器按钮不起作用,输入范围从 1970 年开始,而不是我的第一个日期。我想这与日期格式有关,但我无法弄清楚...
我正在读取一个 JSON 文件内容,其中包含以毫秒为单位的日期条目,例如:1527465600000。
图表显示得很好,图表底部的范围选择器也工作得很好,并且 X 轴上的日期按预期很好地格式化了。我想要的是范围选择器按钮工作,并且范围选择器输入过滤器从我的第一个日期开始,而不是从 1970 年 1 月 1 日开始。
这是我的highcharts代码:
{% block javascripts %}
{% load static %}
<script src="https://code.highcharts.com/stock/highstock.js"></script>
<script>
fetch("{% static 'highcharts_1.json' %}")
.then(response => response.json())
.then(mydata => {
console.log(mydata.sample1,mydata.dates)
Highcharts.chart('mychart_1', {
chart: {
zoomType: 'x',
type: 'spline',
},
xAxis: {
type: 'category',
categories: mydata.dates,
labels:{
formatter: function() {
return Highcharts.dateFormat('%d %b %Y',
this.value);
},
align: 'right',
rotation: -90,
},
},
yAxis: {
min: 0,
max: 1,
tickInterval: 0.1,
title: {
text: 'Score'
}
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'middle'
},
credits : {
enabled : false
},
navigator :{
enabled: true
},
scrollbar :{
enabled: true
},
rangeSelector: {
enabled: true,
allButtonsEnabled: true,
inputEnabled: true,
buttons: [{
type: 'month',
count: 1,
text: '1m'
}, {
type: 'month',
count: 3,
text: '3m'
}, {
type: 'month',
count: 6,
text: '6m'
}, {
type: 'ytd',
text: 'YTD'
}, {
type: 'all',
text: 'All'
}],
selected: 3
},
series: [{
name: 'sample1',
data: mydata.sample1,
}],
plotOptions: {
series: {
label: {
connectorAllowed: false,
},
},
},
responsive: {
rules: [{
condition: {
maxWidth: 1000
},
chartOptions: {
legend: {
layout: 'horizontal',
align: 'center',
verticalAlign: 'bottom'
}
}
}]
}
});
});
</script>
{% endblock javascripts %}
谢谢你的帮助 !这是我正在使用的代码的小提琴:https ://jsfiddle.net/ssoj_tellig/408xoat9/13/
解决方案
如果你想让 rangeSelector 工作,你需要改变你的数据格式:
const data = [
[x, y],
[x, y],
...
]
其中 x 是时间格式。基于您的演示:
var mydates = [
[1527465600000, 29.9],
[1528070400000, 71.5],
[1528675200000, 106.4],
[1529280000000, 129.2],
[1529884800000, 144.0],
[1530489600000, 176.0],
[1531094400000, 135.6],
[1531699200000, 148.5],
[1532304000000, 216.4],
[1532908800000, 194.1],
[1533513600000, 95.6],
[1534118400000, 54.4]
];
推荐阅读
- javascript - 未捕获的类型错误:无法读取 HTMLAnchorElement 处未定义的属性“顶部”。
- django - Django LOGGING 不打印到控制台和文件
- reactjs - Rematch/Reducers - 编写测试用例时出错
- android - [Android] Plaid App 中的 LiveData 混淆
- python - Python3 打印CR和一些文本时,文本没有被擦除
- bash - linux将输出存储到变量的概念是什么?
- java - 从服务器通过 API 传输数据完成后,如何使用远程数据构建 recyclerview
- java - Quarkus Panache Hibernate:如何清除缓存,使实体世界匹配数据库状态?
- javascript - 首次使用后删除该功能
- excel - 包含 INC 后跟一系列数字的过滤器