Chart JS插件根据值更改线条颜色

时间:2020-02-12 14:28:16

标签: javascript charts chartjs-2.6.0 react-chartjs

我正在尝试创建折线图,其中线(和点)的颜色取决于所绘制的值。例如,如果该值高于以下阈值[0, 115, 125],则颜色分别为['green', 'yellow', 'red']

该要求与本示例中实现的要求几乎相同:https://jsfiddle.net/egamegadrive16/zjdwr4fh/

区别在于我正在使用react-chart-js-2,因此无法以相同的方式访问draw()方法。相反,建议创建一个插件来操纵图表。

这是目前的插件代码:

import { Chart } from "react-chartjs-2";

class variableLineColorUtils {
  selectColor(value, thresholds, colors) {
    let color = colors[0];
    thresholds.every((limit, index) => {
      if (value < limit) return false;
      else color = colors[index];
      return true;
    });

    return color;
  }
}

const variableLineColor = {
  id: "variableLineColor",
  afterDraw: (chart, easing) => {
    const options = chart.options.variableLineColor;
    if (options) {
      const utils = new variableLineColorUtils();
      const datasets = chart.config.data.datasets;

      datasets.forEach((set, i) => {
        const points = chart.getDatasetMeta(i).data;
        points.forEach((point, index) => {
          const color = utils.selectColor(
            datasets[i].data[point._index],
            options.thresholds,
            options.colors
          );

          point.custom = { borderColor: color, backgroundColor: color };
        });
        chart.update();
      });
    }
  }
};

Chart.pluginService.register(variableLineColor);

export default variableLineColor;

这些是用于插件的options

variableLineColor: {
  thresholds: [0, 115, 125],
  colors: ["green", "yellow", "red"]
}

此方法仅修改点本身的颜色,而不修改点之间的线。该行保留在图表的默认backgroundColor中。

如何修改线条本身的颜色?

4 个答案:

答案 0 :(得分:1)

尝试使用borderColor代替backgroundColor,请参阅ChartJs StackOverflow。它可以解决您的问题。

答案 1 :(得分:1)

为此,您将不得不像检查以下代码那样对数据进行某种处理。我怀疑您是否可以使用该插件。

import React from 'react';
import { Line } from 'react-chartjs-2';

const data = {
  labels: [1, 2, 3, 4, 5, 6, 7, 8, 9],
  datasets: [
    {
      label: 'First one',
      fill: false,
      borderColor: 'gray',
      data: [null, null, 2, 0, 3],
    },
    {
      label: 'Second one',
      fill: false,
      borderColor: 'blue',
      data: [2, 4, 2, null, null, null, null, null, null],
    },
    {
      label: 'Third one',
      fill: false,
      borderColor: 'cyan',
      data: [null, null, null, null, 3, 4, 1, null, null],
    },
  ],
}

function App() {
  return (
    <div>
      <Line
        data={data}
      />
    </div>
  );
}



export default App

答案 2 :(得分:0)

使用borderColor或color代替线条的backgroundColor。

答案 3 :(得分:0)

您可以使用 plugins 数组创建一个新的 beforeRender 插件来完成此操作。

plugins: [{
  beforeRender: (x, options) => {
    const c = x.chart;
    const dataset = x.data.datasets[0];
    const yScale = x.scales['y-axis-0'];
    const yPos = yScale.getPixelForValue(0);

    const gradientFill = c.ctx.createLinearGradient(0, 0, 0, c.height);
    gradientFill.addColorStop(0, 'rgb(86,188,77)');
    gradientFill.addColorStop(yPos / c.height, 'rgb(86,188,77)');
    gradientFill.addColorStop(yPos / c.height, 'rgb(229,66,66)');
    gradientFill.addColorStop(1, 'rgb(229,66,66)');

    const model = x.data.datasets[0]._meta[Object.keys(dataset._meta)[0]].dataset._model;
    model.borderColor = gradientFill;
  },
}];

结果看起来像这样:

enter image description here

这也适用于背景颜色,只需将 model.borderColor 行更改为 model.backgroundColor。例如:

enter image description here