javascript - Accessing data values in c3js in data events - Stack Overflow

I am trying to figure out, if there is a way to access the values from the Company data array in the on

I am trying to figure out, if there is a way to access the values from the Company data array in the onclick event. So far using the api functions I only get to the Users array.

var chartCompany = c3.generate({
    bindto: '#users-chart',        
    data: {
        x: 'Company',
        url: '/ajax_call',
        mimeType: 'json',
        type: 'bar',
        axes: {
            Company: 'x'
        },
        onclick: function (d, i) { console.log(chartCompany.data()); }
    },
    axis: {
        x: {
            type: 'category',
             show: false
        },
    }
});

And the json response from the server:

{
 "Company": ["Company 1", "Company 2", "Company 2"],
 "Users"  : [10, 20, 30]
}

Any help/ideas will be highly appreciated.

[Edit 1] To elaborate on my question: When clicking on a data from Users (which is displayed as bars) I would like to get the corresponding Company.

[Edit 2] Working static example: /

I am trying to figure out, if there is a way to access the values from the Company data array in the onclick event. So far using the api functions I only get to the Users array.

var chartCompany = c3.generate({
    bindto: '#users-chart',        
    data: {
        x: 'Company',
        url: '/ajax_call',
        mimeType: 'json',
        type: 'bar',
        axes: {
            Company: 'x'
        },
        onclick: function (d, i) { console.log(chartCompany.data()); }
    },
    axis: {
        x: {
            type: 'category',
             show: false
        },
    }
});

And the json response from the server:

{
 "Company": ["Company 1", "Company 2", "Company 2"],
 "Users"  : [10, 20, 30]
}

Any help/ideas will be highly appreciated.

[Edit 1] To elaborate on my question: When clicking on a data from Users (which is displayed as bars) I would like to get the corresponding Company.

[Edit 2] Working static example: http://jsfiddle/et37a9t2/

Share Improve this question edited May 28, 2015 at 23:31 stefita asked May 28, 2015 at 7:10 stefitastefita 1,7932 gold badges20 silver badges35 bronze badges 2
  • I'd be happy to help, but can you put together a fiddle of your chart without the ajax call, just a standalone version? – Sean Commented May 28, 2015 at 17:48
  • I put up a working example: jsfiddle/et37a9t2 – stefita Commented May 28, 2015 at 23:33
Add a ment  | 

1 Answer 1

Reset to default 7

After a lot of trying out different things, I finally just looked at the chart object itself and discovered a property categories. This is populated, when the x-axis is declared to be of type category as it is in my case. So to get the data from the x-axis one needs to call the .categories() function.

        ...
        },
        onclick: function (d, i) { console.log(chartCompany.categories()[d.index]); }
    },
    ...

When clicking on a bar in the chart the corresponding category will be returned.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745341891a4623363.html

相关推荐

  • javascript - Accessing data values in c3js in data events - Stack Overflow

    I am trying to figure out, if there is a way to access the values from the Company data array in the on

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信