Results 1 to 7 of 7

Thread: generate random data for a store (MVC)

  1. #1
    Sencha User
    Join Date
    Aug 2012
    Posts
    91
    Answers
    6
    Vote Rating
    0
      0  

    Default Answered: generate random data for a store (MVC)

    Hey all,
    I'm trying to generate a random data set for a store but I'm having trouble. My generator function is undefined, I'm probably declaring it in the wrong place/way or calling it in the wrong way. Here is my code (for my store file).

    Code:
    Ext.define('TR.store.Trends', {
            extend: 'Ext.data.Store',
            model: 'TR.model.Trends',
            generateData : function(){
                    var data = [];
                    //logic to populate  data array missing, for now I am just
                    //pushing a single entry
                    data.push({
                            name: '2012-12-10',
                            number: 10,
                            percent: 8.7,
                    });
                    return data;
            },
            data:this.generateData(),
    });
    Any help on this is much appreciated

  2. At defining time of TR.store.Trends, 'this' is often window or some other object, not the store instance which will be existed only when you create an instance of the store. So this.generateData will often be undefined.
    Try to fix your code as below:
    Code:
    Ext.define('TR.store.Trends', {
        extend: 'Ext.data.Store',
        model: 'TR.model.Trends',
        generateData : function(){
            var data = [];
            //logic to populate  data array missing, for now I am just
            //pushing a single entry
            data.push({
                name: '2012-12-10',
                number: 10,
                percent: 8.7,
            });
            return data;
        },
        // data:this.generateData(),
        constructor: function(){
            this.data = this.generateData();
            this.callParent(arguments);
        }
    });

  3. #2
    Sencha User
    Join Date
    Jan 2012
    Posts
    1,376
    Answers
    347
    Vote Rating
    116
      1  

    Default

    At defining time of TR.store.Trends, 'this' is often window or some other object, not the store instance which will be existed only when you create an instance of the store. So this.generateData will often be undefined.
    Try to fix your code as below:
    Code:
    Ext.define('TR.store.Trends', {
        extend: 'Ext.data.Store',
        model: 'TR.model.Trends',
        generateData : function(){
            var data = [];
            //logic to populate  data array missing, for now I am just
            //pushing a single entry
            data.push({
                name: '2012-12-10',
                number: 10,
                percent: 8.7,
            });
            return data;
        },
        // data:this.generateData(),
        constructor: function(){
            this.data = this.generateData();
            this.callParent(arguments);
        }
    });

  4. #3
    Sencha User existdissolve's Avatar
    Join Date
    Jan 2010
    Location
    Kansas
    Posts
    522
    Answers
    32
    Vote Rating
    89
      1  

    Default

    You might try something like this:

    PHP Code:
    Ext.define('TestStore', {
        
    extend'Ext.data.Store',
        
    model'TestModel',
        
    storeId'TestStore',
        
    generateData: function() {
            var 
    me this,
                
    data = [];
            
    // generate 50 records
            
    for( var i=0;i<50;i++) {        
                
    data.push({
                    
    dateme.randomDate(new Date(201201), new Date()),
                    
    numberMath.floorMath.random() * 1000 ),
                    
    percent: ( ( Math.random() * 1000 ) / 3.2 ).toFixed),
                });
            }
            return 
    data;
        },
        
    randomDate: function(startend) {
            return new 
    Date(
                
    start.getTime() + Math.random() * (end.getTime() - start.getTime())
            )
        },
        
    constructor: function() {
            var 
    me this;
            
    me.callParent(arguments);
            
    me.add(me.generateData())
        }
    }); 
    Here's a live example: http://jsfiddle.net/existdissolve/8NMJG/

  5. #4
    Sencha User
    Join Date
    Aug 2012
    Posts
    91
    Answers
    6
    Vote Rating
    0
      0  

    Default

    Thank you both for your help, I got my store working and generating decent random data. Here is what I ended up using:
    Code:
    Ext.define('TR.store.Trends', {
            extend: 'Ext.data.Store',
            model: 'TR.model.Trends',
            generateData: function() {
                    var     me = this,
                            data = [],
                            currentDate = new Date(),
                            total = Math.floor((Math.random()*400)+100),
                            whatsLeft = total,
                            newpercent = 0;
                    var sample = 20;
                    for( var i=0;i<sample;i++) {
                            var newnumber = Math.floor((Math.random()*(whatsLeft)/(sample/4))+0);
                            whatsLeft = whatsLeft - newnumber;
                            newpercent = newpercent + ((newnumber/total)*100);
                            var newDate = new Date();
                            newDate.setDate(currentDate.getDate()+i);
                            data.push({
                                    date: newDate,
                                    number: newnumber,
                                    percent: newpercent,
                            });
                    }
                    return data;
            },
            constructor: function() {
                    var me = this;
                    me.callParent(arguments);
                    me.add(me.generateData())
            }
    });
    Now my follow up challenge is making a button to "reload" the store on the chart, displaying a new set of random data. I havn't spent a ton of time on this yet but I'm guessing stores have some sort of a reload or refresh methon

  6. #5
    Sencha User existdissolve's Avatar
    Join Date
    Jan 2010
    Location
    Kansas
    Posts
    522
    Answers
    32
    Vote Rating
    89
      1  

    Default

    For the reloading bits, you might take a look at the charts examples in the docs. They have a ton of reload examples

  7. #6
    Sencha User
    Join Date
    Aug 2012
    Posts
    91
    Answers
    6
    Vote Rating
    0
      0  

    Default

    got it! Thanks for pointing me in the right direction

    Code:
            reload:function(button){
                    var store = Ext.data.StoreManager.lookup('Trends');
                    store.loadData(store.generateData());
            }

  8. #7
    Sencha User
    Join Date
    Mar 2016
    Posts
    33
    Vote Rating
    0
      0  

    Default

    Quote Originally Posted by existdissolve View Post
    You might try something like this:

    PHP Code:
    Ext.define('TestStore', {
        
    extend'Ext.data.Store',
        
    model'TestModel',
        
    storeId'TestStore',
        
    generateData: function() {
            var 
    me this,
                
    data = [];
            
    // generate 50 records
            
    for( var i=0;i<50;i++) {        
                
    data.push({
                    
    dateme.randomDate(new Date(201201), new Date()),
                    
    numberMath.floorMath.random() * 1000 ),
                    
    percent: ( ( Math.random() * 1000 ) / 3.2 ).toFixed),
                });
            }
            return 
    data;
        },
        
    randomDate: function(startend) {
            return new 
    Date(
                
    start.getTime() + Math.random() * (end.getTime() - start.getTime())
            )
        },
        
    constructor: function() {
            var 
    me this;
            
    me.callParent(arguments);
            
    me.add(me.generateData())
        }
    }); 
    Here's a live example: http://jsfiddle.net/existdissolve/8NMJG/

    Can you help me with this example in ExtJS 3.4?
    My code for 3.4:
    PHP Code:

    Ext
    .define('TestStore', {
            
    extend'Ext.data.Store',
            
    //model: 'TestModel',
            
    fields: [
                {
    name'date'},
                {
    name'number'},
                {
    name'percent'}
            ],
            
    storeId'TestStore',
            
    generateData: function() {
                var 
    me this,
                    
    data = [];
                
    // generate 10 records
                
    for( var i=0;i<10;i++) {
                    
    data.push({
                        
    dateme.randomDate(new Date(201201), new Date()),
                        
    numberMath.floorMath.random() * 1000 ),
                        
    percent: ( ( Math.random() * 1000 ) / 3.2 ).toFixed),
                    });
                }
                
    console.log(data);
                return 
    data;
            },
            
    randomDate: function(startend) {
                return new 
    Date(
                    
    start.getTime() + Math.random() * (end.getTime() - start.getTime())
                );
            },
            
    constructor: function() {
                
    console.log('constructor!');
                var 
    me this;
                
    //me.callParent(arguments);
                //me.add(me.generateData())
                
    me.superclass.constructor.apply(mearguments);
                
    //me.loadData(me.generateData(), true);
                
    me.add(me.generateData());
            }
        }); 

    I have error
    PHP Code:
    TypeErrorrecord.join is not a function 


    But i change add to loadData, I have another error:
    PHP Code:
    TypeErrorthis.reader is undefined 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •