删除标签并仅在条形图的工具提示中显示值 [英] Remove the label and show only value in tooltips of a bar chart

查看:15
本文介绍了删除标签并仅在条形图的工具提示中显示值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用


我要做的就是删除条形图工具提示中显示的标签.

var ctx = document.getElementById("myChart");var myChart = new Chart(ctx, {类型:'酒吧',数据: {标签: [男人",女性",未知"],数据集:[{标签:男人",数据:[60、40、20],背景颜色: ['rgba(158, 216, 202, 0.75)','rgba(255, 150, 162, 0.75)','rgba(160, 160, 160, 0.75)']},{标签:女性",数据:[40,70,10],背景颜色: ['rgba(158, 216, 202, 0.5)','rgba(255, 150, 162, 0.5)','rgba(160, 160, 160, 0.5)']},{标签:'未知',数据:[33,33,34],背景颜色: ['rgba(158, 216, 202, 0.25)','rgba(255, 150, 162, 0.25)','rgba(160, 160, 160, 0.25)']}]},选项: {工具提示:{回调:{标签:功能(工具提示项,数据){var datasetLabel = '';var label = data.labels[tooltipItem.index];返回 data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index];}}}}});

<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.3.0/Chart.bundle.min.js"></script><canvas id="myChart" width="400" height="200"></canvas>

解决方案

只需使用带有空值的 title 选项.像这样:

回调:{标题:功能(工具提示项,数据){返回 '​​';},标签:功能(工具提示项,数据){var datasetLabel = '';var label = data.labels[tooltipItem.index];返回 data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index];}}

查看 更新的 jsfiddle

I am using ChartJS library to create charts. In the tooltip, I am showing the data value from the dataset I created. It works in case if the chart type is doughnut. Otherwise it doesn't work in case or bar or horizontalbar charts.

Whatever I do it shows the data with the labels.

Working JSFiddle of Doughnut Chart with data in tooltip.


Working JSFiddle of Bar Chart with label + data in tooltip.

All I want to do is to remove the label shown in the tooltips of the bar chart.

var ctx = document.getElementById("myChart");

var myChart = new Chart(ctx, {
  type: 'bar',
  data: {
    labels: [
      "Men",
      "Women",
      "Unknown"
    ],
    datasets: [{
        label: 'Men',
        data: [60, 40, 20],
        backgroundColor: [
          'rgba(158, 216, 202, 0.75)',
          'rgba(255, 150, 162, 0.75)',
          'rgba(160, 160, 160, 0.75)'
        ]
      },
      {
        label: 'Women',
        data: [40, 70, 10],
        backgroundColor: [
          'rgba(158, 216, 202, 0.5)',
          'rgba(255, 150, 162, 0.5)',
          'rgba(160, 160, 160, 0.5)'
        ]
      },
      {
        label: 'Unknown',
        data: [33, 33, 34],
        backgroundColor: [
          'rgba(158, 216, 202, 0.25)',
          'rgba(255, 150, 162, 0.25)',
          'rgba(160, 160, 160, 0.25)'
        ]
      }
    ]
  },
  options: {
    tooltips: {
      callbacks: {
        label: function(tooltipItem, data) {
          var datasetLabel = '';
          var label = data.labels[tooltipItem.index];
          return data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index];
        }
      }
    }
  }
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.3.0/Chart.bundle.min.js"></script>

<canvas id="myChart" width="400" height="200"></canvas>

解决方案

just use the title option with an empty value. Like this:

callbacks: {
        title: function(tooltipItems, data) {
          return '';
        },
        label: function(tooltipItem, data) {
          var datasetLabel = '';
          var label = data.labels[tooltipItem.index];
          return data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index];
        }
      }

see updated jsfiddle

这篇关于删除标签并仅在条形图的工具提示中显示值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