【问题标题】:Adding line over stacked line chart with ChartJS使用 ChartJS 在堆积折线图上添加线条
【发布时间】:2019-07-10 00:53:18
【问题描述】:

我正在尝试在我的背景线上使用堆叠选项重新创建下面的图表

但我的尝试没有成功,结果如下图所示

$(function() {
  var areaChartCanvas = $('#areaChart').get(0).getContext('2d')

  var areaChartData = {
    labels: ['', '', ''],
    datasets: [{
      backgroundColor: 'transparent',
      borderColor: 'black',
      pointRadius: false,
      data: [32, 12, 28],
      type: 'line'
    }, {
      backgroundColor: 'red',
      pointRadius: false,
      data: [20, 20, 20]
    }, {
      backgroundColor: 'orange',
      pointRadius: false,
      data: [40, 40, 40]
    }, {
      backgroundColor: 'cyan',
      pointRadius: false,
      data: [60, 60, 60]
    }]
  }

  var areaChartOptions = {
    maintainAspectRatio: false,
    responsive: true,
    legend: {
      display: false
    },
    scales: {
      xAxes: [{
        gridLines: {
          display: true,
        }
      }],
      yAxes: [{
        gridLines: {
          display: true,
        },
        stacked: true
      }]
    }
  }
  var areaChart = new Chart(areaChartCanvas, {
    type: 'line',
    data: areaChartData,
    options: areaChartOptions
  })
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.min.js"></script>
<canvas id="areaChart" style="height:250px"></canvas>

理想情况下,我希望能够创建具有不同颜色的“区域”,这些颜色将根据我传递给它的间隔进行堆叠。

例如:

青色 - 20

橙色 - 20

红色 - 20

但目前,我正在做

青色 - 60

橙色 - 40

红色 - 20

【问题讨论】:

    标签: chart.js


    【解决方案1】:

    如果我理解正确的话,我会考虑不同的方法并用Plugin扩展图表[1](灵感来自https://stackoverflow.com/a/49303362/863110

    Chart.pluginService.register({
      beforeDraw: function (chart, easing) {
        if (chart.config.options.fillColor) {
          var ctx = chart.chart.ctx;
          var chartArea = chart.chartArea;
          ctx.save();
          let delta = 0;
          const chartHeight = chartArea.bottom - chartArea.top;
          const bottomBarHeight = chart.height - chartHeight - chartArea.top;
          chart.config.options.fillColor.map(color => {
            const colorHeight = chartHeight * (color[0] / 100);
            const colorBottom = chartArea.bottom + colorHeight;
            ctx.fillStyle = color[1];
    
            const x = chartArea.left,
                  y = chart.height - bottomBarHeight - colorHeight - delta,
                  width = chartArea.right - chartArea.left,
                  height = colorHeight;
    
            delta += height;
            ctx.fillRect(x, y, width, height);
            ctx.restore();
          })
        }
      }
    });
    
    var chartData = {
      labels: ['a', 'b', 'c', 'd'],
      datasets: [{
        label: 'value',
        borderColor: 'blue',
        data: [30, 50, 25, 10]
      }]
    };
    
    var ctx = document.getElementById("myChart").getContext("2d");
    var myBar = new Chart(ctx, {
      type: 'line',
      data: chartData,
      options: {
        scales: {
          yAxes: [{ ticks: { max: 60 } }]
        },
        legend: { display: false },
        fillColor: [
          [20, 'red'],
          [20, 'blue'],
          [20, 'green'],
          [20, 'pink'],
          [20, 'yellow'],
        ]
      }
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.min.js"></script>
    
    <canvas id="myChart" height="300" width="500"></canvas>

    https://jsbin.com/lisuvuq/2/edit?js,output


    [1] - 使用插件,您可以自定义图表的行为。当您使用插件时,您将获得一个 API,因此您可以“监听”图表的生命周期事件(例如beforeDraw)并调用您自己的代码。 API 调用您的函数并为您提供有关图表的数据,以便您可以在代码中使用它。
    在此示例中,我们使用 API 来 (1) 在绘制图表之前运行代码 (2) 使用数据来计算不同颜色的面积。 (3) 根据计算绘制额外的形状(ctx.fillRect)。

    【讨论】:

    • +1 非常好。曲线下区域的背景颜色如何?它可以像 OP 的示例中那样透明吗?
    • 谢谢!当然。这只是一个例子。更新了答案。
    • 插件部分有点让我失望,但这正是我想要达到的结果。顺便说一句,如何指定 yAxes 刻度之间的增量间隔?
    • 我添加了关于插件的解释(而且,官方文档很棒)。如果还不清楚,请告诉我。
    • 关于蜱虫,你在找stepSizejsbin.com/lisuvuq/3/edit?js,output
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-02
    • 2016-02-22
    • 2022-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多