views:

1257

answers:

3

I am trying to provide my own labelFunction for a CategoryAxis programatically but am completely stumped. The regular way is to do it in your MXML file, but I want to do it in my Actionscript file.

The regular way of doing it is:

<mx:Script>
    <![CDATA[
       private function categoryAxis_labelFunc(item:Object, 
                prevValue:Object, 
                axis:CategoryAxis, 
                categoryItem:Object):String {
                return "Some String";
            }
      ]]>
</mx:Script>

<mx:CategoryAxis labelFunction="categoryAxis_labelFunc" />

But I want to achieve the same thing in my subclass of CategoryAxis, something like:

public class FauxDateAxis extends CategoryAxis {

 public function FauxDateAxis() {
  super();
        labelFunction = categoryAxis_labelFunc // Doesn't work of course.
 }

        private function categoryAxis_labelFunc(item:Object, 
                prevValue:Object, 
                axis:CategoryAxis, 
                categoryItem:Object):String {
            return "Another String";
 } 

}
+1  A: 

This question got me curious, so I went off and tried it.

The labelFunction on CategoryAxis has a slightly different signature than what I am seeing here. For me, this is what works:

function(item:Object, field:String, index:int, pct:Number)

I am not a Flex charts wizard, so perhaps you know something I don't, but when I use that signature in this matter,

public function FauxDateAxis() {
    super();
    labelFunction = function(item:Object, field:String, index:int, pct:Number) {
       return "string";
    }
}

It works for me in Flex 3 Pro.

I see that the code sample you provided looks a lot like http://blog.flexexamples.com/2007/11/16/creating-a-custom-label-function-on-a-flex-linechart-controls-category-axis/ (I tried to see if I could find an example of the signature you provided). I see other people using this signature too.

This isn't much of an answer; I don't recall this part of the charts API changing between Flex 2 and Flex 3, but maybe this post helps you with your problem.

Mitch Haile
Randy Stegbauer
+2  A: 

Well, I'm baffled by your problem, because it works absolutely fine for me.

I took the example application for CategoryAxis from the Adobe Flex site: http://livedocs.adobe.com/flex/3/langref/index.html?mx/charts/CategoryAxis.html&amp;mx/charts/class-list.html, added your code verbatim (well except for adding package and import statments), and it worked just like you want it to.

In the example, I modified the line

<mx:CategoryAxis id="haxis" categoryField="Date" title="Date"/>

to read

<local:FauxDateAxis id="haxis" categoryField="Date" title="Date"/>

and it displayed "Another String" at the base of each column.

I'm using Flex 3, if that matters.

Good Luck, Randy Stegbauer

Randy Stegbauer
A: 

Just I though, I don't think it will make a difference, but maybe change your label function scope to protected rather than private???

maclema