Search code examples
chart.jsreact-chartjsreact-chartjs-2

How to make labels on both side from horizontal bar chart js


I want to make side labels for double horizontal bar on both side like this: pic 1, pic 2

can someone help me idk how to do it, im new in react and chartjs

this post continued from: How to make multiple horizontal bar chartjs

here's what i code:

  • this the data:

data for chart

export const dataPasienKeluarMasuk = {
  type: 'bar',
  labels: [
    [0, 1, 2, 3,4],    // expect output 0 - 4
    [5, 6, 7, 8, 9],   // expect output 5 - 9
    [10, 14],          // ext..
    [15, 19],
    [20, 24],
    [25, 29],
    [30, 34],
  ],
  datasets: [
    {
      label: 'Pasien Masuk',
      xAxisID: 'A',
      data: [100, 90, 80, 70, 60],
      backgroundColor: 'red',
    },
    {
      label: 'Pasien Keluar',
      xAxisID: 'A',
      data: [-100, -90, -80, -70, -60],
      backgroundColor: 'blue',
    },
  ],
}
  • this the chart:

multiple y horizontal bar

import { HorizontalBar } from 'react-chartjs-2'
import { dataPasienKeluarMasuk } from ...blabla

<HorizontalBar
  data={dataPasienKeluarMasuk}
  height={227}
  options={{
    responsive: true,
    title: {
      display: true,
      text: 'Data Pasien Keluar Masuk',
      fontSize: 20,
    },
    legend: {
      display: true,
      position: 'bottom',
    },
    scales: {
      xAxes: [
        {
          stacked: true,
          scaleLabel: {
            display: true,
            labelString: 'Jumlah Pasien (orang)',
          },
          ticks: {
            // Include a dollar sign in the ticks
            callback: (value) => Math.abs(value),
          },
        },
      ],
      yAxes: [
        {
          stacked: true,
          ticks: {
            display: true,
            reverse: true,
          },
        },
      ],
    },
    tooltips: {
      callbacks: {
        label: (tooltipItem, data) => {
          let ds = data.datasets[tooltipItem.datasetIndex]
          return (
            ds.label + ': ' + Math.abs(ds.data[tooltipItem.index])
          )
        },
      },
    },
  }}
/>

Solution

  • You can obtain the desired result by defining a second y-axis as follows:

    {
      type: 'category',
      position: 'right',
      offset: true,
      ticks: {
        reverse: true,
      },
      gridLines: {
        display: false
      }
    }
    

    Please take a look at your amended and runnable code below:

    new Chart(document.getElementById('canvas'), {
      type: 'horizontalBar',
      data: {
        labels: ['0-4', '5-9', '10-14', '15-19', '20+'],
        datasets: [{
            label: 'Pasien Masuk',
            data: [100, 90, 80, 70, 60],
            backgroundColor: 'red',
          },
          {
            label: 'Pasien Keluar',
            data: [-100, -75, -60, -75, -70],
            backgroundColor: 'blue',
          },
        ]
      },
      options: {
        responsive: true,
        title: {
          display: true,
          text: 'Data Pasien Keluar Masuk',
          fontSize: 20,
        },
        legend: {
          position: 'bottom',
        },
        tooltips: {
          callbacks: {
            label: (tooltipItem, data) => {
              let ds = data.datasets[tooltipItem.datasetIndex];
              return ds.label + ': ' + Math.abs( ds.data[tooltipItem.index]);
            }
          }
        },
        scales: {
          xAxes: [{
            stacked: true,
            ticks: {
              callback: value => Math.abs(value)
            }
          }],
          yAxes: [{
            stacked: true,
            ticks: {
              reverse: true,
            }
          },
          {
            type: 'category',
            position: 'right',
            offset: true,
            ticks: {
              reverse: true,
            },
            gridLines: {
              display: false
            }
          }]
        }
      }
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script>
    <canvas id="canvas" height="100"></canvas>