问题
I am very confused by the way dates are handled by dimple (or maybe it is just D3).
My problem can be divided in two questions:
My dates come as a column in my csv file in the format
%Y-%m-%d %H:%M:%S
. In order to convert them to date objects, I currently use the same loop I used to have in "vanilla" d3.data.forEach(function (d) { var format = d3.time.format("%Y-%m-%d %H:%M:%S"); d.Date = format.parse(d.Date); });
Is there a quicker way to do this with? Maybe with the timeField function? Or dateParseFormat? I might be wrong but those functions looks like helpers to handle dates.
The main reason why I want to control my time objects is to be able to reformat them. Documentation seems to indicate that the tickFormat function can be used for this. Unfortunately I also failed at using it. My best guess:
var x = myChart.addCategoryAxis("x", "Date");
x.tickFormat(d3.time.format("%Y-%m")); // For example for a year-month format
only returns
x.tickFormat is not a function
Thanks a lot,
Xavier
回答1:
The time formatting relates to time axes. You don't need your loop if you want to use the time axis, just use:
myChart.addTimeAxis("x", "Date", "%Y-%m-%d %H:%M:%S", "%Y-%m");
where "%Y-%m-%d %H:%M:%S" is your input and "%Y-%m" is your output format. The reason for the "x.tickFormat is not a function" error is that it isn't :). You can set the tickFormat used by the time axis as a property if you wish but it isn't required if you use the factory method above:
myTimeAxis.tickFormat = "%Y-%m";
But this only works with a time axis.
An example of the time axes in action can be found here:
http://dimplejs.org/advanced_examples_viewer.html?id=advanced_time_axis
Hope that helps
John
来源:https://stackoverflow.com/questions/20195870/dimple-time-format-juggling