gpt4 book ai didi

javascript - 无法使用MyChart功能

转载 作者:行者123 更新时间:2023-12-03 00:06:49 52 4
gpt4 key购买 nike

我有一个在 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 函数添加到原型(prototype)中以使用 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/

52 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com