[Ext JS 6 By Example 翻译] 第7章 - 图表(chart)

本文详细介绍ExtJS中的图表功能,包括柱状图、条形图、区域图、折线图及饼图等,并通过实例项目费用分析器展示了这些图表的具体应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载自:http://www.jeeboot.com/archives/1229.html


本章中将探索在 ExtJS 中使用不同类型的图表并使用一个名为费用分析的示例项目结束本章所学。以下是将要所学的内容:

  • 图表类型
  • 条形图 和 柱形图 图表
  • 区域 和 折线 图表
  • 饼图 图表
  • 3 D 图表
  • 费用分析 – 示例项目

图表

在第一章中提过,我说 ExtJS 是一站式的几乎能满足你对所有的 JavaScript 框架的需求。这当然还包括了图表功能。

图表类型

有三种类型的图表:笛卡尔图表,极坐标图表, 和 空间图表。

笛卡尔图表

Ext.chart.CartesianChart (xtype: cartesian or chart)

一个笛卡尔图表具有两个方向:X 和 Y 。默认 X 是水平的,Y 是垂直的。使用笛卡尔坐标图表有柱形图,条形图,区域,折线和散射。

极坐标图表

Ext.chart.PolarChart (xtype: polar)

这个图表有两个轴:角向和径向。图表的标绘值用极坐标来表达,有饼图和雷达图。

 

空间图表

Ext.chart.SpaceFillingChart (xtype: spacefilling)

这个图表填充图表的面积。

 

条形图和柱状图

使用条形图和柱状图,你至少需要提供 store ,axes ,series。

基本的柱状图

首先,我们创建一个 store 使用硬编码的内置数据,如以下代码所示:

[javascript]  view plain  copy
  1. Ext.define('MyApp.model.Population', {  
  2.   extend : 'Ext.data.Model',  
  3.   fields : [ 'year''population' ]  
  4. });  
  5.    
  6. Ext.define('MyApp.store.Population', {  
  7.   extend : 'Ext.data.Store',  
  8.   storeId : 'population',  
  9.   model : 'MyApp.model.Population',  
  10.   data : [{  
  11.     "year" : "1610",  
  12.     "population" : 350  
  13.   },{  
  14.     "year" : "1650",  
  15.     "population" : 50368  
  16.   },{  
  17.     "year" : "1700",  
  18.     "population" : 250888  
  19.   },{  
  20.     "year" : "1750",  
  21.     "population" : 1170760  
  22.   },{  
  23.     "year" : "1800",  
  24.     "population" : 5308483  
  25.   },{  
  26.     "year" : "1900",  
  27.     "population" : 76212168  
  28.   },{  
  29.     "year" : "1950",  
  30.     "population" : 151325798  
  31.   },{  
  32.     "year" : "2000",  
  33.     "population" : 281421906  
  34.   },{  
  35.     "year" : "2010",  
  36.     "population" : 308745538  
  37.   }]  
  38. });  
  39. var store = Ext.create("MyApp.store.Population");  

使用 Ext.chart.CartesianChart (xtype: cartesian 或 chart ) 创建图表并应用上面所创建的 store 。

[javascript]  view plain  copy
  1. Ext.create('Ext.Container', {  
  2.   renderTo : Ext.getBody(),  
  3.   width : 500,  
  4.   height : 500,  
  5.   layout : 'fit',  
  6.   items : [ {  
  7.     xtype : 'chart',  
  8.     insetPadding : {  
  9.       top : 60,  
  10.       bottom : 20,  
  11.       left : 20,  
  12.       right : 40  
  13.     },  
  14.     store : store,  
  15.     axes : [ {  
  16.       type : 'numeric',  
  17.       position : 'left',  
  18.       grid : true,  
  19.       title : {  
  20.         text : 'Population in Millions',  
  21.         fontSize : 16  
  22.       }  
  23.     }, {  
  24.       type : 'category',  
  25.       title : {  
  26.         text : 'Year',  
  27.         fontSize : 16  
  28.       },  
  29.       position : 'bottom'  
  30.     }],  
  31.     series : [ {  
  32.       type : 'bar',  
  33.       xField : 'year',  
  34.       yField : [ 'population' ]  
  35.     } ],  
  36.     sprites : {  
  37.       type : 'text',  
  38.       text : 'United States Population',  
  39.       font : '25px Helvetica',  
  40.       width : 120,  
  41.       height : 35,  
  42.       x : 100,  
  43.       y : 40  
  44.     }  
  45.   }]  
  46. });  

前面代码中比较重要的是 axes ,series ,和 sprite 。axes 有三种类型:numeric ,time ,和 category 。

而在 series 中,你可以看到设置类型为 bar 。在 ExtJS 中,将会呈现为柱状图或条形图, 虽然你指定了类型为 bar ,但是默认是作为柱状图展示的,如果你想要条形图,需要在图表配置中设置 flipXY  为 true。

这里 sprites 的配置相当简单。sprites 是一个可选项,不是必须的。sprites 这里使用的 text 的类型,就是画出来一个图形,设置字体 25 号,宽度 120,高度 35,并且 x 的位置为 100 ,y 的位置是 40 ,sprites 是ExtJS 中画图的一个对象,这里我们只是用来写了一行字。sprites 也可以接受一个数组,但是这里我们只用一个设置。

这个 insetPadding 属性是用于指定图表的 padding 。

以下截图为输出结果:

 1

条形图

如之前所说,为了得到条形图,你可以使用同样的代码,但要指定 flipXY 为 true 并改变相应坐标的位置,下面把原来 year 换到了左边。如以下代码所示:

[javascript]  view plain  copy
  1. Ext.create('Ext.Container', {  
  2.   renderTo : Ext.getBody(),  
  3.   width : 500,  
  4.   height : 500,  
  5.   layout : 'fit',  
  6.   items : [ {  
  7.     xtype : 'chart',  
  8.     flipXY : true,  
  9.     insetPadding : {  
  10.       top : 60,  
  11.       bottom : 20,  
  12.       left : 20,  
  13.       right : 40  
  14.     },  
  15.     store : store,  
  16.     axes : [ {  
  17.       type : 'numeric',  
  18.       position : 'bottom',  
  19.       grid : true,  
  20.       title : {  
  21.         text : 'Population in Millions',  
  22.         fontSize : 16  
  23.       }  
  24.     }, {  
  25.       type : 'category',  
  26.       title : {  
  27.         text : 'Year',  
  28.         fontSize : 16  
  29.       },  
  30.       position : 'left'  
  31.     }],  
  32.     series : [ {  
  33.       type : 'bar',  
  34.       xField : 'year',  
  35.       yField : [ 'population' ]  
  36.     }],  
  37.     sprites : {  
  38.       type : 'text',  
  39.       text : 'United States Population',  
  40.       font : '25px Helvetica',  
  41.       width : 120,  
  42.       height : 35,  
  43.       x : 100,  
  44.       y : 40  
  45.     }  
  46.   }]  
  47. });  

下列截图为以上的代码的输出结果:

1

堆叠条形图

现在假设你想在柱状图的每一个分类上标绘两个值。 你可以将他们堆叠起来或者在每个分类上使用两条图形。

我们更新柱状图例子来展示一个堆叠图表。为此我们需要在 store 中额外添加一个数值字段,同时在 series 中我们需要为 yField 指定两个字段。你也可以堆叠超过两个字段,但是本例中我们只用两个字段,看看下面的代码:

[javascript]  view plain  copy
  1. Ext.define('MyApp.model.Population', {  
  2.   extend : 'Ext.data.Model',  
  3.   fields : [ 'year''total''slaves' ]  
  4. });  
  5.    
  6. Ext.define('MyApp.store.Population', {  
  7.   extend : 'Ext.data.Store',  
  8.   storeId : 'population',  
  9.   model : 'MyApp.model.Population',  
  10.   data : [{  
  11.     "year" : "1790",  
  12.     "total" : 3.9,  
  13.     "slaves" : 0.7  
  14.   },{  
  15.     "year" : "1800",  
  16.     "total" : 5.3,  
  17.     "slaves" : 0.9  
  18.   },{  
  19.     "year" : "1810",  
  20.     "total" : 7.2,  
  21.     "slaves" : 1.2  
  22.   },{  
  23.     "year" : "1820",  
  24.     "total" : 9.6,  
  25.     "slaves" : 1.5  
  26.   },{  
  27.     "year" : "1830",  
  28.     "total" : 12.9,  
  29.     "slaves" : 2  
  30.   },{  
  31.     "year" : "1840",  
  32.     "total" : 17,  
  33.     "slaves" : 2.5  
  34.   },{  
  35.     "year" : "1850",  
  36.     "total" : 23.2,  
  37.     "slaves" : 3.2  
  38.   },{  
  39.     "year" : "1860",  
  40.     "total" : 31.4,  
  41.     "slaves" : 4  
  42.   }]  
  43. });  
  44. var store = Ext.create("MyApp.store.Population");  
  45.    
  46. Ext.create('Ext.Container', {  
  47.   renderTo : Ext.getBody(),  
  48.   width : 500,  
  49.   height : 500,  
  50.   layout : 'fit',  
  51.   items : [ {  
  52.     xtype : 'cartesian',  
  53.     store : store,  
  54.     insetPadding : {  
  55.       top : 60,  
  56.       bottom : 20,  
  57.       left : 20,  
  58.       right : 40  
  59.     },  
  60.     axes : [ {  
  61.       type : 'numeric',  
  62.       position : 'left',  
  63.       grid : true,  
  64.       title : {  
  65.         text : 'Population in Millions',  
  66.         fontSize : 16  
  67.       }  
  68.     }, {  
  69.       type : 'category',  
  70.       title : {  
  71.         text : 'Year',  
  72.         fontSize : 16  
  73.       },  
  74.       position : 'bottom'  
  75.     }],  
  76.     series : [ {  
  77.       type : 'bar',  
  78.       xField : 'year',  
  79.       yField : [ 'total''slaves' ]  
  80.     }],  
  81.     sprites : {  
  82.       type : 'text',  
  83.       text : 'United States Slaves Distribution 1790 to 1860',  
  84.       font : '20px Helvetica',  
  85.       width : 120,  
  86.       height : 35,  
  87.       x : 60,  
  88.       y : 40  
  89.     }  
  90.   }]  
  91. });  

堆叠柱状图的输出如下列截图:

1

如果你想呈现多个字段,并且让它们未叠加,那么只需要简单的设置 series 的 stacked 属性为 false 即可,请看以下输出:

 1

在图表中还有很多可用的选项。让我们瞧一瞧一些常用的选项:

  • tooltip: 这个属性可以在 series 里添加一个提示信息,鼠标放在每个柱状图上是会提示相应的信息。
  • legend: 这可以呈现图表的说明到任意的四个边上
  • sprites: 这个可以接收一个数组对象,上面介绍过啦,它是可以在你图标上添加一些图形,文本等等。

下面这是使用的相同的 store ,只是用了一些上面提的其他的选项:

[javascript]  view plain  copy
  1. Ext.create('Ext.Container', {     
  2.   renderTo: Ext.getBody(),     
  3.   width: 500,     
  4.   height: 500,     
  5.   layout: 'fit',     
  6.   items: [{  
  7.     xtype: 'chart',       
  8.     legend: { docked: 'bottom' },  
  9.     insetPadding: {   
  10.       top: 60,   
  11.       bottom: 20,   
  12.       left: 20,   
  13.       right: 40   
  14.     },       
  15.     store: store,       
  16.     axes: [{         
  17.       type: 'numeric',         
  18.       position: 'left',         
  19.       grid: true,  
  20.       title: {   
  21.         text: 'Population in Millions',   
  22.         fontSize: 16   
  23.       },         
  24.       minimum: 0,  
  25.     }, {         
  26.       type: 'category',  
  27.       title: {   
  28.         text: 'Year',   
  29.         fontSize: 16   
  30.       },         
  31.       position: 'bottom',  
  32.     }],       
  33.     series: [{         
  34.       type: 'bar',         
  35.       xField: 'year',         
  36.       stacked: false,         
  37.       title: ['Total''Slaves'],         
  38.       yField: ['total''slaves'],         
  39.       tooltip: {           
  40.         trackMouse: true,           
  41.         style: 'background: #fff',           
  42.         renderer: function (storeItem, item) {             
  43.           this.setHtml('In ' + storeItem.get('year') + ' ' + item. field + ' population was ' + storeItem.get(item.field) + ' m');  
  44.         }  
  45.       }  
  46.     }],       
  47.     sprites: [{         
  48.       type: 'text',  
  49.       text: 'United States Slaves Distribution 1790 to 1860',         
  50.       font: '20px Helvetica',  
  51.       width: 120,         
  52.       height: 35,         
  53.       x: 60,         
  54.       y: 40  
  55.     },{         
  56.       type: 'text',  
  57.       text: 'Source: http://www.wikipedia.org',         
  58.       fontSize: 10,         
  59.       x: 12,         
  60.       y: 440  
  61.     }]  
  62.   }]  
  63. });  

输出你看到页脚,提示信息和说明信息都在下图:

 1

3D 柱状图

如果你改变 series 的类型为 bar3d ,就能获得 3D 的柱状图,如下列截图所示:

1

区域和折线图

区域和折线图也属于是笛卡尔图表。

区域图表

呈现一个区域图,使用下列代码简单的替换前面的例子里的 series :

[javascript]  view plain  copy
  1. series: [ {  
  2.   type : 'area',  
  3.   xField : 'year',  
  4.   stacked : false,  
  5.   title : [ 'Total''slaves' ],  
  6.   yField : [ 'total''slaves' ],  
  7.   style : {  
  8.     stroke : "#94ae0a",  
  9.     fillOpacity : 0.6,  
  10.   }  
  11. } ]  

以上代码所示的输出:

 1

类似于堆叠柱状图,你也可以在 series 中设置 stacked 为true 来实现堆叠。如果你在之前例子上将 stacked 改为 true ,那么将获得下列结果输出:

1

折线图

使用下列的配置在上面的例子中的 series ,折线图显示图下图:

[javascript]  view plain  copy
  1. series: [ {  
  2.   type : 'line',  
  3.   xField : 'year',  
  4.   title : [ 'Total' ],  
  5.   yField : [ 'total' ]  
  6. },{  
  7.   type : 'line',  
  8.   xField : 'year',  
  9.   title : [ 'Slaves' ],  
  10.   yField : [ 'slaves' ]  
  11. }]  

1

 

饼图

这是一个在很多应用中都很常用的图表和报表工具。呈现一个饼图使用 Ext.chart.PolarChart (xtype: polar) 。

基本的饼图

指定类型为 pie ,还要指定 angleField  和 label 来呈现饼图。angleField  这是角度字段,如以下代码所示:

[javascript]  view plain  copy
  1. Ext.define('MyApp.store.Expense', {  
  2.   extend : 'Ext.data.Store',  
  3.   alias : 'store.expense',  
  4.   fields : [ 'cat''spent' ],  
  5.   data : [ {  
  6.     "cat" : "Restaurant",  
  7.     "spent" : 100  
  8.   }, {  
  9.     "cat" : "Travel",  
  10.     "spent" : 150  
  11.   }, {  
  12.     "cat" : "Insurance",  
  13.     "spent" : 500  
  14.   }, {  
  15.     "cat" : "Rent",  
  16.     "spent" : 1000  
  17.   }, {  
  18.     "cat" : "Groceries",  
  19.     "spent" : 400  
  20.   }, {  
  21.     "cat" : "Utilities",  
  22.     "spent" : 300  
  23.   } ]  
  24. });  
  25.    
  26. var store = Ext.create("MyApp.store.Expense");  
  27.    
  28. Ext.create('Ext.Container',{  
  29.   renderTo : Ext.getBody(),  
  30.   width : 600,  
  31.   height : 500,  
  32.   layout : 'fit',  
  33.   items : [ {  
  34.     xtype : 'polar',  
  35.     legend : {  
  36.       docked : 'bottom'  
  37.     },  
  38.     insetPadding : {  
  39.       top : 100,  
  40.       bottom : 20,  
  41.       left : 20,  
  42.       right : 40  
  43.     },  
  44.     store : store,  
  45.     series : [ {  
  46.       type : 'pie',  
  47.       angleField : 'spent',  
  48.       label : {  
  49.         field : 'cat',  
  50.       },  
  51.       tooltip : {  
  52.         trackMouse : true,  
  53.           renderer : function(storeItem, item) {  
  54.             var value = ((parseFloat(storeItem.get('spent')/ storeItem.store.sum('spent')) * 100.0).toFixed(2));  
  55.             this.setHtml(storeItem.get('cat') + ': ' + value + '%');  
  56.           }  
  57.       }  
  58.     } ]  
  59.   } ]  
  60. });  

