-->

how to display month in sequence wise in dc.js

2019-08-04 14:29发布

问题:

I'm making a barchart using the Dimensional Charting javascript library dc.js. I am trying to display barchart in sequence like jan,feb,mar,apr.

I can't understand how to put the months in sequence. I have attached fiddile https://jsfiddle.net/jusjkm8j/27/ .

The data is in the following format.

 Sku      Stars partner review  Date
 KBU12     5       Amazon   Preferred over the r.   02/05/2016
 KBU12     5       Amazon   Gorgeous fixture    05/06/2016
 KBU12     2       Amazon   Awesome value!  02/02/2016
 KBU12      2      Homedepot    Kitchen Remodel 06/09/2016
 KPF1650    3   Homedepot   Wow 02/08/2016
 KPF1650    4   Homedepot   Sharp!  05/04/2015
 KPF1650    5   Homedepot   cool design 05/05/2015
 KPF1650    1   Amazon  Beautiful   09/08/2015
 KPF1650    2   Amazon  Very happy  09/11/2016
 GV-100     3   Amazon  Great quality   12/12/2015
 GV-100     5   Homedepot   Love it 01/03/2015
 GV-100     1   Homedepot   Sad I had to return it  12/10/2014
 GV-100     4   Homedepot   Beautiful But Stiff 12/11/2014
 GV-100     2   Homedepot   Kitchen Faucet  10/04/2014
 KBU24      3   Build       I like the look of faucet   04/05/2014
 KBU24      5   Build       Commercial style without    06/07/2016
 KBU24      4   Build       Very Cool   06/07/2014
 KBU24      1   Build       Excellent!  01/04/2014
 KBU24      3   Build       A beautiful Vessel Sink 07/06/2013
 KBU24      5   Amazon      Beautiful addition  05/04/2013

回答1:

with an ordinal scale the axis will be ordered by the identity function.

One way to achieve this would be to change your month formatter to

d3.time.format('%m.%b'); // 01.Jan

Then you can on your chart do

spendHistChart22.xAxis().tickFormat(d => d.substr(3))

this will order them correctly and also display only the name of the month.

See fiddle: https://jsfiddle.net/b96exL8b/