1、echarts簡介
ECharts,一個使用 JavaScript 實現的開源可視化庫,可以流暢的運行在 PC 和移動設備上,兼容當前絕大部分瀏覽器(IE8/9/10/11,Chrome,Firefox,Safari等),底層依賴輕量級的矢量圖形庫 ZRender,提供直觀,交互豐富,可高度個性化定制的數據可視化圖表。
ECharts 提供了常規的折線圖、柱狀圖、散點圖、餅圖、K線圖,用於統計的盒形圖,用於地理數據可視化的地圖、熱力圖、線圖,用於關系數據可視化的關系圖、treemap、旭日圖,多維數據可視化的平行坐標,還有用於 BI 的漏斗圖,儀表盤,並且支持圖與圖之間的混搭。
2、使用案例
第一步:echarts初始化
第二步:定義option,
第三步:echarts加載option
echarts初始化:
var right_bottom_chart = echarts.init(document.getElementById("right_bottom"));
定義option
var getLBOptionConfig = function(data_res, monthArr , index){ var data_arr = getDataArr(data_res); right_bottom_option = { baseOption:{ timeline:{ axisType :'category', orient:'vertical', autoPlay :true, playInterval :15000, right: '5', left:'320', top:'0', bottom:'0', label:{ interval:0, formatter: function (item) { var str = item.split("-")[1]; return parseInt(str)+'月'; } }, data:monthArr, currentIndex : index, controlStyle:{ showPlayBtn :false } } }, options:[ { title: { text: '租賃人口', textStyle:{ color:'#fff' } }, textStyle:{ color:'#B3E4A1' }, grid: { }, angleAxis: { type: 'category', data: cities, axisLabel:{ show:true, interval : 0 } }, radiusAxis: { }, polar: { }, tooltip: { show: true, formatter: function (params) { var id = params.dataIndex; return cities[id] + '<br>最低:' + data_arr[id][0] + '<br>最高:' + data_arr[id][1] + '<br>平均:' + data_arr[id][2]; } }, series: [{ type: 'bar', itemStyle: { normal: { color: 'transparent' } }, data: data_arr.map(function (d) { return d[0]; }), coordinateSystem: 'polar', stack: '最大最小值', silent: true }, { type: 'bar', data: data_arr.map(function (d) { return d[1] - d[0]; }), coordinateSystem: 'polar', name: '價格范圍', stack: '最大最小值' }, { type: 'bar', itemStyle: { normal: { color: 'transparent',/*設置bar為隱藏,撐起下面橫線*/ } }, data: data_arr.map(function (d) { return d[2]; }), coordinateSystem: 'polar', stack: '均值', silent: true, barGap: '-100%', z: 10 }, { type: 'bar', itemStyle: { normal: { color: 'green',/*設置bar為隱藏,撐起下面橫線*/ } }, data: data_arr.map(function (d) { return 8; }), coordinateSystem: 'polar', name: '均值', stack: '均值', barGap: '-100%', z: 10 }] }] } right_bottom_option.options=[]; monthArr.forEach(function(n){ right_bottom_option.options.push(getTemplate(data_arr)); }); return right_bottom_option; };
echarts加載option:
right_bottom_chart.setOption(LBoption,true);
時間軸代碼片:
timeline:{ axisType :'category', orient:'vertical', autoPlay :true, playInterval :15000, right: '5', left:'320', top:'0', bottom:'0', label:{ interval:0, formatter: function (item) { var str = item.split("-")[1]; return parseInt(str)+'月'; } }, data:monthArr, currentIndex : index, controlStyle:{ showPlayBtn :false } }
時間軸時間監聽:
right_bottom_chart.on('timelinechanged', function (timeLineIndex) {
var month_str = monthArr[timeLineIndex.currentIndex];
if(month_str){
loadRightBottomCON(right_bottom_chart, month_str, timeLineIndex.currentIndex);
}
});
抽取模板
var getTemplate = function(data_arr){ return { title: { text: '租賃人口', textStyle:{ color:'#fff' } }, textStyle:{ color:'#B3E4A1' }, grid: { }, angleAxis: { type: 'category', data: cities, axisLabel:{ show:true, interval : 0 } }, radiusAxis: { }, polar: { }, tooltip: { show: true, formatter: function (params) { var id = params.dataIndex; return cities[id] + '<br>最低:' + data_arr[id][0] + '<br>最高:' + data_arr[id][1] + '<br>平均:' + data_arr[id][2]; } }, series: [{ type: 'bar', itemStyle: { normal: { color: 'transparent' } }, data: data_arr.map(function (d) { return d[0]; }), coordinateSystem: 'polar', stack: '最大最小值', silent: true }, { type: 'bar', data: data_arr.map(function (d) { return d[1] - d[0]; }), coordinateSystem: 'polar', name: '價格范圍', stack: '最大最小值' }, { type: 'bar', itemStyle: { normal: { color: 'transparent',/*設置bar為隱藏,撐起下面橫線*/ } }, data: data_arr.map(function (d) { return d[2]; }), coordinateSystem: 'polar', stack: '均值', silent: true, barGap: '-100%', z: 10 }, { type: 'bar', itemStyle: { normal: { color: 'green',/*設置bar為隱藏,撐起下面橫線*/ } }, data: data_arr.map(function (d) { return 8; }), coordinateSystem: 'polar', name: '均值', stack: '均值', barGap: '-100%', z: 10 }] }; }
揮一揮衣袖,不帶走一片雲彩