简体   繁体   English

数据被过滤chartJs时如何保持圆角?

[英]How to keep rounded bar corners when data is filtered chartJs?

I came up with this JSFiddle: https://www.jsfiddle.net/gcb1dyou which has rounded chartJs bar corners.Problem is when legend clicked to filter data,corners disappear like below我想出了这个 JSFiddle: https://www.jsfiddle.net/gcb1dyou它有圆角的 chartJs 条形角。问题是当图例点击过滤数据时,角消失如下在此处输入图像描述

When I clicked orange label as you can see rounded border disappeared on the yellow bar.当我单击橙色 label 时,您可以看到黄色条上的圆形边框消失了。

var lastVisible = 0;
for (var findLast = 0, findLastTo = this._chart.data.datasets.length; findLast < findLastTo; findLast++) {
if (!this._chart.getDatasetMeta(findLast).hidden) {
  lastVisible = findLast;
  if (this._chart.data.datasets[findLastTo - 1].data[this._index] == 0) {
    lastVisible -= 1;
  }
}

} Here I tried to add another if to make lastVisible findLast-1 when data is hidden(legend clicked) and previous index is null but didn't work在这里,我尝试添加另一个 if 以使 lastVisible findLast-1 当数据被隐藏(图例点击)并且先前的索引是 null 但没有工作

else{
          if(this._chart.data.datasets[findLastTo - 1].data[this._index] == 0){
            lastVisible=findLastTo-2;
          }
        }

How can I solve this?Expecting to see your answers.我该如何解决这个问题?期待看到你的答案。

You already have check to apply rounded corners when not hidden (filtered).您已经检查在未隐藏(过滤)时应用圆角。 Just need to add condition to apply rounded corners when filtered assuming that zeros are not shown.假设未显示零,只需添加条件以在过滤时应用圆角。

Something like this:像这样的东西:

if (!this._chart.getDatasetMeta(findLast).hidden) {
    lastVisible = findLast;
    if (this._chart.data.datasets[findLastTo - 1].data[this._index] == 0) {
        lastVisible -= 1;
    }
} else {
    if (this._chart.data.datasets[findLast].data[this._index] == 0) {
        lastVisible = findLast;
        lastVisible -= 1;
    }
}

Here is modified JSFiddle: https://jsfiddle.net/xopr36g5/这里是修改后的 JSFiddle: https://jsfiddle.net/xopr36g5/

I fixed this problem by calculating the depth of dataset dynamically.Thanks for answers我通过动态计算数据集的深度解决了这个问题。谢谢你的回答

 var lastVisible;
  var datasetsLength = this._chart.data.datasets.length;
  this._chart.data.datasets.map((e,index)=>{
    lastVisible=datasetsLength-1;
    //to find the depth of datasets and get non-zero value
    for(var i=lastVisible;i>0;i--){
    if(!this._chart.getDatasetMeta(i).hidden){
      if(this._chart.data.datasets[i].data[this._index] != 0){
        lastVisible = i;
        break;
      }
    }
    } 
  })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM