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

Stacked bar chart? #226

Closed
Scalpel78 opened this issue Oct 15, 2017 · 4 comments
Closed

Stacked bar chart? #226

Scalpel78 opened this issue Oct 15, 2017 · 4 comments

Comments

@Scalpel78
Copy link

Hi, I'm looking for a Vue component that can do stacked bar.

Does vue-chartjs support the Stacked Bar type? (http://www.chartjs.org/samples/latest/charts/bar/stacked.html)

@apertureless
Copy link
Owner

Sure.

https://stackoverflow.com/questions/37249439/chartjs-v2-0-stacked-bar-chart

You only have to pass the stacked: true flag to the options.

options: {
    scales: {
      yAxes: [{
        stacked: true,
        ticks: {
          beginAtZero: true
        }
      }],
      xAxes: [{
        stacked: true,
        ticks: {
          beginAtZero: true
        }
      }]

    }

@Scalpel78
Copy link
Author

Thanks, got one step closer to acheiving the graph I want. But the data isn't stacked the same way as in the example. This is a screenshot of my graph:

image

Which is created from this:

import { Bar } from 'vue-chartjs'

export default {
  extends: Bar,
  mounted () {
    // Overwriting base render method with actual data.
    this.renderChart({
      labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
      datasets: [
        {
          label: 'Invoiced',
          backgroundColor: '#12c44c',
          data: [800, 900, 1000, 850, 820, 920, 700, 1010, 999, 820, 500]
        },
        {
          label: 'Order',
          backgroundColor: 'red',
          data: [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200]
        }
      ],
      options: {
        scales: {
          yAxes: [{
            stacked: true
          }],
          xAxes: [ {
            stacked: true
          }]
        },
        legend: {
          display: true
        },
        responsive: true,
        maintainAspectRatio: false
      }
    })
  }
}

But I'd like it to be stacked like this:
image

Any idea what I have to change to get it stacked like that?

@apertureless
Copy link
Owner

apertureless commented Oct 15, 2017

Well it is working fine for me:

https://codepen.io/apertureless/pen/zEmNpp

You also need to change the categoryPercentage and barPercentage so the bar charts are not that thin.

Do you have the current version of chart.js installed?

@Scalpel78
Copy link
Author

Thanks, got it working now. Think the problem was that my options needed to be passed in as the second parameter to renderChart.

This is the final version that works:

import { Bar } from 'vue-chartjs'

export default {
  extends: Bar,
  mounted () {
    // Overwriting base render method with actual data.
    this.renderChart({
      labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
      datasets: [
        {
          type: 'line',
          label: 'Budget',
          backgroundColor: 'brown',
          fill: false,
          data: [1020, 1020, 1020, 1020, 1020, 350, 600, 1020, 1020, 1020, 1020, 1020]
        },
        {
          type: 'bar',
          label: 'Invoiced',
          backgroundColor: '#12c44c',
          data: [1050, 900, 1000, 850, 820, 420, 700, 1010, 999, 340, 0, 0]
        },
        {
          type: 'bar',
          label: 'Order',
          backgroundColor: 'red',
          data: [0, 0, 0, 0, 0, 0, 0, 0, 0, 800, 120, 90]
        }
      ]}, {
        title: {
          display: true,
          text: 'Oversikt'
        },
        scales: {
          yAxes: [{
            stacked: true
          }],
          xAxes: [ {
            stacked: true,
            categoryPercentage: 0.5,
            barPercentage: 1
          }]
        },
        legend: {
          display: true
        },
        responsive: true,
        maintainAspectRatio: false
      }
    )
  }
}

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

2 participants