咳咳,第一篇博客寫一下自己踩過的Echarts有關折線圖的坑
實例代碼:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Echarts 圖表數據顏色和圖例不對應</title> <!-- 引入 echarts.js --> <script src="https://cdn.staticfile.org/echarts/4.3.0/echarts.min.js"></script> </head> <body> <div id="ec_div" style="width: 500px; height: 500px; position: absolute; left: 50%; transform: translate(-50%)"> </div> <script> var eccharts_one = echarts.init(document.getElementById("ec_div")); var option = { title: { text: '例子' }, tooltip: {}, legend: { data: ['男','女'] }, xAxis: { data: ['一年級','二年級','三年級','四年級'] }, yAxis: {}, series: [{ name: '男', type: 'line', data: [10,18,12,16], itemStyle: { normal: { lineStyle: 'yellow' } } },{ name: '女', type: 'line', data: [11,24,17,12], itemStyle: { normal: { lineStyle: 'blue' } } }] } eccharts_one.setOption(option); </script> </body> </html>
效果:
我發現我同事有這樣寫過自定義Echarts折線圖表的顏色,通過效果圖可以發現並不起作用。
糾正:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Echarts 圖表數據顏色和圖例不對應</title> <!-- 引入 echarts.js --> <script src="https://cdn.staticfile.org/echarts/4.3.0/echarts.min.js"></script> </head> <body> <div id="ec_div" style="width: 500px; height: 500px; position: absolute; left: 50%; transform: translate(-50%)"> </div> <script> var eccharts_one = echarts.init(document.getElementById("ec_div")); var option = { title: { text: '例子' }, tooltip: {}, legend: { data: ['男','女'] }, xAxis: { data: ['一年級','二年級','三年級','四年級'] }, yAxis: {}, series: [{ name: '男', type: 'line', data: [10,18,12,16], itemStyle: { color: 'yellow' // 新加的代碼 // normal: { // lineStyle: 'yellow' // 注釋掉的無作用代碼 // } } },{ name: '女', type: 'line', data: [11,24,17,12], itemStyle: { color: 'blue' // 新加的代碼 // normal: { // lineStyle: 'blue' // 注釋掉的無作用代碼 // } } }] } eccharts_one.setOption(option); </script> </body> </html>
修改后的效果: