3

I want to change the size of a specific point on a line chart in Chart.js. I saw in this answer how to change the color of a point but I can't find a solution for changing its size. Any ideas?

// dataArray and labelsArray are hard-coded arrays of int values.
var lineChartData = {
    datasets: [{
        data: dataArray,
        pointStrokeColor: "#fff",
        fillColor: "rgba(220,220,220,0.5)",
        pointColor: "rgba(220,220,220,1)",
        strokeColor: "rgba(220,220,220,1)"
    }],
    labels: labelsArray
};

// Changing color of point #5
myLineChart.datasets[0].points[4].fillColor =  "#FF0000";

// Changing point's size
// TODO:
Community
  • 1
  • 1
Yulian
  • 6,262
  • 10
  • 65
  • 92

2 Answers2

5

Late Answer. But it'll be helpful for the people who is searching for this.

Code to set different point size in line chart in Chart.js.

var speedCanvas = document.getElementById("speedChart");

var pointRadius=[];
var dataFirst = {
    label: "Car A - Speed (mph)",
    data: [10, 59, 75, 25, 20, 65, 40],
    lineTension: 0.3,
    fill: false,
    borderColor: 'red',
    backgroundColor: 'transparent',
    pointBackgroundColor: 'green',
    pointBorderColor:'green',
    pointRadius: pointRadius
  };

var speedData = {
  labels: ["0s", "10s", "20s", "30s", "40s", "50s", "60s"],
  datasets: [dataFirst]
};

var chartOptions = {
  legend: {
    display: true,
    position: 'top',
    labels: {
      boxWidth: 80,
      fontColor: 'black'
    }
  }
};

var lineChart = new Chart(speedCanvas, {
  type: 'line',
  data: speedData,
  options: chartOptions
});

for(var i=0;i<this.lineChart.data.datasets[0].data.length;i++){
  pointRadius.push(i);
}
lineChart.update();

The below code is to set the point size.

for(var i=0;i<this.lineChart.data.datasets[0].data.length;i++){
  pointRadius.push(i);
}
lineChart.update();

This method is working in latest versions of ChartJS

Running CodePen Example : CodePen Example

Sulthan
  • 354
  • 3
  • 19
  • You should add at least a little comment, why this is working: Some point properties are "indexabe" (see https://www.chartjs.org/docs/latest/general/options.html#indexable-options). Means: If you define a single property, like pointRadius = 5. Chart.js will not automatically create a property for every point created. So you cannot just adress chart.data.datasets[0].pointRadius[5]. You have to provide the property as an array, either via callback (https://stackoverflow.com/a/59119270/2360229) or fixed, like pointRadius = [1,2,3,4,5] – n.r. Oct 04 '20 at 21:41
0

You can Simply increase the size of dot in line chart follow the Documentation of Chart.js. There is customizing method is available.

You can try this:

var myLineChart = Chart.Line(ctx, {
    pointDot: false,
    pointLabelFontSize: 20
});

lineChartData = {
datasets: [{
    data: dataArray,
    pointStrokeColor: "#fff",
    fillColor: "rgba(220,220,220,0.5)",
    pointColor: "rgba(220,220,220,1)",
    strokeColor: "rgba(220,220,220,1)"
}],
labels: labelsArray
};

// Changing color of point #5
   myLineChart.datasets[0].points[4].fillColor =  "#FF0000";

pointLabelFontSize: 20 // Font Size in pixel

Refrence1

Linechart

Community
  • 1
  • 1
Akhilesh Singh
  • 1,724
  • 2
  • 19
  • 35