first question here.
I have a BarChart showing several normal bars and a BarSeries, like this:
<mx:BarChart id="barchart" dataProvider="{model.myList}" type="clustered">
<mx:horizontalAxis>
<mx:LinearAxis autoAdjust="true"/>
</mx:horizontalAxis>
<mx:verticalAxis>
<mx:CategoryAxis categoryField="name"/>
</mx:verticalAxis>
<mx:series>
<mx:BarSeries dataProvider="{model.myList}" xField="myValue"/>
</mx:series>
</mx:BarChart>
When a user clicks on a button, i need to calculate some values, put them on the “myCalculatedValue” and add another BarSeries as a comparison. I’m doing this:
var barSerie:BarSeries = new BarSeries();
barSerie.dataProvider = model.myList;
barSerie.xField = "myCalculatedValue";
barchart.series.push(barSerie);
But the BarChart does not change at all. Is there some way to refresh the chart after adding the new BarSeries?
The short answer is to use this code:
The LONG answer, to give you an understanding of how this works:
The way flex knows when to refresh a ui component that is data backed, such as a chart, is on the
function set dataProviderproperty, or equivalent (in this caseseries).Here is the code from
mx.charts.chartClasses.ChartBase:Notice that the relevant “invalidate” methods are called at the end of the set method.
If you say
barChart.series.push(x), theset seriesmethod is never called, only thegetter. Therefore in order to force the chart to know there was a change to the series, you need to assign a new value to the series.It is worth pointing out that even assigning the series to itself will cause an invalidate, although it isn’t good coding
barChart.series = barChart.series