How do I invert an y-axis using chart.js (v2.0-dev)
Asked Answered
C

6

21

I am creating line charts with 2 y-axes. one of those axes has two datasets but both run from the range of 0-100. The higher number being the better one. The second y-axis is usually lower in range (often in the single digits) and the best result is 1.

How can I invert the second y-axis so that 1 is at the top of the chart?

(I will try to find a solution myself, but at 5k+ lines in chart.js, it might take a while )

Thanks ^_^

Caspar answered 9/11, 2015 at 11:22 Comment(3)
can you provide a fiddle of what you have so farSylphid
Finally (wipes sweat off forehead) jsfiddle.net/nwc8ys34/13Caspar
So in the example you would like 4 at the top and 8 at the bottomSylphid
S
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/

Sylphid answered 9/11, 2015 at 15:26 Comment(3)
Awesome! - Thanks ^_^Caspar
Is this possible in 1.0.2? The current stable version.Aircrewman
as far as i know there is no support for this in 1.xSylphid
O
23

Using v 2.7.0, and the following seems to work:

options: {
  scales: {
    yAxes: [{
      ticks: {
        reverse: true,
      }
    }]
  }
}
Oly answered 5/2, 2018 at 12:21 Comment(1)
This will just swap the direction not the graph data.Ruttger
S
4

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>
Sagamore answered 29/10, 2021 at 14:17 Comment(0)
B
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
        }
    }
},
Bullbat answered 1/2, 2023 at 12:39 Comment(0)
J
1

If this is what you mean by inverting the chart. indexAxis allows you to flip the chart based on axes.

options: {
 indexAxis: "y",
}
Juliannajulianne answered 7/11, 2023 at 9:10 Comment(0)
I
1

For a simple line graph on chart.js v4:

On X Axis:

const options = {
    scales: {
        x: {
            reverse: true,
        }
    }
}

On Y Axis:

const options = {
    scales: {
        y: {
            reverse: true,
        }
    }
}

This will reverse the data for that particular axis.

Ideate answered 4/3 at 17:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.