在Mouseenter Generatelegend Chartjs上突出显示甜甜圈段2



正如标题所述,我正在尝试根据传说中的图表中的一个段( on mouseenter event )。我正在使用 Generatelegend 来创建我的自定义图例,但我不确定如何将Mouseenter事件附加到每个项目。

这是我到目前为止的链接

window.onload = function() {
setTimeout(function(){
  var outerCircle = new Chart(document.getElementById("outer-circle"),{
    "type":"doughnut",
    "data":{
      "labels":["Beer","Wine","Pisco","Vodka","Rum","Tequila"],
      "datasets":[{
        "label":"Drinks",
        "data":[
          30,
          20,
          5,
          15,
          15,
          15
        ],
        "backgroundColor":[
          "#fdc694",
          "#ad937c",
          "#d8c2ae",
          "#bab8b6",
          "#e5aa74",
          "#fcf0e5"
        ]
      }]
    },
    "options":{
      "legend":{
        "display":false
      },
      "tooltips": {
        "mode": 'label',
        "callbacks": {
          "label": function(tooltipItem, data) {
            return " "+data['labels'][tooltipItem['index']] + " " + data['datasets'][0]['data'][tooltipItem['index']] + '%';
          }
        }
      }
    }
  });
  var diagramLegend = document.getElementById('diagram-legend');
  diagramLegend.innerHTML = outerCircle.generateLegend();
  }, 300);
  }

任何提示或帮助的代码都很棒,我一直在花几个小时,但没有成功,文档对此并不清楚。

您可以使用以下功能突出显示甜甜圈图的相应段,当悬停在自定义传奇项目上时:

function highlightSegment(chart, index, isHighlight) {
   var activeSegment = chart.getDatasetMeta(0).data[index];
   if (isHighlight) chart.updateHoverStyle([activeSegment], null, true);
   else chart.updateHoverStyle([activeSegment], null, false);
   chart.draw();
}

但是,首先,您需要附加两个事件列表(MouseEnter和MouseLeave) 到您的每个 li (legend) elements/itemp,传递图表 - 传说 - 项目索引和布尔值(是否添加/删除突出显示)分别为第一个,第二和第三参数。

<sᴏʀᴋɪɴɢxᴀᴍᴘʟᴇ>

var outerCircle = new Chart(document.getElementById("outer-circle"), {
   "type": "doughnut",
   "data": {
      "labels": ["Beer", "Wine", "Pisco", "Vodka", "Rum", "Tequila"],
      "datasets": [{
         "label": "Drinks",
         "data": [
            30,
            20,
            5,
            15,
            15,
            15
         ],
         "backgroundColor": [
            "#fdc694",
            "#ad937c",
            "#d8c2ae",
            "#bab8b6",
            "#e5aa74",
            "#fcf0e5"
         ]
      }]
   },
   "options": {
      "legend": {
         "display": false
      },
      "tooltips": {
         "mode": 'label',
         "callbacks": {
            "label": function(tooltipItem, data) {
               return " " + data['labels'][tooltipItem['index']] + " " + data['datasets'][0]['data'][tooltipItem['index']] + '%';
            }
         }
      }
   }
});
var diagramLegend = document.getElementById('diagram-legend');
diagramLegend.innerHTML = outerCircle.generateLegend();
/*** Highlight Doughnut Segment on Legend Hover ***/
var legendItems = document.querySelectorAll('#diagram-legend li');
legendItems.forEach(function(item, itemIndex) {
   item.addEventListener('mouseenter', function() {
      highlightSegment(outerCircle, itemIndex, true);
   });
   item.addEventListener('mouseleave', function() {
      highlightSegment(outerCircle, itemIndex, false);
   });
});
function highlightSegment(chart, index, isHighlight) {
   var activeSegment = chart.getDatasetMeta(0).data[index];
   if (isHighlight) chart.updateHoverStyle([activeSegment], null, true);
   else chart.updateHoverStyle([activeSegment], null, false);
   chart.draw();
}
#pie-wrapper {
   width: 300px;
   height: 300px;
}
#diagram-legend ul {
   list-style: none;
   padding: 0;
   margin: 0;
}
#diagram-legend ul li {
   margin-bottom: 10px;
}
#diagram-legend ul li span {
   width: 15px;
   height: 15px;
   display: inline-block;
   vertical-align: sub;
   margin-right: 10px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
<div id="pie-wrapper">
   <canvas id="outer-circle" width="300" height="300"></canvas>
</div>
<div id="diagram-legend"></div>

    color: [
      {
        backgroundColor: ['#92d06f', '#73c8b8', '#3bb9ab'],
        borderColor: 'rgba(255, 255, 255, 0)',
        hoverBackgroundColor: ['#92d06f', '#73c8b8', '#3bb9ab'],
        hoverBorderColor: ['#92d06f', '#73c8b8', '#3bb9ab'],
        hoverBorderWidth: 10,
        hoverRadius: 0
      }
   ],

它在鼠标上工作正常,Enter Enter在Donut Chart的Angular 2(NG2图表)中突出显示了活动段

相关内容

  • 没有找到相关文章

最新更新