0

我正在尝试使用日期而不是默认的 1、2、3、4 等向我的 X 轴添加轴标签。

从服务器返回的 date_time 也需要转换为可读的日期/时间。

任何帮助表示赞赏。

代码

<script type="text/javascript"> 

var chart;
var store;

require(["dojo/request/xhr",
    "dojo/json",
    "dojo/store/Memory",
    "dojo/store/Observable",
    "dojox/charting/StoreSeries",
    "dojox/charting/Chart2D", 
    "dojox/charting/plot2d/Lines", 
    "dojox/charting/axis2d/Default",    
    "dojo/domReady!"], 

    function(xhr, JSON, Memory, Observable, StoreSeries, Chart) {

    xhr("json_result.php",{
            handleAs: "json"
        }).then(function(data){ 

            store = Observable(new Memory({data: data}));

            chart = new Chart("graph");

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

            chart.addAxis("x", {title:"Time", titleOrientation:"away"});
            chart.addAxis("y", {vertical: true, majorTick: false, title:"Load"});

            chart.addSeries("Load Cell 1", new StoreSeries(store, { query: { load_cell_id:0 }}, "kn"));
            chart.addSeries("Load Cell 2", new StoreSeries(store, { query: { load_cell_id:1 } }, "kn"));

            chart.render();
        });         
});


</script>

JSON 结果

[{"date_time":1351280845,"load_cell_id":0,"kn":56.8},{"date_time":1351280845,"load_cell_id":1,"kn":45},{"date_time":1351367241,"load_cell_id":0,"kn":23.7},{"date_time":1351367241,"load_cell_id":1,"kn":34.9},{"date_time":1351417945,"load_cell_id":0,"kn":56.9},{"date_time":1351417945,"load_cell_id":1,"kn":67.8},{"date_time":1353914066,"load_cell_id":0,"kn":12.4},{"date_time":1353914066,"load_cell_id":1,"kn":19.43},{"date_time":1353992714,"load_cell_id":0,"kn":45.8},{"date_time":1353992714,"load_cell_id":1,"kn":40.8}]
4

1 回答 1

0

尝试以下,它关于控制索引(主要),然后在标签生成函数中格式化 data-date_time。

ddl =>             "dojo/date/locale", require this

chart.addAxis("x", {
    title: "Time",
    titleOrientation: "away",
    includeZero: true,
    //from: 0, to: (data.length -1),
    minorTicks: false,
    labelFunc: function(n){              
        // I am assuming that your timestamp needs to be multiplied by 1000. 
        var date = new Date(parseInt(data[n].date_time) * 1000);
        return ddl.format(date,  {
            selector: "date",
            datePattern: "dd MMMM"
        });

    }
});

次要刻度将在当前主要刻度上产生 0.1、0.2 的加法 - 并且与数组索引配合得不好。

于 2012-10-29T23:24:04.600 回答