javascript 如何将缺失数据的 highcharts 默认设置为 0
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10395061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make highcharts default to 0 for missing data
提问by Wraithan
I have a time series at a 1 minute interval. I would like to display that in a chart with missing points as 0.
我有一个间隔 1 分钟的时间序列。我想将其显示在缺失点为 0 的图表中。
I've found xAxis.ordinal and turned that off which displays the time series properly spaced out. The issue is that it draws lines between the points directly without going to 0 for the missing data.
我找到了 xAxis.ordinal 并将其关闭,以正确间隔显示时间序列。问题是它直接在点之间画线,而没有为缺失的数据设置为 0。
采纳答案by eolsson
One way is by pre-processing the data, replacing null
with 0
:
一种方法是预处理数据,替换null
为0
:
var withNulls = [null, 12, 23, 45, 3.44, null, 0];
var noNulls = withNulls.map(function (item) {
return (item === null) ? 0 : item;
});
Example saved on jsfiddle: http://jsfiddle.net/7mhMP/
保存在 jsfiddle 上的示例:http: //jsfiddle.net/7mhMP/
回答by Justus Romijn
I've thorougly searched the API reference of HighCharts, but they don't offer that option. What they do offer is the option to disable the connection of points when null
data is in between (only line and area charts):
我已经彻底搜索了 HighCharts 的API 参考,但他们不提供该选项。他们提供的是当null
数据介于两者之间时禁用点连接的选项(仅限折线图和面积图):
var chart = new Highcharts.Chart({
plotOptions: {
line: {
connectNulls: false
}
}
});
This is the default setting according to the API, so I'm not sure why your data does connect with null
values in between. Maybe they have changed the defaults lately?
这是根据 API 的默认设置,所以我不确定为什么您的数据确实与null
两者之间的值相关联。也许他们最近更改了默认值?
I feel this is a good representation of your data, because defaulting null
to 0
seems misleading.
我觉得这很好地代表了您的数据,因为默认null
为0
似乎具有误导性。
回答by Aaronlong31
You hava to fill the missed data as 0. Because highchart don't konw xAxis interval.
您必须将丢失的数据填充为 0。因为 highchart 不知道 xAxis 间隔。
If you have a time series at a 1 minuteinterval, you should set every missing minutedata to 0; If you have a time series at a 1 dayinterval, you should set every missing daydata to 0.
如果您有一个以 1分钟为间隔的时间序列,则应将每个缺失的分钟数据设置为 0;如果您有一个间隔为 1天的时间序列,则应将每个缺失的日期数据设置为 0。
回答by osyan
I have the same problem and what i understand is that you should set your data structure in database as VARCHAR and set the default value to null so where you don't have data it becomes NULL. Then it accept null values and highchart don't display and connect missing areas with null
values.
In addition you have to receive data as NULL
and don't change them to ''
.
see this working example:
我有同样的问题,我的理解是您应该将数据库中的数据结构设置为 VARCHAR 并将默认值设置为 null,以便在没有数据的情况下它变为 NULL。然后它接受空值并且 highchart 不显示和连接缺失区域与null
值。
此外,您必须接收数据 asNULL
并且不要将它们更改为''
.
看到这个工作示例:
var chart = new Highcharts.Chart({
chart: {
defaultSeriesType: 'spline', // use line or spline
renderTo: 'container1'
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
series: [{
data: [9, 4,null, 2, 1, 0, 6, 5, 4, 1, 6, 4]
}]
});
hereis the fiddle with two different chart using null
and ''
这是使用null
和的两个不同图表的小提琴''
UPDATE:
If you are using parseINT to push data you should remember that parseINT(NULL)
gives you NAN
so for this bug try to manually push NULL with something like this:
更新:
如果你使用parseInt函数将数据推你应该记住,parseINT(NULL)
让你NAN
所以对于这个错误尝试使用这样的手动推NULL:
var val = parseInt(rows[i]);
if (isNaN(val))
{
val = null;
result.push(val);
}