javascript - dygraph 改变线条颜色

标签 javascript dygraphs

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

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

我做了一个 JsFiddle here .

这是我的选项绘图:

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 option , 但是,它允许您为每个点绘制任何颜色的任何形状:

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
        }
      }
    });

这是一个 fully-worked fiddle .查看custom-circles demo drawPointCallback 的更多示例。

关于javascript - dygraph 改变线条颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48683519/

相关文章:

javascript - Meteor - 使用 Meteor.call 的返回值时出现问题

javascript - 为什么 ECMAScript 第 4 版被完全废弃了?

javascript - Bootstrap DateTimePicker 将时间设置为午夜

javascript - 绘图 : stackedgraph is used per-series?

javascript - 如何使用D3对 Angular 函数绘制曲线?

javascript - 用于检查单击了哪个按钮的脚本不起作用

javascript - 如何修复 Dygraphs 范围选择错误?

r - dygraphs/highcharter 在两个图上突出显示 - 交互性

r - 将Dygraph中的y轴更改为不是科学计数法

pie-chart - 我们如何使用 dygraph 制作饼图