Advertisement
MrPolywhirl

ExtJS5-DynamicChart

Feb 12th, 2015
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*global Ext:false */
  2. Ext.application({
  3.     name: 'ChartExample',
  4.     launch: function() {
  5.         Ext.create('Ext.panel.Panel', {
  6.             layout: 'fit',
  7.             minWidth: 650,
  8.             height: 500,
  9.             items: [{
  10.                 xtype: 'cartesian',
  11.                 animate: true,
  12.                 legend : {
  13.                     docked : 'bottom'
  14.                 },
  15.                 store: Ext.create('Ext.data.Store', {
  16.                     autoLoad : true,
  17.                     fields: ['name', 'data1', 'data2', 'data3', 'data4', 'data5'],
  18.                     proxy: {
  19.                         type: 'ajax',
  20.                         url: 'sample.json',
  21.                         reader : {
  22.                             type : 'json'
  23.                         }
  24.                     }
  25.                 }),
  26.                 axes: [{
  27.                     type: 'numeric',
  28.                     position: 'left',
  29.                     fields: ['data1'],
  30.                     title: {
  31.                         text: 'Sample Values',
  32.                         fontSize: 15
  33.                     },
  34.                     grid: true,
  35.                     minimum: 0
  36.                 }, {
  37.                     type: 'category',
  38.                     position: 'bottom',
  39.                     fields: ['name'],
  40.                     title: {
  41.                         text: 'Sample Values',
  42.                         fontSize: 15
  43.                     }
  44.                 }],
  45.                
  46.                 initComponent : function() {
  47.                     var me = this;
  48.                    
  49.                     me.store.on('load', function(store, records, successful, eOpts) {
  50.                         var fields = me.extractFields(records, ['id','name']);
  51.                         me.setUpChart(fields);
  52.                     });
  53.                    
  54.                     me.callParent();
  55.                 },
  56.                
  57.                 setUpChart : function(fields) {
  58.                     var me = this;
  59.                    
  60.                     var colors = ['#F00', '#0F0', '#00F', '#0FF', '#F0F', '#FF0'];
  61.                    
  62.                     me.setSeries(fields.map(function(field, index) {
  63.                         return me.createSeries({
  64.                             xField: 'name',
  65.                             yField: field,
  66.                             title : [ me.camelToTitle(field) ],
  67.                             colors : [ colors[index] ]
  68.                         });
  69.                     }));
  70.                 },
  71.                
  72.                 extractFields : function(records, ignore, loop) {
  73.                     ignore = ignore || [];
  74.                     var fieldMap = {};
  75.                     Ext.Array.every(records, function(record) {
  76.                         var data = record.data || {};
  77.                         for (var field in data) {
  78.                             fieldMap[field] = true;
  79.                         }
  80.                         return loop;
  81.                     });
  82.                     return Object.keys(fieldMap).filter(function(field) {
  83.                         return ignore.indexOf(field) == -1;
  84.                     }).sort();
  85.                 },
  86.                
  87.                 createSeries : function(config) {
  88.                     return Ext.apply({
  89.                         type: 'line',
  90.                         highlight: {
  91.                             size: 7,
  92.                             radius: 7
  93.                         }
  94.                     }, config);
  95.                 },
  96.                
  97.                 camelToTitle : function(str, delimiter) {
  98.                     return str.replace(/([A-Z][a-z]+)/g, ' $1') // Words beginning with UC
  99.                         .replace(/([A-Z][A-Z]+)/g, ' $1')       // "Words" of only UC
  100.                         .replace(/([^A-Za-z ]+)/g, ' $1')       // "Words" of non-letters
  101.                         .trim()                                 // Remove any leading/trailing spaces
  102.                         .replace(/[ ]/g, delimiter || ' ');     // Replace all spaces with the delim
  103.                 }
  104.             }],
  105.             renderTo: Ext.getBody()
  106.         });
  107.     }
  108. });
  109.  
  110. /** === Data ===
  111. [{
  112.     'name': 'metric one',
  113.     'data1': 10,
  114.     'data2': 12,
  115.     'data3': 14,
  116.     'data4': 8,
  117.     'data5': 13
  118. }, {
  119.     'name': 'metric two',
  120.     'data1': 7,
  121.     'data2': 8,
  122.     'data3': 16,
  123.     'data4': 10,
  124.     'data5': 3
  125. }, {
  126.     'name': 'metric three',
  127.     'data1': 5,
  128.     'data2': 2,
  129.     'data3': 14,
  130.     'data4': 12,
  131.     'data5': 7
  132. }, {
  133.     'name': 'metric four',
  134.     'data1': 2,
  135.     'data2': 14,
  136.     'data3': 6,
  137.     'data4': 1,
  138.     'data5': 23
  139. }, {
  140.     'name': 'metric five',
  141.     'data1': 27,
  142.     'data2': 38,
  143.     'data3': 36,
  144.     'data4': 13,
  145.     'data5': 33
  146. }]
  147. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement