小编典典

无法将jSon数据存储获取到ExtJS(Sencha Touch)图表中:显示错误“无法读取未定义的属性'length'”

json

编辑 我正在使用为柱形图提供的Sencha触摸图示例。代码如下

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']
            }]
        }
    });

}

编辑: 当我在调试器中检查此数据时,现在可以加载json数据,但我的图表仍未显示。这是我更新的代码

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
});

如果有人做错了事,请让我知道。

旧帖子: 我正在使用json输出绑定到我的Sencha柱形图。我的代码如下-

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'));
            });
        }
    } 
});

这是从网址返回的json数据

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

但这不会显示图形,而是在sencha-touch.js文件中出现了javascript错误“未捕获的类型错误:无法读取未定义的属性长度”。

如果我直接从json输出中对数据进行硬编码,那么它将起作用

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'}
    ]
});

另外,当我从ExtDesigner加载jsonStore时,它工作正常。当我在Secha图表index.js文件中复制相同的代码时,它不起作用。

有人可以让我知道我在代码中做错了什么吗?


阅读 250

收藏
2020-07-27

共1个答案

小编典典

我在Sencha同事的帮助下解决了这个问题。谢谢丹。解决方法如下:

  1. 在Web服务器上运行示例,因为从文件系统加载JSON文件有时可能会导致问题。
  2. 构造json数据如下

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

    }

  3. 使用以下数据存储

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

    });

2020-07-27