0

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.

kaissun
  • 3,044
  • 4
  • 20
  • 35

3 Answers3

0

Instead using your chart as DataProvider, you could create individual LegendItems

    <mx:Legend>
        <mx:LegendItem label="Profit" fill="#e48701">           
        </mx:LegendItem>
    </mx:Legend>

edit2: try this

    <mx:Legend dataProvider="{new ArrayCollection(myChart.legendData).getItemAt(0)}">
Pete TNT
  • 8,293
  • 4
  • 36
  • 45
  • Thank you for helping, but i just want to use the DataProvider for the legend as the data is dynamically built – kaissun Jul 25 '13 at 09:53
  • 1
    see the edit. Could you be bit more specific about the data: do the legends change? – Pete TNT Jul 25 '13 at 10:18
  • First, the second solution didn't work: the legendItems object is always empty as i dynamically set the data. About the data, i read it from XML file. That's why i don't know exactly the lineseries just i have a property that i could use it do not showing the legend. Is any property could be related to lineseries component itself ? – kaissun Jul 25 '13 at 10:31
  • I don't think there is such thing. See another edit, does that work? Do you need X amount of legends or just the "Profit", no matter how many lineseries there are. – Pete TNT Jul 25 '13 at 10:52
  • Yes It is fine i could show only the first legend just by using callLater function. But when applying this solution the custom legend item is not applied. Do you know the cause? thanks – kaissun Jul 25 '13 at 10:57
  • The problem is exactly here `this.setStyle("legendMarkerRenderer", customLegend);`. The classfactory is not applied or the property legendMarkerRenderer does not exist but before it works fine without amending the legend dataprovider – kaissun Jul 25 '13 at 11:50
0

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];
}
kaissun
  • 3,044
  • 4
  • 20
  • 35
0

Checkout http://flexdevtips.blogspot.com/2009/10/linechart-with-checkbox-legend.html It might give you some ideas.

oluwaseun
  • 81
  • 1
  • 2