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

overlapping bar graph with bar different width #3946

Closed
karthiksd9 opened this issue Feb 22, 2017 · 7 comments
Closed

overlapping bar graph with bar different width #3946

karthiksd9 opened this issue Feb 22, 2017 · 7 comments

Comments

@karthiksd9
Copy link

I don't know whether it is possible or not. But here is my requirement:

I want a chart having 2 bar overlapped over one another (not stacked).
The width of inner bar should be smaller than the outer bar.

If this is possible can anyone post a sample code in fiddle.

Thanks in advance.

@etimberg
Copy link
Member

@karthiksd9 this is mostly possible. You can take a look at #3849 to get some ideas

@karthiksd9
Copy link
Author

karthiksd9 commented Feb 23, 2017

@etimberg Thanks for the reply.
Overlapping bar graph is possible. But how to change the width of bar. I tried using 2 x-axis and set different thickness to it using barThickness. But its not working.
Here's the fiddle. https://jsfiddle.net/karthiksd9/17hvoa9t/8/

Finally i need one smaller size bar inside a larger one.

@karthiksd9
Copy link
Author

@etimberg .Please help me. can this be achieved using categoryPercentage and barPercentage somehow?

How to have 2 categoryPercentage and barPercentage in a same graph?

@etimberg
Copy link
Member

It's unfortunately not possible to have 2 categoryPercentage options on the same graph. It might be easier to make a new chart type that places and sizes the bars the way you would like

@potatopeelings
Copy link
Contributor

You could also write a plugin to override the thicknesses for one of the datasets after they have been set.

    Chart.plugins.register({
        afterDatasetsUpdate: function (chart) {
            Chart.helpers.each(chart.getDatasetMeta(0).data, function (rectangle, index) {
                rectangle._view.width = rectangle._model.width = 30;
            });
        },
    })

    ...

See https://jsfiddle.net/17hvoa9t/11/

f

However, since this messes around with the internal variables, I don't think there won't be a guarantee that it will work though version changes.

Cheers!

Note - because of #3849, you can't use v2.5.0. You'll need to use v2.4.0 or the next release.

@potatopeelings
Copy link
Contributor

potatopeelings commented Feb 26, 2017

I checked this again to figure out why the 2nd x axis wasn't working and realized you don't actually need a plugin that messes with internal variables.

For the same effect as above, set your 2nd x axis properties like so

    ...
    {
      display: false,
      stacked: true,
      id: "bar-x-axis2",
      barThickness: 70,
      // these are needed because the bar controller defaults set only the first x axis properties
      type: 'category',
      categoryPercentage: 0.8,
      barPercentage: 0.9,
      gridLines: {
        offsetGridLines: true
      }
    }],

The bar controller defaults (and the core controller's initConfig) set the first x axis's properties, but it doesn't do anything for your 2nd x axis - so we need to set it explicitly (including categoryPercentage and barPercentage - even if we are using only barThickness).

Fiddle - https://jsfiddle.net/17hvoa9t/12/

Note - the caveat about #3849 still holds. You can't use v2.5.0. You'll need to use v2.4.0 or the next release.

@karthiksd9
Copy link
Author

WOW! @etimberg , @potatopeelings Thanks a lot guys. This is just perfect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants