简体   繁体   English

Chartjs在调整大小时更改值

[英]Chartjs change values on resize

I'm using chartjs for generating charts on the page. 我正在使用chartjs在页面上生成图表。 If I resize the canvas the chart change the values on the graph. 如果我调整画布的大小,则图表会更改图形上的值。 It happens only if I use my custom plugin. 仅当我使用自定义插件时,它才会发生。

plugins: [horizonalLinePlugin]

If I remove custom plugin it works fine. 如果我删除自定义插件,则效果很好。 I'm also not sure what hook should I use. 我也不确定应该使用哪个钩子。 It would be best if the new line I try to render will be behind the grid lines. 最好是我尝试渲染的新线位于网格线之后。

Why is this happening? 为什么会这样呢? Here is my code: 这是我的代码:

 function median(values) { values.sort(function(a, b) { return a - b; }); if (values.length === 0) return 0 var half = Math.floor(values.length / 2); if (values.length % 2) return values[half]; else return (values[half - 1] + values[half]) / 2.0; } var horizonalLinePlugin = { beforeDraw: function(chartInstance) { var yValue; var yMinValue; var yMaxValue; var yScale = chartInstance.scales["y-axis-0"]; var canvas = chartInstance.chart; var ctx = canvas.ctx; var index; var line; var style; if (chartInstance.data.datasets[0].data) { yValue = median(chartInstance.data.datasets[0].data); yMinValue = chartInstance.options.scales.yAxes[0].ticks.min; yMaxValue = chartInstance.options.scales.yAxes[0].ticks.max; ctx.lineWidth = 3; if (yValue) { ctx.beginPath(); ctx.moveTo(26, ((canvas.height - 25) * ((yMaxValue - yValue) / (yMaxValue - yMinValue)))); ctx.lineTo(canvas.width - 15, ((canvas.height - 25) * ((yMaxValue - yValue) / (yMaxValue - yMinValue)))); ctx.strokeStyle = '#f4f4f4'; ctx.fillStyle = '#f4f4f4'; ctx.stroke(); } return; } } }; //Chart.pluginService.register(horizonalLinePlugin); var randomScalingFactor = function() { return Math.ceil(Math.random() * 10.0 + Math.random() * 60.0); }; var config = { plugins: [horizonalLinePlugin], type: 'line', data: { labels: ['12.6.', '13.6.', '14.6.', '15.6.', '16.6.', '17.6.', '18.6.', '19.6.', '20.6.'], datasets: [{ label: '', backgroundColor: '#2198e8', borderColor: '#2198e8', borderWidth: 3, fill: false, pointRadius: 2, data: [ randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor() ], }] }, options: { responsive: true, legend: { display: false }, title: { display: false, text: '' }, tooltips: { mode: 'index', intersect: false, }, hover: { mode: 'nearest', intersect: true }, scales: { yAxes: [{ gridLines: { drawBorder: false, color: ['#f4f4f4'], lineWidth: [1] }, ticks: { min: 0, max: 100, stepSize: 10 } }] } } }; window.onload = function() { var ctx = document.getElementById('statistics-graph').getContext('2d'); window.myLine = new Chart(ctx, config); }; 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.bundle.js"></script> <div class="graph-element"> <canvas id="statistics-graph"></canvas> </div> 

Thanks for any help. 谢谢你的帮助。

The problem is in your medians function. 问题出在您的中位数函数中。 You are sorting the array, and so the chart is reacting to that. 您正在对数组进行排序,因此图表对此做出了反应。 A simple thing to do would be to slice into a new array. 一个简单的事情就是切成一个新的数组。 I renamed the paramater to inValues and the set the original values = inValues.slice() 我将参数重命名为inValues并设置了原始值= inValues.slice()

function median(inValues) {
  const values = inValues.slice();

  values.sort(function(a, b) {
    return a - b;
  });

  if (values.length === 0) return 0

  var half = Math.floor(values.length / 2);

  if (values.length % 2)
    return values[half];
  else
    return (values[half - 1] + values[half]) / 2.0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM