繁体   English   中英

dygraph改变线条颜色

[英]dygraph change line color

我想更改带有值的 dygraph 图表的线条颜色。
例如,当我的数据大于 2 时,我想更改颜色。

我尝试选项颜色,但只是更改点颜色。

在这里制作了一个JsFiddle

这是我的选项dygraph:

color: function (color, d) {
    if (typeof d.index === 'undefined') { return color; }

    var val = columns[0][d.index + 1];

    if (val >= 0 && val <= 150) {
        color = 'green';
    } else if (val > 150 && val <= 300) {
        color = 'yellow';
    } else if (val > 300) {
        color = 'red';
    }

    return color;
}

正如您所发现的,您无法为函数设置color 不过,您可以使用稍微更通用的drawPointCallback选项,它可以让您为每个点绘制任何颜色的任何形状:

function coloredCircle(g, series, ctx, cx, cy, color, radius, idx) {
  var y = g.getValue(idx, 1);
  var pointColor = y < 5 ? 'green' : 'blue';
  ctx.save();
  ctx.fillStyle = pointColor;
  ctx.beginPath();
  ctx.arc(cx, cy, radius, 2 * Math.PI, false);
  ctx.closePath();
  ctx.fill();
}

g = new Dygraph(
    div, data,
    {
      series: {
        Y: {
          drawPointCallback: coloredCircle,
          pointSize: 5,
          drawPoints: true
        }
      }
    });

这是一个完整的 fiddle 有关drawPointCallback更多示例,请参阅自定义圆圈演示

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM