Echart使用,看了就會的
阿新 • • 發佈:2018-12-29
最簡單的demo參照官網http://echarts.baidu.com/tutorial.html#5%20%E5%88%86%E9%92%9F%E4%B8%8A%E6%89%8B%20ECharts
jquery載入:
<script type="text/javascript" src="js/echarts.min.js"></script> <script type="text/javascript" src="compass/js/jquery-1.8.3.min.js"></script> <script type="text/javascript"> $(function(){ //var app.title = '2000-2016年中國汽車銷量及增長率'; var chart=echarts.init(document.getElementById("main")); var option = { title: { text: '2000-2016年中國汽車銷量及增長率' }, tooltip: { trigger: 'axis' }, legend: { data: ['增速','銷量'] }, xAxis: [{ type: 'category', data: ['2000','2001','2002','2003','2004','2005','2006','2007','2008','2009','2010','2011','2012','2013','2014','2015','2016'] }], yAxis: [{ type: 'value', name: '增速', min: 0, max: 50, position: 'right', axisLabel: { formatter: '{value} %' } }, { type: 'value', name: '銷量', min: 0, max: 3000, position: 'left' }], series: [{ name: '增速', type: 'line', stack: '總量', label: { normal: { show: true, position: 'top', } }, lineStyle: { normal: { width: 3, shadowColor: 'rgba(0,0,0,0.4)', shadowBlur: 10, shadowOffsetY: 10 } }, data: [1,13,37,35,15,13,25,21,6,45,32,2,4,13,6,4,11] }, { name: '銷量', type: 'bar', yAxisIndex: 1, stack: '總量', label: { normal: { show: true, position: 'top' } }, data: [209,236,325,439,507,576,722,879,938,1364,1806,1851,1931,2198,2349,2460,2735] }] }; chart.setOption(option); }) </script>
載入的區域
<div id="main" style="width: 500px; height: 350px;"></div>
折線圖和柱狀圖對比
通過點選Echart 圖表右上角的 色塊 可以控制對應圖表的顯隱。但在多條柱狀圖中,對應的圖形複雜(可以調整柱狀圖之間的距離解決),不符合圖表直觀清晰的特徵,所以通過改寫成折線圖的方式更有利。在只有兩支柱狀圖時,使用柱狀圖較好。
2. 多圖表展示
一般一個option 對應一張圖表,但是也可以將多張圖表通過一個option
var edu = ['專科', '本科', '碩士', '博士' ]; var eduS = [200, 120, 256, 250]; var city = ['北京', '上海', '深圳', '廣州', '杭州', '豐富', '浮點', '佛山', '長沙', '成都' ]; var cityS = [200, 120, 256, 250, 200, 180, 200, 190, 124, 200]; var comp = ['100-200', '200-300', '300-500', '<100' ]; var compS = [200, 120, 256, 250]; var exper = ['1年經驗', '2年經驗', '3年經驗', '4年經驗', '5年經驗' ]; var experS = [250, 200, 180, 190, 200]; var colors = ['#96d668', '#01babc', '#1a98f8', '#7049f0']; option = { backgroundColor: "#2d3548", title: [{ text: "學歷與崗位工資", left: "70%", textStyle: { color: "#fff", fontSize: "14" } }, { left: "25%", text: "城市與崗位工資", textStyle: { color: "#fff", fontSize: "14" } }, { text: "公司規模與崗位工資", left: "70%", top: "52%", textStyle: { color: "#fff", fontSize: "14" } }, { left: "25%", top: "52%", text: "工作經驗與崗位工資", textStyle: { color: "#fff", fontSize: "14" } } ], grid: [{ x: '8%', y: '8%', width: '40%', height: "40%", containLabel: true }, { x2: '8%', y: '8%', width: '40%', height: "40%", containLabel: true }, { x: '8%', y2: '0', width: '40%', height: "40%", bottom: "2%", containLabel: true }, { x2: '8%', y2: '0', width: '40%', height: "40%", bottom: "2%", containLabel: true }, ], tooltip: { formatter: '{b}:<br/> {c}' }, xAxis: [ { gridIndex: 0, type: 'category', boundaryGap: true, //座標軸兩邊留白 data: edu, axisLabel: { //座標軸刻度標籤的相關設定。 interval: 0, //設定為 1,表示『隔一個標籤顯示一個標籤』 margin: 15, textStyle: { color: '#ddd', fontStyle: 'normal', fontFamily: '微軟雅黑', fontSize: 12, } }, axisTick: { show: false, }, axisLine: { //座標軸軸線相關設定 lineStyle: { color: '#ddd', opacity: 0.2 } }, splitLine: { show: false, } }, { gridIndex: 1, type: 'category', boundaryGap: true, //座標軸兩邊留白 data: comp, axisLabel: { //座標軸刻度標籤的相關設定。 interval: 0, //設定為 1,表示『隔一個標籤顯示一個標籤』 margin: 15, textStyle: { color: '#ddd', fontStyle: 'normal', fontFamily: '微軟雅黑', fontSize: 12, } }, axisTick: { //座標軸刻度相關設定。 show: false, }, axisLine: { //座標軸軸線相關設定 lineStyle: { color: '#ddd', opacity: 0.2 } }, splitLine: { //座標軸在 grid 區域中的分隔線。 show: false, } }, { gridIndex: 2, type: 'category', boundaryGap: true, //座標軸兩邊留白 data: exper, axisLabel: { //座標軸刻度標籤的相關設定。 interval: 0, //設定為 1,表示『隔一個標籤顯示一個標籤』 margin: 15, textStyle: { color: '#ddd', fontStyle: 'normal', fontFamily: '微軟雅黑', fontSize: 12, } }, axisTick: { //座標軸刻度相關設定。 show: false, }, axisLine: { //座標軸軸線相關設定 lineStyle: { color: '#ddd', opacity: 0.2 } }, splitLine: { //座標軸在 grid 區域中的分隔線。 show: false, } }, { gridIndex: 3, type: 'category', boundaryGap: true, //座標軸兩邊留白 data: city, axisLabel: { //座標軸刻度標籤的相關設定。 interval: 0, //設定為 1,表示『隔一個標籤顯示一個標籤』 margin: 15, textStyle: { color: '#ddd', fontStyle: 'normal', fontFamily: '微軟雅黑', fontSize: 12, } }, axisTick: { //座標軸刻度相關設定。 show: false, }, axisLine: { //座標軸軸線相關設定 lineStyle: { color: '#ddd', opacity: 0.2 } }, splitLine: { //座標軸在 grid 區域中的分隔線。 show: false, } } ], yAxis: [{ gridIndex: 0, axisLabel: { textStyle: { color: '#a8aab0', fontStyle: 'normal', fontFamily: '微軟雅黑', fontSize: 12, } }, axisLine: { show: false }, axisTick: { show: false }, splitLine: { show: true, lineStyle: { color: ['#fff'], opacity: 0.06 } } }, { gridIndex: 1, axisLabel: { textStyle: { color: '#a8aab0', fontStyle: 'normal', fontFamily: '微軟雅黑', fontSize: 12, } }, axisLine: { show: false }, axisTick: { show: false }, splitLine: { show: true, lineStyle: { color: ['#fff'], opacity: 0.06 } } }, { gridIndex: 2, axisLabel: { textStyle: { color: '#a8aab0', fontStyle: 'normal', fontFamily: '微軟雅黑', fontSize: 12, } }, axisLine: { show: false }, axisTick: { show: false }, splitLine: { show: true, lineStyle: { color: ['#fff'], opacity: 0.06 } } }, { gridIndex: 3, axisLabel: { textStyle: { color: '#a8aab0', fontStyle: 'normal', fontFamily: '微軟雅黑', fontSize: 12, } }, axisLine: { show: false }, axisTick: { show: false }, splitLine: { show: true, lineStyle: { color: ['#fff'], opacity: 0.06 } } }, ], series: [ { type: 'line', xAxisIndex: 0, yAxisIndex: 0, itemStyle: { //圖形樣式 normal: { color: colors[3], }, }, data: eduS }, { type: 'line', xAxisIndex: 1, yAxisIndex: 1, itemStyle: { //圖形樣式 normal: { color: colors[2], }, }, data: compS }, { type: 'line', xAxisIndex: 2, yAxisIndex: 2, itemStyle: { //圖形樣式 normal: { color: colors[1], }, }, data: experS }, { type: 'line', xAxisIndex: 3, yAxisIndex: 3, itemStyle: { //圖形樣式 normal: { color: colors[0], }, }, data: cityS } ] };
同理也可以展示三張圖表
var edu = ['專科', '本科', '碩士', '博士'
];
var eduS = [200, 120, 256, 250];
var comp = ['100-200', '200-300', '300-500', '<100'
];
var compS = [200, 120, 256, 250];
var exper = ['1年經驗', '2年經驗', '3年經驗', '4年經驗', '5年經驗'
];
var experS = [250, 200, 180, 190, 200];
var colors = ['#96d668', '#01babc', '#1a98f8', '#7049f0'];
titles = ["學歷與崗位數量", "公司規模與崗位數量", "工作經驗與崗位數量"];
option = {
backgroundColor: "#2d3548",
color: colors ,
legend: {
orient: 'vertical',
bottom:'15%',
right:"10%",
itemGap: 20,
textStyle:{color:"#aaa"},
data:[
'學歷', '城市', '經驗'
]
},
title: [{
text: titles[1],
left: "70%",
textStyle: {
color: "#fff",
fontSize: "14"
}
}, {
left: "25%",
text: titles[0],
textStyle: {
color: "#fff",
fontSize: "14"
}
},{
left: "25%",
top: "52%",
text: titles[2],
textStyle: {
color: "#fff",
fontSize: "14"
}
},
{
left: "62%",
top: "55%",
text: "工作經驗與崗位數量",
subtext:"根據招聘資訊分析所得",
textStyle: {
color: "#fff",
fontSize: "24",
align:"center",
}
}
],
grid: [{
x: '8%',
y: '8%',
width: '40%',
height: "40%",
containLabel: true
},
{
x2: '8%',
y: '8%',
width: '40%',
height: "40%",
containLabel: true
},
{
x: '8%',
y2: '0',
width: '40%',
height: "40%",
bottom: "2%",
containLabel: true
},
],
tooltip: {
formatter: '{b}:<br/> {c}'
},
xAxis: [
{
gridIndex: 0,
type: 'category',
boundaryGap: true, //座標軸兩邊留白
data: edu,
axisLabel: { //座標軸刻度標籤的相關設定。
interval: 0, //設定為 1,表示『隔一個標籤顯示一個標籤』
margin: 15,
textStyle: {
color: '#ddd',
fontStyle: 'normal',
fontFamily: '微軟雅黑',
fontSize: 12,
}
},
axisTick: {
show: false,
},
axisLine: { //座標軸軸線相關設定
lineStyle: {
color: '#ddd',
opacity: 0.2
}
},
splitLine: {
show: false,
}
},
{
gridIndex: 1,
type: 'category',
boundaryGap: true, //座標軸兩邊留白
data: comp,
axisLabel: { //座標軸刻度標籤的相關設定。
interval: 0, //設定為 1,表示『隔一個標籤顯示一個標籤』
margin: 15,
textStyle: {
color: '#ddd',
fontStyle: 'normal',
fontFamily: '微軟雅黑',
fontSize: 12,
}
},
axisTick: { //座標軸刻度相關設定。
show: false,
},
axisLine: { //座標軸軸線相關設定
lineStyle: {
color: '#ddd',
opacity: 0.2
}
},
splitLine: { //座標軸在 grid 區域中的分隔線。
show: false,
}
},
{
gridIndex: 2,
type: 'category',
boundaryGap: true, //座標軸兩邊留白
data: exper,
axisLabel: { //座標軸刻度標籤的相關設定。
interval: 0, //設定為 1,表示『隔一個標籤顯示一個標籤』
margin: 15,
textStyle: {
color: '#ddd',
fontStyle: 'normal',
fontFamily: '微軟雅黑',
fontSize: 12,
}
},
axisTick: { //座標軸刻度相關設定。
show: false,
},
axisLine: { //座標軸軸線相關設定
lineStyle: {
color: '#ddd',
opacity: 0.2
}
},
splitLine: { //座標軸在 grid 區域中的分隔線。
show: false,
}
}
],
yAxis: [{
gridIndex: 0,
name:"/ 崗位數量",
nameTextStyle:{color:"#a8aab0"},
axisLabel: {
textStyle: {
color: '#a8aab0',
fontStyle: 'normal',
fontFamily: '微軟雅黑',
fontSize: 12,
}
},
axisLine: {
show: false
},
axisTick: {
show: false
},
splitLine: {
show: true,
lineStyle: {
color: ['#fff'],
opacity: 0.06
}
}
},
{
gridIndex: 1,
name:"/ 崗位數量",
nameTextStyle:{color:"#a8aab0"},
axisLabel: {
textStyle: {
color: '#a8aab0',
fontStyle: 'normal',
fontFamily: '微軟雅黑',
fontSize: 12,
}
},
axisLine: {
show: false
},
axisTick: {
show: false
},
splitLine: {
show: true,
lineStyle: {
color: ['#fff'],
opacity: 0.06
}
}
},
{
gridIndex: 2,
name:"/ 崗位數量",
nameTextStyle:{color:"#a8aab0"},
axisLabel: {
textStyle: {
color: '#a8aab0',
fontStyle: 'normal',
fontFamily: '微軟雅黑',
fontSize: 12,
}
},
axisLine: {
show: false
},
axisTick: {
show: false
},
splitLine: {
show: true,
lineStyle: {
color: ['#fff'],
opacity: 0.06
}
}
}
],
series: [
{
type: 'line',
name:'學歷',
xAxisIndex: 0,
yAxisIndex: 0,
symbolSize: 8,
symbol: 'circle',
data: eduS
},
{
type: 'line',
name:'城市',
xAxisIndex: 1,
yAxisIndex: 1,
symbolSize: 8,
symbol: 'circle',
data: compS
},
{
type: 'line',
name:'經驗',
xAxisIndex: 2,
yAxisIndex: 2,
symbolSize: 8,
symbol: 'circle',
data: experS
}
]
}
一個屬性多種結果的展示
某一個職位在多個城市的薪資分佈階段
//TODO: 不可更改薪資範圍,否則顏色
yName = ["30K-50K", "20K-30K", "15K-20K", "10K-15K", "8K-10K", "6K-8K", "4K-6K"];
option = {
backgroundColor: '#2d3548',
title: [{
text: "Java",
right:"25%",
textStyle: {
color: "#fff",
fontSize: "14"
}
},{
left:"25%",
text: "Python",
textStyle: {
color: "#fff",
fontSize: "14"
}
},
{
text: "SQL",
right:"25%",
top:"50%",
textStyle: {
color: "#fff",
fontSize: "14"
}
},{
left:"25%",
top:"50%",
text: "TOMACAT",
textStyle: {
color: "#fff",
fontSize: "14"
}
}
],
grid: [{
x: '8%',
y: '8%',
width: '40%',
height:"40%",
containLabel: true
},
{
x2: '8%',
y: '8%',
width: '40%',
height:"40%",
containLabel: true
},
{
x: '8%',
y2: '0',
width: '40%',
height:"40%",
containLabel: true
},
{
x2: '8%',
y2: '0',
width: '40%',
height:"40%",
containLabel: true
},
],
xAxis: [{
gridIndex: 0,
axisTick: {
show: false
},
axisLabel: {
show: false
},
splitLine: {
show: false
},
axisLine: {
show: false
}
},
{
gridIndex: 1,
axisTick: {
show: false
},
axisLabel: {
show: false
},
splitLine: {
show: false
},
axisLine: {
show: false
}
},
{
gridIndex: 2,
axisTick: {
show: false
},
axisLabel: {
show: false
},
splitLine: {
show: false
},
axisLine: {
show: false
}
},
{
gridIndex: 3,
axisTick: {
show: false
},
axisLabel: {
show: false
},
splitLine: {
show: false
},
axisLine: {
show: false
}
},
],
yAxis: [{
gridIndex: 0,
interval: 0,
data: yName,
axisTick: {
show: false
},
axisLabel: {
show: true,
textStyle: {
color: "#ddd"
}
},
splitLine: {
show: false
},
axisLine: {
show: false
},
}, {
gridIndex: 1,
interval: 0,
data: yName,
axisTick: {
show: false
},
axisLabel: {
show: true,
textStyle: {
color: "#ddd"
}
},
splitLine: {
show: false
},
axisLine: {
show: false
},
},
{
gridIndex: 2,
interval: 0,
data: yName,
axisTick: {
show: false
},
axisLabel: {
show: true,
textStyle: {
color: "#ddd"
}
},
splitLine: {
show: false
},
axisLine: {
show: false
},
}, {
gridIndex: 3,
interval: 0,
data: yName,
axisTick: {
show: false
},
axisLabel: {
show: true,
textStyle: {
color: "#ddd"
}
},
splitLine: {
show: false
},
axisLine: {
show: false
},
}
],
series: [
{
name: 'python',
type: 'bar',
xAxisIndex: 0,
yAxisIndex: 0,
itemStyle: {
// normal:{color:'#4f9aff',barBorderRadius:[0,10,10,0],}
normal: {
barBorderRadius: [0, 8, 8, 0],
borderWidth: 0,
borderColor: '#fff',
label:{
show:true,
position:'right',
formatter:function(v){
return v.value + "%";
},
},
color: function(params) {
// build a color map as your need.
var colorList = [
'#b250ff',// 最後一個
'#4849b5',
'#ffa800',
'#E5C37D',
'#7f80e1b3', '#5c6d9ab3',
'#837e86',
'#fdc86a'
];
return colorList[params.dataIndex]
},
shadowBlur: 20,
shadowColor: 'rgba(0, 0, 0, 0.5)'
}
},
barGap: '0',
barWidth:'20',
barCategoryGap: '5%',
data: [10, 32, 28, 18, 2, 5, 5, ]
},
{
name: 'Java',
type: 'bar',
xAxisIndex: 1,
yAxisIndex: 1,
itemStyle: {
// normal:{color:'#4f9aff',barBorderRadius:[0,10,10,0],}
normal: {
barBorderRadius: [0, 8, 8, 0],
borderWidth: 0,
borderColor: '#fff',
label:{
show:true,
position:'right',
formatter:function(v){
return v.value + "%";
},
},
color: function(params) {
// build a color map as your need.
var colorList = [
'#b250ff',// 最後一個
'#4849b5',
'#ffa800',
'#E5C37D',
'#7f80e1b3', '#5c6d9ab3',
'#837e86',
'#fdc86a'
];
return colorList[params.dataIndex]
},
shadowBlur: 20,
shadowColor: 'rgba(0, 0, 0, 0.5)'
}
},
barGap: '0',
barWidth:'20',
barCategoryGap: '5%',
data: [10, 42, 8, 28, 2, 5, 5, ]
},
{
name: 'SQL',
type: 'bar',
xAxisIndex: 2,
yAxisIndex: 2,
itemStyle: {
// normal:{color:'#4f9aff',barBorderRadius:[0,10,10,0],}
normal: {
barBorderRadius: [0, 8, 8, 0],
borderWidth: 0,
borderColor: '#fff',
label:{
show:true,
position:'right',
formatter:function(v){
return v.value + "%";
},
},
color: function(params) {
// build a color map as your need.
var colorList = [
'#b250ff',// 最後一個
'#4849b5',
'#ffa800',
'#E5C37D',
'#7f80e1b3', '#5c6d9ab3',
'#837e86',
'#fdc86a'
];
return colorList[params.dataIndex]
},
shadowBlur: 20,
shadowColor: 'rgba(0, 0, 0, 0.5)'
}
},
barGap: '0',
barWidth:'20',
barCategoryGap: '5%',
data: [10, 32, 28, 18, 2, 5, 5, ]
},
{
name: 'TOMACAT',
type: 'bar',
xAxisIndex: 3,
yAxisIndex: 3,
itemStyle: {
// normal:{color:'#4f9aff',barBorderRadius:[0,10,10,0],}
normal: {
barBorderRadius: [0, 8, 8, 0],
borderWidth: 0,
borderColor: '#fff',
label:{
show:true,
position:'right',
formatter:function(v){
return v.value + "%";
},
},
color: function(params) {
// build a color map as your need.
var colorList = [
'#b250ff',// 最後一個
'#4849b5',
'#ffa800',
'#E5C37D',
'#7f80e1b3', '#5c6d9ab3',
'#837e86',
'#fdc86a'
];
return colorList[params.dataIndex]
},
shadowBlur: 20,
shadowColor: 'rgba(0, 0, 0, 0.5)'
}
},
barGap: '0',
barWidth:'20',
barCategoryGap: '5%',
data: [10, 42, 8, 28, 2, 5, 5, ]
}
]
};
排行榜效果
需要對返回的值進行排序
var jobNums = [135, 213, 311, 211, 341, 681, 123, 231, 111, 321].sort();
var avgSalary = [182, 234, 290, 170, 744, 630, 323, 412, 121, 400].sort();
var yAxisData = ['資料分析師', '資料探勘師', '資料產品經理', '資料研發工程師', '大資料視覺化工程師', '大資料專家',
'分析工程師', '資料工程師', '資料庫設計師', '資料架構師'
];
option = {
backgroundColor: "#2d3548",
title: [{
text: "崗位薪資和數量",
x: 'center',
textStyle: {
color: "#fff",
fontSize: "14"
}
}, ],
grid: [{
x: '8%',
y: '10%',
width: '40%',
containLabel: true
},
{
x2: '8%',
y: '10%',
width: '40%',
containLabel: true
},
],
tooltip: {
formatter: '{a}<br/>{b} ({c})'
},
xAxis: [{
gridIndex: 0,
axisTick: {
show: false
},
axisLabel: {
show: false
},
splitLine: {
show: false
},
axisLine: {
show: false
}
},
{
gridIndex: 1,
axisTick: {
show: false
},
axisLabel: {
show: false
},
splitLine: {
show: false
},
axisLine: {
show: false
}
},
],
yAxis: [{
gridIndex: 0,
interval: 0,
data: yAxisData,
axisTick: {
show: false
},
axisLabel: {
show: true,
textStyle: {
color: "#ddd"
}
},
splitLine: {
show: false
},
axisLine: {
show: true,
lineStyle: {
color: "#821eff"
}
},
}, {
gridIndex: 1,
interval: 0,
data: yAxisData,
axisTick: {
show: false
},
axisLabel: {
show: true,
textStyle: {
color: "#ddd"
}
},
splitLine: {
show: false
},
axisLine: {
show: true,
lineStyle: {
color: "#4f9aff"
}
},
}],
legend: {
data: ['平均工資', '崗位數量'],
x: "right",
textStyle: {
color: "#ddd"
}
},
series: [{
name: '平均工資',
type: 'bar',
xAxisIndex: 0,
yAxisIndex: 0,
barWidth: '45%',
itemStyle: {
normal: {
barBorderRadius: [0, 8, 8, 0],
color: {
type: 'bar',
colorStops: [{
offset: 0,
color: '#821eff' // 0% 處的顏色
}, {
offset: 1,
color: '#4f9aff' // 100% 處的顏色
}],
globalCoord: false, // 預設為 false
},
}
},
label: {
normal: {
show: true,
position: "right",
textStyle: {
color: "#9EA7C4"
}
}
},
data: jobNums,
}, {
name: '崗位數量',
type: 'bar',
xAxisIndex: 1,
yAxisIndex: 1,
barWidth: '45%',
itemStyle: {
normal: {
barBorderRadius: [0, 8, 8, 0],
color: {
type: 'bar',
colorStops: [{
offset: 0,
color: '#4f9aff' // 0% 處的顏色
}, {
offset: 1,
color: '#4bf3ff' // 100% 處的顏色
}],
globalCoord: false, // 預設為 false
},
}
},
label: {
normal: {
show: true,
position: "right",
textStyle: {
color: "#9EA7C4"
}
}
},
data: avgSalary,
},
]
};
一個實體的兩個屬性
首先想到的是在一個圖形中展示兩支柱狀圖,由於這兩個屬性可能對應的值大小或者型別不同,會導致兩個屬性各自對應的圖形高低不一致。同時需設立兩個 y軸 。使用條形圖和折線圖的組合方式是一個不錯的選擇。
純轉載,為了自己保留下來借鑑的