来自数据库的日期和时间值将在轴上使用

时间:2012-10-29 17:20:40

标签: javascript sql dojo dojox.charting

我正在尝试将轴标签添加到我的X轴上,而不是默认的1,2,3,4等等。

从服务器返回的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}]

1 个答案:

答案 0 :(得分: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的增加 - 并且与阵列指数不一致。