1

 

圆环饼图

这仅需要在之前的例子中设置 donut 属性的值为 40 ,你将获取下列图表。donut 这个值是半径的百分比:

1

3D饼图

在 ExtJS 6 中,对 3D 饼图做出了一些改进。现在 3D 饼图支持 label 和可配置的 3D 切面,例如厚度,变形等等。

我们使用同样的 model 和 store 使用前面饼图的例子创建一个 3D 饼图,如下:

[javascript]  view plain  copy
  1. Ext.create('Ext.Container',{  
  2.   renderTo : Ext.getBody(),  
  3.   width : 600,  
  4.   height : 500,  
  5.   layout : 'fit',  
  6.   items : [ {  
  7.     xtype : 'polar',  
  8.     legend : {  
  9.       docked : 'bottom'  
  10.     },  
  11.     insetPadding : {  
  12.       top : 100,  
  13.       bottom : 20,  
  14.       left : 80,  
  15.       right : 80  
  16.     },  
  17.     store : store,  
  18.     series : [ {  
  19.       type : 'pie3d',  
  20.       donut : 50,  
  21.       thickness : 70,  
  22.       distortion : 0.5,  
  23.       angleField : 'spent',  
  24.       label : {  
  25.         field : 'cat'  
  26.       },  
  27.       tooltip : {  
  28.         trackMouse : true,  
  29.         renderer : function(storeItem, item) {  
  30.           var value = ((parseFloat(storeItem.get('spent')/ storeItem.store.sum('spent')) * 100.0).toFixed(2));  
  31.             this.setHtml(storeItem.get('cat') + ': ' + value+ '%');  
  32.         }  
  33.       }  
  34.     } ]  
  35.   } ]  
  36. });  

下面的图片显示了上面代码的输出:

 1

费用分析器 – 示例项目

又是项目时间,现在你已经了解了 ExtJS 中不同的图表类型,我们来创建一个示例项目名为 费用分析器。下面是最终设计效果:

 1

1

我们使用 Sencha Cmd 来构建应用。运行下列命令:

[plain]  view plain  copy
  1. sencha -sdk <path to SDK>/ext-6.0.0.415/ generate app EA ./expenseanalyzer  

之后我们移除所有不必要的文件和代码,添加一些额外的文件。目录结构如下:

 1

下列代码是创建 grid 。这个 List 视图继承自 Ext.grid.Panel ,数据使用 expense store ,它有三列:

[javascript]  view plain  copy
  1. Ext.define('EA.view.main.List', {  
  2.   extend : 'Ext.grid.Panel',  
  3.   xtype : 'mainlist',  
  4.   maxHeight : 400,  
  5.   requires : [ 'EA.store.Expense' ],  
  6.   title : 'Year to date expense by category',  
  7.   store : {  
  8.     type : 'expense'  
  9.   },  
  10.   columns : {  
  11.     defaults : {  
  12.       flex : 1  
  13.     },  
  14.     items : [ {  
  15.       text : 'Category',  
  16.       dataIndex : 'cat'  
  17.     }, {  
  18.       formatter : "date('F')",  
  19.       text : 'Month',  
  20.       dataIndex : 'date'  
  21.     }, {  
  22.       text : 'Spent',  
  23.       dataIndex : 'spent'  
  24.     } ]  
  25.   }  
  26. });  

我并没有在这里使用分页。maxHeight 是用于限制 grid 的高度,同时开启滚动条,因为有更多的数据。

以下代码创建了 expense store 。这个 store 使用了内嵌数据。这里我们并没有单独为 store 创建 model :

