原文 http://blog.csdn.net/lixuekun820/article/details/5485042

Summary:

Adobe 的 Flex Chart提供了很强大的功能,通过简单的设置就可以实现一些较复杂的效果。比如通过ColumnSet将多个柱状图以overlaid形式显示,并添加条线图。但思想是无止境的,老外要求我们在柱状图上有斜线的效果等。

Requirement:

ColumnChart 的能够显示线条,不同的柱状图显示不同的倾斜度、粗细及颜色。

Solution:

自定义ColumnSeries的itemRenderer. 

定义 LinesRenderer.as 如下:

package 

{

    import flash.display.Graphics;

    import flash.geom.Point;

    import mx.charts.renderers.BoxItemRenderer;

    public class LinesRenderer extends BoxItemRenderer

    {

    	/**

    	* properties:

    	* lineGap is the gap between in lines

    	* lineAngle is line's angle    	 

    	* */

        public var lineGap : Number=4;

        public var lineAngle : Number = 75;

        /**

        * style settings:

        * linethickness

        * lineColor

        * lineAlpha

        * */        

        public var lineThickness : Number = 1;

        public var lineColor : uint = 0x000000;

        public var lineAlpha : Number = 1;

        /**

        * Private properties:

        * xIncreace: x add value

        * yIncreace: y add value

        * maxIncreace: the max value of increace

        * 

        * */

        private var xIncreace : Number;

        private var yIncreace : Number;

        private var maxIncreace : Number;        

        private static const RADIAN_UINT : Number = Math.PI / 180;        

        private var startPoint : Point = new Point(0, 0);

        private var endPoint : Point = new Point(0, 0);

        private var currentPoint : Point = new Point(0, 0);

        public function LinesRenderer()

        {

            super();

        }

        override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void

        {

            super.updateDisplayList(unscaledWidth, unscaledHeight);

            if (this.height == 0 || this.width == 0)

            {

                return;

            }

var g:Graphics=graphics;

            g.lineStyle(lineThickness, lineColor, lineAlpha);

            drawShadeLine(g);

            g.endFill();

        }

        /**

        * draw lines function

        **/

        private function drawShadeLine(g : Graphics) : void

        {

        	var absHeight : Number = Math.abs(height);

        	var absWidth : Number = Math.abs(width);

        	currentPoint.x = 0;

        	currentPoint.y = 0;

        	if(lineAngle == 0)

        	{

        	if(height < 0)

        	{

        	currentPoint.y = - absHeight;

        	}

        	for(var i : int = 1; i * lineGap < absHeight; i++)

        	{

        	g.moveTo(currentPoint.x, currentPoint.y + i * lineGap);

        	g.lineTo(currentPoint.x + absWidth, currentPoint.y + i * lineGap);

        	}

        	}

        	else if(lineAngle == 90)

        	{

        	if(height < 0)

        	{

        	currentPoint.y = - absHeight;    

        	}

        	for(var t : int = 1; t * lineGap < absHeight; t++)

        	{

        	g.moveTo(currentPoint.x + t * lineGap, currentPoint.y);

        	g.lineTo(currentPoint.x + t * lineGap, currentPoint.y + absHeight);

        	}

        	}

        	else if(lineAngle > 0 && lineAngle < 90)

        	{

        	if(height < 0)

        	{

        	currentPoint.y = - absHeight;

        	}

        	xIncreace = lineGap / Math.cos(lineAngle * RADIAN_UINT);

           yIncreace = lineGap / Math.sin(lineAngle * RADIAN_UINT);

           maxIncreace = Math.max(absHeight + absWidth/Math.tan(lineAngle * RADIAN_UINT)

           	,absWidth + absHeight * Math.tan(lineAngle * RADIAN_UINT));

        	for (var j:int = 1; (j * xIncreace < maxIncreace || j * yIncreace < maxIncreace); j++)

           {

           	startPoint.y = currentPoint.y + j * yIncreace;

if (startPoint.y > currentPoint.y + absHeight)

{

startPoint.x = Math.min((startPoint.y - currentPoint.y - absHeight) * Math.tan(lineAngle * RADIAN_UINT), 

currentPoint.x + absWidth);

startPoint.y = currentPoint.y + absHeight;

} else 

{

startPoint.x = currentPoint.x;

}          

endPoint.x  = currentPoint.x + j * xIncreace;

if (endPoint.x > currentPoint.x + absWidth)

{

endPoint.y = Math.min(currentPoint.y + (endPoint.x - currentPoint.x - absWidth)/Math.tan(lineAngle * RADIAN_UINT), 

currentPoint.y + absHeight);

endPoint.x = currentPoint.x + absWidth;

} else 

{

endPoint.y = currentPoint.y;

}

               g.moveTo(startPoint.x, startPoint.y);

               g.lineTo(endPoint.x, endPoint.y);

           }

        	}

        	else if(lineAngle > 90 && lineAngle < 180)

        	{

        	if(height > 0)

        	{

        	currentPoint.y = height;

        	}

        	xIncreace = lineGap / Math.sin((180 - lineAngle) * RADIAN_UINT);

           yIncreace = lineGap / Math.cos((180 - lineAngle) * RADIAN_UINT);

           maxIncreace = Math.max(absHeight + absWidth * Math.tan((180 - lineAngle) * RADIAN_UINT)

           	,absWidth + absHeight / Math.tan((180 - lineAngle) * RADIAN_UINT));

        	for (var k:int = 1; (k * xIncreace < maxIncreace || k * yIncreace < maxIncreace); k++)

           {

           	startPoint.y = currentPoint.y - k * yIncreace;

if (startPoint.y < currentPoint.y - absHeight)

{

startPoint.x = Math.min(Math.abs(currentPoint.y - absHeight - startPoint.y) / Math.tan((180 - lineAngle) * RADIAN_UINT), 

currentPoint.x + absWidth);

startPoint.y = currentPoint.y - absHeight;

} else 

{

startPoint.x = currentPoint.x;

}          

endPoint.x  = currentPoint.x + k * xIncreace;

if (endPoint.x > currentPoint.x + absWidth)

{

endPoint.y = Math.max(currentPoint.y - ((endPoint.x - currentPoint.x - absWidth) * Math.tan((180 - lineAngle) * RADIAN_UINT)), 

currentPoint.y - absHeight);

endPoint.x = currentPoint.x + absWidth;

} else 

{

endPoint.y = currentPoint.y;

}

               g.moveTo(startPoint.x, startPoint.y);

               g.lineTo(endPoint.x, endPoint.y);

           }

        	}

        }

    }

}

