DIV CSS 佈局教程網

ExtJS ColumnChart 為不同系列設定不同顏色的實作
編輯:JavaScript綜合知識     

ExtJS 的圖表要讓不同資料系列呈現不同顏色,做法是透過 series 陣列中每一項的 style.color。以下以「工單積壓監控」為例,用同一個 store 同時畫出工單量與閥值兩個柱狀系列,並搭配表格做細節檢視。

資料來源

var chartStore = new Ext.data.JsonStore({
  root: 'root',
  fields: [
    { name: 'ne',     type: 'string' },  // 網元
    { name: 'confine', type: 'int'   },  // 閥值
    { name: 'bill',   type: 'string' }   // 工單數
  ],
  sortInfo: { field: 'bill', direction: 'ASC' }
});

var obj = {
  root: [
    { ne: '網元一', confine: 80,  bill: 150 },
    { ne: '網元二', confine: 150, bill: 140 },
    { ne: '網元三', confine: 180, bill: 160 }
    // 其餘資料略
  ]
};
chartStore.loadData(obj);

圖表:兩個系列、兩種顏色

關鍵就在 series 中每個 column 的 style.color,一個用淺藍、一個用紅色:

var linechart = new Ext.chart.LineChart({
  title: '工單積壓圖表',
  xtype: 'linechart',
  url: AIUPP_ROOT + '/css/resources/charts.swf',
  store: chartStore,
  tipRenderer: function (chart, record) {
    var ne = record.get('ne');
    return String.format('網元:{0}n工單量:{1}n閥值:{2}',
                         ne, record.get('bill'), record.get('confine'));
  },
  series: [{
    type: 'column',
    displayName: '工單個數',
    id: "billId",
    xField: 'ne',
    yField: 'bill',
    style: { color: 0x99BBE8, size: 20 }
  }, {
    type: 'column',
    displayName: '閥值',
    xField: 'ne',
    yField: 'confine',
    style: { color: '#ff0000', size: 20 }
  }],
  chartStyle: {
    legend: { display: "top" },
    xAxis: {
      color: 0x69aBc8,
      majorTicks: { color: 0x69aBc8, length: 4 },
      minorTicks: { color: 0x69aBc8, length: 2 },
      majorGridLines: { size: 1, color: 0xeeeeee }
    },
    yAxis: {
      color: 0x69aBc8,
      majorTicks: { color: 0x69aBc8, length: 4 },
      minorTicks: { color: 0x69aBc8, length: 2 },
      majorGridLines: { size: 1, color: 0xdfe8f6 }
    }
  }
});

顏色值可以用 0xRRGGBB 數字或 '#rrggbb' 字串,兩種寫法都支援。若想在執行期更換顏色,可在 show 事件中透過 linechart.series[n].style.color 重新指定後呼叫重繪。

表格:超標列自動標色

搭配的 GridPanel 用 getRowClass 判斷工單量是否超過閥值,超過就套上紅底樣式:

var cm = new Ext.grid.ColumnModel([
  new Ext.grid.RowNumberer(),
  { header: '網元',   dataIndex: 'ne' },
  { header: '工單量', dataIndex: 'bill' },
  { header: '閥值',   dataIndex: 'confine' }
]);

var grid = new Ext.grid.EditorGridPanel({
  title: '工單積壓監控統計',
  cm: cm,
  store: chartStore,
  sm: new Ext.grid.RowSelectionModel({ singleSelect: true }),
  stripeRows: true,
  loadMask: { msg: "數據加載中....." },
  clicksToEdit: 2,
  enableColumnMove: false,
  bbar: new Ext.PagingToolbar({
    pageSize: 24,
    store: chartStore,
    displayInfo: true,
    displayMsg: '顯示第 {0} 條到第 {1} 條記錄,總共 {2} 條',
    emptyMsg: '無記錄'
  }),
  viewConfig: {
    forceFit: true,
    sortAscText: '正序',
    sortDescText: '降序',
    getRowClass: function (record, rowIndex, rowParams, store) {
      if (record.data.bill >= record.data.confine) {
        return 'x-grid-back-red';
      }
    }
  }
});

版面組合

最後用 TabPanel 把圖表與表格放在同一頁,外層再用 Viewport 以 border 版面填滿視窗;上方的查詢條件區(間隔時間、分組方式、開始/停止按鈕)以 FormPanel 實作,需要時再把 north 區塊取消註解即可顯示。

var contentPanel = new Ext.TabPanel({
  region: 'center',
  enableTabScroll: true,
  activeTab: 0,
  items: [linechart, grid]
});

var vp = new Ext.Viewport({
  layout: 'border',
  items: [{ region: 'center', layout: 'fit', margins: '5 5 5 5', items: [contentPanel] }]
});
vp.show();

提醒一點:這個範例使用的 charts.swf 是 Flash 圖表,ExtJS 3.x 的作法。Flash 已全面停止支援,若要在今日環境運行,請改用 ExtJS 4 以後基於 SVG 的 Chart 套件,series 的 color 設定概念相同。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved