我想在图表中设置最大水平线,如危险边界线

enter image description here

在上图中,我有一个折线图,我需要在其中设置类似蓝色的水平线.

我已经查看了ChartJS文档,但没有找到任何参考.

I have need to set same as below image enter image description here

推荐答案

看看这个图表.js插件

此链接提供:http://www.java2s.com/example/javascript/chart.js/draw-horizontal-lines-in-chartjs.html

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.3.js"></script> 
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.6/Chart.bundle.min.js"></script> 
      <script type="text/javascript">
    $(window).load(function(){//from  www  . j a  va  2 s  .  c  o  m
var canvas = document.getElementById("myChart");
var ctx = canvas.getContext("2d");
var horizonalLinePlugin = {
  afterDraw: function(chartInstance) {
    var yScale = chartInstance.scales["y-axis-0"];
    var canvas = chartInstance.chart;
    var ctx = canvas.ctx;
    var index;
    var line;
    var style;
    if (chartInstance.options.horizontalLine) {
      for (index = 0; index < chartInstance.options.horizontalLine.length; index++) {
        line = chartInstance.options.horizontalLine[index];
        if (!line.style) {
          style = "rgba(169,169,169, .6)";
        } else {
          style = line.style;
        }
        if (line.y) {
          yValue = yScale.getPixelForValue(line.y);
        } else {
          yValue = 0;
        }
        ctx.lineWidth = 3;
        if (yValue) {
          ctx.beginPath();
          ctx.moveTo(0, yValue);
          ctx.lineTo(canvas.width, yValue);
          ctx.strokeStyle = style;
          ctx.stroke();
        }
        if (line.text) {
          ctx.fillStyle = style;
          ctx.fillText(line.text, 0, yValue + ctx.lineWidth);
        }
      }
      return;
    };
  }
};
Chart.pluginService.register(horizonalLinePlugin);
var data = {
  labels: ["January", "February", "March", "April", "May", "June", "July"],
  datasets: [{
    label: "My First dataset",
    fill: false,
    lineTension: 0.1,
    backgroundColor: "rgba(75,192,192,0.4)",
    borderColor: "rgba(75,192,192,1)",
    borderCapStyle: 'butt',
    borderDash: [],
    borderDashOffset: 0.0,
    borderJoinStyle: 'miter',
    pointBorderColor: "rgba(75,192,192,1)",
    pointBackgroundColor: "#fff",
    pointBorderWidth: 1,
    pointHoverRadius: 5,
    pointHoverBackgroundColor: "rgba(75,192,192,1)",
    pointHoverBorderColor: "rgba(220,220,220,1)",
    pointHoverBorderWidth: 2,
    pointRadius: 1,
    pointHitRadius: 10,
    data: [65, 59, 80, 81, 56, 55, 40],
  }]
};
var myChart = new Chart(ctx, {
  type: 'line',
  data: data,
  options: {
    "horizontalLine": [{
      "y": 82,
      "style": "rgba(255, 0, 0, .4)",
      "text": "max"
    }, {
      "y": 60,
      "style": "#00ffff",
    }, {
      "y": 44,
      "text": "min"
    }]
  }
});
    });

      </script> 
   </head> 
   <body> 
      <canvas id="myChart" width="400" height="400"></canvas>  
   </body>
</html>

Javascript相关问答推荐

在React中获取数据后,如何避免不必要的组件闪现1秒?

Snowflake JavaScript存储过程返回成功,尽管预期失败

如何使覆盖div与可水平滚动的父div相关?

分层树视图

将旋转的矩形zoom 到覆盖它所需的最小尺寸&S容器

try 使用javascript隐藏下拉 Select

Webpack在导入前混淆文件名

Nextjs 13.4 Next-Auth 4.2登录(&Quot;凭据&,{});不工作

在画布中调整边上反弹框的大小失败

搜索功能不是在分页的每一页上进行搜索

每次重新呈现时调用useState initialValue函数

将基元传递给THEN处理程序

为列表中的项目设置动画

Cherrio JS返回父div的所有图像SRC

JavaScript&;Reaction-如何避免在不使用字典/对象的情况下出现地狱?

bootstrap S JS赢得了REACT中的函数/加载

KeyboardEvent:检测到键具有打印的表示形式

Reaction-在同一页内滚动导航(下拉菜单)

将匿名函数附加到链接的onClick属性

此上下文在JavaScript中