主要是對配置的一些說明,項目源碼githup地址
https://github.com/shengbid/echarts-series
看一下option的配置,除去series里面的配置,其他屬性都是常見的配置,不多展開
option: { title: { text: '上證指數', left: 0 }, tooltip: { trigger: 'axis', axisPointer: { type: 'cross' } }, legend: { data: ['日K', 'MA5', 'MA10', 'MA20', 'MA30'] }, grid: { left: '10%', right: '10%', bottom: '15%' }, xAxis: { type: 'category', data: [], scale: true, boundaryGap: false, axisLine: {onZero: false}, splitLine: {show: false}, splitNumber: 20, min: 'dataMin', max: 'dataMax' }, yAxis: { scale: true, splitArea: { show: true } }, dataZoom: [ { type: 'inside', start: 50, end: 100 }, { show: true, type: 'slider', top: '90%', start: 50, end: 100 } ], }
主要看一下series配置里面的K線圖,其他4組數據就是普通的折線圖,先看一個注釋掉K線圖的效果,就是簡單的多條折線圖

OK,現在把其他4組數據注釋,只留下K線圖,就是現在這樣

然后詳細說明一下這里面的配置
data: k線圖數據,格式[open, close, lowest, highest] (即:[開盤值, 收盤值, 最低值, 最高值]) 示例: data: [[11, 22, 33, 44], [12, 23, 34, 45]....]

itemStyle: 單個 K 線圖數據的圖形樣式,默認陽線紅色,陰線綠色
itemStyle: { color: '#ec0000', // 陽線顏色 color0: '#00da3c', // 陰線顏色 borderColor: '#8A0000', // 陽線邊框顏色 borderColor0: '#008F28' // 陰線邊框顏色 },
markPoint: 圖表標注,就是圖上4個圈圈標注
markPoint: {data: []}
data: [ // 有幾組數據就有幾個標注 { name: 'XX標點', // 標注名稱,可不填,不會展示出來 coord: ['2013/5/31', 2300], // 標注的坐標X軸, Y軸 value: 2300, // 標注的數字 itemStyle: { color: 'rgb(41,60,85)' // 標注的顏色 } }, { name: 'highest value', // 特殊的標注類型,用於標注最大值最小值等 // 'min' 最小值。 // 'max' 最大值。 // 'average' 平均值 type: 'max', valueDim: 'highest' // K線圖的取值維度open, close, lowest, highest }, { name: 'lowest value', type: 'min', valueDim: 'lowest' }, { name: 'average value on close', type: 'average', valueDim: 'close' } ],

markLine: 圖表標線,就是圖上三條虛線
markLine: { symbol: ['none', 'none'], // 線段兩端的圖表,默認開始是圓圈,結束是箭頭 data: [ [ // 數組格式可以有兩個值,表示線段的起點和終點 { name: 'from lowest to highest', type: 'min', valueDim: 'lowest', // K線圖的取值維度open, close, lowest, highest symbol: 'circle', symbolSize: 10, label: { show: false }, emphasis: { label: { show: false } } }, { type: 'max', valueDim: 'highest', symbol: 'circle', symbolSize: 10, label: { show: false }, emphasis: { label: { show: false } } } ], { // 對象形式為直線 name: 'min line on close', type: 'min', valueDim: 'close' }, { name: 'max line on close', type: 'max', valueDim: 'close' } ] }
完整代碼,vue
<template> <div class="container"> <div class="chart-container"> <div id="lineChart" style="width:900px;height:500px"></div> </div> </div> </template> <script> import data from './chartdata/candlestickdata' // 圖表數據 export default { name: 'candlestickChart', data() { return { option: { title: { text: '上證指數', left: 0 }, tooltip: { trigger: 'axis', axisPointer: { type: 'cross' } }, legend: { data: ['日K', 'MA5', 'MA10', 'MA20', 'MA30'] }, grid: { left: '10%', right: '10%', bottom: '15%' }, xAxis: { type: 'category', data: [], scale: true, boundaryGap: false, axisLine: {onZero: false}, splitLine: {show: false}, splitNumber: 20, min: 'dataMin', max: 'dataMax' }, yAxis: { scale: true, splitArea: { show: true } }, dataZoom: [ { type: 'inside', start: 50, end: 100 }, { show: true, type: 'slider', top: '90%', start: 50, end: 100 } ], series: [ { name: '日K', type: 'candlestick', data: [], itemStyle: { color: '#ec0000', color0: '#00da3c', borderColor: '#8A0000', borderColor0: '#008F28' }, markPoint: { label: { formatter: function (param) { return param != null ? Math.round(param.value) : ''; } }, data: [ { name: 'XX標點', coord: ['2013/5/31', 2300], value: 2300, itemStyle: { color: 'rgb(41,60,85)' } }, { name: 'highest value', type: 'max', valueDim: 'highest' }, { name: 'lowest value', type: 'min', valueDim: 'lowest' }, { name: 'average value on close', type: 'average', valueDim: 'close' } ], tooltip: { formatter: function (param) { return param.name + '<br>' + (param.data.coord || ''); } } }, markLine: { symbol: ['none', 'none'], data: [ [ { name: 'from lowest to highest', type: 'min', valueDim: 'lowest', symbol: 'circle', symbolSize: 10, label: { show: false }, emphasis: { label: { show: false } } }, { type: 'max', valueDim: 'highest', symbol: 'circle', symbolSize: 10, label: { show: false }, emphasis: { label: { show: false } } } ], { name: 'min line on close', type: 'min', valueDim: 'close' }, { name: 'max line on close', type: 'max', valueDim: 'close' } ] } }, { name: 'MA5', type: 'line', data: [], smooth: true, lineStyle: { opacity: 0.5 } }, { name: 'MA10', type: 'line', data: [], smooth: true, lineStyle: { opacity: 0.5 } }, { name: 'MA20', type: 'line', data: [], smooth: true, lineStyle: { opacity: 0.5 } }, { name: 'MA30', type: 'line', data: [], smooth: true, lineStyle: { opacity: 0.5 } }, ] }, lineChart: null, candleData: { categoryData: [], values: [] } } }, mounted() { this.candleData = this.splitData(data) console.log(this.candleData) this.option.xAxis.data = this.candleData.categoryData this.option.series[0].data = this.candleData.values this.option.series[1].data = this.calculateMA(5) this.option.series[2].data = this.calculateMA(10) this.option.series[3].data = this.calculateMA(20) this.option.series[4].data = this.calculateMA(30) this.getLineChart() }, methods: { // 設置折線圖 getLineChart() { this.lineChart = this.$echart.init(document.getElementById('lineChart')) this.lineChart.setOption(this.option) }, splitData(rawData) { var categoryData = []; var values = [] for (var i = 0; i < rawData.length; i++) { categoryData.push(rawData[i].splice(0, 1)[0]); values.push(rawData[i]) } return { categoryData: categoryData, values: values } }, calculateMA(dayCount) { var result = []; for (var i = 0, len = this.candleData.values.length; i < len; i++) { if (i < dayCount) { result.push('-'); continue; } var sum = 0; for (var j = 0; j < dayCount; j++) { sum += this.candleData.values[i - j][1] } result.push(sum / dayCount) } return result } } } </script> <style lang="less"> </style>