如何在React ChartJS 2中隐藏我的折线图中的一些点?

时间:2018-02-08 11:47:29

标签: javascript reactjs chart.js react-chartjs

当我自己的值为0时,我想隐藏或更改点的颜色。

ChartJS的早期版本,您可以通过以下代码访问这些点元素:

var myChart = new Chart(ctx).Line(data);
for (var i = 1; i <= data.datasets[0].data.length - 1; i++) 
   if (data.datasets[0].data[i - 1] === data.datasets[0].data[i])
      myChart.datasets[0].points[i].display = false;

示例:http://jsfiddle.net/3tok57dL/

我正在使用chart.js 2.7.0和react-chartjs-2 2.6.2,我找不到如何访问这些元素。

感谢。

2 个答案:

答案 0 :(得分:2)

您可以访问一些应用于点的数据集属性(请参阅http://www.chartjs.org/docs/latest/charts/line.html),例如pointRadiuspointBackgroundColor

检查小提琴已更新:http://jsfiddle.net/beaver71/79d1bnf2/

var ctx = document.getElementById("myChart").getContext("2d");

var data = {
  labels: ["January", "February", "March", "April", "May", "June", "July", "January", "February", "March", "April", "May", "June", "July", "January", "February", "March", "April", "May", "June", "July"],
  datasets: [{
    label: "My Second dataset",
    fillColor: "rgba(151,187,205,0.2)",
    strokeColor: "rgba(151,187,205,1)",
    pointColor: "rgba(151,187,205,1)",
    pointStrokeColor: "#fff",
    pointHighlightFill: "#fff",
    pointHighlightStroke: "rgba(151,187,205,1)",
    data: [0, 0, 0, 0, 0, 10, 10, 10, 10, 10, 19, 86, 27, 90, 28, 48, 40, 19, 86, 27, 90]
  }]
};
var options = {};
var myChart = new Chart(ctx, {
  type: 'line',
  data: data,
  options: options
});

checkPoints = function(remove) {
  myChart.data.datasets[0].pointBackgroundColor = [];
  myChart.data.datasets[0].pointRadius = [];
  for (var i = 1; i <= data.datasets[0].data.length - 1; i++) {
    if (data.datasets[0].data[i - 1] === data.datasets[0].data[i]) {
      if (remove) {
        myChart.data.datasets[0].pointRadius[i] = 0;
      }
      myChart.data.datasets[0].pointBackgroundColor[i] = 'red';
    } else {
      myChart.data.datasets[0].pointBackgroundColor[i] = 'blue';
    }
  }
  myChart.update();
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.bundle.min.js"></script>

<button onclick="checkPoints()">check points</button>
<button onclick="checkPoints(1)">check points and remove</button>

<canvas id="myChart" height="300" width="800"></canvas>

答案 1 :(得分:1)

您可能需要查看API方法getDatasetMeta

  

此返回的数据包含用于构建图表的所有元数据。

使用此元数据,您可以将特定点的radius设置为零以隐藏它们。

我用下面的chart.js 2.7.1复制了你的例子:

&#13;
&#13;
var ctx = document.getElementById("myChart").getContext("2d");

var data = {
  labels: ["January", "February", "March", "April", "May", "June", "July", "January", "February", "March", "April", "May", "June", "July", "January", "February", "March", "April", "May", "June", "July"],
  datasets: [{
    label: "My Second dataset",
    fillColor: "rgba(151,187,205,0.2)",
    strokeColor: "rgba(151,187,205,1)",
    pointColor: "rgba(151,187,205,1)",
    pointStrokeColor: "#fff",
    pointHighlightFill: "#fff",
    pointHighlightStroke: "rgba(151,187,205,1)",
    data: [0, 0, 0, 0, 0, 10, 10, 10, 10, 10, 19, 86, 27, 90, 28, 48, 40, 19, 86, 27, 90]
  }]
};
var myChart = new Chart(ctx, {
  type: 'line',
  data: data
});

var meta = myChart.getDatasetMeta(0);
for (var i = 1; i <= data.datasets[0].data.length - 1; i++) 
{
   if (data.datasets[0].data[i - 1] === data.datasets[0].data[i])
   {   		
   		meta.data[i]._model.radius = 0;
   }
}
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.min.js"></script>
<canvas id="myChart" height="300" width="800"></canvas>
&#13;
&#13;
&#13;