javascript - 使用chart.js刷新图表的图像

标签 javascript charts chart.js refresh

我正在开发一个显示每日个人工作数据的应用程序。 我将数据存储在名为“dailyTimePerProjectPerWeek”的数组中,现在我定义了 3 周的数据。 我在 html 中放置了两个按钮“上周”和“下周”,用于在几周之间导航。 第一次加载页面时,它会显示与上述数组的最高索引相对应的最近一周。 问题是,它对我来说可视化数据做了一些奇怪的事情,就像我创建了几个图表一样。 如何正确刷新图表图像? 这是我的 html 代码:

<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>Charts4DailyProgress</title>



      <link rel="stylesheet" href="css/style.css">


</head>

<body>


<body>
<h1>Weekly Time per Project</h1>
<div id="canvas-container">
<canvas id="ctx" width="1000"></canvas>
<button type="button" onclick="decrementWeek()">Previous Week</button>
<button type="button" onclick="incrementWeek()">Next Week</button>

  </div>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
  <script  src="js/index.js"></script>

</body>


</html>

这是我的 JavaScript 代码:

var dailyTimePerProjectPerWeek =[];
dailyTimePerProjectPerWeek[0] = {
      labels: ['M', 'TU','W','TH','F','SA','SU'], // responsible for how many bars are gonna show on the chart
      // create 12 datasets, since we have 12 items
      // data[0] = labels[0] (data for first bar - 'Standing costs') | data[1] = labels[1] (data for second bar - 'Running costs')
      // put 0, if there is no data for the particular bar
      datasets: [{
         label: 'Master Project. Second Part',
         data: [300, 480, 360,180, 240, 300,480],
         backgroundColor: '#D4AF37'
      }, {
         label: 'Guild Ideas - Learning Angular',
         data: [60, 0, 240, 180, 120, 0, 60],
         backgroundColor: '#C0C0C0'
      }, {
         label: 'Charts For Daily Progress',
         data: [60, 180, 120, 180, 120, 120, 0],
         backgroundColor: '#133a7c'
      }, {
         label: 'Project Manager',
         data: [120, 180, 120, 120, 0],
         backgroundColor: '#109618'
      }, {
         label: 'TOOYS',
         data: [0, 180, 120, 0, 120, 0,0],
         backgroundColor: '#990099'
      }, {
         label: 'Web Pc Explorer',
         data: [0, 0, 120, 180, 0, 120, 0],
         backgroundColor: '#54161F'
      }, {
         label: 'Mind Maps Program',
         data: [0, 0, 180, 180, 0, 0, 0],
         backgroundColor: '#708238'
      }, {
         label: 'Chain System',
         data: [0, 0, 180, 0, 0, 0],
         backgroundColor: '#E86100'
      }, {
         label: 'Code Generator',
         data: [60, 0, 0, 0, 0, 0],
         backgroundColor: '#F81894'
      }, {
         label: 'Electronic Brain',
         data: [0, 0, 0, 0, 0, 0,240],
         backgroundColor: '#6cc4ee'
      }]
   }

dailyTimePerProjectPerWeek[1] = {
      labels: ['M', 'TU','W','TH','F','SA','SU'], // responsible for how many bars are gonna show on the chart
      // create 12 datasets, since we have 12 items
      // data[0] = labels[0] (data for first bar - 'Standing costs') | data[1] = labels[1] (data for second bar - 'Running costs')
      // put 0, if there is no data for the particular bar
      datasets: [{
         label: 'Master Project. Second Part',
         data: [0, 480, 360,180, 240, 300,480],
         backgroundColor: '#D4AF37'
      }, {
         label: 'Guild Ideas - Learning Angular',
         data: [0, 0, 240, 180, 120, 0, 60],
         backgroundColor: '#C0C0C0'
      }, {
         label: 'Charts For Daily Progress',
         data: [0, 180, 120, 180, 120, 120, 0],
         backgroundColor: '#133a7c'
      }, {
         label: 'Project Manager',
         data: [0, 180, 120, 120, 0],
         backgroundColor: '#109618'
      }, {
         label: 'TOOYS',
         data: [0, 180, 120, 0, 120, 0,0],
         backgroundColor: '#990099'
      }, {
         label: 'Web Pc Explorer',
         data: [0, 0, 120, 180, 0, 120, 0],
         backgroundColor: '#54161F'
      }, {
         label: 'Mind Maps Program',
         data: [0, 0, 180, 180, 0, 0, 0],
         backgroundColor: '#708238'
      }, {
         label: 'Chain System',
         data: [0, 0, 180, 0, 0, 0],
         backgroundColor: '#E86100'
      }, {
         label: 'Code Generator',
         data: [0, 0, 0, 0, 0, 0],
         backgroundColor: '#F81894'
      }, {
         label: 'Electronic Brain',
         data: [0, 0, 0, 0, 0, 0,240],
         backgroundColor: '#6cc4ee'
      }]
   }
