C3.js exclude columns from CSV

巧了我就是萌 提交于 2019-12-01 09:34:37

Inspired by @Armani I've ended up doing this, which is a combination of using d3.csv function with c3, as I didn´t find a way to do it with c3 URL.

  1. First I used D3 to load the CSV file into a JSON object
  2. Modify the data as required (not filtering)
  3. Feed C3 with the json object
  4. Use the "keys" property inside the c3 call to only show the fields I want.

d3.csv( _dataPath, function ( d ) {

  var data = d;

  if ( configdata.axis.x.source_units == "meses" && configdata.axis.x.units == "años" )
  {
    data[ configdata.axis.x.property_key ] = data[ configdata.axis.x.property_key ] / 12;
  }

  return data;

}, function( error, data ) {

  if (!error)
  {

    var percentiles = [];
    configdata.percentilesData.forEach( function( p ) {
      percentiles.push( p.name );
    });

    var x_axis_label = configdata.axis.x.label + ' (' + configdata.axis.x.units + ')';
    var y_axis_label = configdata.axis.y.label + ' (' + configdata.axis.y.units + ')';

    var chart = c3.generate({
      bindto: '#chart',
      data: {
        json: data,
        type: 'line',
        keys: {
          x: configdata.axis.x.property_key,
          value: percentiles
        }
      },
      axis: {
        y: {
          label: y_axis_label
        },
        x: {
          label: x_axis_label
        }
      },
      tooltip: {
        show: false
      },
      point: {
        show: false
      }
    });

  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>

you have to add a filter to your file's columns/keys

d3.csv("yourfile.csv", function(csv) {
 csv = csv.filter(function(key) { 
  return key != "Sex" && key != "L" && key != "M" && key != "S" ;
   }); 
 });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!