Echarts-java後端封裝option ---(二)餅圖測試用例
阿新 • • 發佈:2019-02-02
餅圖測試
後端程式碼:
@Controller
@RequestMapping("/echart")
public class EchartsController {
/**
* 餅圖 測試用例
* @return
*/
@RequestMapping(value = "/pie",method = RequestMethod.POST,
produces = {"application/text;charset=utf-8"})
@ResponseBody
public String pie(){
//需要的資料
String title = "搜尋引擎統計";
String[] searchs = {"百度","必應","豆瓣","搜狗"};
int[] datas = {123,456,789,555};
//建立option物件
Option option = new GsonOption();
//設定標題 二級標題 標題位置
option.title().text(title).subtext("二級標題" ).x("center");
//設定工具欄 展示 能標記
option.toolbox().show(true).feature(Tool.mark);
//設定顯示工具格式
option.tooltip().show(true).formatter("{a}</br>{b}:{c}/個");
//設定圖例 圖例位置 圖例對齊方式 豎列對齊
option.legend().data(searchs).x("left").orient(Orient.vertical);
//填充資料
Pie pie = new Pie();//建立餅圖物件
//設定餅圖的標題 半徑、位置
pie.name(title).radius("55%").center("50%","50%");
//填充資料
for(int i = 0; i < searchs.length; i++){
Map<String,Object> map = new HashMap<>();
map.put("value",datas[i]);//填充餅圖資料
map.put("name",searchs[i]);//填充餅圖資料對應的搜尋引擎
pie.data(map);
}
option.series(pie); //設定資料
return option.toString();
}
}
返回的json資料:
{
"title": {"text": "搜尋引擎統計", "subtext": "二級標題", "x": "center"},
"toolbox": {
"feature": {
"mark": {
"show": true,
"title": {"markUndo": "刪除輔助線", "markClear": "清空輔助線", "mark": "輔助線開關"},
"lineStyle": {"color": "#1e90ff", "type": "dashed", "width": 2}
}
}, "show": true
},
"tooltip": {"formatter": "{a}</br>{b}:{c}/個", "show": true},
"legend": {"orient": "vertical", "data": ["百度", "必應", "豆瓣", "搜狗"], "x": "left"},
"series": [{
"center": ["50%", "50%"],
"radius": "55%",
"name": "搜尋引擎統計",
"type": "pie",
"data": [{"name": "百度", "value": 123}, {"name": "必應", "value": 456}, {
"name": "豆瓣",
"value": 789
}, {"name": "搜狗", "value": 555}]
}]
}
前端程式碼:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
//一定要指定div的高度和寬度以便能撐起圖示
<div id="echart"style="height:300px;width:600px;>
</div>
<script src="/static/jquery.min.js"></script>
<script src="/static/echarts.min.js"></script>
<script>
//圖表的初始化
var echarts = echarts.init(document.getElementById('echart');
initEcharts();
function initEcharts(){
echarts.clear();
echarts.showLoading({text: '正在努力的讀取資料中...'});
$.ajax({
url:"/echarts/pie",
type:"post",
success:function(data){
var option = eval("("+data+")");
echarts.setOption(option);
echarts.hideLoading();
}
})
}
</script>
效果圖:
多少不凡、只因不甘