how to change the color of an mx:PieSeriesItem slice dynamically
I am creating a program where a change in a value causes the color of a pie slice in an mx:PieChart to change dynamically.
the code that is supposed to change the color is similar to this:
(pieChart.pieChartSeries.items[i] as PieSeriesItem).fill = new mx.graphics.SolidColor(0,0.2); // black, almost opaque
tracing out the results of this appears to apply the color to the particular PieSeriesItem, but the color does not change visually. I checked the AS reference and the fill property i开发者_运维技巧s of type iFill and should accept a solidColor instance. (note: I have also tried 0x000000 as a valid color, the first argument of SolidColor takes an uint)
Any thoughts on why the color would not be applied? comments appreciated.
[edit] Since I posted the question originally, I've added a few changes to help clarify the question.
- I am actually using an MXML component that extends < mx:PieChart > . Originally I was trying to set the color as listed above within an event handler in the main application. What I have done since then was invoke a method in my extended PieChart component, like so:
(in main applicaion)
private function someEventHandler(evt:someEvent):void{
pieChart.setPieSliceColor((pieChart.pieChartSeries.items[i] as PieSeriesItem));
}
In the custom pie Component, I have the following method defined:
public setPieSliceColor(item:PieSeiesItem):void{ item.fill = new mx.graphics.SolidColor(0x0000ff,1); this.invalidateSeriesStyles();
}
now the change in color actually occurs, but is not seen until the user moves the mouse over the respective pie slice! It's strange.
Any additional comments are appreciated.
thanks again
Edward
This seems correct, but I guess you'll need to redraw the control after you set this property. Have you tried calling the invalidateDisplayList and/or the invalidateProperties methods on the chart?
You could also use a fill function on the series to do this.
I got it...
something like this will work.
public function setPieSliceColor(item:PieSeriesItem):void{
item.fill = new mx.graphics.SolidColor(0x0000ff,1);
this.setFocus();
this.invalidateSeriesStyles();
// note this line
item.currentState = "rollOver";
}
It appears that although I was changing the color, the color would not effect until I actually mouseOver the slice...So setting the slice's (or item) currentState appears to do the trick.
精彩评论