• <legend id='lPsA4'><style id='lPsA4'><dir id='lPsA4'><q id='lPsA4'></q></dir></style></legend>
      <bdo id='lPsA4'></bdo><ul id='lPsA4'></ul>

    <tfoot id='lPsA4'></tfoot>

      <small id='lPsA4'></small><noframes id='lPsA4'>

      1. <i id='lPsA4'><tr id='lPsA4'><dt id='lPsA4'><q id='lPsA4'><span id='lPsA4'><b id='lPsA4'><form id='lPsA4'><ins id='lPsA4'></ins><ul id='lPsA4'></ul><sub id='lPsA4'></sub></form><legend id='lPsA4'></legend><bdo id='lPsA4'><pre id='lPsA4'><center id='lPsA4'></center></pre></bdo></b><th id='lPsA4'></th></span></q></dt></tr></i><div id='lPsA4'><tfoot id='lPsA4'></tfoot><dl id='lPsA4'><fieldset id='lPsA4'></fieldset></dl></div>
      2. Chart.js - 在折线图中为背景的特定部分着色

        时间:2023-11-02
      3. <tfoot id='F3vdD'></tfoot>
          <bdo id='F3vdD'></bdo><ul id='F3vdD'></ul>

            <tbody id='F3vdD'></tbody>
          • <small id='F3vdD'></small><noframes id='F3vdD'>

              <legend id='F3vdD'><style id='F3vdD'><dir id='F3vdD'><q id='F3vdD'></q></dir></style></legend>
            1. <i id='F3vdD'><tr id='F3vdD'><dt id='F3vdD'><q id='F3vdD'><span id='F3vdD'><b id='F3vdD'><form id='F3vdD'><ins id='F3vdD'></ins><ul id='F3vdD'></ul><sub id='F3vdD'></sub></form><legend id='F3vdD'></legend><bdo id='F3vdD'><pre id='F3vdD'><center id='F3vdD'></center></pre></bdo></b><th id='F3vdD'></th></span></q></dt></tr></i><div id='F3vdD'><tfoot id='F3vdD'></tfoot><dl id='F3vdD'><fieldset id='F3vdD'></fieldset></dl></div>

                1. 本文介绍了Chart.js - 在折线图中为背景的特定部分着色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  I have a line chart much like this one: http://www.chartjs.org/samples/latest/charts/line/basic.html

                  I would like to color the areas -100 < y < -40 and 40 < y < 100 a slight tint of red to indicate that point that fall in that area are in a danger zone.

                  This was a quick sketch using paint. Anything similar to this is welcome. How can I do this? I tried looking into the documentation but found nothing.

                  I currently have a workaround using a line chart stacked over a horizontal bar chart but it is far from ideal.

                  Thanks in advance!

                  解决方案

                  You can use annotation plugin and in particular Box annotations.

                  Here below there is an example with Scatter chart:

                  var randomScalingFactor = function() {
                    return (Math.random() > 0.5 ? 1.0 : -1.0) * Math.round(Math.random() * 100);
                  };
                  var randomColor = function(opacity) {
                    return 'rgba(' + Math.round(Math.random() * 255) + ',' + Math.round(Math.random() * 255) + ',' + Math.round(Math.random() * 255) + ',' + (opacity || '.3') + ')';
                  };
                  var data = {
                    datasets: [{
                      label: "My First dataset",
                      data: [{
                        x: randomScalingFactor(),
                        y: randomScalingFactor(),
                      }, {
                        x: randomScalingFactor(),
                        y: randomScalingFactor(),
                      }, {
                        x: randomScalingFactor(),
                        y: randomScalingFactor(),
                      }, {
                        x: randomScalingFactor(),
                        y: randomScalingFactor(),
                      }, {
                        x: randomScalingFactor(),
                        y: randomScalingFactor(),
                      }, {
                        x: randomScalingFactor(),
                        y: randomScalingFactor(),
                      }, {
                        x: randomScalingFactor(),
                        y: randomScalingFactor(),
                      }]
                    }, {
                      label: "My Second dataset",
                      data: [{
                        x: randomScalingFactor(),
                        y: randomScalingFactor(),
                      }, {
                        x: randomScalingFactor(),
                        y: randomScalingFactor(),
                      }, {
                        x: randomScalingFactor(),
                        y: randomScalingFactor(),
                      }, {
                        x: randomScalingFactor(),
                        y: randomScalingFactor(),
                      }, {
                        x: randomScalingFactor(),
                        y: randomScalingFactor(),
                      }, {
                        x: randomScalingFactor(),
                        y: randomScalingFactor(),
                      }, {
                        x: randomScalingFactor(),
                        y: randomScalingFactor(),
                      }]
                    }]
                  };
                  data.datasets.forEach(function(dataset) {
                    dataset.borderColor = randomColor(0.4);
                    dataset.backgroundColor = randomColor(0.1);
                    dataset.pointBorderColor = randomColor(0.7);
                    dataset.pointBackgroundColor = randomColor(0.5);
                    dataset.pointBorderWidth = 1;
                  });
                  
                  var ctx = document.getElementById("canvas").getContext("2d");
                  window.myScatter = new Chart(ctx, {
                  	type: 'scatter',
                    data: data,
                    options: {
                      scales: {
                        xAxes: [{
                          position: 'bottom',
                          gridLines: {
                            zeroLineColor: "rgba(0,255,0,1)"
                          },
                          scaleLabel: {
                            display: true,
                            labelString: 'x axis'
                          },
                        }],
                        yAxes: [{
                          position: 'left',
                          gridLines: {
                            zeroLineColor: "rgba(0,255,0,1)"
                          },
                          scaleLabel: {
                            display: true,
                            labelString: 'y axis'
                          },
                          ticks: {
                          	min: -100,
                            max: 100
                          }
                        }]
                      },
                      annotation: {
                        drawTime: "afterDraw",
                        events: ['dblclick'],
                        annotations: [{
                        	id: 'low-box',
                          type: 'box',
                          xScaleID: 'x-axis-1',
                          yScaleID: 'y-axis-1',
                          xMin: -100,
                          xMax: 100,
                          yMin: -100,
                          yMax: -40,
                          backgroundColor: 'rgba(255, 0, 0, 0.3)',
                          //borderColor: 'rgb(255, 0, 0)',
                          borderWidth: 1
                        },{
                        	id: 'hi-box',
                          type: 'box',
                          xScaleID: 'x-axis-1',
                          yScaleID: 'y-axis-1',
                          xMin: -100,
                          xMax: 100,
                          yMin: 100,
                          yMax: 40,
                          backgroundColor: 'rgba(255, 0, 0, 0.3)',
                          //borderColor: 'rgb(255, 0, 0)',
                          borderWidth: 1
                        }]
                      }
                    }
                  });

                  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.bundle.min.js"></script>
                  <script src="https://cdnjs.cloudflare.com/ajax/libs/chartjs-plugin-annotation/0.5.7/chartjs-plugin-annotation.min.js"></script>
                  
                  <canvas id="canvas"></canvas>

                  这篇关于Chart.js - 在折线图中为背景的特定部分着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Chartjs 隐藏数据集图例 v3 下一篇:角度图添加水平线

                  相关文章

                      <bdo id='GmUoQ'></bdo><ul id='GmUoQ'></ul>
                  1. <i id='GmUoQ'><tr id='GmUoQ'><dt id='GmUoQ'><q id='GmUoQ'><span id='GmUoQ'><b id='GmUoQ'><form id='GmUoQ'><ins id='GmUoQ'></ins><ul id='GmUoQ'></ul><sub id='GmUoQ'></sub></form><legend id='GmUoQ'></legend><bdo id='GmUoQ'><pre id='GmUoQ'><center id='GmUoQ'></center></pre></bdo></b><th id='GmUoQ'></th></span></q></dt></tr></i><div id='GmUoQ'><tfoot id='GmUoQ'></tfoot><dl id='GmUoQ'><fieldset id='GmUoQ'></fieldset></dl></div>

                    <small id='GmUoQ'></small><noframes id='GmUoQ'>

                    <tfoot id='GmUoQ'></tfoot>

                  2. <legend id='GmUoQ'><style id='GmUoQ'><dir id='GmUoQ'><q id='GmUoQ'></q></dir></style></legend>