[javascript]  view plain  copy
  1. Ext.define('EA.store.Expense', {  
  2.   extend : 'Ext.data.Store',  
  3.   alias : 'store.expense',  
  4.   storeId : 'expense',  
  5.   fields : [ {  
  6.     name : 'date',  
  7.     type : 'date'  
  8.   },  
  9.   'cat',  
  10.   'spent'  
  11.   ],  
  12.   data : {  
  13.     items : [{  
  14.       "date" : "1/1/2015",  
  15.       "cat" : "Restaurant",  
  16.       "spent" : 100  
  17.     },{  
  18.       "date" : "1/1/2015",  
  19.       "cat" : "Travel",  
  20.       "spent" : 22  
  21.     },{  
  22.       "date" : "1/1/2015",  
  23.       "cat" : "Insurance",  
  24.       "spent" : 343  
  25.     }]  
  26.   },  
  27.   proxy : {  
  28.     type : 'memory',  
  29.     reader : {  
  30.       type : 'json',  
  31.       rootProperty : 'items'  
  32.     }  
  33.   }  
  34. });  

继续创建柱状图。在柱状图中我们将使用另外一个 store 叫做 expensebyMonth store ,我们将从 expense 的数据填充进来。

下列 3D 柱状图有两个 axis 类型: numeric 和 category 。我们使用日期字段的月部分作为 category 。renderer 属性用于呈现日期字段的月份部分:

[javascript]  view plain  copy
  1. Ext.define('EA.view.main.Bar', {  
  2.   extend : 'Ext.chart.CartesianChart',  
  3.   requires : [ 'Ext.chart.axis.Category',  
  4.   'Ext.chart.series.Bar3D',  
  5.   'Ext.chart.axis.Numeric',  
  6.   'Ext.chart.interactions.ItemHighlight' ],  
  7.   xtype : 'mainbar',  
  8.   height : 500,  
  9.   padding : {  
  10.     top : 50,  
  11.     bottom : 20,  
  12.     left : 100,  
  13.     right : 100  
  14.   },  
  15.   legend : {  
  16.     docked : 'bottom'  
  17.   },  
  18.   insetPadding : {  
  19.     top : 100,  
  20.     bottom : 20,  
  21.     left : 20,  
  22.     right : 40  
  23.   },  
  24.   store : {  
  25.     type : 'expensebyMonthStore'  
  26.   },  
  27.   axes : [ {  
  28.     type : 'numeric',  
  29.     position : 'left',  
  30.     grid : true,  
  31.     minimum : 0,  
  32.     title : {  
  33.       text : 'Spendings in $',  
  34.       fontSize : 16  
  35.     }  
  36.   }, {  
  37.     type : 'category',  
  38.     position : 'bottom',  
  39.     title : {  
  40.       text : 'Month',  
  41.       fontSize : 16  
  42.     },  
  43.     label : {  
  44.       font : 'bold Arial',  
  45.       rotate : {  
  46.         degrees : 300  
  47.       }  
  48.     },  
  49.     renderer : function(date) {  
  50.       return [ "Jan""Feb""Mar""Apr""May" ][date.getMonth()];  
  51.     }  
  52.   } ],  
  53.   series : [ {  
  54.     type : 'bar3d',  
  55.     xField : 'date',  
  56.     stacked : false,  
  57.     title : [ 'Total' ],  
  58.     yField : [ 'total' ]  
  59.   } ],  
  60.   sprites : [ {  
  61.     type : 'text',  
  62.     text : 'Expense by Month',  
  63.     font : '20px Helvetica',  
  64.     width : 120,  
  65.     height : 35,  
  66.     x : 60,  
  67.     y : 40  
  68.   } ]  
  69. });  

现在为上面的柱状图创建 store 。model 为 MyApp.model.ExpensebyMonth 。这个 store 将用来显示每个月的花费总数。数据是通过对 expense store 的日期字段进行分组后填充的,现在我们瞧一瞧 data 属性是如何配置填充数据的:

[javascript]  view plain  copy
  1. Ext.define('MyApp.model.ExpensebyMonth', {  
  2.   extend : 'Ext.data.Model',  
  3.   fields : [ {  
  4.     name : 'date',  
  5.     type : 'date'  
  6.   }, 'total' ]  
  7. });  
  8.    
  9. Ext.define('MyApp.store.ExpensebyMonth', {  
  10.   extend : 'Ext.data.Store',  
  11.   alias : 'store.expensebyMonthStore',  
  12.   model : 'MyApp.model.ExpensebyMonth',  
  13.   data : (function() {  
  14.     var data = [];  
  15.     var expense = Ext.createByAlias('store.expense');  
  16.     expense.group('date');  
  17.     var groups = expense.getGroups();  
  18.     groups.each(function(group) {  
  19.       data.push({  
  20.         date : group.config.groupKey,  
  21.         total :group.sum('spent')  
  22.       });  
  23.     });  
  24.     return data;  
  25.   })()  
  26. });  

以下代码用于生成饼图。这个图表使用的的 store 是 expense ,但只显示了一次选择一个月的数据。在主视图上添加了一个下拉框用于选择月份。

这个 beforerender 事件是用于过滤 expense store 里用于显示的数据,只加载一月份的数据:

[javascript]  view plain  copy
  1. Ext.define('EA.view.main.Pie', {  
  2.   extend : 'Ext.chart.PolarChart',  
  3.   requires : [ 'Ext.chart.series.Pie3D' ],  
  4.   xtype : 'mainpie',  
  5.   height : 800,  
  6.   legend : {  
  7.     docked : 'bottom'  
  8.   },  
  9.   insetPadding : {  
  10.     top : 100,  
  11.     bottom : 20,  
  12.     left : 80,  
  13.     right : 80  
  14.   },  
  15.   listeners : {  
  16.     beforerender : function() {  
  17.       var dateFiter = new Ext.util.Filter({  
  18.         filterFn : function(item) {  
  19.           return item.data.date.getMonth() == 0;  
  20.         }  
  21.       });  
  22.       Ext.getStore('expense').addFilter(dateFiter);  
  23.     }  
  24.   },  
  25.   store : {  
  26.     type : 'expense'  
  27.   },  
  28.   series : [ {  
  29.     type : 'pie3d',  
  30.     donut : 50,  
  31.     thickness : 70,  
  32.     distortion : 0.5,  
  33.     angleField : 'spent',  
  34.     label : {  
  35.       field : 'cat',  
  36.     }  
  37.   } ]  
  38. });  

截止目前,我们创建好了 grid ,柱状图,饼图,和这个应用所需要的 store 。现在需要在主视图上把他们联系起来。以下代码展示了 main 视图的经典工具包里的片段。main 视图是一个选项卡控件,为每个选项卡指定视图:

[javascript]  view plain  copy
  1. Ext.define('EA.view.main.Main', {  
  2.   extend : 'Ext.tab.Panel',  
  3.   xtype : 'app-main',  
  4.   requires : [  
  5.   'Ext.plugin.Viewport',  
  6.   'Ext.window.MessageBox',  
  7.   'EA.view.main.MainController',  
  8.   'EA.view.main.List',  
  9.   'EA.view.main.Bar',  
  10.   'EA.view.main.Pie'  
  11.   ],  
  12.   controller : 'main',  
  13.   autoScroll : true,  
  14.   ui : 'navigation',  
  15.   // Truncated code  
  16.   items : [ {  
  17.     title : 'Year to Date',  
  18.     iconCls : 'fa-bar-chart',  
  19.     items : [ {  
  20.       html : '<h3>Your average expense per month is: ' +Ext.createByAlias('store.expensebyMonthStore').average('total') +'</h3>',  
  21.       height : 70,  
  22.     },{  
  23.       xtype : 'mainlist'  
  24.     },{  
  25.       xtype : 'mainbar'  
  26.     }]  
  27.   },{  
  28.     title : 'By Month',  
  29.     iconCls : 'fa-pie-chart',  
  30.     items : [ {  
  31.       xtype : 'combo',  
  32.       value : 'Jan',  
  33.       fieldLabel : 'Select Month',  
  34.       store : [ 'Jan''Feb''Mar''Apr''May' ],  
  35.       listeners : {  
  36.         select : 'onMonthSelect'  
  37.       }  
  38.     }, {  
  39.       xtype : 'mainpie'  
  40.     } ]  
  41.   } ]  
  42. });  


总结

在本章中,我们在 ExtJS 中学习到不同的图表。并且创建了一个示例项目来演示他们的应用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值