为什么我的按钮不起作用?

时间:2018-05-28 10:31:46

标签: javascript html chart.js

您好我使用javascript和chart.js在HTML中编写了此代码:

var chart_labels = ['06:00', '09:00', '12:00', '15:00', '18:00', '21:00'];
var temp_dataset = ['1', '8', '10', '10', '9', '7'];
var rain_dataset = ['0', '0', '6', '32', '7', '2'];
var ctx = document.getElementById("forecast").getContext('2d');
var config = {
  type: 'bar',
  data: {
    labels: chart_labels,
    datasets: [{
      type: 'line',
      label: "Temperature (Celsius)",
      yAxisID: "y-axis-0",
      fill: false,
      data: temp_dataset,
    }, {
      type: 'bar',
      label: "Precipitation (%)",
      yAxisID: "y-axis-1",
      data: rain_dataset,
    }]
  },
  options: {
    scales: {
      yAxes: [{
        position: "left",
        "id": "y-axis-0",
      }, {
        position: "right",
        "id": "y-axis-1",
      }]
    }
  }
};
var forecast_chart = new Chart(ctx, config);
$("#0").click(function() {
  var data = forecast_chart.config.data;
  data.datasets[0].data = temp_dataset;
  data.datasets[1].data = rain_dataset;
  data.labels = chart_labels;
  forecast_chart.update();
});
$("#1").click(function() {
  var chart_labels = ['00:00', '03:00', '06:00', '09:00', '12:00', '15:00', '18:00', '21:00'];
  var temp_dataset = ['5', '3', '4', '8', '10', '11', '10', '9'];
  var rain_dataset = ['0', '0', '1', '4', '19', '19', '7', '2'];
  var data = forecast_chart.config.data;
  data.datasets[0].data = temp_dataset;
  data.datasets[1].data = rain_dataset;
  data.labels = chart_labels;
  forecast_chart.update();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js"></script>

<canvas id="forecast" width="300" height="150"></canvas>
<button id="0" type="button">Dataset 1</button>
<button id="1" type="button">Dataset 2</button>

但问题是,当我点击按钮时它没有效果......根据你的问题在哪里?根据我的说法,我正确地定义了我的功能,所以它应该没问题但事实并非如此。

我该如何解决这个问题?

谢谢!

2 个答案:

答案 0 :(得分:1)

当您使用jquery选择器来选择按钮时,您的事件处理程序和chart.js的代码应该在$(document).ready()内。因此它将起作用。检查以下代码 -

<script>
$(document).ready(function(){
 var chart_labels = ['06:00', '09:00', '12:00', '15:00', '18:00', '21:00'];
var temp_dataset = ['1', '8', '10', '10', '9', '7'];
var rain_dataset = ['0', '0', '6', '32', '7', '2'];
var ctx = document.getElementById("forecast").getContext('2d');
var config = {
    type: 'bar',
    data: {
        labels: chart_labels,
        datasets: [{
            type: 'line',
            label: "Temperature (Celsius)",
            yAxisID: "y-axis-0",
            fill: false,
            data: temp_dataset,
        }, {
            type: 'bar',
            label: "Precipitation (%)",
            yAxisID: "y-axis-1",
            data: rain_dataset,
        }]
    },
    options: {
        scales: {
            yAxes: [{
                position: "left",
                "id": "y-axis-0",
            }, {
                position: "right",
                "id": "y-axis-1",
            }]
        }
    }
};
var forecast_chart = new Chart(ctx, config);
$("#0").click(function() {
    var data = forecast_chart.config.data;
    data.datasets[0].data = temp_dataset;
    data.datasets[1].data = rain_dataset;
    data.labels = chart_labels;
    forecast_chart.update();
});
$("#1").click(function() {
    var chart_labels = ['00:00', '03:00', '06:00', '09:00', '12:00', '15:00', '18:00', '21:00'];
    var temp_dataset = ['5', '3', '4', '8', '10', '11', '10', '9'];
    var rain_dataset = ['0', '0', '1', '4', '19', '19', '7', '2'];
    var data = forecast_chart.config.data;
    data.datasets[0].data = temp_dataset;
    data.datasets[1].data = rain_dataset;
    data.labels = chart_labels;
    forecast_chart.update();
});
});

</script>

这是一个有效的演示:https://jsfiddle.net/7t6vfdwf/2/

您可以看到按钮正在点击更改数据集。

答案 1 :(得分:0)

您的HTML ID应以字母而非数字

开头
SyntaxError: '#1' is not a valid selector