Example: 创建一个App,使ColumnSeries 的ItemRender 为linesRender

ColumnChartApp.mxml

<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:local="*">

<mx:Script>

    import mx.collections.ArrayCollection;

    [Bindable]

    public var dataCollection:ArrayCollection = new ArrayCollection([

        {Month: "Jan", Income: 2000, AverageIncome:1620 },

        {Month: "Feb", Income: 600, AverageIncome:1620},

        {Month: "Mar", Income: 1500,AverageIncome:1620},

       {Month: "Apr", Income: 2500, AverageIncome:1620},

       {Month: "May", Income: 1500, AverageIncome:1620}]);       

    </mx:Script>

    <mx:Panel>

        <mx:ColumnChart id="myChart" dataProvider="{dataCollection}"

        	 showDataTips="true" type="overlaid">

            <mx:horizontalAxis>

                <mx:CategoryAxis dataProvider="{dataCollection}" 

                	 categoryField="Month"/>

            </mx:horizontalAxis>

            <mx:series>                

                <mx:ColumnSeries  xField="Month" yField="AverageIncome"  displayName="Average"

                	>

                	<mx:itemRenderer>

                	<mx:Component>

                	<local:LinesRenderer lineAngle="45" lineGap="5"  lineColor="0xffff00"/>

                	</mx:Component>

                	</mx:itemRenderer>

                </mx:ColumnSeries>

            </mx:series>

        </mx:ColumnChart>

    </mx:Panel>

</mx:Application>

注:代码可以独立运行,LinesRenderer.as 中的drawShadeLine()算法不是很理想,有待于改进。

