最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Change size of a specific point on a line chart in Chart.js - Stack Overflow

programmeradmin1浏览0评论

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:

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:
Share Improve this question edited May 23, 2017 at 12:25 CommunityBot 11 silver badge asked May 3, 2016 at 8:36 YulianYulian 6,78911 gold badges71 silver badges103 bronze badges 7
  • show your plete code of linechart – Akhilesh Singh Commented May 3, 2016 at 8:38
  • Maybe this answer can help you: stackoverflow./questions/31522001/… – Jeroen Bellemans Commented May 3, 2016 at 8:43
  • @JeroenBellemans - I can't find anything useful there. Could you be more specific please? – Yulian Commented May 3, 2016 at 8:47
  • @AkhileshSingh - I updated my question. – Yulian Commented May 3, 2016 at 8:47
  • Wasn't there an accepted answer with the options to increase the size of the dots & labels? – Jeroen Bellemans Commented May 3, 2016 at 8:51
 |  Show 2 more ments

2 Answers 2

Reset to default 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

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

发布评论

评论列表(0)

  1. 暂无评论