jquery 如何重置Chart.js的图表?

iqxoj9l9  于 2023-01-12  发布在  jQuery
关注(0)|答案(2)|浏览(226)

当我再次尝试用ChartJs在画布上重新加载图形时遇到问题。在进行新的搜索时,我在图形上移动鼠标时离开了以前的图形数据。我想知道如何重新启动图形。
功能

function cargar_datos(datasL,dataP,dataR){
  var ctx = $("#myChart")
  var chart = new Chart(ctx, {
    type: 'line',
    data:
    {
      labels: datasL,
      datasets:
      [{
        label: "Rendimiento",
        borderColor: 'rgb(255, 99, 132)',
        backgroundColor: ['rgba(255,200,200,0)'],
        borderWidth: 2,
        pointBackgroundColor: "red",
        pointBorderColor: "rgba(250,10,10,0.1)",
        pointBorderWidth: "10",
        pointStyle: "rectRounded",
        data:dataP,
        },
        {
        label: "Aplicado",
        borderColor: 'rgb(0, 143, 255)',
        backgroundColor: ['rgba(112, 171, 219, 0.2)'],
        borderWidth: 2,
        pointBackgroundColor: "blue",
        pointBorderColor: "rgba(144, 140, 174, 0.3)",
        pointBorderWidth: "10",
        pointStyle: "rectRounded",
        data: dataR
      }]
    },
    options: {
        tooltips: {
                 position: 'average',
                mode: 'index',
                intersect: false,
          },
  }
  });
  chart.destroy();
}

日本

$(document).on('click','#Mostrarb',function(){
    cargar_datos(labels,rend,porc);
});

该超文本标记语言

<div class="box-body">
      <canvas id="myChart" width="200" height="35"></canvas>
 </div>
y0u0uwnf

y0u0uwnf1#

这是因为每次调用chart.js函数时,都会创建一个新的chart.js示例,创建一个外部变量图:

var chart;
function cargar_datos(datasL,dataP,dataR){
var ctx = $("#myChart")
chart = new Chart(ctx, {
type: 'line',
    data:
        {
        labels: datasL,
        datasets:
            [{
                label: "Rendimiento",
                borderColor: 'rgb(255, 99, 132)',
                backgroundColor: ['rgba(255,200,200,0)'],
                borderWidth: 2,
                pointBackgroundColor: "red",
                pointBorderColor: "rgba(250,10,10,0.1)",
                pointBorderWidth: "10",
                pointStyle: "rectRounded",
                data:dataP,
                },
                {
                label: "Aplicado",
                borderColor: 'rgb(0, 143, 255)',
                backgroundColor: ['rgba(112, 171, 219, 0.2)'],
                borderWidth: 2,
                pointBackgroundColor: "blue",
                pointBorderColor: "rgba(144, 140, 174, 0.3)",
                pointBorderWidth: "10",
                pointStyle: "rectRounded",
                data: dataR
            }]
    },
    options: {
        tooltips: {
                position: 'average',
                mode: 'index',
                intersect: false,
        },
    }
});
chart.destroy();
}
eagi6jfj

eagi6jfj2#

只有当chart.destroy()new Chart()之前使用,而不是在new Chart()之后使用时,这种方法才能正常工作。

相关问题