[Chartjs]-How do I invert an y-axis using chart.js (v2.0-dev)

30πŸ‘

βœ…

Dev 2.0 of chart js supports an option to reverse the ticks when setting up the axis so your declaration of the second axis would become

{
    type: "invertedLinear", // only linear but allow scale type registration. This allows extensions to exist solely for log scale for instance
    display: true,
    position: "right",
    id: "y-axis-2",
    ticks: {
        reverse: true
    },
    // grid line settings
    gridLines: {
        drawOnChartArea: false, // only want the grid lines for one axis to show up
    }
}

Here it is in action https://jsfiddle.net/nwc8ys34/15/

23πŸ‘

Using v 2.7.0, and the following seems to work:

options: {
  scales: {
    yAxes: [{
      ticks: {
        reverse: true,
      }
    }]
  }
}

3πŸ‘

In v3 the scale configs have been changed so the desired behaviour can be accomplished like so:

const options = {
  type: 'line',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
        label: '# of Votes',
        data: [40, 80, 100, 70, 60, 80],
        borderColor: 'pink'
      },
      {
        label: '# of Points',
        data: [7, 11, 5, 8, 3, 7],
        borderColor: 'orange',
        yAxisID: 'y2'
      }
    ]
  },
  options: {
    scales: {
      y: {},
      y2: {
        position: 'right',
        reverse: true
      }
    }
  }
}

const ctx = document.getElementById('chartJSContainer').getContext('2d');
const chart = new Chart(ctx, options);
<body>
  <canvas id="chartJSContainer" width="600" height="400"></canvas>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.6.0/chart.js"></script>
</body>

1πŸ‘

Chart.js 4:

datasets: [{
    label: "Strength",
    yAxisID: "y1",
    data: mappedData.sigStrength
}, {
    label: "Bars",
    yAxisID: "y2",
    data: mappedData.sigBars
}]
scales: {
    y1: {
        type: "linear",
        display: true,
        position: "left",
        reverse: true
    },
    y2: {
        type: "linear",
        display: true,
        position: "right",
        ticks: {
            stepSize: 1
        }
    }
},

Leave a comment