从JSON数据创建简单的DOJO折线图

时间:2012-04-04 20:32:49

标签: json dojo

我尝试使用jSON数据在DOJO中绘制一个简单的折线图,但没有成功,非常感谢任何帮助。

我返回了以下数据片段:

[{"id":"24","Entry":"2012-02-10","Symbol":"Various","Type":"Speculativ","Call1":"0","Call2":"0","Put1":"0","Put2":"0","Cost":"0.00","Qty":"0","PnL":"383","R":"0.00","Risk":"6691"},    
{"id":"23","Entry":"2012-02-01","Symbol":"VariousSp","Type":"Vertical","Call1":"0","Call2":"0","Put1":"0","Put2":"0","Cost":"0.00","Qty":"0","PnL":"341","R":"0.00","Risk":"19160"}]

我试图直接从数据存储中绘制,构建一个数组,一切都无济于事。我对Dojo很新,所以我很确定我犯了一个简单的错误 -

我的代码(目前存在如下):

var PnLStore;
    var cumPnL = [];

    require([
            "dojox/charting/Chart",
            "dojo/store/JsonRest",
            "dojo/store/Memory",
            "dojo/store/Cache",
            "dojox/charting/StoreSeries",
            "dojox/charting/plot2d/Lines",
            "dojox/charting/axis2d/Default",
            "dojox/charting/themes/Dollar",
            "dojox/charting/plot2d/Markers",
            "dojo/domReady!"],

            function( Chart, JsonRest, Memory, Cache, StoreSeries, Lines) {

                    PnLStore = Cache(JsonRest({target:"pullTradesClosed.php"}), Memory());

                    var chart = new Chart("chartDiv");

                    chart.addPlot("default", {
                            type: "Lines",
                            markers: true
                    });

                    chart.addAxis("x");
                    chart.addAxis("y", { vertical: true, fixLower: "major", fixUpper: "major" });

                    PnLStore.query({id:"id"}).forEach(
                      function(trade) {
                        cumPnL.push (Number(trade.PnL));
                    });

                    chart.addSeries("PnL", cumPnL, {stroke:"green"});
                    chart.render(); 

            });

虽然可能不是很明显,但我试图将ID与PnL进行对比。

1 个答案:

答案 0 :(得分:0)

如果您想将图表绑定到数据存储区,就像您正在做的那样,最好使用storeseries:

chart.addSeries("y", new StoreSeries(store, { query: {} }, "PnL"));

请参阅: http://dojotoolkit.org/documentation/tutorials/1.7/charting_advanced/

示例

您也可以使用数据集:

addSeries(“PnL”,new dojox.charting.DataSeries(store,{query:{}},“PnL”))。

请参阅: Dojox charting programattically using store series 相关问题