我有一个功能来更新JavaScript中的图表。

码:

    function updateChartAjaxCall(data1, data2)
    {
        //ajax call here
        $.ajax({
            type: 'POST',
            url: 'data.php',
            dataType: 'html',
            data: {data1:data1,data2:data2},
            success:function(data){

                myChart.removeData();
                myChart.addData('newLabel', [23, 33, 4]);
            },
            error:function (xhr, ajaxOptions, thrownError){
                console.log(thrownError);
            },
            complete: function(){
            }
        });
        event.preventDefault();
    }

$(document).ready(function(){

    $.ajax({
        url: "http://localhost/chartjs/data.php",
        method: "GET",
        success: function(data) {
            console.log(data);
            var op1 = [];
            var op2 = [];

            let data_keys = Object.keys(data[0])
            console.log(data_keys)
            first = data_keys[0] //1st element
            second = data_keys[1] //2nd element

            for(var i in data) {
                op1.push(data[i][first])
                op2.push(data[i][second])

            }

         var chartdata = {
         labels: op1,
         datasets : [
             {
             label: 'SystemID and Apparent Power (VA)',
             backgroundColor: 'rgba(250, 50, 50, 0.4)',
             borderColor: 'rgba(500, 50, 50, 0.5)',
             hoverBackgroundColor: 'rgba(500, 30, 30, 0.2)',
             hoverBorderColor: 'rgba(500, 30, 30, 0.3)',
             data: op2
         }
         ]
  };

  var ctx = document.getElementById('mycanvas').getContext('2d');

  var myChart = new Chart(ctx, {
    type: 'line',
    data: chartdata,
    options: {
    scales: {
       xAxes: [{
               ticks: {
                fontSize: 10
               }
              }],
        yAxes: [{
            ticks: {
                fontSize: 10
            }
        }]
            }
         }
      });
    },
    error: function(data) {
    console.log(data);
    }
    });


});


我得到的错误:

app.js:11 Uncaught ReferenceError: myChart is not defined
at Object.success (app.js:11)
at i (jquery.min.js:2)
at Object.fireWith [as resolveWith] (jquery.min.js:2)
at y (jquery.min.js:4)
at XMLHttpRequest.c (jquery.min.js:4)


有人可以帮忙,为什么我会出现此错误?我正在使用chartJS库。我想念什么?

我更新了代码以显示myChart的声明
我需要全局的updateChartAjaxCall函数,因为我是从另一个.html文件调用它来更新data1data2的。

最佳答案

发生错误是因为在方法updateChartAjaxCall的范围内,myChart不存在,因为您在'$ {document).ready(function(){'函数内声明了它

这应该工作

function addData(chart, label, data) {
    chart.data.labels.push(label);
    chart.data.datasets.forEach((dataset) => {
        dataset.data.push(data);
    });
    chart.update();
}

function removeData(chart) {
    chart.data.labels.pop();
    chart.data.datasets.forEach((dataset) => {
        dataset.data.pop();
    });
    chart.update();
}

var myChart;

function updateChartAjaxCall(data1, data2) {
    //ajax call here
    $.ajax({
        type: 'POST',
        url: 'data.php',
        dataType: 'html',
        data: {
            data1: data1,
            data2: data2
        },
        success: function (data) {

           removeData(myChart);
           addData(myChart, 'newLabel', [23, 33, 4]);
        },
        error: function (xhr, ajaxOptions, thrownError) {
            console.log(thrownError);
        },
        complete: function () {}
    });
    event.preventDefault();
}

$(document).ready(function () {

    $.ajax({
        url: "http://localhost/chartjs/data.php",
        method: "GET",
        success: function (data) {
            console.log(data);
            var op1 = [];
            var op2 = [];

            let data_keys = Object.keys(data[0])
            console.log(data_keys)
            first = data_keys[0] //1st element
            second = data_keys[1] //2nd element

            for (var i in data) {
                op1.push(data[i][first])
                op2.push(data[i][second])

            }

            var chartdata = {
                labels: op1,
                datasets: [{
                    label: 'SystemID and Apparent Power (VA)',
                    backgroundColor: 'rgba(250, 50, 50, 0.4)',
                    borderColor: 'rgba(500, 50, 50, 0.5)',
                    hoverBackgroundColor: 'rgba(500, 30, 30, 0.2)',
                    hoverBorderColor: 'rgba(500, 30, 30, 0.3)',
                    data: op2
                }]
            };

            var ctx = document.getElementById('mycanvas').getContext('2d');

            myChart = new Chart(ctx, {
                type: 'line',
                data: chartdata,
                options: {
                    scales: {
                        xAxes: [{
                            ticks: {
                                fontSize: 10
                            }
                        }],
                        yAxes: [{
                            ticks: {
                                fontSize: 10
                            }
                        }]
                    }
                }
            });
        },
        error: function (data) {
            console.log(data);
        }
    });


});


您还可以将utils函数添加到原型以使用myChart中的函数

Chart.prototype.removeData = function() {
    this.data.labels.pop();
    this.data.datasets.forEach((dataset) => {
        dataset.data.pop();
    });
    this.update();
}

Chart.prototype.addData = function(label, data) {
    this.data.labels.push(label);
    this.data.datasets.forEach((dataset) => {
        dataset.data.push(data);
    });
    this.update();
}

关于javascript - 无法使用MyChart功能,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54929197/

10-11 23:39