萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 腳本程式專題 >> JavaScript

ExtJS ColumnChart 多系列不同顏色配置實現代碼

需求

在 ExtJS 4.x 中使用 `Ext.chart.Chart` (或 `LineChart`/`ColumnChart`) 繪製混合圖表 (柱狀圖 + 折線圖),並為不同系列 (Series) 指定不同顏色。

完整示例代碼 (ExtJS 4.0.7+)


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(); });

關鍵配置點解析

  1. series 數組:每個對象代表一個數據系列,獨立配置 `type` (column/line/bar/area/scatter 等)、`xField`/`yField` 映射字段、`style` 樣式對象。
  2. style.fill:系列填充顏色 (柱狀/面積圖)。十六進制字符串 `'#RRGGBB'` 或 RGB `'rgb(r,g,b)'`。
  3. style.stroke:系列描邊顏色 (柱狀邊框/折線顏色)。
  4. type 混合**:同一圖表可混合 `column`、`line`、`area` 等類型,實現混合圖表。
  5. 顏色自動分配**:若不指定 `style.fill`,ExtJS 會從主題色板自動分配,但建議業務語義明確時手動指定。
  6. 動態更新顏色**:`seriesItem.style.fill = newColor; chart.redraw();` 但需注意 `redraw()` 會重繪整個圖表,性能一般。

常見問題

  • 柱狀圖重疊**:多個 `column` 系列默認會並排分組。若想疊加,需設 `stacked: true` 在 series 或 axis 配置中。
  • 顏色不生效**:確保 `style` 對象屬性名用字符串 (如 `'stroke-width'`) 或駝峰 (`strokeWidth`),ExtJS 內部會標準化。
  • Flash 渲染器**:ExtJS 4/5 早期版本圖表依賴 Flash (`charts.swf`),現代瀏覽器已不支持。ExtJS 6+ 現代工具包採用 **Sencha Charts (HTML5 Canvas/SVG)**,配置結構不同 (如 `Ext.chart.CartesianChart`),請遷移至新版。

現代 ExtJS 6/7 (Modern/Classic) 寫法提示


// 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 更現代、性能更好、無插件依賴。

copyright © 萬盛學電腦網 all rights reserved