ECharts 標準餅圖 動態獲取json資料
ECharts 標準餅圖 動態獲取json資料
效果圖如下:
一.html部分
<div id="firstPieChart" style="width:100%; height:400px;"></div>
二.js部分
<script type="text/javascript">
function loadOneColumn() {
var myChart = echarts.init(document.getElementById('firstPieChart'));
// 顯示標題,圖例和空的座標軸
myChart.setOption({
color: ['#ff7d27', '#47b73d', '#fcc36e', '#57a2fd', "#228b22"],//餅圖顏色
title: {
text: '資訊釋出排行',
subtext: '純屬虛構',
x:'center'
},
tooltip: {
trigger: 'item',
formatter: "{a} <br/>{b} : {c} ({d}%)"
},
legend: {
orient: 'vertical',
x: 'left',
data: []
},
toolbox: {
show: true,
feature: {
mark: { show: true },
dataView: { show: true, readOnly: false },
magicType: {
show: true,
type: ['pie', 'funnel'],
option: {
funnel: {
x: '25%',
width: '50%',
funnelAlign: 'left',
max: 1548
}
}
},
restore: { show: true },
saveAsImage: { show: true }
}
},
series: [{
name: '釋出排行',
type: 'pie',
radius: '55%',
center: ['50%', '60%'],
data: []
}]
});
myChart.showLoading(); //資料載入完之前先顯示一段簡單的loading動畫
var names = []; //類別陣列(用於存放餅圖的類別)
var brower = [];
$.ajax({
type: 'get',
url: 'json/chart/column/index_fbph.txt',//請求資料的地址
dataType: "json", //返回資料形式為json
success: function (result) {
//請求成功時執行該函式內容,result即為伺服器返回的json物件
$.each(result.list, function (index, item) {
names.push(item.department); //挨個取出類別並填入類別陣列
brower.push({
name: item.department,
value: item.num
});
});
myChart.hideLoading(); //隱藏載入動畫
myChart.setOption({ //載入資料圖表
legend: {
data: names
},
series: [{
data: brower
}]
});
},
error: function (errorMsg) {
//請求失敗時執行該函式
alert("圖表請求資料失敗!");
myChart.hideLoading();
}
});
};
loadOneColumn();
</script>
三.josn格式如下:
{"list":[{"department":"和平區","num":480},{"department":"河西區","num":380},{"department":"河東區","num":366},{"department":"河北區","num":320},{"department":"南開區","num":300}]}