我正在使用Google Pie Chart进行数据可视化并在页面加载时显示数据,现在当用户选择任何类并且数据将使用ajax更新时,我不得不更新饼图,但是我不知道如何更新饼图使用ajax,因此请尝试为您提供如何更新饼图的指南。
<div>
<select name="class" id="class" class="selectpicker">
<option value="1">Class 1</option>
<option value="2">Class 2</option>
</select>
</div>
<div id="donutchart" style="width: 900px; height: 350px;"></div>
<div id="chart"></div>
<div id="labelOverlay">
<p class="used-size piecolor">50<span>%</span></p>
<p class="total-size piecolor"> Progress</p>
</div>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#class').on('changed.bs.select', function (e, clickedIndex, newValue, oldValue) {
var selected = $(e.currentTarget).val();
if (selected > 0) {
$.ajax({
url: '/reports/?classId=' + selected + '&type=progress',
type: 'get',
dataType: 'html',
beforeSend: function () {
}
}).done(function (learnerProgress) {
}).fail(function (jqXHR, ajaxOptions, thrownError) {
});
}
});
});
</script>
<script type="text/javascript">
google.charts.load("current", {
packages: ["corechart"]
});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Effort', 'Amount given'],
['Mastered', 80],
['Acquired', 40],
['Under Acquisition', 40],
['Needs More Practice', 20],
['Not started', 20],
]);
var options = {
//is3D:true,
'tooltip': {
trigger: 'none'
},
pieSliceText: "none",
enableInteractivity: false,
pieHole: 0.7,
pieSliceTextStyle: {
color: 'black',
},
slices: {
0: {
color: '#009487'
},
1: {
color: '#88C157'
},
2: {
color: '#FFEA55'
},
3: {
color: '#FF972D'
},
4: {
color: '#FA463D'
}
}
//legend: 'none'
};
var chart = new google.visualization.PieChart(document.getElementById('donutchart'));
chart.draw(data, options);
google.load('visualization', '1', options);
}
</script>
答案 0 :(得分:1)
首先,谷歌的加载语句将默认等待页面加载。
请使用-> google.charts.load
而不是-> $(document).ready
一旦google已加载,创建图表,选项并设置选择更改事件。
假设ajax数据采用以下格式,
[
['Effort', 'Amount given'],
['Mastered', 80],
['Acquired', 40],
['Under Acquisition', 40],
['Needs More Practice', 20],
['Not started', 20],
]
请参阅以下代码段...
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var chart = new google.visualization.PieChart(document.getElementById('donutchart'));
var options = {
tooltip: {
trigger: 'none'
},
pieSliceText: "none",
enableInteractivity: false,
pieHole: 0.7,
pieSliceTextStyle: {
color: 'black',
},
slices: {
0: {
color: '#009487'
},
1: {
color: '#88C157'
},
2: {
color: '#FFEA55'
},
3: {
color: '#FF972D'
},
4: {
color: '#FA463D'
}
}
};
$('#class').on('changed.bs.select', function (e, clickedIndex, newValue, oldValue) {
var selected = $(e.currentTarget).val();
if (selected > 0) {
drawChart(selected);
}
});
function drawChart(selected) {
$.ajax({
url: '/reports/?classId=' + selected + '&type=progress',
type: 'get',
dataType: 'html',
beforeSend: function () {
}
}).done(function (learnerProgress) {
var data = google.visualization.arrayToDataTable(learnerProgress);
chart.draw(data, options);
}).fail(function (jqXHR, ajaxOptions, thrownError) {
});
}
// on page load
drawChart($('#class').val());
});
注意:不需要以下内容,这是旧版本的load语句。
google.load('visualization', '1', options);