3
votes

I am using Chart.js to generate a horizontal stacked bar chart. The chart currently looks like this: enter image description here

This chart shows the user after how many years they should restorate a specific component of a house. I am trying to change this to in which year the user should do the restoration. Adding the current year to the values results to the following: enter image description here

This is pretty much what I need if I could set the starting value of the x-axis to the current year. I tried to do this setting the minimum value like this:

options: {
    scales: {
        xAxes: [{
            ticks: {
                min: 2017
            },
        ...

Unfortunatly results in not displaying the datasets at all like this:

enter image description here

I tried all combinations with adding the current year and setting the minimum values but nothing results in a useful chart.

In the following you can see my current source code:

var mainChart_ctx = document.getElementById("main_chart").getContext("2d");

var mainChart_config = {
  type: 'horizontalBar',
  data: {
    labels: ['Kellerdecke', 'Fenster', 'Außenwand', 'Erdberührter Boden', 'Dach'],
    datasets: [{
        label: 'Beginn ab heute',
        backgroundColor: 'transparent',
        data: [4, 21, 25, 25, 25],
        borderColor: '#666',
        borderWidth: 1
      },
      {
        label: 'Sanierungsdauer',
        backgroundColor: '#ffcc00',
        data: [2, 5, 5, 5, 5],
        borderColor: '#666',
        borderWidth: 1
      },
      {
        label: 'Mittlere Restlebensdauer',
        backgroundColor: 'orange',
        data: [39, 0, 38, 51, 37],
        borderColor: '#666',
        borderWidth: 1
      },
      {
        label: 'Maximale Restlebensdauer',
        backgroundColor: 'orangered',
        data: [20, 0, 0, 0, 0],
        borderColor: '#666',
        borderWidth: 1
      }
    ]
  },
  options: {
    tooltips: {
      enabled: true
    },
    legend: {
      display: false
    },
    title: {
      display: true,
      text: 'Sanierungsfahrplan',
      fontSize: 24
    },
    scales: {
      xAxes: [{
        ticks: {
          min: 0 /* Todo: change to current year? */
        },
        stacked: true,
        scaleLabel: {
          display: true,
          labelString: 'Jahre',
          fontSize: 16
        }
      }],
      yAxes: [{
        ticks: {
          stepSize: 10
        },
        stacked: false,
        scaleLabel: {
          display: true,
          labelString: 'Bauteil',
          fontSize: 16
        },
      }]
    }
  }
};

mainChart = new Chart(mainChart_ctx, mainChart_config)
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.bundle.min.js"></script>

<canvas id="main_chart"></canvas>
2
If you change the min value from 0 to 3 in your snippet, something ugly happens. It seems like a bug of the stacked bar chart. You could report this at github.com/chartjs/Chart.js/issues.xnakos

2 Answers

1
votes

I was able to get a suitable result using a callback like this:

xAxes: [{
    ticks: {
        min: 0,
        callback: function (value, index, values) {
            return value + 2017;
        }
     },
...
]
0
votes

I found that suggestedMin works well:

options: {
  scales: {
      xAxes: [{
          display: true,
          ticks: {
              suggestedMin: 2017
          }
      }]
  }
}