breaking up line chart in flex charting - apache-flex

I am wondering how will i proceed with the following requirement. I need to create a line chart of business data of the last 8 quarters. It is easy, but the requirement is that, there should not be a connection between last year's Q4 and this years Q1. In effect, using the same array collection i need to split the line chart so that it looks like two diffferent lines on the same chart. Any idea how to proceed with it.
Thanks, PK

First, you can add multiple line series by a criteria in your Array Collection:
<mx:series>
<mx:LineSeries
yField="Profit"
displayName="Profit"
/>
<mx:LineSeries
yField="Expenses"
displayName="Expenses"
/>
</mx:series>
You can change the width and color of the lines for each series by using the tag. Making each line a different stroke and color.
<?xml version="1.0"?>
<!-- charts/BasicLineStroke.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script><![CDATA[
import mx.collections.ArrayCollection;
[Bindable]
public var expenses:ArrayCollection = new ArrayCollection([
{Month:"Jan", Profit:2000, Expenses:1500, Amount:450},
{Month:"Feb", Profit:1000, Expenses:200, Amount:600},
{Month:"Mar", Profit:1500, Expenses:500, Amount:300}
]);
]]></mx:Script>
<mx:Panel title="Line Chart With Strokes">
<mx:LineChart id="myChart"
dataProvider="{expenses}"
showDataTips="true"
>
<mx:horizontalAxis>
<mx:CategoryAxis
dataProvider="{expenses}"
categoryField="Month"
/>
</mx:horizontalAxis>
<mx:series>
<mx:LineSeries
yField="Profit"
displayName="Profit"
>
<mx:lineStroke>
<mx:Stroke
color="0x0099FF"
weight="20"
alpha=".2"
/>
</mx:lineStroke>
</mx:LineSeries>
<mx:LineSeries
yField="Expenses"
displayName="Expenses"
>
<mx:lineStroke>
<mx:Stroke
color="0x0044EB"
weight="20"
alpha=".8"
/>
</mx:lineStroke>
</mx:LineSeries>
</mx:series>
</mx:LineChart>
<mx:Legend dataProvider="{myChart}"/>
</mx:Panel>
</mx:Application>

Related

Flex overlaid ColumnSet vertical axis not updating in ColumnChart

I have a neat chart that is rendering overlaid columns with values in excess of 6000 but my vertical axis stays at 0-100, as if my dataProvider was being ignored.
The problem here is with "msAxis"
autoAdjust didn't change anything. Do I need to force its values manually?
<mx:ColumnChart xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
width="100%" height="100%"
dataTipFunction="formatDataTip"
fontSize="12"
showDataTips="true"
initialize="init()"
>
<mx:horizontalAxis>
<mx:CategoryAxis id="haxis"
categoryField.time="t"
categoryField.exchange="x"
title.time="Time"
title.exchange="Exchange"
labelFunction.time="renderTime"
labelFunction.exchange="renderExchange"
/>
</mx:horizontalAxis>
<mx:horizontalAxisRenderers>
<mx:AxisRenderer axis="{haxis}" canDropLabels="true"/>
</mx:horizontalAxisRenderers>
<mx:verticalAxisRenderers>
<mx:AxisRenderer axis="{msAxis}"
placement="left"
canDropLabels="true"/>
<mx:AxisRenderer axis="{oAxis}"
placement="right"
canDropLabels="true"
/>
</mx:verticalAxisRenderers>
<mx:series>
<mx:ColumnSet type="overlaid">
<mx:verticalAxis>
<mx:LinearAxis id="msAxis" autoAdjust="true" />
</mx:verticalAxis>
<mx:series>
<mx:ColumnSeries
xField.time="t" xField.exchange="x"
yField="ml"
fill="{sc_red}"
displayName="Max Latency"
showDataEffect="{interpolateIn}"
/>
<mx:ColumnSeries
xField.time="t" xField.exchange="x"
yField="al"
fill="{sc_blue}"
displayName="Average Latency"
showDataEffect="{interpolateIn}"
/>
</mx:series>
</mx:ColumnSet>
<mx:LineSeries yField="o"
displayName="Orders"
showDataEffect="{interpolateIn}"
>
<mx:verticalAxis>
<mx:LinearAxis id="oAxis" interval="1" />
</mx:verticalAxis>
</mx:LineSeries>
</mx:series>
</mx:ColumnChart>
I think you should affect your series to their corresponding axis using the verticalAxis property.

LineChart Broken Line With DateTimeAxis

