Chart.js重叠的工具提示文字

时间:2019-03-18 20:22:02

标签: javascript chart.js

我有一个chart.js折线图:

var myLineChart = new Chart(ctx, {
                type: 'line',
                data: lineChartData,
                options: {
                    tooltips: {
                        titleSpacing: 5,
                    },
                    responsive: true,
                    legend: {
                        display: false,
                    },
                    scales: {
                        yAxes: \[
                            {
                                gridLines: {
                                    color: '#354657',
                                },
                                ticks: {
                                    fontColor: '#fff',
                                    stepSize: 5,
                                },
                            },
                        \],
                        xAxes: \[
                            {
                                gridLines: {
                                    color: '#354657',
                                },
                                ticks: {
                                    fontColor: '#fff',
                                },
                            },
                        \],
                    },
                },]

问题是,它使用重叠的文本呈现工具提示,如下所示: 1]

这应该显示当前为41.9。但是它们是重叠的。我尝试更改titlespacing,但是没有做任何事情。我该如何解决?

1 个答案:

答案 0 :(得分:0)

我个人为所有自定义图表执行的操作是创建一个自定义标题/标签,与我作为图表选项传递的内容无关。我喜欢控制HTML /输出到标签的内容。您可以使用回调键完成此操作

  tooltips: {
    enabled: true,
    position: "nearest",
    caretSize: 20,
    mode: "index",
    intersect: false,
    titleFontSize: 16,
    titleFontColor: "white",
    backgroundColor: COLORS.DARK,
    callbacks: {
      title: (tooltipItem, _) => {
        return formatDate("LT", tooltipItem[0].xLabel);
      },
      label: (tooltipItem, _) => {
        const { yLabel } = tooltipItem;
        return `${yLabel} Sessions Recorded`;
      }
    }
  },
相关问题