简体   繁体   English

图表js。 如何通过工具提示的中心对齐文本?

[英]Chart js. How to align text by the center of the tooltip?

bodyAlign and titleAlign don't work, maybe somebody knows how to align text by the center of tooltip? bodyAlign 和 titleAlign 不起作用,也许有人知道如何通过工具提示的中心对齐文本?

在此处输入图片说明

https://jsfiddle.net/Dima1501/m7s43hrs/2900/ https://jsfiddle.net/Dima1501/m7s43hrs/2900/

var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
  type: 'doughnut',
  data: {
    labels: ["Red", "Blue", "Yellow"],
    datasets: [{
      label: '# of Votes',
      data: [12, 19, 3],
      backgroundColor: [
      ],
      borderColor: [
      ],
      borderWidth: 1
    }]
  },
 options: {
    tooltips: {
    yAlign: 'bottom',
            xAlign: 'center',
    xPadding: 25,
            yPadding: 15,
      xPadding: 45,
      titleAlign: 'center',
            footerAlign: 'center',
            bodyAlign: 'center',
  callbacks: {
  },
  backgroundColor: '#ccc',
  titleFontSize: 16,
  titleFontColor: '#0066ff',
  bodyFontColor: '#000',
  bodyFontSize: 14,
  displayColors: false
   }
  }
});

You are using 2.5.0, which has bug.您使用的是 2.5.0,它有错误。 Update to latest 2.8.0, its working fine already.更新到最新的 2.8.0,它已经可以正常工作了。 https://jsfiddle.net/zhuhang95/utc9wenx/4/ https://jsfiddle.net/zhuhang95/utc9wenx/4/

Usually, using titleAlign and bodyAlign should do the trick.通常,使用titleAlignbodyAlign应该可以解决问题。

Chart.js docs: https://www.chartjs.org/docs/latest/configuration/tooltip.html#alignment Chart.js 文档: https ://www.chartjs.org/docs/latest/configuration/tooltip.html#alignment

        options: {
            tooltips: {
                titleAlign: 'center',
                bodyAlign: 'center'
            }
        }

Your align properties were messing things up.你的align properties事情搞砸了。 Give this a try:试试这个:

https://jsfiddle.net/m7s43hrs/2951/ https://jsfiddle.net/m7s43hrs/2951/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM