首页 > 解决方案 > chartJs:当我将鼠标悬停在其中一个点时,我想显示两个数据集图例?

问题描述

当前行为

我是新手。另外,我从 1 月到 12 月开始使用硬代码标签。我如何将标签从一月到现在。另一个问题是,当我将鼠标悬停到某个点时,您可以在图片中看到。仅显示该点工具提示我想查看位于同一条线上的所有点工具提示

代码

var ctx = document.getElementById("myChart");
  ctx.height = 50;
  var myChart = new Chart(ctx, {
      type: 'line',
      data: {
          labels: monthNames,
          datasets: [{
              label: "Sales",
              data: [2000, 1200, 3000, 5000, 1000, 1300],
              pointHoverBackgroundColor:  'rgb(255, 99, 132)',  
              backgroundColor: [
                  'rgba(255, 99, 132, 0.2)',
                  'rgba(54, 162, 235, 0.2)',
                  'rgba(255, 206, 86, 0.2)',
                  'rgba(75, 192, 192, 0.2)',
                  'rgba(153, 102, 255, 0.2)',
                  'rgba(255, 159, 64, 0.2)'
              ],
              borderColor: [
                  'rgba(255,99,132,1)',
                  'rgba(54, 162, 235, 1)',
                  'rgba(255, 206, 86, 1)',
                  'rgba(75, 192, 192, 1)',
                  'rgba(153, 102, 255, 1)',
                  'rgba(255, 159, 64, 1)'
              ],
              borderWidth: 1
          },
          {
              label: "gross sales",
              data: [1500, 2200, 3000, 2800, 1122, 2345],
              pointHoverBackgroundColor:  'rgb(66, 133, 244)',   
              backgroundColor: [
                  'rgb(66, 133, 244, 0.2)',                 
              ],
              borderColor: [
                  'rgb(66, 133, 244)',                  
              ],
              borderWidth: 1
          }]
      },
      options: {
        title:{
          display: "true",
          text: "Sales Reports",
          fontSize: "22"
        },
        tooltips:{
          intersect: false
        },
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero:true
                }
            }]
        }
      }

});

标签: d3.jschart.js

解决方案


您可以设置悬停相交属性来实现此目的。这是给你的小提琴-> http://jsfiddle.net/6n3w0qkh/5/

希望能帮助到你!

var ctx = document.getElementById("myChart");  
  var myChart = new Chart(ctx, {
      type: 'line',
      data: {
          labels: ['A', 'B', 'C', 'D', 'E', 'F'],
          datasets: [{
              label: "Sales",
              data: [2000, 1200, 3000, 5000, 1000, 1300],
              pointHoverBackgroundColor:  'rgb(255, 99, 132)',  
              backgroundColor: [
                  'rgba(255, 99, 132, 0.2)',
                  'rgba(54, 162, 235, 0.2)',
                  'rgba(255, 206, 86, 0.2)',
                  'rgba(75, 192, 192, 0.2)',
                  'rgba(153, 102, 255, 0.2)',
                  'rgba(255, 159, 64, 0.2)'
              ],
              borderColor: [
                  'rgba(255,99,132,1)',
                  'rgba(54, 162, 235, 1)',
                  'rgba(255, 206, 86, 1)',
                  'rgba(75, 192, 192, 1)',
                  'rgba(153, 102, 255, 1)',
                  'rgba(255, 159, 64, 1)'
              ],
              borderWidth: 1
          },
          {
              label: "gross sales",
              data: [1500, 2200, 3000, 2800, 1122, 2345],
              pointHoverBackgroundColor:  'rgb(66, 133, 244)',   
              backgroundColor: [
                  'rgb(66, 133, 244, 0.2)',                 
              ],
              borderColor: [
                  'rgb(66, 133, 244)',                  
              ],
              borderWidth: 1
          }]
      },
      options: {
        title:{
          display: "true",
          text: "Sales Reports",
          fontSize: "22"
        },
        tooltips: {
            mode: 'index',
            intersect: false
        },
        hover: {
            mode: 'nearest',
            intersect: true
        },
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero:true
                }
            }]
        }
      }
});

推荐阅读