瀑布圖
Apache ECharts 中沒有瀑布圖系列,但我們可以使用堆疊長條圖模擬效果。
假設資料陣列中的值表示相較於前一個值的增加或減少。
var data = [900, 345, 393, -108, -154, 135, 178, 286, -119, -361, -203];
也就是說,第一個資料是 900
,第二個資料 345
表示將 345
加到 900
,依此類推。當以階梯式瀑布圖呈現此資料時,我們可以使用三個系列:第一個是不可互動的透明系列,用於實現懸浮長條效果,第二個系列用於表示正數,第三個系列用於表示負數。
var data = [900, 345, 393, -108, -154, 135, 178, 286, -119, -361, -203]; var help = []; var positive = []; var negative = []; for (var i = 0, sum = 0; i < data.length; ++i) { if (data[i] >= 0) { positive.push(data[i]); negative.push('-'); } else { positive.push('-'); negative.push(-data[i]); } if (i === 0) { help.push(0); } else { sum += data[i - 1]; if (data[i] < 0) { help.push(sum + data[i]); } else { help.push(sum); } } } option = { title: { text: 'Waterfall' }, grid: { left: '3%', right: '4%', bottom: '3%', containLabel: true }, xAxis: { type: 'category', splitLine: { show: false }, data: (function() { var list = []; for (var i = 1; i <= 11; i++) { list.push('Oct/' + i); } return list; })() }, yAxis: { type: 'value' }, series: [ { type: 'bar', stack: 'all', itemStyle: { normal: { barBorderColor: 'rgba(0,0,0,0)', color: 'rgba(0,0,0,0)' }, emphasis: { barBorderColor: 'rgba(0,0,0,0)', color: 'rgba(0,0,0,0)' } }, data: help }, { name: 'positive', type: 'bar', stack: 'all', data: positive }, { name: 'negative', type: 'bar', stack: 'all', data: negative, itemStyle: { color: '#f33' } } ] };
線上範例