如何在 Chart.js 中重新格式化工具提示?

新手上路,请多包涵

如何在 Chart.js 中重新格式化工具提示?该图表的 x 轴为时间,y 轴为销售额,工具提示显示 x 和 y 的数据值。到目前为止,工具提示可以默认工作,但我想更改我们在工具提示中看到的值。我可以通过重新定义“时间”中的 tooltipFormat 字段来重新格式化工具提示中的时间。但我没有找到类似的 y 轴数据。例如,显示“$1600”而不是“Daily Ticket Sales:1600”。

示例工具提示格式图像

谁能告诉我应该在哪里进行更改?

“自定义”回调函数可以解决这里的问题吗?这是代码,谢谢!

     var dates=data.linechart.dates;
    var times=[];
    for (var i=0; i<dates.length; i++) {
        times.push(moment(dates[i],'YYYY/MM/DD'));
    }
    // console.log(dates);
    // console.log(times);

    var salesData = {
    labels: times,

    datasets: [
        {
            label: "Daily Ticket Sales",
            fill: false,
            lineTension: 0,
            backgroundColor: "#fff",
            borderColor: "rgba(255,88,20,0.4)",
            borderCapStyle: 'butt',
            borderDash: [],
            borderDashOffset: 0.0,
            borderJoinStyle: 'miter',
            pointBorderColor: "rgba(255,88,20,0.4)",
            pointBackgroundColor: "#fff",
            pointBorderWidth: 1,
            pointHoverRadius: 5,
            pointHoverBackgroundColor: "rgba(255,88,20,0.4)",
            pointHoverBorderColor: "rgba(220,220,220,1)",
            pointHoverBorderWidth: 2,
            pointRadius: 3,
            pointHitRadius: 10,
            data: data.linechart.sales,
        }
        ]
    };

    var ctx = document.getElementById("daily_sale").getContext("2d");
    var myLineChart = new Chart(ctx, {
        type: 'line',
        data: salesData,
        options: {

            showLines: true,
            responsive: true,
            legend:{display:false},
            tooltips:{
                // backgroundColor:'rgba(0,255,0,0.8)',
                custom: function(tooltip) {

                    // tooltip will be false if tooltip is not visible or should be hidden
                    if (!tooltip) {
                        return;
                    }
                    else{
                    console.log(tooltip);
                    }
                }
            },
            scales:
            {
                xAxes: [{
                    type: "time",
                    time: {
                        displayFormat:'MM/DD/YY',
                        tooltipFormat: 'MM/DD/YY',
                    //     unit: 'day',
                    }
                }],
                yAxes: [{
                    ticks:{ userCallback: function(value, index, values) {
                                                // $ sign and thousand seperators
                                                return  '$'+value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
                                            },
                    },
                }],
            },
        }
    });

原文由 Peng Cao 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 474
2 个回答

您可以在回调函数中自定义标签。

 tooltips: {
          callbacks: {
                        label: function(tooltipItem, data) {
                            return "Daily Ticket Sales: $ " + tooltipItem.yLabel;
                        },
                    }
            }

原文由 LeonF 发布,翻译遵循 CC BY-SA 3.0 许可协议

scales: {
    xAxes: [{
      type: 'time',
      time: {
          tooltipFormat:'MM/DD/YYYY', // <- HERE
          displayFormats: {
             'millisecond':'HH:mm:ss',
             'second': 'HH:mm:ss',
             'minute': 'HH:mm:ss',
             'hour': 'HH:mm:ss',
             'day': 'HH:mm:ss',
             'week': 'HH:mm:ss',
             'month': 'HH:mm:ss',
             'quarter': 'HH:mm:ss',
             'year': 'HH:mm:ss',
          }
        }
    }]
}

原文由 user2531429 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题