chartJs:当我将鼠标悬停在一个点上时,我想显示两个数据集图例吗?

时间:2018-10-12 11:20:30

标签: d3.js chart.js

current behaviour

我是新手。另外,我从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
                }
            }]
        }
      }

});

1 个答案:

答案 0 :(得分:4)

您可以设置悬停相交属性来实现此目的。这是为您准备的小提琴-> 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
                }
            }]
        }
      }
});