问题描述:

I am trying to plot a chart in D3.js using is native CSV parser [ d3.csv() - https://github.com/mbostock/d3/wiki/CSV ]

An example of a grouped bar chart plotted via d3.csv() is here - http://bl.ocks.org/mbostock/3887051

The issue I am facing is that I am unable to get the chart working when the headers in the CSV are unknown.

For example in the above example the chart works only if the headers in the CSV file is

State,Under 5 Years,5 to 13 Years,14 to 17 Years,18 to 24 Years,25 to 44 Years,45 to 64 Years,65 Years and Over

If I use the headers like

Year,Age,Gender

then the chart does not work. The chart needs to be working with any kind of CSV files with any headers.

Would appreciate any help to build a dynamic chart with unknown headers.

Thanks!

网友答案:

I got the solution for this.

function glow(i){
d3.csv("inputs/conData.csv",function(data){

//CSV File parsing
    var gl = [];

    var keys = Object.keys(data[0]);
    data.forEach(function(d) {
        gl.push(d[keys[i]]);
    });
}

This function will accept the column number as an argument for which the header needs to be returned.

Reference: https://groups.google.com/forum/#!topic/d3-js/dAfM2Mk_NWY

相关阅读:
Top