您可以使用 AJAX 请求从服务器获取数据。下面是一个使用 PHP 构建数据的示例。尽管您需要根据地区使其有条件。
onRegionClick: function(element, code, region) {
$.ajax('/get_chart_data.php', {
data: {region: region},
dataType: 'json',
success: function(response) {
new Chart(pie).Doughnut(response.pieData, pieOptions);
}
});
get_chart_data.php
<?php
// Check which region was passed
//$_REQUEST['region']
// Based on region build chart data
$chartData = new stdClass();
$pieData = array();
$pie1= new stdClass();
$pie1->value = 20;
$pie1->color = '#878BB6';
$pieData[] = $pie1;
$pie2= new stdClass();
$pie2->value = 40;
$pie2->color = '#4ACAB4';
$pieData[] = $pie2;
$chartData->pieData = $pieData;
echo json_encode($chartData);
?>
一种基于区域的切换方法
<?php
$region1Pie = array(20, '#878BB6', 40, '#4ACAB4', 10, '#FF8153', 30, '#FFEA88');
$region2Pie = array(15, '#878BB6', 20, '#4ACAB4', 25, '#FF8153', 30, '#FFEA88');
$region3Pie = array(9, '#878BB6', 60, '#4ACAB4', 25, '#FF8153', 12, '#FFEA88');
$chartData = new stdClass();
$pieData = array();
// Swtich based on region
switch($_REQUEST['region']) {
case 1:
$pieArray = $region1Pie;
break;
case 2:
$pieArray = $region2Pie;
break;
case 3:
$pieArray = $region3Pie;
break;
}
for($i=0; $i<count($pieArray); $i+=2) {
$pie= new stdClass();
$pie->value = $pieArray[$i];
$pie->color = $pieArray[$i+1];
$pieData[] = $pie;
}
$chartData->pieData = $pieData;
echo json_encode($chartData);
?>