【发布时间】:2018-08-08 13:02:41
【问题描述】:
我对任何类型的编程语言都非常陌生,但我非常幸运地找到了开始我正在从事的项目所需的东西。我正在尝试使用 Highcharts 从 SQL 数据库为最终用户直观地生成数据。我需要将我的 xAxis 标记长达一年(2018 年 1 月 - 2019 年 1 月、2018 年 2 月 - 2019 年 2 月等),以便在每月有新的每月数据时向左移动并“隐藏”或“裁剪”图表中的旧数据已输入 SQL。
Highcharts.chart('container', {
chart: {
type: 'column'
},
title: {
text: 'Monthly Average Rainfall'
},
subtitle: {
text: 'Source: WorldClimate.com'
},
xAxis: {
categories: [
'Jan 2018',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec',
'Jan 2019'
],
crosshair: true
},
yAxis: {
min: 0,
title: {
text: 'Rainfall (mm)'
}
},
tooltip: {
headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
'<td style="padding:0"><b>{point.y:.1f} mm</b></td></tr>',
footerFormat: '</table>',
shared: true,
useHTML: true
},
plotOptions: {
column: {
pointPadding: 0.2,
borderWidth: 0
}
},
series: [{
name: 'Tokyo',
data: [49.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4, 90]
}, {
name: 'New York',
data: [83.6, 78.8, 98.5, 93.4, 106.0, 84.5, 105.0, 104.3, 91.2, 83.5, 106.6, 92.3, 90]
}, {
name: 'London',
data: [48.9, 38.8, 39.3, 41.4, 47.0, 48.3, 59.0, 59.6, 52.4, 65.2, 59.3, 51.2, 90]
}, {
name: 'Berlin',
data: [42.4, 33.2, 34.5, 39.7, 52.6, 75.5, 57.4, 60.4, 47.6, 39.1, 46.8, 51.1, 90]
}]
});
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/modules/export-data.js"></script>
<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
所以目前我看起来像上面这样,当添加 2019 年 2 月时,它应该将 2018 年 1 月从图表中移出,第一组数据应该是 2018 年 2 月到 2019 年 2 月。
Highcharts.chart('container', {
chart: {
type: 'column'
},
title: {
text: 'Monthly Average Rainfall'
},
subtitle: {
text: 'Source: WorldClimate.com'
},
xAxis: {
categories: [
'Feb 2018',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec',
'Jan',
'February 2019'
],
crosshair: true
},
yAxis: {
min: 0,
title: {
text: 'Rainfall (mm)'
}
},
tooltip: {
headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
'<td style="padding:0"><b>{point.y:.1f} mm</b></td></tr>',
footerFormat: '</table>',
shared: true,
useHTML: true
},
plotOptions: {
column: {
pointPadding: 0.2,
borderWidth: 0
}
},
series: [{
name: 'Tokyo',
data: [49.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4, 90]
}, {
name: 'New York',
data: [83.6, 78.8, 98.5, 93.4, 106.0, 84.5, 105.0, 104.3, 91.2, 83.5, 106.6, 92.3 , 90]
}, {
name: 'London',
data: [48.9, 38.8, 39.3, 41.4, 47.0, 48.3, 59.0, 59.6, 52.4, 65.2, 59.3, 51.2, 90]
}, {
name: 'Berlin',
data: [42.4, 33.2, 34.5, 39.7, 52.6, 75.5, 57.4, 60.4, 47.6, 39.1, 46.8, 51.1, 90]
}]
});
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/modules/export-data.js"></script>
<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
手动编辑月份进出是我目前正在做的事情 - 类似于上面的示例,如果可以自动将其向左移动会很好。提前致谢!
【问题讨论】:
-
那么,如果我理解正确的话,您每个月都会执行一个新查询,从而导致每个系列有 12 个值?而数据是唯一非静态的东西?
-
抱歉,唯一的变化将是添加数据的新月份,当添加新月份时,xAxis 会将数据和标签向左移动。过去几个月的数据在输入后将保持不变
-
这与您在问题中要求的
remove有点冲突。您能否更新您的问题以明确? -
我想我可能以错误的方式使用删除这个词来表达我的目的。我想它更像是在添加新月份时“隐藏”xAxis 上第一个月的数据,或者将其从图表中裁剪出来,因此在任何给定时间只显示 13 个月。这与我的目标非常接近,但 xAxis 上的标签限制为 13 个,使用月份作为 xAxis 标签,每月仅更新一次。 jsfiddle.net/gh/get/library/pure/highcharts/highcharts/tree/…
标签: javascript jquery html highcharts