[Chartjs]-Point Style property with Inverted Image in Line Chart.js

2👍

Chart js allows an array of pointStyles to be supplied instead of just 1. It also allows images to be supplied in place of a preset pointer style.

var image = document.getElementById('source');
var options = {
  type: 'line',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
        label: '# of Votes',
        data: [12, -19, 3, 5, 2, 3],
        borderWidth: 1,
        pointStyle: ['triangle', image, 'triangle', 'triangle', 'triangle', 'triangle'],
        pointRadius: '10',
        pointBackgroundColor: 'red'
      },

    ],
    pointStyle: 'triangle'
  },
  options: {
    scales: {
      yAxes: [{
        ticks: {
          reverse: false
        }
      }]
    }
  }
}

var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.3.0/Chart.js"></script>
<canvas id="chartJSContainer" width="600" height="400"></canvas>
<div style="display:none;">
  <img id="source" src="https://image.flaticon.com/icons/png/128/25/25224.png">
</div>

To then make this dynamic you could map the data in the datset to produce the pointerStyle array

let pointerStyles = [12,-19,2,3,4].map(value=>{
 return value >=0 ? 'triangle' : 'other-triangle'
});

console.log(pointerStyles);

Leave a comment