简体   繁体   English

如何在RGraph中使用日期作为值并沿X轴使用?

[英]How can I use dates as values and along the X axis in RGraph?

I'm trying out the horizontal bar RGraph demo with this code and it works great: 我正在尝试使用此代码进行单杠RGraph演示,并且效果很好:

var data = [1, 40, 30];
var hbar = new RGraph.HBar('myCanvas', data);
hbar.Set('chart.labels', ['Richard', 'Alex', 'Nick']);
hbar.Set('chart.background.barcolor1', 'white');
hbar.Set('chart.background.barcolor2', 'white');
hbar.Set('chart.background.grid', true);
hbar.Set('chart.colors', ['red']);
hbar.Draw();

Is there a way I can use Date objects instead of numbers? 有没有一种方法可以使用Date对象而不是数字? I couldn't get it to work with stuff like 我无法将其与类似

var data = [new Date(1000), new Date(2000), new Date(3000)];

In that case, you can do something like : 在这种情况下,您可以执行以下操作:

var data, dates = [new Date("11/16/2011"), new Date("11/17/2011"), new Date("11/18/2011")], labels = [];
// Get the date from the date objects
for(var i = 0, len = dates.length; i < len; i++) {
    // Data for the graph
    data[i] = dates[i].getDate();

    // Labels for each data entry
    labels[i] = dates[i].getDate() + "/" + dates[i].getMonth() + "/" + dates[i].getYear();  
}
var hbar = new RGraph.HBar('myCanvas', data);
hbar.Set('chart.labels', labels);

/// rest of the code 

Date object on MDN : https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date MDN上的日期对象: https//developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM