Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

95
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!