Chartjs 的副作用仅适用于 *一些* 客户

2023-11-02前端开发问题
3

本文介绍了Chartjs 的副作用仅适用于 *一些* 客户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在处理几个 Chart.js 图表,发现了一些奇怪的行为.

在这个小提琴中:.

任何线索可能导致我的浏览器和其他浏览器/计算机出现这种情况?

编辑,我意识到小提琴很大,所以这就是我本质上正在做的事情:

//分配和创建所有画布上下文var ctx = $("#graph1").get(0).getContext("2d");var ctx2 = $("#graph2").get(0).getContext("2d");var ctx3 = $("#graph3").get(0).getContext("2d");//实例化新图表并传入生成的数据var myChart = new Chart(ctx).Line(graph1Generator("day"));var myDoughnutChart = new Chart(ctx2).Doughnut(graph2Generator("day"));var myPieChart = new Chart(ctx3).Pie(graph3Generator("day"));

然后我添加(为每个按钮)一个事件侦听器,该侦听器销毁每个画布,并使用新信息创建一个新画布.以下是星期"按钮的示例:

weekButton.addEventListener("click", function(){myChart.destroy();myChart = new Chart(ctx).Line(graph2Generator("Week"));myDoughnutChart.destroy();myDoughnutChart = new Chart(ctx2).Doughnut(graph2Generator("week"));myPieChart.destroy();myPieChart = new Chart(ctx3).Pie(graph3Generator("week"));});

解决方案

我在 canvas 中定义 padding 的行为相同.我删除了 padding,现在没有调整大小.

I was working on a couple of Chart.js charts and have found some strange behavior.

In this fiddle: http://jsfiddle.net/h42tv3xv/ people have been getting a variety of side effects when pressing the buttons "Day", "Week", or "Month".

What it should do: Simply refresh the chart with potentially new information displayed(the pie and doughnut actually don't have anything new)

What it should not do: As a side effect for some, it is growing the size of the <canvas> on each click. It does it for me. But it doesn't do it for a lot of other people accessing this fiddle. Here is a screen shot:

Why is it doing this for some people and not others? How can I remedy this problem? If you are curious, I initially asked a question about something else about these charts, but this could be somewhat related.

Any clue what could be causing this in my browser and other browsers/computers?

Edit, I realize the fiddle is large, so this is what I am doing essentially:

// Assign and Create all canvas contexts 
    var ctx = $("#graph1").get(0).getContext("2d");
    var ctx2 =  $("#graph2").get(0).getContext("2d");
    var ctx3 =  $("#graph3").get(0).getContext("2d");

// Instantiate new charts and pass in generated data
    var myChart = new Chart(ctx).Line(graph1Generator("day"));
    var myDoughnutChart = new Chart(ctx2).Doughnut(graph2Generator("day"));
    var myPieChart = new Chart(ctx3).Pie(graph3Generator("day"));

then I am adding (for each button) an event listener that destroys each canvas, and creates a new one with new information. Here is an example of the "week" button:

weekButton.addEventListener("click", function(){

    myChart.destroy();
    myChart = new Chart(ctx).Line(graph2Generator("Week"));

    myDoughnutChart.destroy();
    myDoughnutChart = new Chart(ctx2).Doughnut(graph2Generator("week"));

    myPieChart.destroy();
    myPieChart = new Chart(ctx3).Pie(graph3Generator("week"));


});

解决方案

I had the same behaviour defining padding definition to my canvas. I removed padding and I have no resizing now.

这篇关于Chartjs 的副作用仅适用于 *一些* 客户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

layui 实现实时刷新一个外部的div
主页面上显示了一个合计,在删除和增加的时候需要更改这个总套数的值: //html代码div class="layui-inline layui-show-xs-block" style="margin-left: 10px" id="sumDiv"spanSOP合计:/spanspan${totalNum}/spanspan套/span/div 于是在我们删除这个条数据后,...
2024-11-14 前端开发问题
156

layui 单选框、复选框、下拉菜单不显示问题如何解决?
1. 如果是ajax嵌套了 页面, 请确保 只有最外层的页面引入了layui.css 和 layui.js ,内层页面 切记不要再次引入 2. 具体代码如下 layui.use(['form', 'upload'], function(){ var form = layui.form; form.render(); // 加入这一句});...
2024-11-09 前端开发问题
313

layui要如何改变时间日历布局大小?
问题描述 我想改变layui时间日历布局大小,这个要怎么操作呢? 解决办法 可以用css样式对时间日历进行重新布局,具体代码如下: !DOCTYPE htmlhtmlheadmeta charset="UTF-8"title/titlelink rel="stylesheet" href="../../layui/css/layui.css" /style#test-...
2024-10-24 前端开发问题
271

layui中表单会自动刷新的问题
layui中表单会自动刷新的问题,因为用到layui的表单,遇到了刷新的问题所以记录一下: script layui.use(['jquery','form','layer'], function(){ var $ = layui.jquery, layer=layui.layer, form = layui.form; form.on('submit(tijiao)', function(data){ a...
2024-10-23 前端开发问题
262

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301