Search code examples
actionscript-3apache-flexlegendlinechartlineseries

Disable a particular legend in a line chart


Is there any possibility to disable a particular legend for LineSeries component in a line chart.

Let say that we have the following code:

<mx:Panel title="Line Chart">
 <mx:LineChart id="myChart" 
    dataProvider="{expenses}" 
    showDataTips="true"
 >
    <mx:horizontalAxis>
       <mx:CategoryAxis 
            dataProvider="{expenses}" 
            categoryField="Month"
        />
    </mx:horizontalAxis>
    <mx:series>
       <mx:LineSeries 
            yField="Profit" 
            displayName="Profit"
       />
       <mx:LineSeries 
            yField="Expenses" 
            displayName="Expenses"
       />
    </mx:series>
 </mx:LineChart>
 <mx:Legend id="legend" dataProvider="{myChart}"/>

It will produce the following line chart:

enter image description here

And this the result that i want:

enter image description here

UPDATE:

Bare in mind that I have to use the legend's DataProvider as myChart because the data is dynamically build. Also, the legend is customized.


Solution

  • Got the solution, as i have custom legend I have to set the data provider legend after updating the line chart:

     // Add listener event to the linechart component for when the legend update completes so it can filter lineseries on the legend's dataprovider in [onUpdateLegendComplete]
        myChart.addEventListener(FlexEvent.UPDATE_COMPLETE, onUpdateLinechartComplete);
    

    And the function is:

    protected function onUpdateLinechartComplete(e:FlexEvent):void 
    {
        legend.dataProvider = myChart.legendData[0];
    }