簡體   English   中英

無法在ChartJS中獲得面積和范圍

[英]Unable to get Area and Range in ChartJS

我是HTML5和ChartJS的新手。 我似乎在附加JS Chart版本1.0.1和2.1.1中發現了兩種不同類型的聲明。 您能對此做簡短的解釋嗎?

另外,我在本地副本中無法在此圖表的后面看到條紋,但出現在JSfiddle中。 鏈接: http : //jsfiddle.net/u20cfpcd/為什么會這樣?

我的代碼:

<!DOCTYPE HTML>

<html>

<head></head>

<body>
  <canvas id="myChart" width="250" height="250"></canvas>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.0/Chart.bundle.min.js"></script>
    <!-- <script src="Chart_v2.min.js"></script> -->

  <script>

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

var config = {
  type: 'stripe',
  data: {
    labels: ["January", "February", "March", "April", "May", "June", "July"],
    datasets: [{
      label: "My First dataset",
      fill: false,
      data: [65, 20, 80, 81, 56, 85, 40],
      width: [12, 4, 5, 13, 12, 2, 19],
      borderColor: "rgba(75,192,192,1)",
      backgroundColor: "rgba(75,192,192,0.4)",
      pointRadius: 0
    }, {
      label: "My Second dataset",
      fill: false,
      data: [80, 81, 56, 85, 40, 65, 20],
      width: [4, 5, 13, 12, 2, 19, 12],
      borderColor: "rgba(192,75,192,1)",
      backgroundColor: "rgba(192,75,192,0.4)",
      pointRadius: 0
    }, {
      label: "My Third dataset",
      fill: false,
      data: [81, 56, 85, 40, 65, 20, 80],
      width: [5, 13, 12, 2, 19, 12, 4],
      borderColor: "rgba(192,102,75,1)",
      backgroundColor: "rgba(192,192,75,0.4)",
      pointRadius: 0
    }]
  },
  options: {
    scales: {
      yAxes: [{
        ticks: {
          min: 0,
          max: 120
        }
      }]
    }
  }
};

var ctx = document.getElementById("myChart").getContext("2d");
new Chart(ctx, config);

</script>
</body>
</html> 

我變了

 if (!(ease == 1 || ease == undefined))

請檢查此代碼: http : //jsfiddle.net/u20cfpcd/2/

如果我認為不是動畫發生者,那么容易就變得不確定,這就是問題所在。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM