Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bar chart with x-axis min and max acts weird with one datapoint #945

Closed
RanzQ opened this issue Jan 27, 2015 · 4 comments
Closed

Bar chart with x-axis min and max acts weird with one datapoint #945

RanzQ opened this issue Jan 27, 2015 · 4 comments
Labels
C-bug Category: This is a bug resolved maybe

Comments

@RanzQ
Copy link

RanzQ commented Jan 27, 2015

I'd like to use the bar chart with a fixed x-axis min, max and values. But the padding and bar width calculation don't work nicely when min and max is set. Try modifying the example http://c3js.org/samples/chart_bar.html like this:

var chart = c3.generate({
    data: {
        columns: [
            ['data1', 30],
            ['data2', 30]
        ],
        type: 'bar'
    },
    axis: {
        x: {
            min: 0,
            max: 5,
            tick: {
                values: [0, 1, 2, 3, 4, 5]
            }
        }
    }
});

Would it be possible to base the calculations on min-max range and tick-count rather than point count in this case?

@aendra-rininsland
Copy link
Member

Hi @RanzQ!

The reason this is happening is that the x axis in this case is index-based. By specifying max: 5, you tell C3 to ensure there's space for five data points. If there's only one (as there is in this case), it'll compress it into one side.

There totally is a bit of weirdness with min and max in the example you've given, though — data1 is half as wide as data2, which doesn't make much sense, even if you're using it oddly (That is, you probably shouldn't specify min/max with indexed axes, C3 will take care of that generally. Min/max are really only used with numerical axes.).

Attaching screencap. @masayuki0812, not sure if I should tag this as bug or invalid?
screen shot 2015-01-27 at 17 08 22

Also, can confirm it occurs in 0.4.9, not sure what version the website is currently running.

Edit: Going with bug for the moment, feel free to re-tag.

@aendra-rininsland aendra-rininsland added the C-bug Category: This is a bug label Jan 27, 2015
@RanzQ
Copy link
Author

RanzQ commented Jan 27, 2015

I made a temp fix by modifying getMaxDataCount to prefer axis_x_tick_values length if specified and greater than maxDataCount:

c3.chart.internal.fn.getMaxDataCount = function () {
    var $$ = this, config = $$.config,
        maxDataCount = $$.d3.max($$.data.targets, function (t) { return t.values.length; }),
        tickValues = config.axis_x_tick_values
    return tickValues && tickValues.length > maxDataCount ? tickValues.length : maxDataCount
};

That fixed the width problem I was having. For the min-max bug I just adjusted the min and max in case of one datapoint and it gets centered. Not sure if that affects other charts.

I'm using this on a timeseries, min and max work well to fit my domain to a specific time range.

@masayuki0812
Copy link
Member

Thank you for looking into this. Yeah, it's not intended, but it's a bug and width of bar should be computed correctly in this situation. So, I fixed by introducing tickInterval, which returns the length between ticks, and compute the width using this function.
I'll release this in the next version 0.4.10 shortly. Thanks.

@masayuki0812
Copy link
Member

0.4.10 has been released, so let me close at the moment. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug resolved maybe
Projects
None yet
Development

No branches or pull requests

3 participants