SAPUI5将数据绑定到视图转发器控件内的图表

时间:2014-07-16 15:01:00

标签: sapui5

在SAPUI5上需要帮助,我正在尝试按如下所述创建一个View。 我正在使用View转发器控件和使用VIZ图表控件的View转发器控件内部。 虽然我能够将数据绑定到转发器控件,但我面临的问题是无法将数据绑定到图表。

以下是我使用的JSON。

    "plants":[
            {
                            "plant":"Plant1",
                            "cCode":"***",
                            "Function":[
                                                            {"FunName":"fun1","Count":10},
                                                            {"FunName":" fun2","Count":10},
                                                            {"FunName":" fun3","Count":10},
                                                            {"FunName":"fun4","Count":0},
                                                            {"FunName":"fun5","Count":11},
                                                            {"FunName":" fun6","Count":10},
                                                            {"FunName":"fun7","Count":10}
                            ]

            },
            {
                            "plant":"Plant2",
                            "cCode":"***",
                            "Function":[
                                                            {"FunName":"fun1","Count":10},
                                                            {"FunName":" fun2","Count":10},
                                                            {"FunName":" fun3","Count":10},
                                                            {"FunName":"fun4","Count":0},
                                                            {"FunName":"fun5","Count":11},
                                                            {"FunName":" fun6","Count":10},
                                                            {"FunName":"fun7","Count":10}
                            ]


            }
            ]

}

我的观点:

createContent : function(oController) {
//  var oModel = new sap.ui.model.json.JSONModel();
//  oModel.loadData("model/plantreport.json");
    var oModel = new sap.ui.model.json.JSONModel({
                                            "plants":[
           {
                            "plant":"Plant1",
                            "cCode":"10",
                            "Function":[
                                                            {"FunName":"fun1","Count":10},
                                                            {"FunName":" fun2","Count":10},
                                                            {"FunName":" fun3","Count":10},
                                                            {"FunName":"fun4","Count":0},
                                                            {"FunName":"fun5","Count":11},
                                                            {"FunName":" fun6","Count":10},
                                                            {"FunName":"fun7","Count":10}
                            ]

            },
            {
                            "plant":"Plant2",
                            "cCode":"20",
                            "Function":[
                                                            {"FunName":"fun1","Count":10},
                                                            {"FunName":" fun2","Count":10},
                                                            {"FunName":" fun3","Count":10},
                                                            {"FunName":"fun4","Count":0},
                                                            {"FunName":"fun5","Count":11},
                                                            {"FunName":" fun6","Count":10},
                                                            {"FunName":"fun7","Count":10}
                            ]


            }
            ]



            });

    //////// CONTROL SECTION ///////////////////////////////////////////////////////////////////
    //
    //create view repeater title (optional)
    var oTitle_NoViews = new sap.ui.commons.Title({
        text:"Testing Please",
        level: sap.ui.commons.TitleLevel.H1
    });
var oLayout = new sap.ui.layout.VerticalLayout();
    // create the row repeater control
    var oViewRepeater_NoViews = new sap.suite.ui.commons.ViewRepeater("vr_noViews",
    {  
            showViews: false,
            noData: new sap.ui.commons.TextView({text: "Sorry, no data available!"}),
            showSearchField: false,
            showMoreSteps: 10, // you can use 'Show More' feature instead of paging

            //set view properties directly to the repeater
            responsive: false,
            itemMinWidth: 210,
            numberOfRows: 12
    });


    oViewRepeater_NoViews.bindAggregation("rows", {
        path : "/plants",
        factory : function(sId, oContext) {
            var sPath = oContext.sPath;  

       //Text
        control = new sap.ui.commons.TextView();
        control.bindProperty("text",oContext.sPath+"/plant");
        oLayout.addContent(control);
        //add content to cell, cell to row


        var oTemplate =  new sap.viz.ui5.data.DimensionDefinition({
                axis: 1,
                name : "Plant Name"
            });
            oTemplate.bindProperty("value", "FunName", function(value) {
                if (value) {
                    return value;
                }
            });

            var oTemplate2 =  new sap.viz.ui5.data.MeasureDefinition({
                name : "Count"
            });
            oTemplate2.bindProperty("value", "Count", function(value) {
                if (value) {
                    return value;
                }
                return 0;
            });

            var oDataset = new sap.viz.ui5.data.FlattenedDataset({
                            dimensions : [ oTemplate ],
                            measures : [ oTemplate2 ]
            });
            oDataset.bindAggregation("data", sPath + "/Function");

            var oBarChart = new sap.viz.ui5.Bar({
                            width : "300px",
                            height : "250px",
                            plotArea : {
                                xAxis : {

                                }
                            },
                            title : {
                                visible : true,
                                text : 'MY Graph'
                            },
                            dataset : oDataset
            });

            oLayout.addContent(oBarChart);
                return  oLayout;            
        }
    });

 oViewRepeater_NoViews.setModel(oModel);

     return new sap.m.Page({
            title: "Plant Report",
            showNavButton: "{device>/isPhone}",
            navButtonPress: [oController.doNavBack, oController],
            content: [oViewRepeater_NoViews],
            headerContent: [],
            footer: new sap.m.Bar({})
        });
}

1 个答案:

答案 0 :(得分:0)

您对VIZCharts的绑定不正确。它可能是复制粘贴的,因为它也使用了命名模型oModel。此外,您从绝对根/plants开始,然后是/Function,而您可能希望从当前Function元素的相对路径plants开始。

如果您将绑定更改为相对路径:data:{path:"Function"}它应该可以工作(您可能需要调整图表的大小以适合视图转发器块。)

通过将其更改为相对路径Function,它将获取当前/plants元素中的数据。