首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ChartJS,禁用多图表数据上的一个绘图的工具提示

ChartJS,禁用多图表数据上的一个绘图的工具提示
EN

Stack Overflow用户
提问于 2020-04-01 14:41:56
回答 1查看 292关注 0票数 0

如何在我的多重绘图上禁用其中一个绘图上的工具提示,而保留其他绘图。我曾尝试在我不想要工具提示的绘图上将'showToolTip‘设置为false,但这不起作用。

代码语言:javascript
复制
let ctx = document.getElementById('myChart').getContext('2d');
                var chart = new Chart(ctx, {
                    type: 'scatter',
                    data :{
                        labels : cLabels,
                        datasets : [{
                            label: 'Points',
                            backgroundColor: 'rgb(255, 99, 132)',
                            borderColor: 'rgb(255, 99, 132)',
                            pointStyle: 'circle',
                            pointRadius: 10,
                            data : cData,
                            showTooltips: false, //<- THis line does not work, and there is a global property to remove all tooltips
                            order: 2,
                        },{
                            label: '',
                            backgroundColor: 'rgb(255, 255, 255)',
                            borderColor: 'rgb(255, 255, 255)',
                            pointStyle: 'circle',
                            pointRadius: 5,
                            data : cData,

                            order: 1,
                        },{
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-02 01:24:52

您可以使用tooltip filter callback函数。如果应该显示工具提示,则返回true,否则返回false。下面的示例指定应仅为第一个数据集显示工具提示。

代码语言:javascript
复制
options: {
  ...
  tooltips: {
    filter: tooltipItem => tooltipItem.datasetIndex == 0
  }

请看下面的runnable代码示例。

代码语言:javascript
复制
const data = [
  { 
    series: [ 
      {date: '2020-01', value: 5 },
      {date: '2020-02', value: 20 },
      {date: '2020-03', value: 10 },
      {date: '2020-04', value: 15 }
    ]
  },
  {
    series: [ 
      {date: '2020-01', value: 10 },
      {date: '2020-02', value: 8 },
      {date: '2020-03', value: 16 },
      {date: '2020-04', value: 12 }
    ]
  }
];
new Chart(document.getElementById('canvas'), {
  type: 'line',
  data: {
    datasets: [{
      label: data[0].name,
      fill: false,
      backgroundColor: 'red',
      borderColor: 'red',
      data: data[0].series.map(x => ({ x: new Date(x.date), y: x.value }))
    }, {
      label: data[1].name,
      fill: false,
      backgroundColor: 'blue',
      borderColor: 'blue',
      data: data[1].series.map(x => ({ x: new Date(x.date), y: x.value }))
    }]
  },
  options: {
    responsive: true,
    title: {
      display: false
    },
    legend: {
      display: false
    },
    tooltips: {
      filter: tooltipItem => tooltipItem.datasetIndex == 0
    },
    scales: {      
      xAxes: [{
        type: 'time',
        time: {
          unit: 'month',
          displayFormats: {
            'month': 'MMM YYYY',
          },
          tooltipFormat: 'MMM YYYY'
        }
      }],
    }
  }
});
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.bundle.min.js"></script>
<canvas id="canvas" height="90"></canvas>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60964989

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档