Chartjs-Chartjs-plugin-zoom: get currently visible values when the page first loads

2👍

You can make use of the animations, they have a property to check if its the first time they have fired, although since you dont want animations you will need to set the main one to a verry low number and at least the tooltip to false and the transition duration of active elements to 0.

const getVisibleValues = ({
  chart
}) => {
  console.log(chart.scales.x.ticks.map(el => ({
    value: el.value,
    label: el.label
  }))) // Map because stack console prints whole circular context which takes long time
}

const options = {
  type: 'line',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
      label: '# of Votes',
      data: [12, 19, 3, 5, 2, 3],
      borderColor: 'pink'
    }]
  },
  options: {
    animation: {
      onComplete: (ani) => {
        if (ani.initial) {
          getVisibleValues(ani)
        }
      },
      duration: 0.0001
    },
    transitions: {
      active: {
        animation: {
          duration: 0
        }
      }
    },
    plugins: {
      tooltip: {
        animation: false
      }
    }
  }
}

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

Leave a comment