【问题标题】:Chart.js two y axes line chart tooltip value incorrect for 2nd y axisChart.js 两个 y 轴折线图工具提示值对于第二个 y 轴不正确
【发布时间】:2020-01-02 18:37:12
【问题描述】:

我用 chart.js 创建了一个工作图表,我想解决一个我注意到的小问题。

看看at this pen.或下面的副本。

// Global settings for all charts
// set default to not fill any lines
Chart.defaults.global.elements.line.fill = false;

// lines chart with 4 lines and time on x axis
var ChartData = {
  labels: ['08:00', '09:00', '10:00', '11:00', '12:00', '13:00', '14:00'],
  datasets: [{
    type: 'line',
    label: 'A',
    yAxisID: "y-axis-1",
    backgroundColor: "rgba(110,250,150,0.3)",
    borderColor: "rgba(110,250,150,0.5)",
    data: [2500, 2600, 4100, 3400, 2150, 3560, 4500]
  }, {
    type: 'line',
    label: 'B',
    yAxisID: "y-axis-1",
    backgroundColor: "rgba(50,255,90,0.3)",
    borderColor: "rgba(50,255,90,0.5)",
    data: [420, 510, 900, 700, 920, 950, 820]
  }, {
    type: 'line',
    label: 'Wait',
    yAxisID: "y-axis-2",
    backgroundColor: "rgba(255,000,000,0.3)",
    borderColor: "rgba(255,000,000,0.8)",
    data: ['00:28', '00:45', '00:15', '00:12', '00:22', '00:55', '00:10']
  }, {
    type: 'line',
    label: 'D',
    yAxisID: "y-axis-1",
    backgroundColor: "rgba(120,180,255,0.3)",
    borderColor: "rgba(120,180,255,0.5)",
    data: [4200, 1100, 1300, 1850, 1780, 2440, 3800]
  }]
};


var ctx = document.getElementById("Chart");
// declare a chart
var chart = new Chart(ctx, {
  type: 'line',
  data: ChartData,
  options: {
    title: {
      display: true,
      text: "Chart.js line Two Y Axis"
    },
    tooltips: {
      mode: 'label'
    },
    responsive: true,
    scales: {
      xAxes: [{
        type: 'time',
          time: {
            parser: 'hh:mm:ss',
            unit: 'seconds',
            unitStepSize: 3600,
            min: '08:00:00',
            max: '14:00:00',
            displayFormats: {
              'seconds': 'HH:mm'
            }
          },
        scaleLabel: {
          display: true,
          labelString: 'Time'
        },
          // makes time durations on x axis stay in linier time scale.
          distribution: 'linear',
      }],
      yAxes: [{
        id: "y-axis-1",
        position: "left",
        scaleLabel: {
          display: true,
          labelString: 'Other Title Here'
        }
      }, {
        id: "y-axis-2",
        position: "right",
          ticks: {
          max: '20:00',
          min: '00:00'
        },
        type: 'time',
          time: {
            parser: 'mm:ss',
            unit: 'seconds',
            unitStepSize: 3600,
            min: '08:00:00',
            max: '14:00:00',
            displayFormats: {
              'seconds': 'MM.ss'
            }
          },
        scaleLabel: {
          display: true,
          labelString: 'Wait (mm:ss)',
          fontColor: 'rgba(255,0,0,0.8)'
        },
        type: 'time',
          time: {
            parser: 'mm:ss',
            unit: 'seconds',
            unitStepSize: 60,
            min: '00:00',
            max: '20:00',
            displayFormats: {
              'seconds': 'mm.ss'
            }
          },
      }]
    }
  }
});

如果将指针移到红线数据上的点上,您可以看到工具提示显示所有其他线的正确值,但不是显示红线的正确值,而是显示时间 I在 x 轴上有。红色值的实际图表线在正确的位置,只有工具提示中的值是错误的。在这个例子中,红线是唯一使用右边第二个 y 轴的线,所以我认为它与此有关。

我确实看过this question here,但我的坐标轴已经设置为时间类型,假设我正确地遵循了那个答案。

我该如何纠正这个问题?

注意:x 轴时间是一天中从早上 8 点到下午 2 点的时间。右边第二个 y 轴上的时间不是一天中的时间,而是以分钟为单位的时间长度。

【问题讨论】:

    标签: javascript chart.js


    【解决方案1】:

    您必须指定每个 Wait 数据的 X 轴
    像这样将它粘贴到您的代码中:

    data: [
        { x: "08:00", y: "00:28" },
        { x: "09:00", y: "00:45" },
        { x: "10:00", y: "00:15" },
        { x: "11:00", y: "00:12" },
        { x: "12:00", y: "00:22" },
        { x: "13:00", y: "00:55" },
        { x: "14:00", y: "00:10" }
    ]
    

    【讨论】:

    • 谢谢。为了解释我从 Soul Eaters 的回答中了解到的,当你在 chart.js 中有第二个 y 轴时,它看起来像一个单一的图表,但它基本上表现得就像两个不同的图表重叠在一起。如果您有相同数量的数据点,我在上面的问题中所做的方式仍然会对齐,但是 chart.js 不确定第二个 y 轴中的哪些值与哪些 x 值对齐,直到您指定喜欢在 Soul 的回答中,这就是它无法在工具提示中显示值的原因。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-20
    • 2022-11-25
    • 1970-01-01
    • 1970-01-01
    • 2019-04-05
    • 2018-06-18
    • 1970-01-01
    相关资源
    最近更新 更多