draw lines on ColumnChart的更多相关文章

  1. Java基础之在窗口中绘图——绘制直线和矩形(Sketcher 2 drawing lines and rectangles)

    控制台程序. import javax.swing.JComponent; import java.util.*; import java.awt.*; import java.awt.geom.*; ...

  2. Compare, sort, and delete duplicate lines in Notepad ++

    Compare, sort, and delete duplicate lines in Notepad ++ Organize Lines: Since version 6.5.2 the app ...

  3. 使用AxisHelper帮助理解View and Data API中的坐标系统

    大家使用View and Data API做三维模型开发,必然首先要理解View and Data API的坐标系统,即XYZ三个轴向分别是怎么定义的.Three.js里面提供了一个AxisHelpe ...

  4. OpenCV特征点检测------ORB特征

    OpenCV特征点检测------ORB特征 ORB是是ORiented Brief的简称.ORB的描述在下面文章中: Ethan Rublee and Vincent Rabaud and Kurt ...

  5. TAQSkinScrollBar 类美化滚动条再讨论

    再说:TAQSkinScrollBar 类美化滚动条,http://www.138soft.com/?p=156  里面有人提到不可以滚动 滚动的改善方法: unit AQSkinScrollBar; ...

  6. Using Headless Mode in the Java SE Platform--转

    原文地址: By Artem Ananiev and Alla Redko, June 2006     Articles Index This article explains how to use ...

  7. HTML5资料

    1 Canvas教程 <canvas>是一个新的用于通过脚本(通常是JavaScript)绘图的HTML元素.例如,他可以用于绘图.制作图片的组合或者简单的动画(当然并不那么简单).It ...

  8. PCA and kmeans MATLAB实现

    MATLAB基础知识 l  Imread:  读取图片信息: l  axis:轴缩放:axis([xmin xmax ymin ymax zmin zmax cmin cmax]) 设置 x.y 和  ...

  9. jquery jqPlot API 中文使用教程

    jqPlot是一个灰常强大的图表工具,曲线,柱状,饼图,应该有尽有,更要命的是,调用方便~~ 官网:http://www.jqplot.com/ 这里贡献上中文教程,基本上所有的api都很齐全,供有需 ...

随机推荐

  1. css 兼容小三角

    <!DOCTYPE><html ><head><meta http-equiv="Content-Type" content=" ...

  2. 关于twitter的GIF变mp4的测试

    这个事是好久之前听说了,今天FQ的时候突然想起来了,就去测试了一下这个gif转MP4到底有多神奇... 这个是我的twitter地址:https://twitter.com/chendatony31 ...

  3. 保存mysql用户的登录信息到~.my.cnf文件;用于方便登录操作。

    原理说明: 在用户调用mysql 这个客户端程序去登录目标服务器时,mysql客户端程序会从本地读取配置文件信息,它要去读的配置文件包括 /etc/my.cnf /etc/mysql/my.cnf ~ ...

  4. 手动升级Delphi控件时,修改inc文件的办法

    以MustangPeakCommonLib.exe控件为例,想让它支持Delphi2010,就需要在D:\Program Files\Common Library\Mustangpeak\Common ...

  5. Delph控制台(Console)程序添加图标和版权信息

    Delphi创建控制台(Console)程序默认是无法添加图标和版权的.经过仔细的对比窗体程序与控制台程序源码,发现窗体程序的工程文中,在uses结束begin开始的地方有一句如下代码:{$R *.r ...

  6. 深入理解MFC子类化

    子类化,通俗来讲就是用自己的窗口处理函数来处理特定消息,并将自己其他消息还给标准(默认)窗口处理函数.在SDK中,通过SetWindowLong来指定一个自定义窗口处理函数:SetWindowLong ...

  7. hdu 5567 sequence1(水)

      问题描述 给定长度为n的序列a,求有多少对i,j(i<j),使得∣ai−aj∣ mod b=c 输入描述 若干组数据(大概5组). 每组数据第一行三个整数n(≤n≤),b,c(≤c<b ...

  8. 步步学LINQ to SQL:使用LINQ检索数据【转】

    [IT168 专稿]该系列教程描述了如何采用手动的方式映射你的对象类到数据表(而不是使用象SqlMetal这样的自动化工具)以便能够支持数据表之间的M:M关系和使用实体类的数据绑定.即使你选择使用了自 ...

  9. QCustomPlot使用手冊(三)

    一.改变范围 QCustomPlot *customplot; customplot->setInteraction(QCP::iRangeDrag,true); 使控件能够拖拉. custom ...

  10. android 实现自己定义状态栏通知(Status Notification)

    在android项目的开发中,有时为了实现和用户更好的交互,在通知栏这一小小的旮旯里,我们通常须要将内容丰富起来,这个时候我们就须要去实现自己定义的通知栏,比如以下360或者网易的样式: 首先我们要了 ...