【问题标题】:Is there any way to show a tooltip for points which are not visible on the chart in Chart.js?有没有办法为 Chart.js 图表上不可见的点显示工具提示?
【发布时间】:2022-07-08 05:27:00
【问题描述】:

我有一个折线图,它的 Y 轴有最小值。有时数据低于这个最小值,我不想因为这一点而拉伸图表,但是当我将光标移动到 X 轴上的值时,我希望有工具提示来显示数据。

我已经创建了一个关于这个问题的代码笔:codepen

var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
  type: "line",
  data: {
    labels: ["R", "B", "Y", "G", "P", "O"],
    datasets: [
      {
         label: "# of Votes",
         data: [12, 13, 11, 12, 9, 13]
      }
    ]
  },
  options: {
    scales: {
      y: {
        min: 10
      },
    },
    interaction: {
      intersect: false,
      mode: 'nearest',
      axis: 'x',
    },
  }
});

【问题讨论】:

    标签: javascript chart.js linechart


    【解决方案1】:

    您可以按照here 的说明定义interaction.mode: 'x'

    请在下面查看您修改后的代码,看看它是如何工作的。

    new Chart('myChart', {
      type: "line",
      data: {
        labels: ["R", "B", "Y", "G", "P", "O"],
        datasets: [{
          label: "# of Votes",
          data: [12, 13, 11, 12, 9, 13]
        }]
      },
      options: {
        scales: {
          y: {
            min: 10
          }      
        },
        interaction: {
          intersect: false,
          mode: 'x'
        }
      }
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.7.0/chart.min.js"></script>
    <canvas id="myChart" height="80"></canvas>

    【讨论】:

    • 谢谢!但是只有当指针真正位于 X 轴上的数据点上时,此解决方案才会显示工具提示。我想保持“最近”的互动价值
    猜你喜欢
    • 1970-01-01
    • 2011-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多