Combine two Y axes into a single tooltip with ChartJS - Javascript Chart.js

Javascript examples for Chart.js:Chart Tooltip

Description

Combine two Y axes into a single tooltip with ChartJS

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.js"></script> 
      <script type="text/javascript">
    window.onload=function(){// w  w w.  ja  v a2 s.  com
const FOOS_BORDER_COLOR = 'rgba(255, 99, 132, 1)';
const FOOS_BG_COLOR = 'rgba(255, 99, 132, 0.4)';
const BARS_BORDER_COLOR = 'rgba(54, 162, 235, 1)';
const BARS_BG_COLOR = 'rgba(54, 162, 235, 0.4)';
const ctx = document.getElementById('myChart');
const data = {
   labels: ['week 1', 'week 2', 'week 3'],
  datasets: [
     {
       label: 'foos',
       data: [1, 2, 3],
       yAxisID: 'y-axis-0',
       backgroundColor: FOOS_BG_COLOR,
       borderColor: FOOS_BORDER_COLOR,
       pointBackgroundColor: FOOS_BORDER_COLOR,
       borderWidth: 2,
       pointBorderColor: '#fff',
       pointBorderWidth: 1,
       pointHoverBackgroundColor: '#fff',
       pointHoverBorderColor: FOOS_BORDER_COLOR,
    },
      {
       label: 'bar',
       data: [200, 300, 100],
       yAxisID: 'y-axis-1',
       backgroundColor: BARS_BG_COLOR,
       borderColor: BARS_BORDER_COLOR,
       pointBackgroundColor: BARS_BORDER_COLOR,
       borderWidth: 2,
       pointBorderColor: '#fff',
       pointBorderWidth: 1,
       pointHoverBackgroundColor: '#fff',
       pointHoverBorderColor: BARS_BORDER_COLOR,
    },
  ],
};
const options = {
  tooltips : {
    mode : 'index'
  },
   scales:
  {
    xAxes: [{
      ticks: {
        display: true,
      },
    }],
    yAxes: [
      {
        type: 'linear',
        id: 'y-axis-0',
        display: false,
        position: 'left',
      },
      {
        type: 'linear',
        id: 'y-axis-1',
        display: false,
        position: 'right',
      },
    ],
  },
};
const myChart = new Chart(ctx, {
    type: 'line',
    data,
    options,
});
    }

      </script> 
   </head> 
   <body> 
      <canvas id="myChart" width="400" height="200"></canvas>  
   </body>
</html>

Related Tutorials