Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

118
Vistas
Get coordinates radarchart ChartJS using getValueForDistanceFromCenter

Recently i started experimenting with chart.js and the building of radarcharts. I mastered the basics (See basic chart in the code), but I would like to use the x y coordinates of the graph for placing texts directly on the canvas. After some digging I found out that it is not possible to use the getValueForPixel or getPixelForTick in a radarchart. See github https://github.com/chartjs/Chart.js/issues/5931. In the connecting thread a new method getValueForDistanceFromCenter is introduced. As I understand it, with this method it would be possible to calculate the distance from the center and use it to get coordinates. I searched the documentation of chart.js and other sites but cannot find any code examples or more information on how to implement this. Could somebody point me in the right direction how to implement the method in the code? I would be very grateful.

var data = {
  labels: ["Ball Skills", "Shooting", "Physical"],
  datasets: [{
    label: [`ikke`, `jij`],
    backgroundColor: "rgba(38,120,255,0.2)",
    borderColor: "rgba(38,120,255, 1)",
    data: [90, 90, 90]
  }]
};

var options = {
  responsive: true,
  tooltips: false,
  title: {
    text: 'Basic example',
    display: true,
    position: `bottom`,
  },
  scale: {
    angleLines: {
      display: true
    },
    ticks: {
      suggestedMin: 0,
      suggestedMax: 100,
      stepSize: 25, 
      maxTicksLimit: 11,
      display: false, 
    }
  },
  legend: {
    labels: {
      padding: 10,
      fontSize: 14,
      lineHeight: 30,
    },
  },
};

var myChart = new Chart(document.getElementById("chart"), {
  type: 'radar',
  data: data,
  options: options
});

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

The radialLinear scale (in version 2.9.4 that I have seen your are using version 2) there is the method getValueForDistanceFromCenter(value) to get the distance from center but there is another method getPointPositionForValue(index, value) which can provide you the point at a specif index of your data.

To use them and to draw what you want on chart using those points, you need to implement a plugin. In the below snippet, I'm drawing a rect between the points at a specific value.

const ctx = document.getElementById("myChart");
const data = {
  labels: ["Ball Skills", "Shooting", "Physical"],
  datasets: [{
    label: [`ikke`, `jij`],
    backgroundColor: "rgba(38,120,255,0.2)",
    borderColor: "rgba(38,120,255, 1)",
    data: [50, 50, 50]
  }]
};

const options = {
  responsive: true,
  tooltips: false,
  title: {
    text: 'Basic example',
    display: true,
    position: `bottom`,
  },
  scale: {
    angleLines: {
      display: true
    },
    ticks: {
      suggestedMin: 0,
      suggestedMax: 100,
      stepSize: 25, 
      maxTicksLimit: 11,
      display: false, 
    }
  },
  legend: {
    labels: {
      padding: 10,
      fontSize: 14,
      lineHeight: 30,
    },
  },
};

const plugin = {
  id: 'getDistance',
  afterDraw(chart) {
    const c = chart.ctx;
    const rScale = chart.scale;
    c.save();
    chart.data.datasets[0].data.forEach(function(item, index) {
      const point = rScale.getPointPositionForValue(0.5 + index, 50);
      c.beginPath();
      c.fillStyle = 'red';
      c.fillRect(point.x - 5, point.y - 5, 10, 10);
      c.fill();
    });
    c.restore();
  }
};

const myChart = new Chart(ctx, {
  type: 'radar',
  plugins: [plugin],
  data: data,
  options: options
});
.myChartDiv {
  max-width: 600px;
  max-height: 400px;
}
<script src="https://cdn.jsdelivr.net/npm/chart.js@2.9.4/dist/Chart.min.js"></script>
<html>
  <body>
    <div class="myChartDiv">
      <canvas id="myChart" width="600" height="400"/>
    </div>
  </body>
</html>

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda