【问题标题】:ChartJs - Is is possible to show only tooltip in one dataset?ChartJs - 是否可以在一个数据集中仅显示工具提示?
【发布时间】:2018-09-07 17:22:50
【问题描述】:

我有两个数据集,一个是一条线,第二个是一个条形。

问题是工具提示在两个数据集中都有效,而我只想在一个栏中工作。

我只想在条形数据集上显示工具提示,可以吗?

这是我的代码。

var chartData = {
  labels: ["January", "February", "March"],
  datasets: [
    {
      type: "line",
      label: "OEE",
      tooltip: false,
      borderColor: window.chartColors.red,
      borderWidth: 2,
      fill: false,
      data: [0,10,0,15]
    },
    {
      type: "bar",
      label: "Paro",
      backgroundColor: window.chartColors.yellow,
      data: [0,6,0,0],
      borderColor: "white",
      borderWidth: 2

    }
  ]
};
window.onload = function() {
  var ctx = document.getElementById("canvas").getContext("2d");
  window.myMixedChart = new Chart(ctx, {
    type: "bar",
    data: chartData,
    options: {
      responsive: true,
      title: {
        display: true,
        text: "Test"
      },
    tooltips: {
      callbacks: {
        title: function(tooltipItem) {
          return "Title";
        },
        label: function(tooltipItem) {
          return "Reason";
        }
      },
      backgroundColor: '#FFF',
      titleFontSize: 16,
      titleFontColor: '#0066ff',
      bodyFontColor: '#000',
      bodyFontSize: 14,
      displayColors: false
    }
    }
  });
};

提前致谢。

【问题讨论】:

    标签: angular chart.js chartjs-2.6.0


    【解决方案1】:

    您可以检查类型并将其禁用为,

    tooltips: {
              filter: function (tooltipItem, data) {
                  var type = data.type;
                  if (type == "Bar") {
                    return true;
                  } else {
                    return false;
                  }
              }
    

    【讨论】:

      【解决方案2】:

      编辑了 Sajeetharan 的代码,这对我有用:

          tooltips: {
            filter: function (tooltipItem, data) {
              const type = data.datasets[tooltipItem.datasetIndex].type;
              if (type === 'bar') {
                return true;
              } else {
                return false;
              }
            }
          }
      

      【讨论】:

      • 谢谢!这很有帮助。
      猜你喜欢
      • 1970-01-01
      • 2021-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-02
      • 1970-01-01
      相关资源
      最近更新 更多