I have a LineChart with a DateTimeAxis as horizontalAxis and a maximum set. If a point is beyond the maximum date, the whole segment will not appear. Yet, is there a way to make it appear until it reachs the border of the chart?
The code here :
<?xml version="1.0"?>
<!-- Simple example to demonstrate the DateTimeAxis class. -->
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx">
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
[Bindable]
public var stockDataAC:ArrayCollection = new ArrayCollection( [
{date:new Date(2005,7,27), close: 32.71},
{date:new Date(2005,7,29), close: 42.11},
{date:new Date(2005,8,4), close: 102.99}]);
]]>
</fx:Script>
<mx:Panel title="DateTimeAxis Example">
<mx:LineChart id="mychart" height="100%" width="100%"
paddingRight="5" paddingLeft="5"
showDataTips="true" dataProvider="{stockDataAC}">
<mx:horizontalAxis>
<mx:DateTimeAxis dataUnits="days" maximum="{new Date(2005,8,2)}"/>
</mx:horizontalAxis>
<mx:verticalAxis>
<mx:LinearAxis baseAtZero="false" />
</mx:verticalAxis>
<mx:series>
<mx:LineSeries yField="close" xField="date" displayName="AAPL"/>
</mx:series>
</mx:LineChart>
</mx:Panel>
</s:Application>
Got the answer on flexcoders : Set filterData="false" on your LineSeries.

flex chart hide a datatip

We get data from multiple feeds and data may or may not exist for a certain date.
So, for points that have no data we send NaN.
Question:
In the below code , is there a way to not show datatip for those that are null.
I have added a datatip function but it does show a small empty square, is it possible to not even show that?
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script><![CDATA[
import mx.charts.HitData;
import mx.collections.ArrayCollection;
[Bindable]
public var DS:ArrayCollection = new ArrayCollection([
{date:"22-Aug-05", expense:1575.9, tax:41.87, price: 4},
{date:"23-Aug-05", expense:NaN, tax:NaN,price: 4},
{date:"24-Aug-05", expense:1507.1, tax:42.77,price:5 },
{date:"25-Aug-05", expense:1568.8 ,tax:48.06, price:5},
]);
public function dtFunc(hd:HitData):String {
if(""+hd.item.expense == "NaN")
return "";
else
return hd.item.expense ;
}
]]></mx:Script>
<mx:SolidColor id="sc1" color="blue" alpha=".8"/>
<mx:Stroke id="s1" color="blue" weight="1"/>
<mx:Panel title="Column Chart With Multiple Axes">
<mx:CartesianChart id="myChart" showDataTips="true" dataTipFunction="dtFunc">
<mx:horizontalAxis>
<mx:CategoryAxis id="h1" categoryField="date"/>
</mx:horizontalAxis>
<mx:horizontalAxisRenderers>
<mx:AxisRenderer placement="bottom" axis="{h1}"/>
</mx:horizontalAxisRenderers>
<mx:verticalAxisRenderers>
<mx:AxisRenderer placement="left" axis="{v1}"/>
<mx:AxisRenderer placement="left" axis="{v3}" visible="false"/>
</mx:verticalAxisRenderers>
<mx:series>
<mx:ColumnSeries id="cs1"
horizontalAxis="{h1}"
dataProvider="{DS}"
yField="expense"
displayName="EXPENSE-BARCHART"
filterData="false"
>
<mx:verticalAxis>
<mx:LinearAxis id="v1" />
</mx:verticalAxis>
</mx:ColumnSeries>
<mx:LineSeries id="cs3" horizontalAxis="{h1}" dataProvider="{DS}" yField="price"
displayName="Price" form="step"
>
<mx:verticalAxis>
<mx:LinearAxis id="v3" />
</mx:verticalAxis>
</mx:LineSeries>
</mx:series>
</mx:CartesianChart>
<mx:Legend dataProvider="{myChart}"/>
</mx:Panel>
</mx:Application>
Thank you Devtron, the http://www.flexdeveloper.eu/forums/flex-charting/disable-datatip-but-keep-mouse-event-on-line-chart/
correctly describes the solution.
You can also set myChart.setStyle("dataTipRenderer",mx.skins.ProgrammaticSkin);
to get rid of the display of datatips.
Two minor issues,
We need to know the value of the tip before clearing the graphics.
The line that connects the datapoint to the rectangle that displays the data could not be removed. But that is good enough for me.
My guess is that you would have to extend the classes for DataTip, to check for NULL hitData objects, to kill/stop the drawing of the display.
I do not think there is a way to do that normally in FLEX. You would have to customize and override the hitData class unfortunately. What a pain!!

flex recover space wasted by invisible axis

I want to show multiple series on a chart.
But this would result in multiple axis, to avoid this I am trying to make some axis invisible.
Question:
When i set the axis renderer's invisible = false, there is a white space that is left behind.
Is there any way to avoid this white space from showing up ?
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script><![CDATA[
import mx.charts.HitData;
import mx.collections.ArrayCollection;
[Bindable]
public var DS:ArrayCollection = new ArrayCollection([
{date:"22-Aug-05", expense:1575.9, tax:41.87, price: 4},
{date:"23-Aug-05", expense:NaN, tax:NaN,price: 4},
{date:"24-Aug-05", expense:1507.1, tax:42.77,price:5 },
{date:"25-Aug-05", expense:1568.8 ,tax:48.06, price:5},
]);
public function dtFunc(hd:HitData):String {
if(""+hd.item.expense == "NaN")
return "";
else
return hd.item.expense ;
}
]]></mx:Script>
<mx:SolidColor id="sc1" color="blue" alpha=".8"/>
<mx:Stroke id="s1" color="blue" weight="1"/>
<mx:Panel title="Column Chart With Multiple Axes">
<mx:CartesianChart id="myChart" showDataTips="true" dataTipFunction="dtFunc">
<mx:horizontalAxis>
<mx:CategoryAxis id="h1" categoryField="date"/>
</mx:horizontalAxis>
<mx:horizontalAxisRenderers>
<mx:AxisRenderer placement="bottom" axis="{h1}"/>
</mx:horizontalAxisRenderers>
<mx:verticalAxisRenderers>
<mx:AxisRenderer placement="left" axis="{v1}"/>
<mx:AxisRenderer placement="left" axis="{v3}" visible="false"/>
</mx:verticalAxisRenderers>
<mx:series>
<mx:ColumnSeries id="cs1"
horizontalAxis="{h1}"
dataProvider="{DS}"
yField="expense"
displayName="EXPENSE-BARCHART"
filterData="false"
>
<mx:verticalAxis>
<mx:LinearAxis id="v1" />
</mx:verticalAxis>
</mx:ColumnSeries>
<mx:LineSeries id="cs3" horizontalAxis="{h1}" dataProvider="{DS}" yField="price"
displayName="Price" form="step"
>
<mx:verticalAxis>
<mx:LinearAxis id="v3" />
</mx:verticalAxis>
</mx:LineSeries>
</mx:series>
</mx:CartesianChart>
<mx:Legend dataProvider="{myChart}"/>
</mx:Panel>
</mx:Application>
try adding attributes showLine="false" showLabels="false" tickPlacement="none" inside axisrenderer.
it works well for me, but I'm not sure if it is already too late for you. Hope it helps.

Draw a line on top of a column chart

Apparently, it is not possible to have a chart with several independant horizontal axis.
What I am trying to achieve is :
display a bar chart, organized by any category
overlay a line (actually a single horizontal line) on top of it (to display some sort of threshold)
I tried doing the following :
[Bindable] public var columnsDataProvider : ArrayCollection =
new ArrayCollection([{"Category" : "Cat1", "Value" : 10},
{"Category" : "Cat2", "Value" : 20}]);
[Bindable] public var lineDataProvider : ArrayCollection =
new ArrayCollection([{"X" : 0, "Y" : 10},
{"X" : 1, "Y" : 10}]);
...
<mx:ColumnChart id="columnChart"
showDataTips="true"
columnWidthRatio="0.5"
paddingTop="10"
paddingLeft="5" paddingRight="5"
width="100%" height="100%">
<mx:horizontalAxis>
<mx:CategoryAxis dataProvider="{columnsDataProvider}" categoryField="Category"/>
</mx:horizontalAxis>
<mx:series>
<mx:ColumnSeries id="series1" yField="Value" xField="Carrier" dataProvider="{columnsDataProvider}">
<mx:horizontalAxis>
<mx:CategoryAxis id="axis1" categoryField="Category"/>
</mx:horizontalAxis>
</mx:ColumnSeries>
<mx:LineSeries id="series2" yField="Y" xField="X" dataProvider=" {lineDataProvider}">
<mx:horizontalAxis>
<mx:LinearAxis id="axis2"/>
</mx:horizontalAxis>
</mx:LineSeries>
</mx:series>
This is the closest to what I want, except that the line does not start span all the graphs vertically (it is drawn only between the two columns of my bar chart).
Is it possible to define a completely independant horizontal axis ?
I also tried drawing directly on the canvas, but the canvas.lineTo() method only works in "data" coordinate ; I would need something that works in "chart/canvas/absolute/whatever" coordinates.
Any help on that ?
Thanks
PH
<mx:ColumnChart id="columnChart" dataProvider="{columnsDataProvider}"
showDataTips="true" columnWidthRatio="0.5"
paddingTop="10"
paddingLeft="5" paddingRight="5"
width="100%" height="100%">
<mx:horizontalAxisRenderers>
<mx:AxisRenderer axis="{h1}" />
<mx:AxisRenderer axis="{h2}" visible="false" />
</mx:horizontalAxisRenderers>
<mx:verticalAxis>
<mx:LinearAxis id="v1"/>
</mx:verticalAxis>
<mx:series>
<mx:ColumnSeries id="series1" yField="Value" xField="Category">
<mx:horizontalAxis>
<mx:CategoryAxis id="h1" categoryField="Category"/>
</mx:horizontalAxis>
</mx:ColumnSeries>
<mx:LineSeries yField="Y" xField="X" dataProvider="{lineDataProvider}">
<mx:horizontalAxis>
<mx:LinearAxis id="h2" maximum="1"/>
</mx:horizontalAxis>
</mx:LineSeries>
</mx:series>

Resources