AmCharts:如何访问图例中显示的值

时间:2016-12-06 16:34:44

标签: javascript charts amcharts

我试图在使用ChartCursor悬停在图表上时操纵图例中显示的值(我已经为它连接了一个侦听器函数,但似乎无法找到每个对象如何显示每个当前图例中的值:

enter image description here

我尝试使用

访问valueText
chart.legend.valueText

但如果我替换valueText,则图例中的每个对象都会出现值集。我希望能够触及图例中的每个对象,并能够操纵每个对象' valueText。

监听器在chartCursor中实现:

"chartCursor": {
        "categoryBalloonEnabled": false,
        "listeners": [{
          "event": "changed",
          "method": cursorChanged
        }]
      },

有人知道如何做到这一点吗?

感谢。

1 个答案:

答案 0 :(得分:2)

要访问图例值,您必须在图例对象中创建valueFunction

    "legend": {
        // ...
        "valueFunction": function(graphDataItem, valueText) {
            //check if valueText is empty. 
            //this only occurs when you're not currently hovered over a value
            //and if you don't have a periodValueText set.
          if (valueText !== " ") { 
            //access the current graph's value through the graph.valueField 
            //inside the graphDataItem parameter, 
            //or the string version of the value in valueText and manipulate it accordingly
            return graphDataItem.dataContext[graphDataItem.graph.valueField]; //current hovered value
          } else {
           return valueText; 
          }
        }
    },

为每个具有可见图例标记的图形对象调用valueFunction

Demo