c3 icon indicating copy to clipboard operation
c3 copied to clipboard

Feature request: Discrete bar chart

Open stevemrd opened this issue 9 years ago • 10 comments

My apologies if feature requests are frowned upon here, but I'm not sure where else to post this. I will also submit this as an issue on the project's github page as wel.

I would like to make a feature request for a "discrete bar chart" as a chart type. Using the existing bar chart, I am unsure how to get my data to display the way I want it, which is each "bar" is its own category and color. Example for the nvd3 library: http://nvd3.org/examples/discreteBar.html

As you can see from their example, each bar is its own category and color with its own label on the X axis. This is the functionality I would love to see in c3, as I use c3 for my line charts currently. (c3's line charts are less buggy than nvd3's.)

If this setup can be somehow replicated in c3, then please let me know how, as I would love to reduce my chart dependencies to just one.

Update: Please also note that I need to be able to toggle individual discrete bars on and off. Someone on google groups was kind enough to make me a fiddle demonstrating that a discrete chart IS possible, but their example only consists of a single category, so toggling that would also show/hide EVERY bard, rather than individual ones.

Fiddle: https://jsfiddle.net/t2j7s20o/

Thanks!

stevemrd avatar Dec 13 '16 16:12 stevemrd

would also love this feature, for now I implemented it like the fiddle you provided and hid the legend/tooltip since it made no sense (because of the colors) but would be nice to be able to toggle each data individually ...

panthony avatar Dec 15 '16 14:12 panthony

The only way I could think of to do it would be to add/remove data to/from the model I have, then push that new data to the chart and reload it. This seems kind of "hackish" to me, because now I can't simply show or hide a specific chart ID. There might also be a small performance hit to doing it this way, which I'm not happy about either.

stevemrd avatar Dec 15 '16 14:12 stevemrd

@stevemrd, I've run into this problem as well. There doesn't seem to be a support solution because C3 bar charts are essentially three-dimensional charts(x, y, and grouping). What im currently doing to workaround this problem is:

  1. multiplying the data series by an identity array to create a matrix with all values zeroed-out except the diagonal axis. For example:
data = [1, 2, 3]
    
transformedData = [
    ["a", 1, 0, 0],
    ["b", 0, 2, 0],
    ["c", 0, 0, 3]
]
  1. Stacking the bar chart:
{ 
    data: {
        ...
        groups: [
            ['a', 'b', 'c']
        ]
    }
}
  1. then ungrouping the tooltip so that only one value is shown when hovering:
{
    tooltip: {
        grouped: false
    }
}

If anyone has a better solution, i'd love to hear it

brmc avatar Dec 16 '16 13:12 brmc

an example: https://jsfiddle.net/t2j7s20o/2/

edit: you should turn off labels too, otherwise it will show the zeroed out values

brmc avatar Dec 16 '16 14:12 brmc

it may be a workaround but the result is rather nice @brmc , thanks for the fiddle !

Then we could hide the '0' from the data labels, map each label to the legend (or hide it but I like being able to hide them)

    labels: {
    	format: function(v) {
      	return v === 0 ? '' : v;
      }
    },
    names: {
      data1: 'A Label',
      data2: 'B Label',
      data3: 'C Label',
      data4: 'D Label'
    }

panthony avatar Dec 16 '16 14:12 panthony

@brmc , your fiddle was a step in the right direction, however there are still a few issues with it:

  • Sometimes when I hide a series in the legend, the legend names switch places. It doesn't always do this, but try to randomly toggle any of the legend on or off and see what I mean.

  • This still seems rather hackish, as I would have to insert 0s for every value that's not supposed to be in that set. That would mean quite a bit of extra JSON in my case, so I'd rather not do this.

Please let me know if you make any other improvements. Thanks. :)

stevemrd avatar Dec 16 '16 14:12 stevemrd

@stevemrd, regarding your first point, that just seems to be a bug unrelated to this workaround. i'd file a report

and to your second point, it's undoubtedly a hack :) But a hack that i've seen necessary in more than one charting library. it's really odd that the default way to present a bar graph would be like this.

Regarding the json comment and not specific to this issue, i suggest doing any data manipulation in the client to preserve clear separation between your data and presentation.

and good luck. I'm not a C3js dev, so I doubt I will be fixing this or making improvements anytime soon. I

brmc avatar Dec 16 '16 15:12 brmc

The sort issue with the legend is probably related to https://github.com/c3js/c3/pull/1741 .

I do have an issue with this workaround though. When I click on one of the visible bar I may have multiple click events triggered (one for the one I clicked on, and apparently always the last hidden one).

The last point may be related to https://github.com/c3js/c3/issues/1658.

I had, in the onClick event, check for null in the point's value and simply ignore that event.

It may be hackish but the result is exactly what I needed, thanks !

panthony avatar Dec 20 '16 15:12 panthony

any updates on this?

shasiuk1 avatar Jan 23 '17 11:01 shasiuk1

@sergejgasjuk I think you should not expect any progresses on this any time soon.

If the proposed workaround works for you (as it did for me) all the better but since there is pretty much no PRs merged anymore, even for bug fixes, you should not expect a new feature such as this to be available any time soon.

panthony avatar Jan 23 '17 13:01 panthony