Chartjs-Chartjs – display radarchart lines from center to corner value

0πŸ‘

βœ…

This is possible by writing a custom inline plugin like so:

var options = {
  type: 'radar',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
        label: '# of Votes',
        data: [12, 19, 3, 5, 2, 3],
        borderWidth: 1
      },
      {
        label: '# of Points',
        data: [7, 11, 5, 8, 3, 7],
        borderWidth: 1
      }
    ]
  },
  options: {
    plugins: {
      customLine: {
        //specify line color
        lineColor: 'red'
      }
    }
  },
  plugins: [{
    id: 'customLine',
    afterDatasetDraw: (chart, args, options) => {
      const {
        ctx,
        chartArea
      } = chart;
      const {
        meta
      } = args;

      // only do the first dataset lines, if you remove this if it will draw lines for all datasets
      if (meta.index > 0) {
        return;
      }

      meta.data.forEach((dataPoint) => {
        //default line color black as fallback if no color is specified
        ctx.strokeStyle = options.lineColor || 'black';
        ctx.beginPath();
        ctx.moveTo(chartArea.width / 2, chartArea.height / 2 + chartArea.top);
        ctx.lineTo(dataPoint.x, dataPoint.y);
        ctx.stroke();
      })

    }
  }]
}

var 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.1.0/chart.js" integrity="sha512-LlFvdZpYhQdASf4aZfSpmyHD6+waYVfJRwfJrBgki7/Uh+TXMLFYcKMRim65+o3lFsfk20vrK9sJDute7BUAUw==" crossorigin="anonymous"></script>
</body>

Leave a comment