0

How do I make the last element in the data active, specifically July, in a js diagram? example https://codepen.io/pichugin/pen/eYgdvBx

[https://codepen.io/pichugin/pen/eYgdvBx][1]
pecha
  • 1

1 Answers1

0

With the new upcomming release of version 3 you can do this with the setActive elements.

Documentation: https://www.chartjs.org/docs/master/developers/api/#setactiveelementsactiveelements

var options = {
  type: 'bar',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
        label: '# of Votes',
        data: [12, 19, 3, 5, 2, 3],
        borderWidth: 1,
        hoverBackgroundColor: 'green',
      },
      {
        label: '# of Points',
        data: [7, 11, 5, 8, 3, 7],
        borderWidth: 1,
        hoverBackgroundColor: 'red',
      }
    ]
  },
  options: {
    scales: {}
  }
}

var ctx = document.getElementById('chartJSContainer').getContext('2d');
const chart = new Chart(ctx, options);
chart.setActiveElements([{
  datasetIndex: 0,
  index: 1,
}, {
  datasetIndex: 1,
  index: 1,
}]);
<body>
  <canvas id="chartJSContainer" width="600" height="400"></canvas>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.0.0-beta.10/chart.js" integrity="sha512-7igYTuENB1pHNsZ/SyzMYrcJAmRCk084yVOsxNNCQAdX1wSYvCeBOgSOMC6wUdKMO76kCJNOpW4jY3UW5CoBnA==" crossorigin="anonymous"></script>
</body>
LeeLenalee
  • 27,463
  • 6
  • 45
  • 69
  • Do You know if that is possible in react-chart-js-2 ? I'm trying to do something similar here https://stackoverflow.com/questions/74041743/set-active-property-on-bar-chart-on-load – AdamKniec Oct 13 '22 at 08:48