Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

96
Visualizações
ChartJS set styling on the last point only

I have a chart where all the visual points are disabled, I have disabled them by doing this:

const options = {
    elements: {
      point: {
        radius: 0,
      },
    }

My issue is that I actually want to have the point enabled only for the very last point, please see this image: http://prntscr.com/1tq0zql

I have found a way to target that last line by using segments:

const data = {
    labels: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    datasets: [
      {
        data: tgpDatasets,
        borderColor: "rgba(103,120,137,1)",
        borderWidth: 1,
        segment: {
          borderColor: ({ p0DataIndex, p1DataIndex }) => p0DataIndex === 8 && p1DataIndex === 9 && "red",
        },
      },
    ],
  };

The above code will make the last line of the chart red, please see this screenshot: http://prntscr.com/1tq155f

My question is how to enable a point for that very last point of the chart and set its porperties, when the radius is controlled from the options object and not from the dataset object?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can use scriptable options for this like so:

const options = {
  type: 'line',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
        label: '# of Votes',
        data: [12, 19, 3, 5, 2, 3],
        borderColor: 'pink',
        backgroundColor: 'pink'
      },
      {
        label: '# of Points',
        data: [7, 11, 5, 8, 3, 7],
        borderColor: 'orange',
        backgroundColor: 'orange'
      }
    ]
  },
  options: {
    elements: {
      point: {
        radius: (ctx) => (ctx.chart.data.datasets[ctx.datasetIndex].data.length - 1 === ctx.index ? 10 : 0)
      }
    }
  }
}

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

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda