<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>techrageo.us &#187; Flex</title>
	<atom:link href="http://techrageo.us/taxonomy/categories/flex/feed/" rel="self" type="application/rss+xml" />
	<link>http://techrageo.us</link>
	<description>insight on technology</description>
	<lastBuildDate>Sat, 22 Oct 2011 12:18:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Conditionally-Colored Flex Charts</title>
		<link>http://techrageo.us/2008/02/20/conditionally-colored-flex-charts/</link>
		<comments>http://techrageo.us/2008/02/20/conditionally-colored-flex-charts/#comments</comments>
		<pubDate>Thu, 21 Feb 2008 00:55:06 +0000</pubDate>
		<dc:creator>grant</dc:creator>
				<category><![CDATA[Flex]]></category>
		<category><![CDATA[Free]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Adobe Flex]]></category>
		<category><![CDATA[chart coloring]]></category>
		<category><![CDATA[conditional coloring]]></category>
		<category><![CDATA[flash graph]]></category>
		<category><![CDATA[Flex Charting]]></category>
		<category><![CDATA[Graphing]]></category>
		<category><![CDATA[Graphs]]></category>

		<guid isPermaLink="false">http://techrageo.us/2008/02/20/conditionally-colored-flex-charts/</guid>
		<description><![CDATA[The charting in Flex is a pretty good tool, but there are a few small features that it would be really nice if it had. One of these would be the ability to have a chart series that changed colors depending on it&#8217;s value. Conditionally colored charts are something that a lot of us (and [...]]]></description>
			<content:encoded><![CDATA[<p>The charting in Flex is a pretty good tool, but there are a few small features that it would be really nice if it had.  One of these would be the ability to have a chart series that changed colors depending on it&#8217;s value.  Conditionally colored charts are something that a lot of us (and our customers) have grown accustomed to and we shouldn&#8217;t have to give it up with Flex.  If you are like me you have probably been &#8220;googling&#8221; every variation of &#8220;flex colored charts&#8221; that you can think of.  I couldn&#8217;t find an answer anywhere, but hopefully your search will end right here.</p>
<p>I wanted the ability to color charts based on their value, giving them red/yellow/green colors based on if they were lower/equal to/higher than a given value.  The ability to use gradients instead of solid colors was also a requirement.  I basically wanted to do something like this:</p>
<div style="width: 490px">
<object width="490" height="490" codebase="http://active.macromedia.com/flash7/cabs/ swflash.cab#version=9,0,0,0"><param name="src" value="http://www.techrageo.us/code/ColorColumnChart.swf"><param name="quality" value="high"><embed src="http://www.techrageo.us/code/ColorColumnChart.swf" width="490" height="490" quality="high" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></embed></object>
</div>
<p>The columns below the line are red, on the line are yellow, and above the line are green.  The need might arise to do more colors than this, so the code should be flexible</p>
<p>But almost as important as the final look of the graph itself was the ability to re-use the code without having to rewrite parts of it for each implementation.  And I wanted to be able to use it in a flex application using only mxml, but still be able to use it in ActionScript if needed.</p>
<p>In the end I wanted to be able to define the threshold or goal as part of the data that the graph used and the code would color the columns based on that data (if it was greater than/less than/equal to). So the code to use the &#8220;coloring object&#8221; would look something like this (where we define High/Mid/Low colors):</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx:series<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx:ColumnSeries</span> <span style="color: #000066;">xField</span>=<span style="color: #ff0000;">&quot;Month&quot;</span> <span style="color: #000066;">yField</span>=<span style="color: #ff0000;">&quot;Income&quot;</span> <span style="color: #000066;">displayName</span>=<span style="color: #ff0000;">&quot;Income2&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx:itemRenderer<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx:Component<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;utils:ColoredSeriesItemRenderer</span> <span style="color: #000066;">valueField</span>=<span style="color: #ff0000;">&quot;Income&quot;</span> <span style="color: #000066;">thresholdField</span>=<span style="color: #ff0000;">&quot;Goal&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;utils:thresholdGradients<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx:Object<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
              <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx:High<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;mx:Object</span> <span style="color: #000066;">gradient</span>=<span style="color: #ff0000;">&quot;[0x55dd55,0x005500]&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/mx:High<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
              <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx:Mid<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;mx:Object</span> <span style="color: #000066;">gradient</span>=<span style="color: #ff0000;">&quot;[0xdddd55,0x555500]&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/mx:Mid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
              <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx:Low<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;mx:Object</span> <span style="color: #000066;">gradient</span>=<span style="color: #ff0000;">&quot;[0xdd5555,0x550000]&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/mx:Low<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/mx:Object<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/utils:thresholdGradients<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/utils:ColoredSeriesItemRenderer<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/mx:Component<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/mx:itemRenderer<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/mx:ColumnSeries<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx:LineSeries</span> <span style="color: #000066;">xField</span>=<span style="color: #ff0000;">&quot;Month&quot;</span> <span style="color: #000066;">yField</span>=<span style="color: #ff0000;">&quot;Goal&quot;</span> <span style="color: #000066;">displayName</span>=<span style="color: #ff0000;">&quot;Goal&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/mx:series<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>But to be flexible I also wanted to be able to define the goal in the code, where I could give it as many ranges with associated colors as I wanted.  This code would look something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx:series<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx:ColumnSeries</span>  <span style="color: #000066;">xField</span>=<span style="color: #ff0000;">&quot;Month&quot;</span> <span style="color: #000066;">yField</span>=<span style="color: #ff0000;">&quot;Income&quot;</span> <span style="color: #000066;">displayName</span>=<span style="color: #ff0000;">&quot;Income1&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx:itemRenderer<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx:Component<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;utils:ColoredSeriesItemRenderer</span> <span style="color: #000066;">valueField</span>=<span style="color: #ff0000;">&quot;Income&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;utils:ranges<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx:Array<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
              <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx:Object</span> <span style="color: #000066;">lowerLimit</span>=<span style="color: #ff0000;">&quot;280&quot;</span> <span style="color: #000066;">color</span>=<span style="color: #ff0000;">&quot;0xFF0000&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
              <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx:Object</span> <span style="color: #000066;">lowerLimit</span>=<span style="color: #ff0000;">&quot;240&quot;</span> <span style="color: #000066;">upperLimit</span>=<span style="color: #ff0000;">&quot;280&quot;</span> <span style="color: #000066;">gradient</span>=<span style="color: #ff0000;">&quot;[0xFFFF00,0x55FF55]&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
              <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx:Object</span> <span style="color: #000066;">lowerLimit</span>=<span style="color: #ff0000;">&quot;220&quot;</span> <span style="color: #000066;">upperLimit</span>=<span style="color: #ff0000;">&quot;240&quot;</span> <span style="color: #000066;">gradient</span>=<span style="color: #ff0000;">&quot;[0x5555FF,0x000099]&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
              <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx:Object</span> <span style="color: #000066;">lowerLimit</span>=<span style="color: #ff0000;">&quot;200&quot;</span> <span style="color: #000066;">upperLimit</span>=<span style="color: #ff0000;">&quot;220&quot;</span> <span style="color: #000066;">gradient</span>=<span style="color: #ff0000;">&quot;[0x0000FF,0x00FF00,0xFF0000]&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
              <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx:Object</span> <span style="color: #000066;">upperLimit</span>=<span style="color: #ff0000;">&quot;200&quot;</span> <span style="color: #000066;">color</span>=<span style="color: #ff0000;">&quot;0xFFFF00&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/mx:Array<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/utils:ranges<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/utils:ColoredSeriesItemRenderer<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/mx:Component<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/mx:itemRenderer<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/mx:ColumnSeries<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/mx:series<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Creating a custom item renderer allowed me (and now you too) to change the colors of the columns as they were being created.  This works so much better than other solutions I saw (laying a filter on top of the columns) because it automatically changes when the column changes.</p>
<p>I created a new object out of it so you can just drop it in the directory where you keep your other library files (and change the package name if needed) and start using it.  I have included it along with some sample files in a handy zip file for you&#8230; <img src='http://techrageo.us/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>I hope this saves you all the time it could have saved me (if I would have found something similar).</p>
<p>Download the <a href="http://techrageo.us/code/ColumnColorChartSource.zip">ColoredSeriesItemRenderer.as object</a> with two full examples that you can compile and try in a handy zip file.</p>
<p><em>If you are not familiar with including custom made objects into an mxml file then you&#8217;ll need to know about packages.  In the sample code I used a package name of &#8220;utils&#8221;, which means that the actionscript object is defined with an &#8220;utils&#8221; package declaration and is in an &#8220;utils&#8221; subdirectory.  Also, the example files will require you to define the xml namespace in the Application tag.  This is done by adding something like this: <strong>xmlns:utils=&#8221;utils.*&#8221;</strong></em></p>
]]></content:encoded>
			<wfw:commentRss>http://techrageo.us/2008/02/20/conditionally-colored-flex-charts/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>Communicating Charts with Flex</title>
		<link>http://techrageo.us/2007/06/19/communicating-charts-with-flex/</link>
		<comments>http://techrageo.us/2007/06/19/communicating-charts-with-flex/#comments</comments>
		<pubDate>Tue, 19 Jun 2007 08:59:46 +0000</pubDate>
		<dc:creator>grant</dc:creator>
				<category><![CDATA[Flex]]></category>
		<category><![CDATA[Free]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Web design]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://techrageo.us/2007/06/19/communicating-charts-with-flex/</guid>
		<description><![CDATA[Getting started Creating graphs with Adobe&#8217;s Flex 2 is a pretty simple task, just throw together some short XML and you&#8217;re done. But like most &#8220;easy&#8221; technologies things get a little more difficult when you want to do more advanced things like making sections of the graphs stand out or keeping multiple graphs in sync [...]]]></description>
			<content:encoded><![CDATA[<h3>Getting started</h3>
<p>Creating graphs with <a href="http://www.adobe.com/products/flex/">Adobe&#8217;s Flex 2</a> is a pretty simple task, just throw together some short XML and you&#8217;re done.  But like most &#8220;easy&#8221; technologies things get a little more difficult when you want to do more advanced things like making sections of the graphs stand out or keeping multiple graphs in sync with one another.  In this tutorial I will take you through how to give your graphs a little more &#8220;flash&#8221; and how to make things happen in one graph when you click on another graph.</p>
<p>If you haven&#8217;t already you can <a href="http://www.adobe.com/products/flex/">download the free Flex 2 SDK from Adobe</a>. And you can <a href="http://www.adobe.com/products/flex/charting/">download a free trial of the Flex 2 charting library</a>.</p>
<p>In your favorite text editor (<a href="http://www.eclipse.org/">Eclipse</a> anyone?) start a new file and start with some basic code:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx:Application</span> <span style="color: #000066;">xmlns:mx</span>=<span style="color: #ff0000;">&quot;http://www.adobe.com/2006/mxml&quot;</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;*&quot;</span></span>
<span style="color: #009900;">  <span style="color: #000066;">xmlns:local</span>=<span style="color: #ff0000;">&quot;*&quot;</span> <span style="color: #000066;">xmlns:view</span>=<span style="color: #ff0000;">&quot;view.*&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx:Script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #339933;">&lt;![CDATA[</span>
<span style="color: #339933;">    import mx.collections.ArrayCollection;</span>
&nbsp;
<span style="color: #339933;">    [Bindable]</span>
<span style="color: #339933;">    private var profitPeriods:ArrayCollection = new ArrayCollection( [</span>
<span style="color: #339933;">    { period: &quot;Qtr1 2006&quot;, profit: 32 },</span>
<span style="color: #339933;">    { period: &quot;Qtr2 2006&quot;, profit: 47 },</span>
<span style="color: #339933;">    { period: &quot;Qtr3 2006&quot;, profit: 62 },</span>
<span style="color: #339933;">    { period: &quot;Qtr4 2006&quot;, profit: 35 },</span>
<span style="color: #339933;">    { period: &quot;Qtr1 2007&quot;, profit: 25 },</span>
<span style="color: #339933;">    { period: &quot;Qtr2 2007&quot;, profit: 55 } ]);</span>
<span style="color: #339933;">  ]]&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/mx:Script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx:Panel</span> <span style="color: #000066;">x</span>=<span style="color: #ff0000;">&quot;10&quot;</span> <span style="color: #000066;">y</span>=<span style="color: #ff0000;">&quot;10&quot;</span> <span style="color: #000066;">width</span>=<span style="color: #ff0000;">&quot;100%&quot;</span> <span style="color: #000066;">height</span>=<span style="color: #ff0000;">&quot;100%&quot;</span> <span style="color: #000066;">title</span>=<span style="color: #ff0000;">&quot;Profit By Period&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx:PieChart</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;profitPie&quot;</span> <span style="color: #000066;">dataProvider</span>=<span style="color: #ff0000;">&quot;{profitPeriods}&quot;</span> <span style="color: #000066;">showDataTips</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000066;">width</span>=<span style="color: #ff0000;">&quot;100%&quot;</span> <span style="color: #000066;">height</span>=<span style="color: #ff0000;">&quot;100%&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx:series<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx:PieSeries</span> <span style="color: #000066;">field</span>=<span style="color: #ff0000;">&quot;profit&quot;</span> <span style="color: #000066;">nameField</span>=<span style="color: #ff0000;">&quot;period&quot;</span> <span style="color: #000066;">labelPosition</span>=<span style="color: #ff0000;">&quot;callout&quot;</span> <span style="color: #000066;">showDataEffect</span>=<span style="color: #ff0000;">&quot;{interpolate}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/mx:series<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/mx:PieChart<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/mx:Panel<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/mx:Application<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<h3>Getting more advanced</h3>
<p>This code basically creates a sample dataset and a pie chart to display it.  Nothing fancy and if we were using an XML feed we wouldn&#8217;t have any need for ActionScript at all, since we are only using it to create the data in this example.</p>
<p>Now let&#8217;s say that your customer (or boss) wants to have the data shown in multiple ways.  For our example let&#8217;s say that he wants to see a pie graph, a bar graph and a data grid.  So he can see the data in whatever way he wants to.  No problem there, throwing another chart and grid on the page is just a few more lines of xml, but what if they are to interact?  Say your boss wants to be able to click on a piece of the pie chart and have the same record highlighted in the pie chart, the column chart and the data grid.  Now we&#8217;re going to have to whip out the ActionScript.</p>
<p>We are going to need three methods, one to highlight the pie graph, one for the column chart and a final one for the data grid.  Then we can call all three of these methods whenever a chart or grid is clicked on.  We are using three methods instead of just one giant method in case we ever want to use them again, in a different circumstance.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">DisplayObject</span>;  <span style="color: #808080; font-style: italic;">//The items in the column chart are DisplayObjects</span>
&nbsp;
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> prevSelectedColumn:DisplayObject;
<span style="color: #808080; font-style: italic;">/*
We want to be able to &quot;unhighlight&quot; whatever column we highlighted last time, so
we'll create a variable to keep it simple. If you are using a graph with
multiple datasets then you'll obviously want to loop through them and reset them
that way instead of using this method.
*/</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/*
* This function will highlight a column/bar in a column/bar chart
* @param barName  The name/id of the bar/column chart
* @param barIndex The position of the column you want to highlight
*/</span>
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> barHighlight<span style="color: #66cc66;">&#40;</span> barName:<span style="color: #0066CC;">String</span>, barIndex:<span style="color: #0066CC;">int</span> <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>prevSelectedColumn<span style="color: #66cc66;">&#41;</span>  <span style="color: #808080; font-style: italic;">//If we selected something before, unselect it:</span>
  <span style="color: #66cc66;">&#123;</span>
    prevSelectedColumn.<span style="color: #006600;">alpha</span> = .8;
    prevSelectedColumn.<span style="color: #006600;">filters</span> = <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>;  <span style="color: #808080; font-style: italic;">//remove the filters</span>
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">//This gets the individual column that we are looking for</span>
  prevSelectedColumn = <span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#91;</span>barName<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">series</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">getChildAt</span><span style="color: #66cc66;">&#40;</span> barIndex <span style="color: #66cc66;">&#41;</span>;
&nbsp;
  <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>prevSelectedColumn<span style="color: #66cc66;">&#41;</span>  <span style="color: #808080; font-style: italic;">//highlight the new column:</span>
  <span style="color: #66cc66;">&#123;</span>
    prevSelectedColumn.<span style="color: #006600;">alpha</span> = <span style="color: #cc66cc;">1</span>;
    prevSelectedColumn.<span style="color: #006600;">filters</span> = <span style="color: #66cc66;">&#91;</span><span style="color: #000000; font-weight: bold;">new</span> GlowFilter<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #000000; font-weight: bold;">new</span> DropShadowFilter<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>;
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/*
* This function will highlight a row in a data grid
* @param dataGridName The name of the data grid that contains the row to highlight
* @param rowIndex   The position of the row in the grid to highlight
*/</span>
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> rowHighlight<span style="color: #66cc66;">&#40;</span> dataGridName:<span style="color: #0066CC;">String</span>, rowIndex:<span style="color: #0066CC;">int</span> <span style="color: #66cc66;">&#41;</span>: <span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#91;</span>dataGridName<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">selectedIndex</span> = rowIndex;  <span style="color: #808080; font-style: italic;">//the easy one...</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/*
* This function &quot;explodes&quot; a piece of pie from a pie chart
* @param pieName  The name of the pie chart that is to be modified
* @param pieIndex The index of the pie piece that is to be modified
*/</span>
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> pieHighlight<span style="color: #66cc66;">&#40;</span> pieName:<span style="color: #0066CC;">String</span>, pieIndex:<span style="color: #0066CC;">int</span> <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">var</span> explodeData:<span style="color: #0066CC;">Array</span> = <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>;  <span style="color: #808080; font-style: italic;">//create an empty array</span>
  explodeData<span style="color: #66cc66;">&#91;</span> pieIndex <span style="color: #66cc66;">&#93;</span> = <span style="color: #cc66cc;">0.15</span>; <span style="color: #808080; font-style: italic;">//Set the index of our pie piece to &gt; 0</span>
  <span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#91;</span>pieName<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">series</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">perWedgeExplodeRadius</span> = explodeData;
  <span style="color: #808080; font-style: italic;">/*
  The pie's wedges can be &quot;exploded&quot; outward to highlight them. The amount is
  controlled by an array, so we just create a empty array (which would set
  everything to no padding) and add padding for the index that is to be
  highlighted. This has the added bonus of un-highlighting the piece that was last
  highlighted (if any). Sweet!
  */</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<h3>Adding events</h3>
<p>Of course, this is using inline ActionScript.  If you want to separate it into another file you&#8217;ll have to use &#8220;Application.application&#8221; instead of &#8220;this&#8221; (see the downloadable GraphHighlighting.as file below).  That should give us a snazzy little effect every time we click on an item in any of the charts or in the grid.   But they won&#8217;t be doing any effects unless we add event handlers to our objects.  Let&#8217;s do that now.</p>
<p>So change the xml for your pie chart and add this to the PieChart tag:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">itemClick=<span style="color: #ff0000;">&quot;pieChartClick(event, 'profitGrid', 'profitBar');&quot;</span></pre></div></div>

<p>And then you can add the ActionScript to handle that event (which, of course, will call our handy three functions):</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/*
* This is run when the pie chart is clicked on
* @param event    The click event
* @param dataGridName The string name of the data grid that should be highlighted
* @param barName  The string name of the column chart that should be highlighted
*/</span>
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> pieChartClick<span style="color: #66cc66;">&#40;</span> event:ChartItemEvent, dataGridName:<span style="color: #0066CC;">String</span>, barName:<span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
  pieHighlight<span style="color: #66cc66;">&#40;</span> event.<span style="color: #006600;">currentTarget</span>.<span style="color: #006600;">id</span> , event.<span style="color: #006600;">hitData</span>.<span style="color: #006600;">chartItem</span>.<span style="color: #0066CC;">index</span> <span style="color: #66cc66;">&#41;</span>;
  rowHighlight<span style="color: #66cc66;">&#40;</span> dataGridName, event.<span style="color: #006600;">hitData</span>.<span style="color: #006600;">chartItem</span>.<span style="color: #0066CC;">index</span><span style="color: #66cc66;">&#41;</span>;
  barHighlight<span style="color: #66cc66;">&#40;</span> barName, event.<span style="color: #006600;">hitData</span>.<span style="color: #006600;">chartItem</span>.<span style="color: #0066CC;">index</span> <span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Then do this for the other chart and the datagrid and you&#8217;ll be set.</p>
<h3>Conclusion and downloads</h3>
<p>So, if you did your calculations correctly, remembered to carry the one and crossed your fingers you probably ended up with something like this:</p>
<div style="width: 490px">
<object width="480" height="480" codebase="http://active.macromedia.com/flash7/cabs/ swflash.cab#version=9,0,0,0"><param name="src" value="http://www.techrageo.us/code/communicateflex/Chart.swf"><param name="quality" value="high"><embed src="http://www.techrageo.us/code/communicateflex/Chart.swf" width="480" height="480" quality="high" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></embed></object>
</div>
<p>Actually, it probably won&#8217;t look like that, but the sample code download does (see below).</p>
<p>It is more likely that you&#8217;ll probably be making the charts communicate using different data (click on one part of a chart and get detailed information in another chart), but this should at least get you started in the right direction.</p>
<p>Download the <a href="http://techrageo.us/code/communicateflex/Chart.mxml">complete inline ActionScript example</a>: This file is the complete example and you should be able to save it and compile it with the mxml compiler that came with the Flex 2 SDK (assuming you have the charting library, or the charting trial library).</p>
<p>Download the <a href="http://techrageo.us/code/communicateflex/GraphHighlighting.as">&#8220;external&#8221; ActionScript methods</a>: This file is what you can use if you want to keep these functions in your library for future use.  I put this in the FlexAS.GraphHighlighting package, so if you don&#8217;t change the package name you should put this into a &#8220;FlexAS/GraphHighlighting/&#8221; subfolder off of your main flex directory.</p>
]]></content:encoded>
			<wfw:commentRss>http://techrageo.us/2007/06/19/communicating-charts-with-flex/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
	</channel>
</rss>

