4 Replies Latest reply on Mar 26, 2008 10:21 AM by breez11

    format verticalAxis labels

    breez11 Level 1
      I have a lineChart with 8 different types of data being displayed in bytes. I want to format the vAxis to format the bytes into kb mb gb ect. I have a function which can do this but I need to change it to work with the labelFunction property. I noticed it is looking to pass in specific arguments to format the label. Also since I have different peices of data being displayed I do not specify anything for the vAxis.categoryField. This value is populated by the series.yField values.

      here is my attempt
      //////////////////////////////////////////////////////////////////////////////
      private function createColumnChart():void{
      hAxis.categoryField = initLabel;
      c.percentWidth = 100;
      c.percentHeight = 100;
      c.showDataTips = true;
      c.horizontalAxis = hAxis;
      c.horizontalAxisRenderer = hAxisRenderer;
      hAxisRenderer.setStyle( "labelRotation", 45 );

      c.verticalAxis = vAxis;
      c.verticalAxisRenderer = vAxisRenderer;
      vAxis.labelFunction = bf.formatToLargest;

      if (traffic[0].hasOwnProperty("p_down")){
      c_series1.xField = initLabel;
      c_series1.yField = "p_down";
      c_series1.displayName = "Preview Downloaded";
      c.series.push(c_series1);
      }

      if (traffic[0].hasOwnProperty("t_down")){
      c_series2.xField = initLabel;
      c_series2.yField = "t_down";
      c_series2.displayName = "Tutorials Downloaded";
      c.series.push(c_series2);
      }

      l.dataProvider = c;
      l.direction = "horizontal";

      addChild( c );
      addChild( l );

      c.dataProvider = traffic;
      }


      //////////// Byte Formatter //////////////////////

      public class ByteFormatter {

      static public const KBYTES : int = 1;
      static public const MBYTES : int = 2;
      static public const GBYTES : int = 3;
      static public const TBYTES : int = 4;
      static public const PBYTES : int = 5;
      static public const EBYTES : int = 6;
      static public const ZBYTES : int = 7;
      static public const YBYTES : int = 8;
      static public const NBYTES : int = 9;
      static public const DBYTES : int = 10;

      private var _formats : Array = [ "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB", "NB", "DB"];

      public function format( myValue : Number, format : int, digits : int = 0 ):String {
      var divider : Number = Math.pow( 1024, format );
      return MathUtil.round(myValue / divider,digits) + " " + _formats[format - 1];
      }

      public function formatToLargest( myValue:Number, field:String, index:Number, digits : int = 0):String {
      for (var i : Number = 10; i > 0; i--) {
      if ( myValue >= Math.pow(1024, i) ) {
      break;
      }
      }

      return format(myValue,i,digits);
      }


      ////////////////// MathUtils //////////////////////////
      public class MathUtil {

      static public function round( value : Number, digits : int = 0 ):Number {
      var rounder : Number = Math.pow( 10, digits );
      return Math.round(value * rounder) / rounder;
      }
      }