c3 icon indicating copy to clipboard operation
c3 copied to clipboard

No way to access categories from event callback

Open lblb opened this issue 12 years ago • 4 comments

I noticed that there is no way to get associated category or time from within event (click, hover etc). All I see is an x index number on axis (http://jsfiddle.net/63EAV/10/). Ideally, I'd like to have reference to categories array.

In fact even API does have access to category names.

For example, show tooltip function calls getXAxisTickFormat(), which returns category value, but this function is private and not accesible from event callback.

Another suggestion is to redefine this in callback function, setting it to c3 object returned from c3.generate. In this case you'll be able to call API functions from event context

lblb avatar Jun 06 '14 07:06 lblb

Hi, Thank you for the suggestion. I agree with your idea that we should be able to access categories and the format.

How about adding formatAPI that returns the current format? I think it would be like this:

var chart = c3.generate({ ... });
var xFormat = chart.format('x'); // return the result of getXAxisTickFormat()

Then, you can call xFormat in event handlers and get a formatted string of x.

I think including the formatted x in args of event handler would another solution for this. In this case, you can get the formatted x without calling any functions, but the solution above is more flexible I think.

And some APIs about category might needs to be added like:

var categories = chart.categories(); // return current categories
chart.categories([...]);  // update current categories
var category = chart.category(x); // return category at index i of current categories

Then, you can access categories array anywhere chart object is accessible.

I think I would like to add chart.format API because this seems the most flexible solution. What do you think?

masayuki0812 avatar Jun 08 '14 11:06 masayuki0812

Hi Masayuki, The API for categories (and time series) is very welcome. Your suggestion looks good for me, it will be flexible.

The only other change I'd like to see is redefining this in event callbacks. It's not always possible to access chart object, so you need a way to pass it around. In my own project where I use your charting lib I already tweaked couple of event handlers like this: __data_onclick.call(c3, d, that);

It's very little of code, but it kicks door wide open :). I hope it makes sense.

lblb avatar Jun 08 '14 15:06 lblb

All right, I think I understand. I'll work on this as your suggestion. Thanks!

masayuki0812 avatar Jun 09 '14 10:06 masayuki0812

Hi, I added category and categories API to access the categories in the chart. Please check the sample I added with the code. And I fixed to define this as chart object as you suggested. Could you try these code? Thanks.

masayuki0812 avatar Jun 12 '14 03:06 masayuki0812