Highcharts数据标签格式


Highcharts dataLabels format

我有一个创建折线图的函数。我给它一个布尔参数来告诉它是否需要小数。

if($decimal)
    $format = "{point.y:.2f}";
else
    $format = "{y}";

和在创建图表时:

    plotOptions: {
            line: {
                dataLabels: {
                    enabled: true, 
                    format: ".$format."
            }
     }

但它不起作用。如果不是".$format.",我输入例如:'{point.y:.2f}',它工作

我需要指出的是,". ."不是问题,因为我把它用于其他选项。

请问我错在哪里?

我猜您需要在包含变量之前将值用引号括起来。

这:

plotOptions: {
            line: {
                dataLabels: {
                    enabled: true, 
                    format: ".$format."
            }
     }

变成这样:

plotOptions: {
            line: {
                dataLabels: {
                    enabled: true, 
                    format: '".$format."'
            }
     }

你应该得到一些javascript错误虽然…是吗?

看看如何在php http://docs.highcharts.com/#preprocessing-data-from-a-database中使用它,当你使用双引号时,你不需要使用点,所以它应该是"$format"。此外,如果您尝试粘贴"{point.y:。如果2f}"作为字符串,它将不起作用,因为它不是JSON。我建议使用formatter来定义带前缀的返回函数。

http://api.highcharts.com/highcharts tooltip.formatter