Chartjs-Chart.js scatter charts: Is there a way to indicate that datapoints of two data sets are on top of each other?

1👍

You can make use of the scriptable options to give the point a different background color if there are multiple points in that spot

Example:

const overlappingPointsCheck = (x, y) => {
  if (!x.parsed) return;

  let overlappingPoints = false;
  for (let i = x.datasetIndex + 1; i < x.chart.data.datasets.length; i++) {
    if (x.parsed.y === x.chart.data.datasets[i].data[x.parsed.x]) {
      overlappingPoints = true;
      break;
    }
  }
  return overlappingPoints;
}

const multipleEntrieColor = 'purple';

const options = {
  type: 'scatter',
  data: {
    labels: [0, 1, 2, 3],
    datasets: [{
        label: '# of Votes',
        data: [12, 11, 3, 5],
        borderWidth: 1,
        backgroundColor: (x, y) => {
          return overlappingPointsCheck(x, y) ? multipleEntrieColor : 'red';
        }
      },
      {
        label: '# of Points',
        data: [7, 11, 5, 8],
        borderWidth: 1,
        backgroundColor: (x, y) => {
          return overlappingPointsCheck(x, y) ? multipleEntrieColor : 'blue';
        }
      },
      {
        label: '# of Points',
        data: [2, 9, 7, 8],
        borderWidth: 1,
        backgroundColor: (x, y) => {
          return overlappingPointsCheck(x, y) ? multipleEntrieColor : 'green';
        }
      }
    ]
  },
  options: {}
}

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.2.0/chart.js"></script>
</body>

Leave a comment