Search code examples
javascripthtmllegendamcharts

amcharts adding legend with the dynamic text


I am using amcharts for rendering the data in column charts, I need the legand to be shown along with the text of the each 'valueField'.

I am trying something like this :

"legend": {"horizontalGap": 10,
    "maxColumns": 1,
    "position": "right",
    "useGraphSettings": true,
    "markerSize": 10,
    "marginTop": 10,
    "labelText":"[[value]]"
}

My JSFiddle

It's showing the different colors well, but the text is not getting displayed. Any suggestions would be highly appreciable!


Solution

  • You need to set the title of the graphs so that the legend knows what to use to replace the labelText of each legend, which defaults to [[title]]:

    https://docs.amcharts.com/3/javascriptcharts/AmLegend#labelText

    It looks like the labelText will not parse any other special placeholders like [[value]] other than [[title]].

    So the fix is, just to remove what you have set as the label text in the legend section (just leave it as default), and add titles on each graph:

    legend: {
        ...,
        // "labelText": "[[value]]"
    },
    ...,
    graphs: [
        {
            id: "g1",
            fillAlphas: .9,
            title: "value 1",
            ...
        },
        {
            id: "g2",
            fillAlphas: .9,
            title: "OR WHATEVER YOU WANT TO DISPLAY",
            ...
        },
        ...
    ]
    

    fiddle: https://jsfiddle.net/davidliang2008/fm7jLkta/