gpt4 book ai didi

javascript - 在 dygraph 中创建图表时是否可以忽略 0 值

转载 作者:行者123 更新时间:2023-11-28 08:38:44 24 4
gpt4 key购买 nike

<html>
<head>
<script type="text/javascript"
src="dygraph-combined.js"></script>
</head>
<body>
<div id="graphdiv"></div>
<script type="text/javascript">
g = new Dygraph(

// containing div
document.getElementById("graphdiv"),

// CSV or path to a CSV file.
"Date,Duration,Error\n" +
"2008-05-07,75,23\n" +
"2008-05-08,70,45\n" +
"2008-05-09,70,0\n" +
"2008-05-10,70,23\n" +
"2008-05-11,70,11\n" +
"2008-05-12,70,\n" +
"2008-05-13,80,33\n",
{
// options go here. See http://dygraphs.com/options.html
legend: 'always',
animatedZooms: true,
title: 'dygraphs chart template',
includeZero: false
}
);
</script>
</body>
</html>

图表是否可以忽略零或空值,以便它直接从 45 到 23 或 11 到 33 连接。在 Excel 中,我们的选项是否存在此选项? includedZero 似乎没有任何影响。

最佳答案

真正简单的答案是删除不需要或不需要值的逗号。

举个例子

"2008-05-09,70,0\n"+
//Should be "2008-05-09,70\n"+

"2008-05-12,70,\n"+
//Should be "2008-05-12,70\n"+

但是,如果您确实无法修改传递给 DyGraphs 的数据,那么您有两个选择

创建图表,提取数据,然后使用 array.pop() 删除等于 0 或 null 的列值,然后使用 updateOptions() 将修改后的数据重新加载回 Dygraphs。您可以使用我在回答here中描述的方法作为起点

或者

创建一个自定义绘图仪(如下所示),仅绘制一条到下一个非零/空值的线。以上两个是更好的解决方案,因为 Dygraphs 仍然会认为在某些时间点存在零值(因为它毕竟在您提供的数据中)。

//Define the custom plotter
var custom_plotter = function(e) {
var context = e.drawingContext;
var points = e.points;
var num_points = points.length;

context.beginPath(); //Start

//Move "paint brush" to the start location
context.moveTo(points[0].canvasx, points[0].canvasy);

var p; //var to store current point
for(var i = 1; i < num_points; i++) {
p = points[i];


if(p.yval != 0 && p.yval != null) {
//Map out a line if the current value is desirable
context.lineTo(p.canvasx, p.canvasy);
}
}
context.stroke(); //Actually draw the lines mapped out in the loop above
context.closePath(); //Close
}

g = new Dygraph(

// containing div
document.getElementById("graphdiv"),

// CSV or path to a CSV file.
"Date,Duration,Error\n" +
"2008-05-07,75,23\n" +
"2008-05-08,70,45\n" +
"2008-05-09,70,0\n" +
"2008-05-10,70,23\n" +
"2008-05-11,70,11\n" +
"2008-05-12,70,\n" +
"2008-05-13,80,33\n",
{
// options go here. See http://dygraphs.com/options.html
legend: 'always',
animatedZooms: true,
title: 'dygraphs chart template',
plotter: custom_plotter, //Pass Dygraphs the custom plotter defined above
}
);

关于javascript - 在 dygraph 中创建图表时是否可以忽略 0 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20768594/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com