dailyTimePerProjectPerWeek[2] = {
      labels: ['M', 'TU','W','TH','F','SA','SU'], // responsible for how many bars are gonna show on the chart
      // create 12 datasets, since we have 12 items
      // data[0] = labels[0] (data for first bar - 'Standing costs') | data[1] = labels[1] (data for second bar - 'Running costs')
      // put 0, if there is no data for the particular bar
      datasets: [{
         label: 'Master Project. Second Part',
         data: [300, 480, 360,180, 240, 300,0],
         backgroundColor: '#D4AF37'
      }, {
         label: 'Guild Ideas - Learning Angular',
         data: [60, 0, 240, 180, 120, 0, 0],
         backgroundColor: '#C0C0C0'
      }, {
         label: 'Charts For Daily Progress',
         data: [60, 180, 120, 180, 120, 120, 0],
         backgroundColor: '#133a7c'
      }, {
         label: 'Project Manager',
         data: [120, 180, 120, 120, 0],
         backgroundColor: '#109618'
      }, {
         label: 'TOOYS',
         data: [0, 180, 120, 0, 120, 0,0],
         backgroundColor: '#990099'
      }, {
         label: 'Web Pc Explorer',
         data: [0, 0, 120, 180, 0, 120, 0],
         backgroundColor: '#54161F'
      }, {
         label: 'Mind Maps Program',
         data: [0, 0, 180, 180, 0, 0, 0],
         backgroundColor: '#708238'
      }, {
         label: 'Chain System',
         data: [0, 0, 180, 0, 0, 0],
         backgroundColor: '#E86100'
      }, {
         label: 'Code Generator',
         data: [60, 0, 0, 0, 0, 0],
         backgroundColor: '#F81894'
      }, {
         label: 'Electronic Brain',
         data: [0, 0, 0, 0, 0, 0,0],
         backgroundColor: '#6cc4ee'
      }]
   }


var currentWeek = dailyTimePerProjectPerWeek.length - 1;
var weekValue = currentWeek; //At first time weekValue points to the current week

function drawData(){
var chart = new Chart(ctx, {
   type: 'bar',
   data: dailyTimePerProjectPerWeek[weekValue],
   options: {
      responsive: false,
      legend: {
         position: 'right' // place legend on the right side of chart
      },
      scales: {
         xAxes: [{
            stacked: true // this should be set to make the bars stacked
         }],
         yAxes: [{
            stacked: true // this also..
         }]
      }
   }
});
}

function incrementWeek(){
 if(weekValue === dailyTimePerProjectPerWeek.length - 1){
   console.log("This is the current week");
 } else {
  weekValue += 1;
  drawData();
 }
}

function decrementWeek(){
 if(weekValue === 0){
   console.log("This is the oldest week of the time series");
 } else {
  weekValue -= 1;
  drawData();
 }
}



/*
function selectWeek(){

}
*/

/*
function fixWeek(){




}*/
//Main Program
var chart = new Chart(ctx, {
   type: 'bar',
   data: dailyTimePerProjectPerWeek[weekValue],
   options: {
      responsive: false,
      legend: {
         position: 'right' // place legend on the right side of chart
      },
      scales: {
         xAxes: [{
            stacked: true // this should be set to make the bars stacked
         }],
         yAxes: [{
            stacked: true // this also..
         }]
      }
   }
});

事实是我使用以下代码来尝试刷新图像:

function drawData(){
var chart = new Chart(ctx, {
   type: 'bar',
   data: dailyTimePerProjectPerWeek[weekValue],
   options: {
      responsive: false,
      legend: {
         position: 'right' // place legend on the right side of chart
      },
      scales: {
         xAxes: [{
            stacked: true // this should be set to make the bars stacked
         }],
         yAxes: [{
            stacked: true // this also..
         }]
      }
   }
});
}

最佳答案

将您的 drawData() 函数更改为:

function drawData() {
  chart.data = dailyTimePerProjectPerWeek[weekValue];
  chart.update();
}

关于javascript - 使用chart.js刷新图表的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53745178/

相关文章:

javascript - Highcharts : Adding Hyperlinks to the X-Axis of the chart

chart.js - 用按钮实现chartjs缩放

javascript - 使用 JavaScript 进行验证 - 如何向所有验证错误消息显示

javascript - c3.js:根据间隙/空值隐藏系列中的点

javascript - While 循环和 Jquery 追加不起作用

javascript - Vue 3 如何正确更新 Highcharts 中的数据?

javascript - json 中的 Chart.js 中未定义的值

asp.net - 从 VB.NET 配置 ChartJS

javascript - 如何设置语义 UI 下拉菜单的宽度

javascript - 如何防止 JavaScript 上的双重绑定(bind)?