当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - c3JS Load Function

问题描述:

  1. I'm making c3js chart that looks like:

    var chart = c3.generate({

    bindto: '#chart',

    data: {

    columns: []

    }

    });

  2. Then I'm trying to use load function from it's api:

    var data = [["Name", "15", "30", "45"], ["x", "1", "2", "3"]];

    function update_graph(arg) {

    var tmp = arg[0][1];

    chart.load({

    xs: {

    tmp : arg[1][0] //binding data to x here and getting an error

    },

    columns: [

    arg[0],

    arg[1]

    ],

    });

    }

    update_graph(data);

And I'm getting: Uncaught Error: x is not defined for id = "Name".

If I set in xs section: Name : arg[1][0] - it does work. But then I will not be able to draw different lines from different arrays with this function. What am I doing wrong ? Thank you.

网友答案:

Working code to make setting key/value to be generic from 'arg' 2D array:- jsFiddle

Solution is to construct the xs object first like:-

var xs = {};
xs[arg[0][0]] = arg[1][0];
xs[arg[2][0]] = arg[3][0];

and then use xs object in the chart.load function like:-

chart.load({
  xs: xs,
  columns: [
    arg[0],
    arg[1],
    arg[2],
    arg[3],
  ],
});

Note that key in xs object is not hardcoded to 'Name' or 'Age' string. Hope this works for you.

网友答案:

Following example is working fine, with multiple lines defined in different arrays.- jsFiddle

var chart = c3.generate({
bindto: '#chart',
data: {
   columns: []
}
});


var data =  [["Name", "15", "30", "45"], ["x", "1", "2", "3"], ["Age", "45", "38", "25"], ["y", "1", "2", "4"]];

function update_graph(arg) {
  var tmp = arg[0][1];
  chart.load({
   xs: {
    Name : arg[1][0],
    Age : arg[3][0]  //binding data to x here and getting an error
   },
   columns: [
     arg[0],
     arg[1],
     arg[2],
     arg[3],
   ],
 });
}
update_graph(data);  

Not sure what the issue is, which version of c3js library are you using? Please refer to the c3js example:- c3js Example

分享给朋友:
您可能感兴趣的文章:
随机阅读: