最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to Reset a Chart of Chart.Js? - Stack Overflow

programmeradmin3浏览0评论

I have a problem when I try to reload the graphic on the canvas with ChartJs again. At the time of making a new search, I left the previous graphic data when moving the mouse over the graphic. I would like to know how to restart the graph.

Function

  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();
}

Js

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

the html

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

I have a problem when I try to reload the graphic on the canvas with ChartJs again. At the time of making a new search, I left the previous graphic data when moving the mouse over the graphic. I would like to know how to restart the graph.

Function

  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();
}

Js

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

the html

<div class="box-body">
      <canvas id="myChart" width="200" height="35"></canvas>
 </div>
Share Improve this question asked Oct 29, 2018 at 22:38 miguel andres zuñiga romeromiguel andres zuñiga romero 431 gold badge1 silver badge3 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

That is because you are creating a new instance of chart.js every time you call the function, create a outiside var chart:

   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();
}

This works fine only when chart.destroy() is used before new Chart(), not after after it.

发布评论

评论列表(0)

  1. 暂无评论