`
it_liuyong
  • 浏览: 97785 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

自定义column renderer

    博客分类:
  • flex
 
阅读更多
<?xml version="1.0" encoding="utf-8"?>

<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
viewSourceURL="src/ProgrammaticSkinning/index.html"
>
<mx:VBox
borderSkin="CustomBorder"
backgroundColor="0xCCCC99"   
backgroundAlpha="0.8"
cornerRadius="14"
paddingLeft="20" paddingTop="20"   
paddingRight="20" paddingBottom="20"

>
<mx:Text text="The VBox has a {&apos;\r&apos;}programmatic skin."/>
</mx:VBox>

</mx:Application>


package  itemRender
{
import flash.display.Graphics;
import flash.geom.Rectangle;

import mx.charts.ChartItem;
import mx.charts.chartClasses.GraphicsUtilities;
import mx.charts.series.ColumnSeries;
import mx.charts.series.items.ColumnSeriesItem;
import mx.collections.ArrayCollection;
import mx.controls.Text;
import mx.core.IDataRenderer;
import mx.core.UIComponent;
import mx.graphics.IFill;
import mx.graphics.IStroke;
import mx.graphics.SolidColor;
import mx.styles.StyleManager;
import mx.utils.ColorUtil;



public class MyCol extends UIComponent implements IDataRenderer 

private var _data:Object;
private var l:Text ;

[Inspectable(environment="none")]

/**
*  The chartItem that this itemRenderer is displaying.
*  This value is assigned by the owning series
*/
public function get data():Object
{
return _data;
}

protected function get showOtherLabel():Boolean{
return true;
}

private function getMMvalue(arr:ArrayCollection,yField:String):Array{
var max:Number=Number.NEGATIVE_INFINITY;
var min:Number=Number.POSITIVE_INFINITY;
for each(var obj:Object in arr){
if(!isNaN(Number(obj[yField]))){
max = Math.max(max,Number(obj[yField]));
min = Math.min(min,Number(obj[yField]));
}
}
return [max,min];
}

protected function get colors():Array{
return ["#008654","#d80000","#B600CC"];
}

private function getColor(max:Boolean=false,min:Boolean=false):String{
var c:String= colors[0];
if(max)
c= colors[1];
else if(min)
c= colors[2];
return c;

}
/**
*  @private
*/
public function set data(value:Object):void
{
if (_data == value)
return;

_data = value;
if(value is ColumnSeriesItem){
if(l&&this.contains(l))
this.removeChild(l);
var csi:ColumnSeriesItem = ColumnSeriesItem(_data);
var cs:ColumnSeries = ColumnSeries(csi.element);
var arr:ArrayCollection = cs.dataProvider as ArrayCollection;
var m:Array=getMMvalue(arr,cs.yField);
var max:Boolean = Number(csi.yValue)==m[0];
var min:Boolean = Number(csi.yValue)==m[1];
if(max||min||(arr.length<13&&showOtherLabel)){
l= new Text();
if(max||min)
l.setStyle("fontWeight","bold");
this.addChild(l);
if((value as ColumnSeriesItem).yValue)
l.text= (value as ColumnSeriesItem).yValue.toString();//data label 标签会截掉最后一位,临时处理
l.setStyle("color",getColor(max,min));
l.setStyle("fontSize","14");
}
}
}

//--------------------------------------------------------------------------
//
//  Overridden methods
//
//--------------------------------------------------------------------------

/**
*  @private
*/
override protected function updateDisplayList(unscaledWidth:Number,
  unscaledHeight:Number):void
{
var fill:IFill;
var state:String = "";

if(_data is ChartItem && _data.hasOwnProperty('fill'))
{
state = _data.currentState;
fill = _data.fill;
}
else
fill = GraphicsUtilities.fillFromStyle(getStyle('fill'));

var color:uint;
var adjustedRadius:Number = 0;

switch(state)
{
case ChartItem.FOCUSED:
case ChartItem.ROLLOVER:
if(StyleManager.isValidStyleValue(getStyle('itemRollOverColor')))
color = getStyle('itemRollOverColor');
else
color = ColorUtil.adjustBrightness2(GraphicsUtilities.colorFromFill(fill),-20);
fill = new SolidColor(color);
adjustedRadius = getStyle('adjustedRadius');
if(!adjustedRadius)
adjustedRadius = 0;
break;
case ChartItem.DISABLED:
if(StyleManager.isValidStyleValue(getStyle('itemDisabledColor')))
color = getStyle('itemDisabledColor');
else
color = ColorUtil.adjustBrightness2(GraphicsUtilities.colorFromFill(fill),20);
fill = new SolidColor(GraphicsUtilities.colorFromFill(color));
break;
case ChartItem.FOCUSEDSELECTED:
case ChartItem.SELECTED:
if(StyleManager.isValidStyleValue(getStyle('itemSelectionColor')))
color = getStyle('itemSelectionColor');
else
color = ColorUtil.adjustBrightness2(GraphicsUtilities.colorFromFill(fill),-30);
fill = new SolidColor(color);
adjustedRadius = getStyle('adjustedRadius');
if(!adjustedRadius)
adjustedRadius = 0;
break;
}

var stroke:IStroke = getStyle("stroke");

var w:Number = stroke ? stroke.weight / 2 : 0;

var rc:Rectangle = new Rectangle(w - adjustedRadius, w - adjustedRadius, width - 2 * w + adjustedRadius * 2, height - 2 * w + adjustedRadius * 2);

var g:Graphics = graphics;
g.clear();
g.moveTo(rc.left,rc.top);
if (stroke)
stroke.apply(g,rc,null);
if (fill)
fill.begin(g,rc,null);

//圆角矩形
g.drawRoundRect(w - adjustedRadius,
w - adjustedRadius, width - 2 * w + adjustedRadius * 2,
height - 2 * w + adjustedRadius * 2,15);

// g.lineTo(rc.right,rc.top);
// g.lineTo(rc.right,rc.bottom);
// g.lineTo(rc.left,rc.bottom);
// g.lineTo(rc.left,rc.top);
if (fill)
fill.end(g);
if(l){
l.setActualSize(l.textWidth+25,l.textHeight);
l.move(-9,0-l.height);
}
}


}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics