I'm attempting to rotate the category labels on my flex chart, but the labelRotation property seems to not work. Based on examples and the livedocs, I can't seem to figure out what I'm doing wrong.
<mx:BarChart id="barchartX" left="40" right="40" bottom="40" paddingBottom="40"
dataProvider="{chartDataObj.series}" showDataTips="false"
itemClick="editItem_clickHandler(event)" includeIn="BAR" visible="true">
<mx:verticalAxis>
<mx:CategoryAxis id="vaxis" categoryField="title"
title="click here to rename this axis" />
</mx:verticalAxis>
<mx:verticalAxisRenderers>
<mx:AxisRenderer axis="{vaxis}" labelRotation="45"
click="axis_clickHandler(event)"/>
</mx:verticalAxisRenderers>
<mx:horizontalAxis>
<mx:LinearAxis id="haxis" title="click here to rename this axis" />
</mx:horizontalAxis>
<mx:horizontalAxisRenderers>
<mx:AxisRenderer axis="{haxis}" labelRotation="0"
click="axis_clickHandler(event)"/>
</mx:horizontalAxisRenderers>
<mx:series>
<mx:BarSeries labelPosition="none" yField="title" xField="value"
fills="{chartDataObj.colors}"/>
</mx:series>
I am including my answer to a similar question, https://stackoverflow.com/a/9129992/892191
The key to making the labelRotation work is embedding of fonts.
There is a good article on Flex 4.6 here
A simple code example would be to add a style to the mxml file,
<fx:Style>
#namespace mx "library://ns.adobe.com/flex/mx";
#font-face{
src: local("Arial");
fontFamily: Arial;
embedAsCFF: false;
}
mx|ColumnChart {
fontFamily: Arial;
fontSize: 10;
}
</fx:Style>
Okay, after searching through the actual code of the AxisRenderer I came up with this:
<fx:Script>
[Embed(source='../assets/verdana.ttf',
fontName='verdanaFontMX',
mimeType='application/x-font',
embedAsCFF='false')]
private var font2:Class;
</fx:Script>
<mx:verticalAxisRenderers>
<mx:AxisRenderer axis="{vaxis}" labelRotation="90"
labelClass="mx.controls.Label"
fontFamily="verdanaFontMX" canStagger="false"
click="axis_clickHandler(event)"/>
</mx:verticalAxisRenderers>
When using a CategoryAxis for an MX chart in a mobile project, the category labels are rendered as Spark Labels, but the rest of the axis labels are rendered as MX Labels. The label rotation works, but a warning is thrown and the category is rendered using the device font instead. To fix this, use the MX Label class instead (labelClass="mx.controls.Label"). I know it doesn't render quickly as the Spark label, but it will render in the font you want.
Next, if you have a category label that's very long, somehow there is division by zero and the whole thing crashes. To avoid this, set canStagger="false" or canDropLabels="true". This avoids whatever measurement errors were happening. However, if the fontSize is set, and the label ends up being too large, the automatic sizing crashes again. So, I guess the easiest solution would be to keep the category labels short.
There is also the possibility of truncating the text as found here: http://help.adobe.com/en_US/flex/using/WS02f7d8d4857b1677355f601a126b41ed60e-8000.html
or, using a custom labelRenderer, but this can get very involved and can have diminishing returns on the amount of work versus the benefit to the look.
Related
I am using a column chart which has a horizontal axis for displaying dates.
Problem is when there are large data points then it shrinks the size of the label for the x-axis and i don't want this to happen.
I am displaying dates in batches but still the label don't occupy the available space. The size of the label shrinks and the text is hardly readable.
Any suggestion on how i could increase the label size irrespective of the number of ticks.
Here is a snippet of my code.
Thank You.
<mx:ColumnChart id = "areaChart"
height = "100%"
width = "100%"
seriesFilters="[]"
showDataTips="false"
type="overlaid"
columnWidthRatio="0.40"
mouseMove="showData(event)"
mouseSensitivity="600"
change="{callLater(showDefaultData)}">
<mx:horizontalAxis>
<mx:CategoryAxis categoryField = "date"
id = "hax"
ticksBetweenLabels="true"
labelFunction="chartsLabel"/>
</mx:horizontalAxis>
<mx:horizontalAxisRenderers>
<mx:AxisRenderer id="har"
axis="{hax}"
textAlign="left"
tickPlacement="none"
placement="bottom"
axisStroke="{Stroke}"/>
</mx:horizontalAxisRenderers>
Define a style for the AxisRenderer and use it in your definition:
Style:
<mx:Style>
.axisStyle {
fontSize:10;
color:blue;
}
</mx:Style>
Usage:
<mx:horizontalAxisRenderers>
<mx:AxisRenderer axis="{axisName}" styleName="axisStyle"/>
</mx:horizontalAxisRenderers>
<mx:horizontalAxis>
<mx:CategoryAxis id="axisName" dataProvider="{dataProvider}"
categoryField="someCategory"/>
</mx:horizontalAxis>
You shall set
canDropLabels="true|false"
canStagger="true|false"
styles for chart's axisrenderer: it shall know what to do if there are too much text.
You can also set scaleX and scaleY properties for axisrenderer if you want to force removal of scaling. But you'll not need it unless you do something tricky.
You can also set gutters for chart and axisrenderer to provide extra space for drawing labels, but again, normally you will not need it.
Unfortunately, teh CategoryAxis doesn't seem to have a renderer and therefore canDropLabels and canStagger cannot be set for it.
I have a flex pie chart:
<mx:PieChart alpha="0.9" fontSize="{QPieSeries_font}" width="100%" top="20" height="320" id="QPieChart"
dataProvider="{dataObj}" horizontalCenter="0" color="0xFFFFFF" fontFamily="MyriadWebPro"
showDataTips="true" dataTipFunction="QdataTipFunction"
dataTipItemsSet="true" itemClick="QPieChart_itemClickHandler(event)">
<mx:series>
<mx:PieSeries id="QPieSeries" labelField="Answer" field="Value" labelPosition="callout"
showDataEffect="QSeriesInterpolate" alpha="1"
reserveExplodeRadius="0.05"/>
</mx:series>
</mx:PieChart>
where the QSeriesInterpolate is a simple interpolation given in the declaration:
<fx:Declarations>
<mx:SeriesInterpolate id="QSeriesInterpolate" duration="500" />
</fx:Declarations>
and the item click event looks like:
protected function QPieChart_itemClickHandler(event:ChartItemEvent):void
{
var ExAr:Array = [];
ExAr[event.hitData.chartItem.index] = 0.05;
QPieSeries.perWedgeExplodeRadius = ExAr;
}
Basically, what annoys me is that when I click a segment to activate the interpolated event, the datatips disappear for it's duration. Whilst this is only half a second, it still detracts from user experience. Is there anything I can do to avoid it??
ALSO! Another thing: Is it obvious to anyone why my dataTipFunction doesnt work? It looks as follows:
public function QdataTipFunction(hitData:HitData):String
{
var a:String = "abc"
return a;
}
Yes it's simple but I was just trying to persuade it to work... to no avail. I think it might be some sort of font issue?
Thanks!
Josh
You might consider looking into Axiis. It is a flex data visualization framework, and has some awesome example charts. The main deal though is you have full control over EVERTHING! Probably overkill for what you are doing, but it really is awesome.
http://www.axiis.org/examples.html
Please find the below code
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
[Bindable]
public var testAC:Array = [
{date:"without", close:50},
{date:"with", close:45}
];
]]>
</mx:Script>
<mx:ColumnChart id="myChart" dataProvider="{testAC}">
<mx:horizontalAxis>
<mx:CategoryAxis categoryField="date"/>
</mx:horizontalAxis>
<mx:verticalAxis>
<mx:CategoryAxis categoryField="close"/>
</mx:verticalAxis>
<mx:series>
<mx:ColumnSeries dataProvider="{testAC}" xField="date" yField="close"/>
<mx:LineSeries dataProvider="{testAC}" xField="date" yField="close"/>
</mx:series>
</mx:ColumnChart>
</mx:Application>
This code is drawing a colum chart with two columns and drawing a line across the top of both columns. I have two requirements :
the line need to be dashed
as of now the line starts from top right corner of the first column to the same corner of the second column. How can i shift the line to the left, so that it starts from center of first column to center of second column.
Regards, PK
You can draw a line between two values on your Cartesian Chart with
<mx:Script><![CDATA[
private function connectTwoPoints(
month1:String, value1:Number,
month2:String, value2:Number):void
{
// Draw Line
canvas.clear();
canvas.lineStyle(4,
0xCCCCCC,
.75,
true,
LineScaleMode.NORMAL,
CapsStyle.ROUND,
JointStyle.MITER,
2);
canvas.moveTo(month1, value1);
canvas.lineTo(month2, value2);
}
]]></mx:Script>
<mx:annotationElements>
<mx:CartesianDataCanvas id="canvas" includeInRanges="true"/>
</mx:annotationElements>
The line that you draw will be an "Annotation Element" using the "Cartesian Data Canvas". Annotation Elements are drawn in the foreground. Perfect Example:
http://livedocs.adobe.com/flex/3/html/help.html?content=charts_eventsandeffects_13.html
For many of my charts with complex skinning I've been using Axiis. It's very Degrafa-like and would allow you to take a degrafa stroke and put it wherever you'd like on your 'dataCanvas'.
Here's an example that is pretty straight fwd:
http://axiis.org/examples/HClusterStackExample.html
'Tisn't the best answer in the land, but using axiis is so simple, and yet it allows for complex fills and strokes that aren't allowed via mxml with plain-ol' flex charting.
Best of luck,
Jeremy
after a long research i finally completed drawing dashed line chart. I used DashedGraphicUtilities provided by Adobe itself to draw the dashed line. I extender the LineSeries and used this DashedGraphicUtilities to draw the dashed line. That solved my first and mail problem. I will update this whenever i get the solution for the second.
And i got the solution for second problem also. The line chart was displaying perfectly as i needed, when i changed the graph type from ColumnChart to CartesianChart. I used column series and line series inside that and the line and columns were coming perfectly.
Regards,
Anoop
I want to use an AreaSeries to display the content of an arrayCollection (the cell values are displayed on the y axis).
The Array collection is dataManager.tagViewTimelineModel.summedPopularity
<mx:AreaSeries id="timeArea" styleName="timeArea" name="A" dataProvider="dataManager.tagViewTimelineModel.summedPopularity" areaStroke="{new Stroke(0x0033CC, 2)}" areaFill="{new SolidColor(0x0033CC, 0.5)}" />
But it doesn't work, the graph is empty....
Am I doing something wrong ?
This is the full code for completeness.
mx:CartesianChart id="AllChart" dataProvider="{dataManager.tagViewTimelineModel.tags}" width="100%" height="100">
<mx:horizontalAxis><mx:CategoryAxis id="horiz1" dataProvider="['1','2','3','4','5','6','7','8','9','10','11','23','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31']"/></mx:horizontalAxis>
<mx:horizontalAxisRenderers><mx:AxisRenderer axis="{horiz1}"/></mx:horizontalAxisRenderers>
<mx:verticalAxis><mx:LinearAxis id="vert1" /></mx:verticalAxis>
<mx:verticalAxisRenderers><mx:AxisRenderer axis="{vert1}"/></mx:verticalAxisRenderers>
<mx:series>
<mx:AreaSeries id="timeArea" styleName="timeArea" name="A" dataProvider="dataManager.tagViewTimelineModel.summedPopularity" areaStroke="{new Stroke(0x0033CC, 2)}" areaFill="{new SolidColor(0x0033CC, 0.5)}" />
</mx:series>
</mx:CartesianChart>
thanks
I think you might need { } around your dataProvider for your AreaSeries declaration. You have it on top for your chart but you probably overrode it below.
I have a linechart and dots showing the places where a datatip pops up. I can change the line to any color I want and I can set the inside color of the dot. But the border color still stays orange (from the default color). Someone knows how I can set this property easy?
this is the code:
<mx:LineSeries id="dayEnergieLineSeries"
yField="number"
showDataEffect="{slideIn}" hideDataEffect="{slideOut}">
<mx:lineStroke>
<mx:Stroke color="#d3ce01" />
</mx:lineStroke>
<mx:itemRenderer>
<mx:Component>
<mx:CircleItemRenderer />
</mx:Component>
</mx:itemRenderer>
</mx:LineSeries>
From the docs:
It renders its area on screen using the fill and stroke styles of its associated series.
So you'll have to assign a stroke to the dayEnergieLineSeries series.
[Bindable] private var _stroke:Stroke = new Stroke(...);
<mx:LineSeries id="dayEnergieLineSeries" stroke="{_stroke}"/>
or if you don't need the data binding, just set it as a style.
PS: There is a good chart explorer available here: http://demo.quietlyscheming.com/ChartSampler/app.html