如何在图表J中绘制水平线,当悬停在显示数据时

发布于 2025-02-13 06:09:58 字数 1173 浏览 0 评论 0原文

ngOnInit(): void {

var myChart = new Chart('myChart', {
  type: 'bar',
  data: {
    labels: ['Recordings'],
    datasets: [
      {
        label: 'A',
        data: [this.data.a],
        borderColor: 'rgba(255,105,180,1)',
        backgroundColor: 'rgba(255,105,180,0.2)',
        barPercentage:0.4,
        borderWidth:2,
        order: 1
      },
      {
        label: 'B',
        data: [this.data.b],
        borderColor: 'rgba(75, 192, 192, 1)',
        backgroundColor: 'rgba(75, 192, 192, 0.2)',
        barPercentage:0.4,
        borderWidth:2,
        order: 2
      },
      {
        label: 'Total Recordings',
        data:[this.data.totalrecordings],
        type:'line',
        borderColor:'rgba(2,117,216,1)',
        backgroundColor:'rgba(2,117,216,0.2)',
        order:0
      }
    ]
  },
  options: {
    responsive: true,
    plugins: {
      legend: {
        position: 'top',
      }
    }
  },
})
}

我希望总记录线图应该是一条水平直线,并且每当我悬停在线上时,都应显示总记录值。目前,图形正在获取图中所示的绘图。

ngOnInit(): void {

var myChart = new Chart('myChart', {
  type: 'bar',
  data: {
    labels: ['Recordings'],
    datasets: [
      {
        label: 'A',
        data: [this.data.a],
        borderColor: 'rgba(255,105,180,1)',
        backgroundColor: 'rgba(255,105,180,0.2)',
        barPercentage:0.4,
        borderWidth:2,
        order: 1
      },
      {
        label: 'B',
        data: [this.data.b],
        borderColor: 'rgba(75, 192, 192, 1)',
        backgroundColor: 'rgba(75, 192, 192, 0.2)',
        barPercentage:0.4,
        borderWidth:2,
        order: 2
      },
      {
        label: 'Total Recordings',
        data:[this.data.totalrecordings],
        type:'line',
        borderColor:'rgba(2,117,216,1)',
        backgroundColor:'rgba(2,117,216,0.2)',
        order:0
      }
    ]
  },
  options: {
    responsive: true,
    plugins: {
      legend: {
        position: 'top',
      }
    }
  },
})
}

I want that the total recordings line graph should be a horizontal straight line and whenever I hover over the line it should show total recordings value. Right now graph is getting plot as shown in image.
current chart img

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明天过后 2025-02-20 06:09:58

我终于找到了出路。答案是

{
  label: 'Total Recordings',
  data: [[data.totalrecordings -0.5, data.totalrecordings + 0.5]]
  categoryPercentage: 1,
  barPercentage: 1,
  borderColor:'rgba(2,117,216,1)',
  backgroundColor:'rgba(2,117,216,0.2)',
  order:0
}

进一步的浮栏,以正确地显示悬停时的总记录数据,请使用此信息

tooltip: {
   mode: 'index',
   callbacks: {
        label: ctx => ctx.dataset.label + ': ' + (ctx.datasetIndex == 2 ? 
                     data.totalrecordings : ctx.parsed.y)
   }
}

I finally found the way out. The answer to this is a floating bar

{
  label: 'Total Recordings',
  data: [[data.totalrecordings -0.5, data.totalrecordings + 0.5]]
  categoryPercentage: 1,
  barPercentage: 1,
  borderColor:'rgba(2,117,216,1)',
  backgroundColor:'rgba(2,117,216,0.2)',
  order:0
}

Further to correctly show the total recordings data when you hover, use this

tooltip: {
   mode: 'index',
   callbacks: {
        label: ctx => ctx.dataset.label + ': ' + (ctx.datasetIndex == 2 ? 
                     data.totalrecordings : ctx.parsed.y)
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文