【问题标题】:Chart.js line chart with area range具有区域范围的 Chart.js 折线图
【发布时间】:2016-08-27 19:20:07
【问题描述】:

我想向Chart.js 折线图添加另一个维度,类似于下面的图表,或this Highcharts example。考虑到 Chart.js 的无数扩展选项,我很难理解在哪里可以实现这样的功能。

理想情况下,我可以提供一个数据集,其字段类似于data,指定某一点的线宽。

【问题讨论】:

    标签: javascript charts chart.js


    【解决方案1】:

    您可以从折线图类型创建新的图表类型来执行此操作


    预览


    脚本

    Chart.defaults.stripe = Chart.helpers.clone(Chart.defaults.line);
    Chart.controllers.stripe = Chart.controllers.line.extend({
      draw: function(ease) {
        var result = Chart.controllers.line.prototype.draw.apply(this, arguments);
    
        // don't render the stripes till we've finished animating
        if (!this.rendered && ease !== 1)
          return;
        this.rendered = true;
    
    
        var helpers = Chart.helpers;
        var meta = this.getMeta();
        var yScale = this.getScaleForId(meta.yAxisID);
        var yScaleZeroPixel = yScale.getPixelForValue(0);
        var widths = this.getDataset().width;
        var ctx = this.chart.chart.ctx;
    
        ctx.save();
        ctx.fillStyle = this.getDataset().backgroundColor;
        ctx.lineWidth = 1;
        ctx.beginPath();
    
        // initialize the data and bezier control points for the top of the stripe
        helpers.each(meta.data, function(point, index) {
          point._view.y += (yScale.getPixelForValue(widths[index]) - yScaleZeroPixel);
        });
        Chart.controllers.line.prototype.updateBezierControlPoints.apply(this);
    
        // draw the top of the stripe
        helpers.each(meta.data, function(point, index) {
          if (index === 0)
            ctx.moveTo(point._view.x, point._view.y);
          else {
            var previous = helpers.previousItem(meta.data, index);
            var next = helpers.nextItem(meta.data, index);
    
            Chart.elements.Line.prototype.lineToNextPoint.apply({
              _chart: {
                ctx: ctx
              }
            }, [previous, point, next, null, null])
          }
        });
    
        // revert the data for the top of the stripe
        // initialize the data and bezier control points for the bottom of the stripe
        helpers.each(meta.data, function(point, index) {
          point._view.y -= 2 * (yScale.getPixelForValue(widths[index]) - yScaleZeroPixel);
        });
        // we are drawing the points in the reverse direction
        meta.data.reverse();
        Chart.controllers.line.prototype.updateBezierControlPoints.apply(this);
    
        // draw the bottom of the stripe
        helpers.each(meta.data, function(point, index) {
          if (index === 0)
            ctx.lineTo(point._view.x, point._view.y);
          else {
            var previous = helpers.previousItem(meta.data, index);
            var next = helpers.nextItem(meta.data, index);
    
            Chart.elements.Line.prototype.lineToNextPoint.apply({
              _chart: {
                ctx: ctx
              }
            }, [previous, point, next, null, null])
          }
    
        });
    
        // revert the data for the bottom of the stripe
        meta.data.reverse();
        helpers.each(meta.data, function(point, index) {
          point._view.y += (yScale.getPixelForValue(widths[index]) - yScaleZeroPixel);
        });
        Chart.controllers.line.prototype.updateBezierControlPoints.apply(this);
    
        ctx.stroke();
        ctx.closePath();
        ctx.fill();
        ctx.restore();
    
        return result;
      }
    });
    

    然后

    ...
    data: ...,
    width: [12, 4, 5, 13, 12, 2, 19],
    ...
    

    小提琴 - http://jsfiddle.net/u20cfpcd/

    注意: 乐队似乎存在间歇性问题,没有出现。评论 if (!this.rendered && ease !== 1) return 似乎可以解决这个问题。感谢@ClaudeBrisson 找出问题和解决方案!

    【讨论】:

    • 这很有帮助。谢谢。为区域 bg 填充设置动画会相对简单吗?所以它在动画后平滑淡入。可以用css过渡来完成吗?
    • 当我将 ChartJS 升级到上述脚本的 2.5+ 部分时,例如; Chart.elements.Line.prototype.lineToNextPoint.apply()。你有没有机会帮我更新它们?
    • 提供的 jsfiddle 好像坏了,只显示简单的线条。
    • @ClaudeBrisson - 刚刚检查并在 IE11 和最新的 Chrome 中工作。动画完成后会出现波段。
    • @potatopeelings 在 chromium 下,从 59 版到 linux 上的 65 版,我必须点击一个数据集才能看到区域,否则只会显示简单的线条。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-17
    • 2017-02-24
    • 2021-03-04
    相关资源
    最近更新 更多