Can't get jSon dataStore into ExtJS (Sencha To

2019-01-29 05:57发布

问题:

EDITED I am using Sencha touch charts sample provided for column charts. The code is as below

new Ext.chart.Panel({
    fullscreen: true,
    title: 'Column Chart',
    dockedItems: [{
        xtype: 'button',
            iconCls: 'help',
            iconMask: true,
            ui: 'plain',
            handler: onHelpTap
        }, {
            xtype: 'button',
            iconCls: 'shuffle',
            iconMask: true,
            ui: 'plain',
            handler: onRefreshTap
        }],
        items: {
            cls: 'column1',
            animate: {
                easing: 'bounceOut',
                duration: 750
            },
            store: window.store1,
            shadow: false,
            gradients: [{
                'id': 'v-1',
                'angle': 0,
                stops: {
                    0: {
                        color: 'rgb(212, 40, 40)'
                    },
                    100: {
                        color: 'rgb(117, 14, 14)'
                    }
                }
            },
            {
                'id': 'v-2',
                'angle': 0,
                stops: {
                    0: {
                        color: 'rgb(180, 216, 42)'
                    },
                    100: {
                        color: 'rgb(94, 114, 13)'
                    }
                }
            },
            {
                'id': 'v-3',
                'angle': 0,
                stops: {
                    0: {
                        color: 'rgb(43, 221, 115)'
                    },
                    100: {
                        color: 'rgb(14, 117, 56)'
                    }
                }
            },
            {
                'id': 'v-4',
                'angle': 0,
                stops: {
                    0: {
                        color: 'rgb(45, 117, 226)'
                    },
                    100: {
                        color: 'rgb(14, 56, 117)'
                    }
                }
            },
            {
                'id': 'v-5',
                'angle': 0,
                stops: {
                    0: {
                        color: 'rgb(187, 45, 222)'
                    },
                    100: {
                        color: 'rgb(85, 10, 103)'
                    }
                }
            }],
            axes: [{
                type: 'Numeric',
                position: 'left',
                fields: ['wins'],
                minimum: 0,
                maximum: 10,
                label: {
                    renderer: function (v) {
                        return v.toFixed(0);
                    }
                },
                title: 'Wins'
            },
            {
                type: 'Category',
                position: 'bottom',
                fields: ['School'],
                title: 'School'
            }],
            series: [{
                type: 'column',
                axis: 'left',
                highlight: true,
                renderer: function (sprite, storeItem, barAttr, i, store) {
                    barAttr.fill = colors[i % colors.length];
                    return barAttr;
                },
                label: {
                    field: 'wins'
                },
                xField: 'School',
                yField: 'wins'
            }],
            interactions: [{
                type: 'panzoom',
                axes: ['bottom']
            }]
        }
    });

}

EDITED: I am now able to load the json data when I check this in the debugger but my chart is still not getting displayed. Here is my updated code

Ext.regModel('Details', { fields: [{ name: 'School' }, { name: 'wins'}] });

// create the Data Store
window.store1 = new Ext.data.Store({
    model: 'Details',
    proxy: {
        type: 'scripttag',
        url: 'http://localhost:2650/AjaxWCFService.svc/GetDataset',
        reader: {
            root: 'data',
            type: 'json'
        }
    },
    autoLoad: true
});

appreciate if anyone could let me know if I am doing anything wrong.

OLD POST: I am using a json output to bind to my Sencha Column chart. My code is as belows -

Ext.regModel('details', { idProperty: 'name', fields: ['School', 'wins'] });
// create the Data Store
window.store1 = new Ext.data.Store({
    model: 'details',
    autoLoad: true,
    proxy: {
        type: 'ajax',
        method: 'POST',
        url: 'http://localhost:2650/AjaxWCFService.svc/GetDataset',
        reader: {
            type: 'json',
            root: 'data'
        }
    },
    listeners: {
        load: function (obj, records) {
            Ext.each(records, function (rec) {
                console.log(rec.get('name'));
            });
        }
    } 
});

This is the json data that is returned from the url

{
   data: [
       {School:'Dukes',wins:'3'},
       {School:'Emmaus',wins:'10'},
       {School:'Maryland',wins:'5'},
       {School:'Virginia',wins:'2'}
   ]
}

But this doesn't display the graph instead I get a javascript error "Uncaught type error: Cannot read property lenght of undefined" in sencha-touch.js file.

If i hard code the data from the json output directly then it works

window.store1 = new Ext.data.JsonStore({
    root: 'data',
    fields: ['School', 'wins'],
    autoLoad: true,
    data: [
        {School:'Dukes',wins:'3'},
        {School:'Emmaus',wins:'10'},
        {School:'Maryland',wins:'5'},
        {School:'Virginia',wins:'2'}
    ]
});

Also when I load the jsonStore from the ExtDesigner it works fine. When I copy the same code in my Secha charts index.js file it doesn't work.

Can someone please let me know what I am doing wrong in my code?

回答1:

I solved this issue with the help of a Sencha folk. Thanks Dan. Here is the solution:

  1. run the sample on a web server as loading JSON files from the file system can sometimes cause problems.
  2. Structure the json data as below

    {
        "data": [
            {
                "School": "Dukes",
                "wins": "3"
            },
            {
                "School": "Emmaus",
                "wins": "10"
            },
            {
                "School": "Maryland",
                "wins": "5"
            },
            {
                "School": "Virginia",
                "wins": "2"
            }
        ]
    }
    
  3. Use the below data store

    window.store1 = new Ext.data.Store({
        model: 'Details',
        proxy: {
            type: 'ajax',
            url: 'GetDataset.json',
            reader: {
                root: 'data',
                type: 'json'
            }
        },
        autoLoad: true
    });
    


回答2:

I'm guessing here, but it seems like a problem I've had before. I believe the problem is with the proxy. Try:

proxy: {
    type: 'rest',
    url: 'http://localhost:2650/AjaxWCFService.svc/GetDataset', 
    reader: {
        root: 'data'
    }

},

Now, you need to define the callback function for the proxy. There's an elegant way with some config, but I honsetly don't know how to do that, so I found out a different solution at the time, the hard way, which is to wrap the JSON which your service returns inside of(like they do here):

Ext.data.JsonP.callback1();

If none of that works, also consider adding the following to your proxy, and emitting a 'total' as part of your JSON response.

totalProperty: 'total'

But keep in mind that I really don't think this will help much at all and that I'm just throwing these ideas out based on a working sample I have.

Try reading this and this. And most of all good luck, that's all of the top of my head mate. Also have a look at this post.

I hope that points you in the right direction. Like I said, I'm not sure, this is my best guess. I'm probably wrong.

Cheers rivanov