在Chart.js中将“阈值”显示为带有标签的水平线

hkmswyz6  于 2022-11-06  发布在  Chart.js
关注(0)|答案(1)|浏览(192)

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

在上面的图像,我有一个折线图,我需要设置像蓝色水平线。
我已经查看了ChartJS文档,但没有找到任何参考。
我需要设置与下面相同的图像

nwnhqdif

nwnhqdif1#

考虑使用chart.js插件
友情链接:http://www.java2s.com/example/javascript/chart.js/draw-horizontal-lines-in-chartjs.html

  1. <html>
  2. <head>
  3. <meta name="viewport" content="width=device-width, initial-scale=1">
  4. <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.3.js"></script>
  5. <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.6/Chart.bundle.min.js"></script>
  6. <script type="text/javascript">
  7. $(window).load(function(){//from www . j a va 2 s . c o m
  8. var canvas = document.getElementById("myChart");
  9. var ctx = canvas.getContext("2d");
  10. var horizonalLinePlugin = {
  11. afterDraw: function(chartInstance) {
  12. var yScale = chartInstance.scales["y-axis-0"];
  13. var canvas = chartInstance.chart;
  14. var ctx = canvas.ctx;
  15. var index;
  16. var line;
  17. var style;
  18. if (chartInstance.options.horizontalLine) {
  19. for (index = 0; index < chartInstance.options.horizontalLine.length; index++) {
  20. line = chartInstance.options.horizontalLine[index];
  21. if (!line.style) {
  22. style = "rgba(169,169,169, .6)";
  23. } else {
  24. style = line.style;
  25. }
  26. if (line.y) {
  27. yValue = yScale.getPixelForValue(line.y);
  28. } else {
  29. yValue = 0;
  30. }
  31. ctx.lineWidth = 3;
  32. if (yValue) {
  33. ctx.beginPath();
  34. ctx.moveTo(0, yValue);
  35. ctx.lineTo(canvas.width, yValue);
  36. ctx.strokeStyle = style;
  37. ctx.stroke();
  38. }
  39. if (line.text) {
  40. ctx.fillStyle = style;
  41. ctx.fillText(line.text, 0, yValue + ctx.lineWidth);
  42. }
  43. }
  44. return;
  45. };
  46. }
  47. };
  48. Chart.pluginService.register(horizonalLinePlugin);
  49. var data = {
  50. labels: ["January", "February", "March", "April", "May", "June", "July"],
  51. datasets: [{
  52. label: "My First dataset",
  53. fill: false,
  54. lineTension: 0.1,
  55. backgroundColor: "rgba(75,192,192,0.4)",
  56. borderColor: "rgba(75,192,192,1)",
  57. borderCapStyle: 'butt',
  58. borderDash: [],
  59. borderDashOffset: 0.0,
  60. borderJoinStyle: 'miter',
  61. pointBorderColor: "rgba(75,192,192,1)",
  62. pointBackgroundColor: "#fff",
  63. pointBorderWidth: 1,
  64. pointHoverRadius: 5,
  65. pointHoverBackgroundColor: "rgba(75,192,192,1)",
  66. pointHoverBorderColor: "rgba(220,220,220,1)",
  67. pointHoverBorderWidth: 2,
  68. pointRadius: 1,
  69. pointHitRadius: 10,
  70. data: [65, 59, 80, 81, 56, 55, 40],
  71. }]
  72. };
  73. var myChart = new Chart(ctx, {
  74. type: 'line',
  75. data: data,
  76. options: {
  77. "horizontalLine": [{
  78. "y": 82,
  79. "style": "rgba(255, 0, 0, .4)",
  80. "text": "max"
  81. }, {
  82. "y": 60,
  83. "style": "#00ffff",
  84. }, {
  85. "y": 44,
  86. "text": "min"
  87. }]
  88. }
  89. });
  90. });
  91. </script>
  92. </head>
  93. <body>
  94. <canvas id="myChart" width="400" height="400"></canvas>
  95. </body>
  96. </html>
展开查看全部

相关问题