导出 amCharts 时格式化货币

Formatting currency when exporting amCharts

我正在使用 amCharts 3.20.9,我成功地绘制了一个图形并且可以将数据导出到一个 XLSX 文件中。但是,我要导出的列之一是货币,有没有办法在生成的文件中设置这种格式?

我的图表脚本是:

var chart = AmCharts.makeChart("graph", {
        "type" : "serial",
        "theme" : "light",
        "dataProvider" : data,
        "valueAxes" : [ {
            "stackType": "regular",
            "gridColor" : "#FFFFFF",
            "gridAlpha" : 0.2,
            "dashLength" : 0,
            "title" : "Metros cúbicos"
        } ],
        "gridAboveGraphs" : true,
        "startDuration" : 1,
        "graphs" : graphs,
        "chartCursor" : {
            "categoryBalloonEnabled" : false,
            "cursorAlpha" : 0,
            "zoomable" : false
        },
        "categoryField" : "formatedTime",
        "categoryAxis" : {
            "gridPosition" : "start",
            "gridAlpha" : 0,
            "tickPosition" : "start",
            "tickLength" : 20,
            "parseDates" : false,
            "labelsEnabled": true,
            "labelFrequency": 3
        },
        "export" : {
            "enabled" : true,
            "fileName" : "Reporte",
            "exportTitles" : true,
            "exportFields" : fields,
            "columnNames" : columnNames,
            "menu" : [ {
                "class" : "export-main",
                "menu" : [ "PDF", "XLSX" ]
            } ]
        }
    });

其中:

graphs 包含图形定义,例如:

[{
                    "balloonText" : "[[formatedTime]]: <b>[[" + sites[i] + "]]</b>",
                    "balloonFunction" : formater,
                    "lineThickness": 1,
                    "lineAlpha" : 0.2,
                    "type" : "line",
                    "valueField" : sites[i]
            }];

字段:["formatedTime"、"Viva Villavicencio"、"Viva Villavicencio_COST_"]

列名:{"formatedTime":"Fecha","Viva Villavicencio":"Metros cúbicos para: Viva Villavicencio","Viva Villavicencio_COST_":"Costo para: Viva Villavicencio"}

到目前为止一切顺利,我的 xlsx 中包含正确的数据,但最后我希望将列 "Viva Villavicencio_COST_" 定义为结果文件中的货币,因此以这种方式进行格式化和显示。

任何帮助将不胜感激。

查看 processData 选项。它需要一个回调函数,让您可以在数据集写入导出文件之前对其进行更改。

因此,添加到您的代码中:

"export": {
  "processData": function(data){
    for(var i = 0; i < data.length; i++){
      data[i].Viva Villavicencio_COST_ = toCurrency(data[i].Viva Villavicencio_COST_);
    }
    return data;
  }
  ...
}

此函数 returns 与以前一样的确切数据集,但具有格式化的 Viva Villavicencio_COST_ 字段。

然后,添加函数toCurrency。我不相信 amCharts 有内置的格式化功能。如果您需要更好的格式化功能,您可以使用 numeral.jsaccounting.js 之类的功能,但现在请尝试:

function toCurrency(value){
  return '$' + value;
}

导出插件的完整文档在此处:https://github.com/amcharts/export

希望对您有所帮助。