在Chart.js中更改折线图上特定点的大小

时间:2016-05-03 08:36:06

标签: javascript chart.js linechart

我想在Chart.js中更改折线图上特定点的大小。我在this answer中看到如何改变点的颜色,但我找不到改变其大小的解决方案。有什么想法吗?

// 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:

2 个答案:

答案 0 :(得分:2)

最新答案。但这对于正在搜索此内容的人会有所帮助。

用于在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();

下面的代码用于设置点大小。

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

此方法在ChartJS的最新版本中有效

运行CodePen示例:CodePen Example

答案 1 :(得分:0)

您可以按照Chart.js的文档简单地增加折线图中点的大小。有定制方法可供使用。

你可以试试这个:

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 //像素字体大小

Refrence1

Linechart