• ExtJS - UI组件 - Chart


    更新记录
    转载请注明出处:https://www.cnblogs.com/cqpanda/p/16587398.html
    2022年8月16日 发布。
    2022年8月13日 从笔记迁移到博客。

    ExtJS教程汇总:https://www.cnblogs.com/cqpanda/p/16328016.html

    Chart(图表)说明

    图表的类型(Chart types)

    说明

    three types of charts: cartesian, polar, and spacefilling(笛卡尔、极坐标和空间填充)

    cartesian chart

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

    A cartesian chart has two directions: X and Y. By default, X is horizontal and Y is vertical. Charts that use the cartesian coordinates are column, bar, area, line, and scatter

    polar chart

    Ext.chart.PolarChart (xtype: polar)

    These charts have two axes: angular and radial. Charts that plot values using the
    polar coordinates are pie and radar

    spacefilling chart

    Ext.chart.SpaceFillingChart (xtype: spacefilling)

    These charts fill the complete area of the chart

    图表的组成

    image

    Series定义图形的内容
    Axes定义图表的轴
    Legend定义图表的图例
    Labels定义图表的标签
    Interactions定义图表的交互
    Sprite定义图表的标题等元素

    定义Legend

    1. legend: {
    2. docked: 'left' //可取值left, top, bottom, right
    3. }

    使用前准备

    如果是直接引入的CSS和JS方式进行ExtJS开发
    将build目录下的packages目录复制到开发目录下
    需要额外引入图形组件的css和js文件

    1. <link rel="stylesheet" href=/packages/charts/modern/modern-material/resources/charts-all-debug.css">
    2. <script src="/packages/charts/modern/charts-debug.js">script>

    常用图表

    折线图(Line Chart)

    语法:

    1. Ext.create('Ext.chart.CartesianChart',{
    2. series: [{
    3. type: 'line',
    4. xField: 'name',
    5. yField: 'G1'
    6. }]
    7. //render, legend and other properties
    8. });

    条形图(Bar Chart)

    1. Ext.create('Ext.chart.CartesianChart',{
    2. series: [{
    3. type: 'bar',
    4. xField: 'name',
    5. yField: 'G1'
    6. }]
    7. //render, legend and other properties
    8. });

    饼图(Pie Chart)

    Ext.chart.PolarChart (xtype: polar)

    语法:

    1. Ext.create('Ext.chart.PolarChart', {
    2. series: [{
    3. Type: 'pie'
    4. ..
    5. }]
    6. //render and other properties.
    7. });

    面积图(Area Chart)

    1. Ext.create('Ext.chart.CartesianChart',{
    2. series: [{
    3. type: 'area',
    4. xField: 'name',
    5. yField: 'G1'
    6. }]
    7. //render, legend and other properties
    8. });

    实例:柱状图

    实例:绘制柱状图(bar chart)

    image

    1. //创建对应的模型
    2. Ext.define('PandaApp.model.WebSiteVisitor', {
    3. extend: 'Ext.data.Model',
    4. fields: [
    5. {
    6. name: 'name',
    7. type: 'string'
    8. },
    9. {
    10. name: 'value',
    11. type: 'int'
    12. }
    13. ]
    14. });
    15. //创建对应的数据存储
    16. Ext.define('PandaApp.store.WebSiteVisitors', {
    17. extend: 'Ext.data.Store',
    18. model: 'PandaApp.model.WebSiteVisitor',
    19. data: [
    20. { name: '1', value: 50 },
    21. { name: '2', value: 154 },
    22. { name: '3', value: 166 },
    23. { name: '4', value: 189 },
    24. { name: '5', value: 199 },
    25. { name: '6', value: 175 },
    26. { name: '7', value: 165 },
    27. { name: '8', value: 126 },
    28. { name: '9', value: 112 },
    29. { name: '10', value: 109 },
    30. { name: '11', value: 87 },
    31. ]
    32. });
    33. //实例化
    34. var store = Ext.create('PandaApp.store.WebSiteVisitors');
    35. //创建图形
    36. Ext.define('PandaApp.view.chart.BarChart', {
    37. extend: 'Ext.chart.Chart',
    38. xtype: 'chart-BarChart',
    39. config: {
    40. animate: true,
    41. store : store,
    42. axes : [
    43. {
    44. type : 'numeric',
    45. position: 'left',
    46. fields : ['value'],
    47. title : 'Value'
    48. },
    49. {
    50. type : 'category',
    51. position: 'bottom',
    52. fields : ['name'],
    53. title : 'Name'
    54. }
    55. ],
    56. series : [
    57. {
    58. type : 'bar',
    59. axis : 'bottom',
    60. xField: 'name',
    61. yField: 'value'
    62. }
    63. ]
    64. }
    65. });
    66. Ext.create('PandaApp.view.chart.BarChart',{
    67. renderTo: Ext.getBody(),
    68. width: 800,
    69. height: 600
    70. });

    实例:绘制柱状图2(bar chart)

    image

    1. //定义测试用的模型
    2. Ext.define('PandaApp.model.Population', {
    3. extend: 'Ext.data.Model',
    4. fields: ['year', 'population']
    5. });
    6. //定义测试用的Store
    7. Ext.define('PandaApp.store.Population', {
    8. extend: 'Ext.data.Store',
    9. storeId: 'population',
    10. model: 'PandaApp.model.Population',
    11. data: [ //内联数据
    12. { "year": "1610","population": 350 },
    13. { "year": "1650","population": 50368 },
    14. { "year": "1700", "population": 250888 },
    15. { "year": "1750","population": 1170760 },
    16. { "year": "1800","population": 5308483 },
    17. { "year": "1900","population": 76212168 },
    18. { "year": "1950","population": 151325798 },
    19. { "year": "2000","population": 281421906 },
    20. { "year": "2010","population": 308745538 },
    21. ]
    22. });
    23. //创建Store实例
    24. var store = Ext.create("PandaApp.store.Population");
    25. //创建容器存放图表组件
    26. Ext.create('Ext.container.Container', {
    27. renderTo: Ext.getBody(),
    28. width: 500,
    29. height: 500,
    30. layout: 'fit',
    31. items: [
    32. {
    33. xtype: 'chart', //图表组件
    34. insetPadding: { top: 60, bottom: 20, left: 20, right: 40 },
    35. store: store, //指定关联的Store
    36. axes: [ //创建轴
    37. {
    38. type: 'numeric', //数值类型的轴
    39. position: 'left', //位置:左侧
    40. grid: true, //开启网格
    41. title: { text: 'Population in Millions', fontSize: 16 },
    42. //指定轴描述
    43. },
    44. {
    45. type: 'category', //分类类型的轴
    46. title: { text: 'Year', fontSize: 16 }, //指定轴的描述
    47. position: 'bottom', //指定轴的位置
    48. }
    49. ],
    50. series: [ //指定内容
    51. {
    52. type: 'bar', //指定类型为栏
    53. xField: 'year', //x轴对应的Store数据字段
    54. yField: ['population'] //y轴对应的Store数据字段
    55. }
    56. ],
    57. sprites: {
    58. type: 'text',
    59. text: 'United States Population', //指定标题
    60. font: '25px Helvetica',
    61. width: 120,
    62. height: 35,
    63. x: 100,
    64. y: 40
    65. }
    66. }
    67. ]
    68. });

    实例:绘制柱状图3

    image

    代码:

    1. //创建测试使用Store
    2. var myChartStore = Ext.create('Ext.data.ArrayStore',{
    3. storeId:'salesStore', //定义Store的Id
    4. fields:[ //定义字段
    5. {name: 'id', type: 'int'},
    6. {name: 'region', type: 'string'},
    7. {name: 'sales', type: 'float'} ,
    8. {name: 'salesb', type: 'float'}
    9. ],
    10. data: [ //定义内联数据
    11. [10001 ,"North", 1500.55 , 1450.66 ],
    12. [10002 ,"South", 2344.99 , 3200.45 ],
    13. [10003 ,"East", 1750.44 , 950.55 ],
    14. [10004 ,"West", 3000.00 , 3200.55 ],
    15. [10005 ,"Central", 4523.45 , 1963.44 ],
    16. [10006 ,"OverSeas", 2489.55, 2786.12 ]
    17. ]
    18. });
    19. //创建图表
    20. var mychart= Ext.create('Ext.chart.CartesianChart', {
    21. renderTo: Ext.getBody(),
    22. width: 500,
    23. height: 600,
    24. store: myChartStore, //关联的Store
    25. insetPadding: { //配置内边距
    26. top: 50,
    27. left: 25,
    28. right: 25,
    29. bottom: 15
    30. },
    31. interactions: 'itemhighlight', //配置交互
    32. axes: [ //配置轴信息
    33. {
    34. type: 'numeric', //配置轴的类型
    35. position: 'left', //配置轴的位置
    36. title: { //配置轴的标题
    37. text: 'Sales 1st to 3th Quarter', //配置轴标题的文本
    38. fontSize: 14, //配置轴标题的字体
    39. fillStyle:'#0d7179' //配置轴标题的颜色
    40. },
    41. fields: 'sales' //配置轴关联的字段
    42. },
    43. {
    44. type: 'category', //配置轴的类型
    45. position: 'bottom', //配置轴的位置
    46. title: {
    47. text: 'Sales by Branch',
    48. fontSize: 18,
    49. fillStyle:'#277cc0'
    50. },
    51. fields: 'region'
    52. }
    53. ],
    54. series: { //配置图表的内容
    55. type: 'bar', //图表类型为柱状图
    56. title:['Main branch','Branch B'], //标题
    57. xField: 'region', //x轴对应的数据
    58. yField: 'sales', //y轴对应的数据
    59. style:{ //配置风格
    60. strokeStyle: '#999999',
    61. fillStyle: '#cccc99'
    62. },
    63. highlight:{ //配置高亮
    64. strokeStyle: '#990000',
    65. fillStyle: '#ffcc66',
    66. lineDash: [5, 3]
    67. },
    68. label: { //配置标签
    69. field:'sales',
    70. display:'insideEnd'
    71. }
    72. },
    73. sprites: {
    74. type: 'text',
    75. text: 'My Company - 2015',
    76. fontSize: 22,
    77. fillStyle: '#993366',
    78. width: 100,
    79. height: 30,
    80. x: 40, // the sprite x position
    81. y: 25 // the sprite y position
    82. }
    83. });

    实例:绘制柱状图(横向)

    image

    添加翻转配置项到chart中

    flipXY: true,     //翻转XY轴

    在轴配置上,翻转对应的轴的位置即可

    1. //定义测试用的模型
    2. Ext.define('PandaApp.model.Population', {
    3. extend: 'Ext.data.Model',
    4. fields: ['year', 'population']
    5. });
    6. //定义测试用的Store
    7. Ext.define('PandaApp.store.Population', {
    8. extend: 'Ext.data.Store',
    9. storeId: 'population',
    10. model: 'PandaApp.model.Population',
    11. data: [ //内联数据
    12. { "year": "1610","population": 350 },
    13. { "year": "1650","population": 50368 },
    14. { "year": "1700", "population": 250888 },
    15. { "year": "1750","population": 1170760 },
    16. { "year": "1800","population": 5308483 },
    17. { "year": "1900","population": 76212168 },
    18. { "year": "1950","population": 151325798 },
    19. { "year": "2000","population": 281421906 },
    20. { "year": "2010","population": 308745538 },
    21. ]
    22. });
    23. //创建Store实例
    24. var store = Ext.create("PandaApp.store.Population");
    25. //创建容器存放图表组件
    26. Ext.create('Ext.container.Container', {
    27. renderTo: Ext.getBody(),
    28. width: 500,
    29. height: 500,
    30. layout: 'fit',
    31. items: [
    32. {
    33. xtype: 'chart', //图表组件
    34. flipXY: true, //翻转XY轴
    35. insetPadding: { top: 60, bottom: 20, left: 20, right: 40 },
    36. store: store, //指定关联的Store
    37. axes: [ //创建轴
    38. {
    39. type: 'numeric', //数值类型的轴
    40. position: 'bottom', //位置:左侧
    41. grid: true, //开启网格
    42. title: { text: 'Population in Millions', fontSize: 16 },
    43. //指定轴描述
    44. },
    45. {
    46. type: 'category', //分类类型的轴
    47. title: { text: 'Year', fontSize: 16 }, //指定轴的描述
    48. position: 'left', //指定轴的位置
    49. }
    50. ],
    51. series: [ //指定内容
    52. {
    53. type: 'bar', //指定类型为栏
    54. xField: 'year', //x轴对应的Store数据字段
    55. yField: ['population'] //y轴对应的Store数据字段
    56. }
    57. ],
    58. sprites: {
    59. type: 'text',
    60. text: 'United States Population', //指定标题
    61. font: '25px Helvetica',
    62. width: 120,
    63. height: 35,
    64. x: 100,
    65. y: 40
    66. }
    67. }
    68. ]
    69. });

    实例:绘制柱状图(多层堆叠)

    image

    代码:

    1. //定义测试使用的模型
    2. Ext.define('PandaApp.model.Population', {
    3. extend: 'Ext.data.Model',
    4. fields: ['year', 'total','slaves']
    5. });
    6. //定义测试使用的数据仓库
    7. Ext.define('PandaApp.store.Population', {
    8. extend: 'Ext.data.Store',
    9. storeId: 'population',
    10. model: 'PandaApp.model.Population',
    11. data: [
    12. { "year": "1790", "total": 3.9, "slaves": 0.7 },
    13. { "year": "1800", "total": 5.3, "slaves": 0.9 },
    14. { "year": "1810", "total": 7.2, "slaves": 1.2 },
    15. { "year": "1820", "total": 9.6, "slaves": 1.5 },
    16. { "year": "1830", "total": 12.9, "slaves": 2 },
    17. { "year": "1840", "total": 17, "slaves": 2.5 },
    18. { "year": "1850", "total": 23.2, "slaves": 3.2 },
    19. { "year": "1860", "total": 31.4, "slaves": 4 },
    20. ]
    21. });
    22. //创建数据仓库实例
    23. var store = Ext.create("PandaApp.store.Population");
    24. //创建容器存放图表组件
    25. Ext.create('Ext.container.Container', {
    26. renderTo: Ext.getBody(),
    27. width: 500,
    28. height: 500,
    29. layout: 'fit',
    30. items: [
    31. {
    32. xtype: 'cartesian', //设置图表组件的类型
    33. store: store, //设置关联的Store
    34. insetPadding: { top: 60, bottom: 20, left: 20, right: 40 },
    35. axes: [ //配置轴
    36. {
    37. type: 'numeric', //数值轴
    38. position: 'left', //配置轴位置左侧
    39. grid: true, //开启网格
    40. title: { text: 'Population in Millions', fontSize: 16 },
    41. //轴描述
    42. },
    43. {
    44. type: 'category', //分类类型轴
    45. title: { text: 'Year', fontSize: 16 }, //轴描述
    46. position: 'bottom', //配置轴位置底部
    47. }
    48. ],
    49. series: [ //配置图表内容
    50. {
    51. type: 'bar', //
    52. xField: 'year', //X轴对应的Store字段
    53. yField: ['total','slaves'] //y轴对应的Store字段
    54. }
    55. ],
    56. sprites: { //图表的标题
    57. type: 'text',
    58. text: 'United States Slaves Distribution 1790 to 1860',
    59. font: '20px Helvetica',
    60. width: 120,
    61. height: 35,
    62. x: 60,
    63. y: 40
    64. }
    65. }
    66. ]
    67. });

    实例:绘制柱状图(多层并排)

    image

    代码:

    1. //定义测试使用的模型
    2. Ext.define('PandaApp.model.Population', {
    3. extend: 'Ext.data.Model',
    4. fields: ['year', 'total','slaves']
    5. });
    6. //定义测试使用的数据仓库
    7. Ext.define('PandaApp.store.Population', {
    8. extend: 'Ext.data.Store',
    9. storeId: 'population',
    10. model: 'PandaApp.model.Population',
    11. data: [
    12. { "year": "1790", "total": 3.9, "slaves": 0.7 },
    13. { "year": "1800", "total": 5.3, "slaves": 0.9 },
    14. { "year": "1810", "total": 7.2, "slaves": 1.2 },
    15. { "year": "1820", "total": 9.6, "slaves": 1.5 },
    16. { "year": "1830", "total": 12.9, "slaves": 2 },
    17. { "year": "1840", "total": 17, "slaves": 2.5 },
    18. { "year": "1850", "total": 23.2, "slaves": 3.2 },
    19. { "year": "1860", "total": 31.4, "slaves": 4 },
    20. ]
    21. });
    22. //创建数据仓库实例
    23. var store = Ext.create("PandaApp.store.Population");
    24. //创建容器存放图表组件
    25. Ext.create('Ext.container.Container', {
    26. renderTo: Ext.getBody(),
    27. width: 500,
    28. height: 500,
    29. layout: 'fit',
    30. items: [
    31. {
    32. xtype: 'chart',
    33. legend: { docked: 'bottom' },
    34. insetPadding: { top: 60, bottom: 20, left: 20, right: 40 },
    35. store: store,
    36. axes: [
    37. {
    38. type: 'numeric',
    39. position: 'left',
    40. grid: true,
    41. title: { text: 'Population in Millions', fontSize: 16 },
    42. minimum: 0,
    43. },
    44. {
    45. type: 'category',
    46. title: { text: 'Year', fontSize: 16 },
    47. position: 'bottom',
    48. }
    49. ],
    50. series: [
    51. {
    52. type: 'bar',
    53. xField: 'year',
    54. stacked: false,
    55. title: ['Total', 'Slaves'],
    56. yField: ['total', 'slaves'],
    57. tooltip: {
    58. trackMouse: true,
    59. style: 'background: #fff',
    60. renderer: function (storeItem, item) {
    61. this.setHtml('In ' + storeItem.get('year') + ' ' + item.field + ' population was ' + storeItem.get(item.field) + ' m');
    62. }
    63. }
    64. }
    65. ],
    66. sprites: [
    67. {
    68. type: 'text',
    69. text: 'United States Slaves Distribution 1790 to 1860',
    70. font: '20px Helvetica',
    71. width: 120,
    72. height: 35,
    73. x: 60,
    74. y: 40
    75. },
    76. {
    77. type: 'text',
    78. text: 'Source: http://www.wikipedia.org',
    79. fontSize: 10,
    80. x: 12,
    81. y: 440
    82. }
    83. ]
    84. }
    85. ]
    86. });

    实例:绘制柱状图(3d)

    image

    代码:

    1. //定义测试使用的模型
    2. Ext.define('PandaApp.model.Population', {
    3. extend: 'Ext.data.Model',
    4. fields: ['year', 'total','slaves']
    5. });
    6. //定义测试使用的数据仓库
    7. Ext.define('PandaApp.store.Population', {
    8. extend: 'Ext.data.Store',
    9. storeId: 'population',
    10. model: 'PandaApp.model.Population',
    11. data: [
    12. { "year": "1790", "total": 3.9, "slaves": 0.7 },
    13. { "year": "1800", "total": 5.3, "slaves": 0.9 },
    14. { "year": "1810", "total": 7.2, "slaves": 1.2 },
    15. { "year": "1820", "total": 9.6, "slaves": 1.5 },
    16. { "year": "1830", "total": 12.9, "slaves": 2 },
    17. { "year": "1840", "total": 17, "slaves": 2.5 },
    18. { "year": "1850", "total": 23.2, "slaves": 3.2 },
    19. { "year": "1860", "total": 31.4, "slaves": 4 },
    20. ]
    21. });
    22. //创建数据仓库实例
    23. var store = Ext.create("PandaApp.store.Population");
    24. //创建容器存放图表组件
    25. Ext.create('Ext.container.Container', {
    26. renderTo: Ext.getBody(),
    27. width: 500,
    28. height: 500,
    29. layout: 'fit',
    30. items: [
    31. {
    32. xtype: 'chart',
    33. legend: { docked: 'bottom' },
    34. insetPadding: { top: 60, bottom: 20, left: 20, right: 40 },
    35. store: store,
    36. axes: [
    37. {
    38. type: 'numeric',
    39. position: 'left',
    40. grid: true,
    41. title: { text: 'Population in Millions', fontSize: 16 },
    42. minimum: 0,
    43. },
    44. {
    45. type: 'category3d', //切换为3d分类类型
    46. title: { text: 'Year', fontSize: 16 },
    47. position: 'bottom',
    48. }
    49. ],
    50. series: [
    51. {
    52. type: 'bar3d', //切换为3d图形
    53. xField: 'year',
    54. stacked: false,
    55. title: ['Total', 'Slaves'],
    56. yField: ['total', 'slaves'],
    57. tooltip: {
    58. trackMouse: true,
    59. style: 'background: #fff',
    60. renderer: function (storeItem, item) {
    61. this.setHtml('In ' + storeItem.get('year') + ' ' + item.field + ' population was ' + storeItem.get(item.field) + ' m');
    62. }
    63. }
    64. }
    65. ],
    66. sprites: [
    67. {
    68. type: 'text',
    69. text: 'United States Slaves Distribution 1790 to 1860',
    70. font: '20px Helvetica',
    71. width: 120,
    72. height: 35,
    73. x: 60,
    74. y: 40
    75. },
    76. {
    77. type: 'text',
    78. text: 'Source: http://www.wikipedia.org',
    79. fontSize: 10,
    80. x: 12,
    81. y: 440
    82. }
    83. ]
    84. }
    85. ]
    86. });

    实例:面积图

    实例:绘制面积图

    image

    代码:

    1. //定义测试使用的模型
    2. Ext.define('PandaApp.model.Population', {
    3. extend: 'Ext.data.Model',
    4. fields: ['year', 'total','slaves']
    5. });
    6. //定义测试使用的数据仓库
    7. Ext.define('PandaApp.store.Population', {
    8. extend: 'Ext.data.Store',
    9. storeId: 'population',
    10. model: 'PandaApp.model.Population',
    11. data: [
    12. { "year": "1790", "total": 3.9, "slaves": 0.7 },
    13. { "year": "1800", "total": 5.3, "slaves": 0.9 },
    14. { "year": "1810", "total": 7.2, "slaves": 1.2 },
    15. { "year": "1820", "total": 9.6, "slaves": 1.5 },
    16. { "year": "1830", "total": 12.9, "slaves": 2 },
    17. { "year": "1840", "total": 17, "slaves": 2.5 },
    18. { "year": "1850", "total": 23.2, "slaves": 3.2 },
    19. { "year": "1860", "total": 31.4, "slaves": 4 },
    20. ]
    21. });
    22. //创建数据仓库实例
    23. var store = Ext.create("PandaApp.store.Population");
    24. //创建容器存放图表组件
    25. Ext.create('Ext.container.Container', {
    26. renderTo: Ext.getBody(),
    27. width: 500,
    28. height: 500,
    29. layout: 'fit',
    30. items: [
    31. {
    32. xtype: 'chart',
    33. legend: { docked: 'bottom' },
    34. insetPadding: { top: 60, bottom: 20, left: 20, right: 40 },
    35. store: store,
    36. axes: [
    37. {
    38. type: 'numeric',
    39. position: 'left',
    40. grid: true,
    41. title: { text: 'Population in Millions', fontSize: 16 },
    42. minimum: 0,
    43. },
    44. {
    45. type: 'category',
    46. title: { text: 'Year', fontSize: 16 },
    47. position: 'bottom',
    48. }
    49. ],
    50. series: [
    51. {
    52. type: 'area', //面积图
    53. xField: 'year',
    54. stacked: false,
    55. title: ['Total','slaves'],
    56. yField: ['total', 'slaves'],
    57. style: {
    58. stroke: "#94ae0a",
    59. fillOpacity: 0.6,
    60. },
    61. tooltip: {
    62. trackMouse: true,
    63. style: 'background: #fff',
    64. renderer: function (storeItem, item) {
    65. this.setHtml('In ' + storeItem.get('year') + ' ' + item.field + ' population was ' + storeItem.get(item.field) + ' m');
    66. }
    67. }
    68. }
    69. ],
    70. sprites: [
    71. {
    72. type: 'text',
    73. text: 'United States Slaves Distribution 1790 to 1860',
    74. font: '20px Helvetica',
    75. width: 120,
    76. height: 35,
    77. x: 60,
    78. y: 40
    79. },
    80. {
    81. type: 'text',
    82. text: 'Source: http://www.wikipedia.org',
    83. fontSize: 10,
    84. x: 12,
    85. y: 440
    86. }
    87. ]
    88. }
    89. ]
    90. });

    实例:绘制折线图

    实例:绘制折线图(单条)

    image

    代码:

    1. //创建对应的模型
    2. Ext.define('PandaApp.model.WebSiteVisitor', {
    3. extend: 'Ext.data.Model',
    4. fields: [
    5. { name: 'Time', type: 'int' },
    6. { name: 'Visitors', type: 'int' }
    7. ]
    8. });
    9. //创建对应的数据存储
    10. Ext.define('PandaApp.store.WebSiteVisitors', {
    11. extend: 'Ext.data.Store',
    12. model: 'PandaApp.model.WebSiteVisitor',
    13. data: [ //直接内联数据
    14. { Time: '1', Visitors: 50 },
    15. { Time: '2', Visitors: 154 },
    16. { Time: '3', Visitors: 166 },
    17. { Time: '4', Visitors: 189 },
    18. { Time: '5', Visitors: 199 },
    19. { Time: '6', Visitors: 175 },
    20. { Time: '7', Visitors: 165 },
    21. { Time: '8', Visitors: 126 },
    22. { Time: '9', Visitors: 112 },
    23. { Time: '10', Visitors: 109 },
    24. { Time: '11', Visitors: 87 },
    25. ]
    26. });
    27. //创建数据存储实例
    28. var store = Ext.create('PandaApp.store.WebSiteVisitors');
    29. //创建图形
    30. Ext.define('PandaApp.view.chart.SiteVisits', {
    31. extend: 'Ext.chart.CartesianChart',
    32. xtype: 'chart-SiteVisits',
    33. config: {
    34. animate: true, //开启动画效果
    35. store : store, //定义关联Store
    36. series : [ //定义图形类型
    37. {
    38. type : 'line', //折线图
    39. smooth: true, //是否平滑
    40. axis : 'left', //轴的位置
    41. xField: 'Time', //x轴的标题
    42. yField: 'Visitors' //y轴的标题
    43. }
    44. ],
    45. axes : [ //定义轴
    46. {
    47. type : 'numeric', //数值类型的轴
    48. grid : true, //开启网格
    49. position : 'left', //位置
    50. fields : ['Visitors'], //内容
    51. title : 'Visitors', //标题
    52. minimum : 0, //最小值
    53. maximum : 200, //最大值
    54. majorTickSteps: 5
    55. },
    56. {
    57. type : 'numeric',
    58. position : 'bottom',
    59. fields : 'Time',
    60. title : 'Time',
    61. minimum : 0,
    62. maximum : 20,
    63. decimals : 0,
    64. constrain : true,
    65. majorTickSteps: 20
    66. }
    67. ]
    68. }
    69. });
    70. //实例化图形
    71. Ext.create('PandaApp.view.chart.SiteVisits',{
    72. renderTo: Ext.getBody(),
    73. width: 800,
    74. height: 600
    75. });

    实例:绘制折线图(多条)

    image

    代码:

    1. //定义测试使用的模型
    2. Ext.define('PandaApp.model.Population', {
    3. extend: 'Ext.data.Model',
    4. fields: ['year', 'total','slaves']
    5. });
    6. //定义测试使用的数据仓库
    7. Ext.define('PandaApp.store.Population', {
    8. extend: 'Ext.data.Store',
    9. storeId: 'population',
    10. model: 'PandaApp.model.Population',
    11. data: [
    12. { "year": "1790", "total": 3.9, "slaves": 0.7 },
    13. { "year": "1800", "total": 5.3, "slaves": 0.9 },
    14. { "year": "1810", "total": 7.2, "slaves": 1.2 },
    15. { "year": "1820", "total": 9.6, "slaves": 1.5 },
    16. { "year": "1830", "total": 12.9, "slaves": 2 },
    17. { "year": "1840", "total": 17, "slaves": 2.5 },
    18. { "year": "1850", "total": 23.2, "slaves": 3.2 },
    19. { "year": "1860", "total": 31.4, "slaves": 4 },
    20. ]
    21. });
    22. //创建数据仓库实例
    23. var store = Ext.create("PandaApp.store.Population");
    24. //创建容器存放图表组件
    25. Ext.create('Ext.container.Container', {
    26. renderTo: Ext.getBody(),
    27. width: 500,
    28. height: 500,
    29. layout: 'fit',
    30. items: [
    31. {
    32. xtype: 'chart',
    33. legend: { docked: 'bottom' },
    34. insetPadding: { top: 60, bottom: 20, left: 20, right: 40 },
    35. store: store,
    36. axes: [
    37. {
    38. type: 'numeric',
    39. position: 'left',
    40. grid: true,
    41. title: { text: 'Population in Millions', fontSize: 16 },
    42. minimum: 0,
    43. },
    44. {
    45. type: 'category', //
    46. title: { text: 'Year', fontSize: 16 },
    47. position: 'bottom',
    48. }
    49. ],
    50. series: [
    51. {
    52. type: 'line',
    53. xField: 'year',
    54. title: ['Total'],
    55. yField: ['total']
    56. },
    57. {
    58. type: 'line',
    59. xField: 'year',
    60. title: ['Slaves'],
    61. yField: ['slaves'],
    62. },
    63. ],
    64. sprites: [
    65. {
    66. type: 'text',
    67. text: 'United States Slaves Distribution 1790 to 1860',
    68. font: '20px Helvetica',
    69. width: 120,
    70. height: 35,
    71. x: 60,
    72. y: 40
    73. },
    74. {
    75. type: 'text',
    76. text: 'Source: http://www.wikipedia.org',
    77. fontSize: 10,
    78. x: 12,
    79. y: 440
    80. }
    81. ]
    82. }
    83. ]
    84. });

    实例:绘制饼图

    实例:绘制饼图

    image

    代码:

    1. //创建对应的数据存储
    2. Ext.define('PandaApp.store.Expense', {
    3. extend: 'Ext.data.Store',
    4. alias: 'store.expense',
    5. fields: [ 'cat', 'spent'],
    6. data: [
    7. { "cat": "Restaurant", "spent": 100},
    8. { "cat": "Travel", "spent": 150},
    9. { "cat": "Insurance", "spent": 500},
    10. { "cat": "Rent", "spent": 1000},
    11. { "cat": "Groceries", "spent": 400},
    12. { "cat": "Utilities", "spent": 300},
    13. ]
    14. });
    15. //实例化
    16. var store = Ext.create("PandaApp.store.Expense");
    17. //创建容器
    18. Ext.create('Ext.Container', {
    19. renderTo: Ext.getBody(),
    20. width: 600,
    21. height: 500,
    22. layout: 'fit',
    23. items: [
    24. {
    25. xtype: 'polar',
    26. legend: { docked: 'bottom' },
    27. insetPadding: { top: 100, bottom: 20, left: 20, right: 40 },
    28. store: store,
    29. series: [
    30. {
    31. type: 'pie',
    32. angleField: 'spent',
    33. label: {
    34. field: 'cat',
    35. },
    36. tooltip: {
    37. trackMouse: true,
    38. renderer: function (storeItem, item) {
    39. var value = ((parseFloat(storeItem.get('spent') /
    40. toreItem.store.sum('spent')) * 100.0).toFixed(2));
    41. this.setHtml(storeItem.get('cat') + ': ' + value + '%');
    42. }
    43. }
    44. }
    45. ]
    46. }
    47. ]
    48. });

    实例:绘制饼图2

    image

    代码:

    1. //创建测试使用的数据Store
    2. var myChartStore = Ext.create('Ext.data.ArrayStore',{
    3. storeId: 'salesStore',
    4. fields:[
    5. {name: 'id', type: 'int'},
    6. {name: 'region', type: 'string'},
    7. {name: 'sales', type: 'float'}
    8. ],
    9. data:[
    10. [10001 ,"North", 15.55],
    11. [10002 ,"South", 23.99],
    12. [10003 ,"East", 17.44],
    13. [10004 ,"West", 30.00],
    14. [10005 ,"Central", 4.1],
    15. [10006 ,"OverSeas", 2.55]
    16. ]
    17. });
    18. //创建图表
    19. var mychart= Ext.create('Ext.chart.PolarChart', {
    20. renderTo: Ext.getBody(),
    21. width: 500,
    22. height: 500,
    23. store: myChartStore, //配置关联的Store
    24. insetPadding: { //设置padding
    25. top: 50,
    26. left: 25,
    27. right: 25,
    28. bottom: 15
    29. },
    30. innerPadding: 20,
    31. interactions: ['rotate', 'itemhighlight'],
    32. theme: 'default-gradients',
    33. legend: {docked: 'bottom'},
    34. series: {
    35. type: 'pie',
    36. angleField:'sales', // xField
    37. label: {
    38. field:'region',
    39. calloutLine: {
    40. length: 60,
    41. width: 3
    42. }
    43. },
    44. highlight: true,
    45. tooltip: {
    46. trackMouse: true,
    47. renderer: function(item, storeItem) {
    48. item.setHtml(storeItem.get('region') + ': ' + storeItem.get('sales'));
    49. }
    50. }
    51. },
    52. sprites: {
    53. type: 'text',
    54. text: 'My Company - 2015',
    55. fontSize: 22,
    56. fillStyle: '#993366',
    57. width: 100,
    58. height: 30,
    59. x: 40, // the sprite x position
    60. y: 25 // the sprite y position
    61. }
    62. });

    实例:绘制饼图(3D)

    image

    代码:

    1. //创建对应的数据存储
    2. Ext.define('PandaApp.store.Expense', {
    3. extend: 'Ext.data.Store',
    4. alias: 'store.expense',
    5. fields: [ 'cat', 'spent'],
    6. data: [
    7. { "cat": "Restaurant", "spent": 100},
    8. { "cat": "Travel", "spent": 150},
    9. { "cat": "Insurance", "spent": 500},
    10. { "cat": "Rent", "spent": 1000},
    11. { "cat": "Groceries", "spent": 400},
    12. { "cat": "Utilities", "spent": 300},
    13. ]
    14. });
    15. //实例化
    16. var store = Ext.create("PandaApp.store.Expense");
    17. //创建容器
    18. Ext.create('Ext.Container', {
    19. renderTo: Ext.getBody(),
    20. width: 600,
    21. height: 500,
    22. layout: 'fit',
    23. items: [
    24. {
    25. xtype: 'polar',
    26. legend: { docked: 'bottom' },
    27. insetPadding: { top: 100, bottom: 20, left: 20, right: 40 },
    28. store: store,
    29. series: [
    30. {
    31. type: 'pie3d',
    32. thickness: 70,
    33. distortion: 0.5,
    34. angleField: 'spent',
    35. label: {
    36. field: 'cat',
    37. },
    38. tooltip: {
    39. trackMouse: true,
    40. renderer: function (storeItem, item) {
    41. var value = ((parseFloat(storeItem.get('spent') /
    42. toreItem.store.sum('spent')) * 100.0).toFixed(2));
    43. this.setHtml(storeItem.get('cat') + ': ' + value + '%');
    44. }
    45. }
    46. }
    47. ]
    48. }
    49. ]
    50. });

    实例:绘制圈图(donut chart)

    实例:绘制圈图

    image

    代码:

    1. //创建对应的数据存储
    2. Ext.define('PandaApp.store.Expense', {
    3. extend: 'Ext.data.Store',
    4. alias: 'store.expense',
    5. fields: [ 'cat', 'spent'],
    6. data: [
    7. { "cat": "Restaurant", "spent": 100},
    8. { "cat": "Travel", "spent": 150},
    9. { "cat": "Insurance", "spent": 500},
    10. { "cat": "Rent", "spent": 1000},
    11. { "cat": "Groceries", "spent": 400},
    12. { "cat": "Utilities", "spent": 300},
    13. ]
    14. });
    15. //实例化
    16. var store = Ext.create("PandaApp.store.Expense");
    17. //创建容器
    18. Ext.create('Ext.Container', {
    19. renderTo: Ext.getBody(),
    20. width: 600,
    21. height: 500,
    22. layout: 'fit',
    23. items: [
    24. {
    25. xtype: 'polar',
    26. legend: { docked: 'bottom' },
    27. insetPadding: { top: 100, bottom: 20, left: 20, right: 40 },
    28. store: store,
    29. series: [
    30. {
    31. type: 'pie',
    32. donut: 50, //在饼图的基础上,加上这个属性
    33. angleField: 'spent',
    34. label: {
    35. field: 'cat',
    36. },
    37. tooltip: {
    38. trackMouse: true,
    39. renderer: function (storeItem, item) {
    40. var value = ((parseFloat(storeItem.get('spent') /
    41. toreItem.store.sum('spent')) * 100.0).toFixed(2));
    42. this.setHtml(storeItem.get('cat') + ': ' + value + '%');
    43. }
    44. }
    45. }
    46. ]
    47. }
    48. ]
    49. });

    实例:绘制圈图(3D)

    image

    代码:

    1. //创建对应的数据存储
    2. Ext.define('PandaApp.store.Expense', {
    3. extend: 'Ext.data.Store',
    4. alias: 'store.expense',
    5. fields: [ 'cat', 'spent'],
    6. data: [
    7. { "cat": "Restaurant", "spent": 100},
    8. { "cat": "Travel", "spent": 150},
    9. { "cat": "Insurance", "spent": 500},
    10. { "cat": "Rent", "spent": 1000},
    11. { "cat": "Groceries", "spent": 400},
    12. { "cat": "Utilities", "spent": 300},
    13. ]
    14. });
    15. //实例化
    16. var store = Ext.create("PandaApp.store.Expense");
    17. //创建容器
    18. Ext.create('Ext.Container', {
    19. renderTo: Ext.getBody(),
    20. width: 600,
    21. height: 500,
    22. layout: 'fit',
    23. items: [
    24. {
    25. xtype: 'polar',
    26. legend: { docked: 'bottom' },
    27. insetPadding: { top: 100, bottom: 20, left: 20, right: 40 },
    28. store: store,
    29. series: [
    30. {
    31. type: 'pie3d',
    32. donut: 50,
    33. thickness: 70,
    34. distortion: 0.5,
    35. angleField: 'spent',
    36. label: {
    37. field: 'cat',
    38. },
    39. tooltip: {
    40. trackMouse: true,
    41. renderer: function (storeItem, item) {
    42. var value = ((parseFloat(storeItem.get('spent') /
    43. toreItem.store.sum('spent')) * 100.0).toFixed(2));
    44. this.setHtml(storeItem.get('cat') + ': ' + value + '%');
    45. }
    46. }
    47. }
    48. ]
    49. }
    50. ]
    51. });

    待合并

    实例:绘制堆积图(stacked chart)

    饼图(Pie Chart)(实例存在问题,需要修复)

    1. Ext.create('Ext.chart.PolarChart', {
    2. renderTo: document.body,
    3. width: 600,
    4. height: 300,
    5. store: {
    6. fields: ['name', 'g1'],
    7. data: [
    8. {"name": "Item-0", "g1": 57},
    9. {"name": "Item-1", "g1": 45},
    10. {"name": "Item-2", "g1": 67}
    11. ]
    12. },
    13. //configure the legend.
    14. legend: {
    15. docked: 'bottom'
    16. },
    17. //describe the actual pie series.
    18. series: [{
    19. type: 'pie',
    20. xField: 'g1',
    21. label: {
    22. field: 'name'
    23. },
    24. donut: 30 //increase or decrease for increasing or decreasing donut area in middle.
    25. }]
    26. });

    折线图(Line Chart)(实例存在问题,需要修复)

    1. Ext.create('Ext.chart.CartesianChart', {
    2. renderTo: document.body,
    3. width: 600,
    4. height: 200,
    5. store: {
    6. fields: ['name', 'g1', 'g2'],
    7. data: [
    8. {"name": "Item-0", "g1": 57, "g2": 59},
    9. {"name": "Item-1", "g1": 45, "g2": 50},
    10. {"name": "Item-2", "g1": 67, "g2": 43},
    11. {"name": "Item-3", "g1": 45, "g2": 18},
    12. {"name": "Item-4", "g1": 30, "g2": 90}
    13. ]
    14. },
    15. legend: {
    16. docked: 'bottom'
    17. },
    18. //define x and y axis.
    19. axes: [{
    20. type: 'numeric',
    21. position: 'left'
    22. }, {
    23. type: 'category',
    24. visibleRange: [0, 1],
    25. position: 'bottom'
    26. }],
    27. //define the actual series
    28. series: [{
    29. type: 'line',
    30. xField: 'name',
    31. yField: 'g1',
    32. title: 'Normal'
    33. }, {
    34. type: 'line',
    35. xField: 'name',
    36. yField: 'g2',
    37. title: 'Smooth'
    38. }]
    39. });

    条形图(Bar Chart)(实例存在问题,需要修复)

    1. Ext.create('Ext.chart.CartesianChart', {
    2. renderTo: document.body,
    3. width: 600,
    4. height: 200,
    5. flipXY: true,
    6. store: {
    7. fields: ['name', 'g1', 'g2'],
    8. data: [
    9. {"name": "Item-0", "g1": 57, "g2": 59},
    10. {"name": "Item-1", "g1": 45, "g2": 50},
    11. {"name": "Item-2", "g1": 67, "g2": 43},
    12. {"name": "Item-3", "g1": 45, "g2": 18},
    13. {"name": "Item-4", "g1": 30, "g2": 90}
    14. ]
    15. },
    16. //set legend configuration
    17. legend: {
    18. docked: 'right'
    19. },
    20. //define the x and y-axis configuration.
    21. axes: [{
    22. type: 'numeric',
    23. position: 'bottom',
    24. grid: true,
    25. minimum: 0
    26. }, {
    27. type: 'category',
    28. position: 'left'
    29. }],
    30. //define the actual bar series.
    31. series: [{
    32. type: 'bar',
    33. xField: 'name',
    34. yField: ['g1', 'g2'],
    35. axis: 'left'
    36. }]
    37. });

    面积图(Area Chart)(实例存在问题,需要修复)

    1. Ext.create('Ext.chart.CartesianChart', {
    2. renderTo: document.body,
    3. width: 600,
    4. height: 200,
    5. store: {
    6. fields: ['name', 'g1', 'g2'],
    7. data: [
    8. {"name": "Item-0", "g1": 57, "g2": 59},
    9. {"name": "Item-1", "g1": 45, "g2": 50},
    10. {"name": "Item-2", "g1": 67, "g2": 43},
    11. {"name": "Item-3", "g1": 45, "g2": 18},
    12. {"name": "Item-4", "g1": 30, "g2": 90}
    13. ]
    14. },
    15. legend: {
    16. docked: 'right'
    17. },
    18. axes: [{
    19. type: 'numeric',
    20. position: 'left',
    21. grid: true
    22. }, {
    23. type: 'category',
    24. position: 'bottom',
    25. visibleRange: [0,5]
    26. }],
    27. series: [{
    28. type: 'area',
    29. xField: 'name',
    30. yField: ['g1', 'g2'],
    31. title: ['G1', 'G2']
    32. }]
    33. });
  • 相关阅读:
    MySQL基础学习总结(三)
    [计算机提升] 计算机进阶概念:路径
    【c++】跟webrtc学std array 3:buffer_id_to_frame_id_
    Linux bash脚本编程学习
    原子类详解
    RabbitMQ------死信队列(消息超时、达到最大长度、消费拒绝)(六)
    学习记录2022
    AirServer最新Win64位个人版投屏软件
    【算法系列】非线性最小二乘-高斯牛顿法
    Postgresql数据类型-布尔类型
  • 原文地址:https://blog.csdn.net/weixin_38304160/article/details/126358339