dc.js icon indicating copy to clipboard operation
dc.js copied to clipboard

Converting a year to a usable date format

Open jonscrut opened this issue 13 years ago • 4 comments

I am trying to use a csv data source to with a column titled "Year" to produce a bar chart. I can't see how to get the Year into a usable date format. Is there anyone that can help?

I am using dc.js 1.2 with d3.v3.min.js

I tried adapting the main example but have no idea what the "e" comes from:

var dateFormat = d3.time.format("%Y");
var numberFormat = d3.format(".2f");

         data.forEach(function(e) {
            e.dd = dateFormat.parse(e.Year);
         });

I also tried this: var dateYear = new Date(d.Year, 0);// The required 0, is for January

The variable is declared as: var yearDimension = data.dimension(function(d){ return d3.time.year(d.dd);});

And I also tried this to give up on the date format completely: .x(d3.scale.ordinal()) //.xUnits(d3.units.ordinal);

Declaring the variable as: var yearDimension = data.dimension(function(d){ return d["Year"];});

and also var yearDimension = data.dimension(function(d){ return (String(d["Year"]));});

but it generated this error:

Uncaught TypeError: Cannot read property 'ordinal' of undefined 

jonscrut avatar Feb 20 '13 10:02 jonscrut

e is the element currently being iterated through. The d3.time.format should work in this case or if you want to create a javascript Date then you need to convert string to a int by simply put a + in front of it. The x scale on your chart should be a d3.time.scale since your x series will be time.

Also ordinal units is a dc construct so it should be dc.units.ordinal if you want to use ordinal series. Checkout this page for some simple examples https://github.com/NickQiZhu/dc.js/blob/master/test/play-ground.html

NickQiZhu avatar Feb 20 '13 19:02 NickQiZhu

Thanks again for your help.

I got the ordinal series working:

  .gap(5)
    .x(d3.scale.ordinal().domain(["2007", "2008", "2009", "2010", "2011"]))
    .xUnits(dc.units.ordinal);

But I still can't get the time series to work.

This is my page: http://dl.dropbox.com/u/135842/dcd3-dashboard/nq-ex-jf/barchart_index.htm

I am getting this error:

   Uncaught TypeError: Cannot call method 'forEach' of undefined 

This is my code:

 var yearBarChart = dc.barChart("#year-bar-chart");



 // load data from a csv file
 d3.csv("export-import.csv", function(csv) {


        var dateFormat = d3.time.format("%Y");
        var numberFormat = d3.format(".2f");

     data.forEach(function(e) {
        e.dd = dateFormat.parse(e.year);
        e.year = d3.time.year(e.dd); // pre-calculate month for better performance
     });



        // feed it through crossfilter
        var data = crossfilter(csv);
        var all = data.groupAll();



        var exporterDimension = data.dimension(function(d){return d["countrycode"];});
        var exporterDimensionGroup = exporterDimension.group().reduceSum(function(d){return d["amount"];});

        var yearDimension = data.dimension(function(d){ return d["year"];});
        var yearDimensionGroup = yearDimension.group().reduceSum(function(d){return d["amount"];});

        var importerDimension = data.dimension(function(d){return d["importer"];});
        var importerDimensionGroup = importerDimension.group().reduceSum(function(d){return d["amount"];});

        var countrynameDimension = data.dimension(function(d){return d["countryname"];});
        var countrynameDimensionGroup = countrynameDimension.group().reduceSum(function(d){return d["amount"];});




 yearBarChart.width(500)
    .height(150)
    .dimension(yearDimension)
    .group(yearDimensionGroup)
    // .stack(dateValueSumGroup)
   //  .stack(dateValueSumGroup, function(d) {
    //     return 10;
   //  })
    .gap(1)
    .x(d3.time.scale()) //.domain([new Date(2006, 1, 1), new Date(2012, 12, 31)])
    .xUnits(d3.time.years)
    .xAxis().ticks(1);     

        dc.renderAll();
    });

jonscrut avatar Feb 20 '13 20:02 jonscrut

This is fixed. I had the wrong variable for the csv file. Correct code is:

    d3.csv("export-import.csv", function(csv) {


        var dateFormat = d3.time.format("%Y");
        var numberFormat = d3.format(".2f");

     csv.forEach(function(e) {
        e.dd = dateFormat.parse(e.year);
        e.year = d3.time.year(e.dd); // pre-calculate month for better performance
     });

Thanks so much for this library, it's fantastic!

jonscrut avatar Feb 20 '13 23:02 jonscrut

Nice. I was just going to say it works on my machien =) Dont forget to take the default dc.css file, it should make your chart a lot prettier.

NickQiZhu avatar Feb 21 '13 03:02 NickQiZhu