Amcharts - 如何在自定义工具提示中格式化 valueField?

Amcharts - how to format valueField inside custom tooltip?

我有一个基本的 AmGraph:

    graph = new AmCharts.AmGraph();

    graph.lineThickness = 4;
    graph.valueAxis = valueAxis;
    graph.fillAlphas = 0;
    graph.valueAxis = valueAxis;
    graph.valueAxis.minimum = 0;

    graph.title = "likes";
    graph.labelText = " ";
    graph.valueField = "likes_1";

问题是,如果我使用自定义气球文本函数,numberFormatter 不会格式化值:

    graph.numberFormatter = {precision: -1, decimalSeparator:",", thousandsSeparator:","};

所以如果我使用这个:

    graph.balloonText = "<b>got: [[likes_1]] </b>";

工具提示如下所示:

   "got: 1000"

如何设置值的格式?如果我尝试使用 javascript 对其进行格式化 (Numeral.js):

    graph.balloonText = "<script>numeral( [[likes_1]] ).format("0,0") </script>";

在构建页面时通配符没有替换为实际值(而是在悬停图表时?),所以我只得到:

    Uncaught ReferenceError: likes_32 is not defined

我该怎么办?

(First I highly recommend to use the newer way of chart initialization! Look for that.)

您可以使用 balloonFunction 而不是 balloonText,这样您就可以使用 JavaScript 更改文本。在那里你可以根据你的意愿格式化数字。
我准备了一个 demo using the AmCharts.formatNumber 格式器。

return AmCharts.formatNumber(graphDataItem.values.value, {
        precision: -1,
        decimalSeparator: '.',
        thousandsSeparator: ','
    }, -1);

你仍然可以在那里使用 Numeral.js(它的语法对我来说似乎更直接)。

return numeral(graphDataItem.values.value).format("0,0");