自动刷新 Google 图表
Auto refresh Google Chart
我有一个 Pie Chart
并希望每 5 秒 auto refresh
一次。这是我的代码:
<script type="text/javascript">
google.load("visualization", "1", { packages: ["corechart"] });
google.setOnLoadCallback(drawChart);
function drawChart() {
var options = {
width: 900,
height: 500,
title: 'Partition des tickets',
legend: 'left',
is3D: true
};
$.ajax({
type: "POST",
url: "Home.aspx/GetPieChartData",
data: '{}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
var data = google.visualization.arrayToDataTable(r.d);
var chart = new google.visualization.PieChart($("#chart_Pie")[0]);
chart.draw(data, options);
},
failure: function (r) {
alert(r.d);
},
error: function (r) {
alert(r.d);
}
});
}
</script>
您可以每 5 秒使用 setTimeout 运行 脚本。我还添加了一个 isDrawing 标志,这样您就不会在绘制时再次触发该函数。
<script type="text/javascript">
var isDrawing = false;
google.load("visualization", "1", { packages: ["corechart"] });
google.setOnLoadCallback(() => {
// draw the first time
drawChart();
// start automatic refresh
setTimeout(() => {
if (!isDrawing) {
drawChart();
}
}, 5000);
});
function drawChart() {
isDrawing = true;
var options = {
width: 900,
height: 500,
title: "Partition des tickets",
legend: "left",
is3D: true
};
$.ajax({
type: "POST",
url: "Home.aspx/GetPieChartData",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(r) {
var data = google.visualization.arrayToDataTable(r.d);
var chart = new google.visualization.PieChart($("#chart_Pie")[0]);
chart.draw(data, options);
isDrawing = false;
},
failure: function(r) {
alert(r.d);
isDrawing = false;
},
error: function(r) {
alert(r.d);
isDrawing = false;
}
});
}
</script>
我修复了它,如果有人想动态更新任何 google 图表,您只需添加 update() 函数,就像您看到的那样
<script type="text/javascript">
google.load("visualization", "1", { packages: ["corechart"] });
google.setOnLoadCallback(drawChart);
function drawChart() {
var options = {
width: 900,
height: 500,
title: 'Partition des tickets',
legend: 'left',
is3D: true,
};
$.ajax({
type: "POST",
url: "Home.aspx/GetPieChartData",
data: '{}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
var data = google.visualization.arrayToDataTable(r.d);
var chart = new google.visualization.PieChart($("#chart_Pie")[0]);
chart.draw(data, options);
updateChart();
},
failure: function (r) {
alert(r.d);
},
error: function (r) {
alert(r.d);
}
});
function updateChart() {
$.ajax({
type: "POST",
url: "Home.aspx/GetPieChartData",
data: '{}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
var data = google.visualization.arrayToDataTable(r.d);
var chart = new google.visualization.PieChart($("#chart_Pie")[0]);
chart.draw(data, options);
},
failure: function (r) {
alert(r.d);
},
error: function (r) {
alert(r.d);
}
});
setTimeout(function () { updateChart() }, 1000);
};
}
</script>
我有一个 Pie Chart
并希望每 5 秒 auto refresh
一次。这是我的代码:
<script type="text/javascript">
google.load("visualization", "1", { packages: ["corechart"] });
google.setOnLoadCallback(drawChart);
function drawChart() {
var options = {
width: 900,
height: 500,
title: 'Partition des tickets',
legend: 'left',
is3D: true
};
$.ajax({
type: "POST",
url: "Home.aspx/GetPieChartData",
data: '{}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
var data = google.visualization.arrayToDataTable(r.d);
var chart = new google.visualization.PieChart($("#chart_Pie")[0]);
chart.draw(data, options);
},
failure: function (r) {
alert(r.d);
},
error: function (r) {
alert(r.d);
}
});
}
</script>
您可以每 5 秒使用 setTimeout 运行 脚本。我还添加了一个 isDrawing 标志,这样您就不会在绘制时再次触发该函数。
<script type="text/javascript">
var isDrawing = false;
google.load("visualization", "1", { packages: ["corechart"] });
google.setOnLoadCallback(() => {
// draw the first time
drawChart();
// start automatic refresh
setTimeout(() => {
if (!isDrawing) {
drawChart();
}
}, 5000);
});
function drawChart() {
isDrawing = true;
var options = {
width: 900,
height: 500,
title: "Partition des tickets",
legend: "left",
is3D: true
};
$.ajax({
type: "POST",
url: "Home.aspx/GetPieChartData",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(r) {
var data = google.visualization.arrayToDataTable(r.d);
var chart = new google.visualization.PieChart($("#chart_Pie")[0]);
chart.draw(data, options);
isDrawing = false;
},
failure: function(r) {
alert(r.d);
isDrawing = false;
},
error: function(r) {
alert(r.d);
isDrawing = false;
}
});
}
</script>
我修复了它,如果有人想动态更新任何 google 图表,您只需添加 update() 函数,就像您看到的那样
<script type="text/javascript">
google.load("visualization", "1", { packages: ["corechart"] });
google.setOnLoadCallback(drawChart);
function drawChart() {
var options = {
width: 900,
height: 500,
title: 'Partition des tickets',
legend: 'left',
is3D: true,
};
$.ajax({
type: "POST",
url: "Home.aspx/GetPieChartData",
data: '{}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
var data = google.visualization.arrayToDataTable(r.d);
var chart = new google.visualization.PieChart($("#chart_Pie")[0]);
chart.draw(data, options);
updateChart();
},
failure: function (r) {
alert(r.d);
},
error: function (r) {
alert(r.d);
}
});
function updateChart() {
$.ajax({
type: "POST",
url: "Home.aspx/GetPieChartData",
data: '{}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
var data = google.visualization.arrayToDataTable(r.d);
var chart = new google.visualization.PieChart($("#chart_Pie")[0]);
chart.draw(data, options);
},
failure: function (r) {
alert(r.d);
},
error: function (r) {
alert(r.d);
}
});
setTimeout(function () { updateChart() }, 1000);
};
}
</script>