简体   繁体   English

如何在堆叠的 Amchart 列顶部显示百分比

[英]How to display percentage on top of column in stacked Amchart

I am using stacked amcharts .我正在使用堆叠 amcharts 。 For example one in Amchart例如在Amchart 中的一个

Now I want to display overall percentage on top of each column.现在我想在每列的顶部显示总体百分比。 How to add percentage如何添加百分比

You can display the percentage instead of the columns' value by setting the graphs' labelText strings to "[[percents]]%" .您可以通过将图表的labelText字符串设置为"[[percents]]%" labelText "[[percents]]%"来显示百分比而不是列的值。 The documentation lists all of the [[shortcodes]] that can be used.该文档列出了所有可以使用的[[shortcodes]]

  "graphs": [{
    // ...
    "labelText": "[[percents]]%",
    // ...
  }, 
  // repeat for each graph object
  ]

Demo 演示

Edit编辑

If you need to display a percentage of category's overall value with respect to the sum of all of your data, you can work around this by providing your category total and sum of all of your columns in your dataProvider , create an invisible line graph that uses the column total and displays the calculated percentage using the labelFunction如果您需要显示类别总价值相对于所有数据总和的百分比,您可以通过在dataProvider提供类别总计和所有列的总和来解决此问题,创建一个不可见的折线图,使用列总计并使用labelFunction显示计算的百分比

Here's an example of what your data would look like:以下是您的数据外观示例:

  "dataProvider": [{
    "year": 2003,
    "europe": 2.5,
    "namerica": 2.5,
    "asia": 2.1,
    "lamerica": 0.3,
    "meast": 0.2,
    "africa": 0.1,
    "columnTotal": 7.7, // total for this column
    "dataTotal": 24.7  // calculated total of the data in all categories
  }, 
  // repeat for each data point
]

And here's the graph setup:这是图形设置:

"graphs": [
    // other graphs omitted
  {
    "showBalloon": false, //ensures that no balloon is displayed
    "visibleInLegend": false, //ensures that this graph is not displayed in the legend
    "lineAlpha": 0, //hides the line
    "labelText": "[[value]]", //required for the labelFunction
    "labelFunction": function(graphDataItem, valueText) { //calculates the percent and displays the label
      return ((graphDataItem.dataContext.columnTotal / graphDataItem.dataContext.dataTotal) * 100).toFixed(2) + "%";
    },
    "valueField": "columnTotal"
  }
]

Demo 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM