在 ExtJS 4.x 中使用 `Ext.chart.Chart` (或 `LineChart`/`ColumnChart`) 繪製混合圖表 (柱狀圖 + 折線圖),並為不同系列 (Series) 指定不同顏色。
Ext.onReady(function() {
// 1. 定義 Store 與數據模型
var chartStore = Ext.create('Ext.data.JsonStore', {
fields: [
{ name: 'ne', type: 'string' }, // 網元名稱 (X 軸類別)
{ name: 'confine', type: 'int' }, // 閥值 (系列 1)
{ name: 'bill', type: 'int' } // 工單數 (系列 2)
],
data: [
{ ne: '網元一', confine: 80, bill: 150 },
{ ne: '網元二', confine: 150, bill: 140 },
{ ne: '網元三', confine: 180, bill: 160 },
{ ne: '網元五', confine: 120, bill: 180 },
{ ne: '網元六', confine: 165, bill: 13 },
{ ne: '網元七', confine: 54, bill: 12 },
{ ne: '網元八', confine: 55, bill: 44 },
{ ne: '網元九', confine: 33, bill: 113 },
{ ne: '網元十', confine: 122, bill: 77 }
]
});
// 2. 定義圖表
var chart = Ext.create('Ext.chart.Chart', {
renderTo: Ext.getBody(),
width: 800,
height: 400,
animate: true,
shadow: true,
store: chartStore,
// 3. 關鍵:axes 配置 (Category X, Numeric Y)
axes: [{
type: 'Category',
position: 'bottom',
fields: ['ne'],
title: '網元',
label: { rotate: { degrees: -45 } } // 標籤傾斜防重疊
}, {
type: 'Numeric',
position: 'left',
fields: ['confine', 'bill'],
title: '數值',
grid: true,
minimum: 0
}],
// 4. 關鍵:series 配置 — 每個系列獨立指定 type、顏色、字段
series: [{
// 系列 1:柱狀圖 - 工單數
type: 'column',
axis: 'left',
xField: 'ne',
yField: 'bill',
title: '工單個數',
style: {
fill: '#99BBE8', // 填充顏色 (十六進制)
stroke: '#6699CC', // 邊框顏色
'stroke-width': 1
},
// 滑鼠提示
tips: {
trackMouse: true,
width: 140,
renderer: function(storeItem, item) {
this.setTitle(storeItem.get('ne') + '
工單: ' + storeItem.get('bill'));
}
},
// 高亮效果
highlight: {
fill: '#336699',
'stroke-width': 2
}
}, {
// 系列 2:柱狀圖 - 閥值 (也可用 line 類型)
type: 'column', // 或 'line'
axis: 'left',
xField: 'ne',
yField: 'confine',
title: '閥值',
style: {
fill: '#FF4444', // 紅色系區分
stroke: '#CC0000',
'stroke-width': 1
},
tips: {
trackMouse: true,
width: 140,
renderer: function(storeItem, item) {
this.setTitle(storeItem.get('ne') + '
閥值: ' + storeItem.get('confine'));
}
}
}, {
// 系列 3:折線圖 - 趨勢 (可選)
type: 'line',
axis: 'left',
xField: 'ne',
yField: 'bill',
title: '工單趨勢',
style: {
stroke: '#27AE60', // 綠色線
'stroke-width': 3,
fill: 'none'
},
markerConfig: {
type: 'circle',
size: 5,
fill: '#27AE60',
stroke: '#fff',
'stroke-width': 2
},
smooth: true // 平滑曲線
}],
// 5. 圖例
legend: {
position: 'top',
labelFont: '12px Helvetica'
},
// 6. 主題/樣式微調
theme: 'Base:gradients' // 內置漸變主題
});
// 7. 動態修改顏色示例 (渲染後)
// chart.series.items[0].style.fill = '#E74C3C';
// chart.redraw();
});
// ExtJS 6.5+ Modern Toolkit (HTML5 Canvas)
Ext.create('Ext.chart.CartesianChart', {
renderTo: document.body,
width: 600,
height: 400,
store: chartStore,
axes: [{
type: 'category',
position: 'bottom',
fields: 'ne'
}, {
type: 'numeric',
position: 'left',
fields: ['bill', 'confine']
}],
series: [{
type: 'bar', // 橫向柱狀圖
xField: 'ne',
yField: 'bill',
style: { fillStyle: '#99BBE8' }, // 注意:fillStyle 而非 fill
tooltip: { trackMouse: true, renderer: 'onTooltip' }
}, {
type: 'line',
xField: 'ne',
yField: 'confine',
style: { strokeStyle: '#FF4444', lineWidth: 3 },
marker: { type: 'circle', radius: 4, fillStyle: '#FF4444' }
}]
});
ExtJS 圖表系列顏色配置核心在 `series` 數組中每個系列獨立設置 `style.fill`/`style.stroke`。舊版 Flash 圖表已棄用,新項目請使用 ExtJS 6.5+ Modern 工具包的 HTML5 Canvas/SVG 圖表,API 更現代、性能更好、無插件依賴。