API Docs for: 3.8.0
Show:

File: charts/js/CategoryAxis.js

  1. /**
  2.  * CategoryAxis manages category data on an axis.
  3.  *
  4.  * @module charts
  5.  * @submodule charts-base
  6.  * @class CategoryAxis
  7.  * @constructor
  8.  * @param {Object} config (optional) Configuration parameters for the Chart.
  9.  * @extends AxisType
  10.  */
  11. function CategoryAxis(config)
  12. {
  13.         CategoryAxis.superclass.constructor.apply(this, arguments);
  14. }

  15. CategoryAxis.NAME = "categoryAxis";

  16. Y.extend(CategoryAxis, Y.AxisType,
  17. {
  18.     /**
  19.      * Formats a label based on the axis type and optionally specified format.
  20.      *
  21.      * @method formatLabel
  22.      * @param {Object} value
  23.      * @param {Object} format Pattern used to format the value.
  24.      * @return String
  25.      */
  26.     formatLabel: function(val, format)
  27.     {
  28.         return val;
  29.     },

  30.     /**
  31.      * Object storing key data.
  32.      *
  33.      * @property _indices
  34.      * @private
  35.      */
  36.     _indices: null,

  37.     /**
  38.      * Constant used to generate unique id.
  39.      *
  40.      * @property GUID
  41.      * @type String
  42.      * @private
  43.      */
  44.     GUID: "yuicategoryaxis",

  45.     /**
  46.      * Type of data used in `Axis`.
  47.      *
  48.      * @property _dataType
  49.      * @readOnly
  50.      * @private
  51.      */
  52.     _type: "category",

  53.     /**
  54.      * Calculates the maximum and minimum values for the `Axis`.
  55.      *
  56.      * @method _updateMinAndMax
  57.      * @private
  58.      */
  59.     _updateMinAndMax: function()
  60.     {
  61.         this._dataMaximum = Math.max(this.get("data").length - 1, 0);
  62.         this._dataMinimum = 0;
  63.     },

  64.     /**
  65.      * Gets an array of values based on a key.
  66.      *
  67.      * @method _getKeyArray
  68.      * @param {String} key Value key associated with the data array.
  69.      * @param {Array} data Array in which the data resides.
  70.      * @return Array
  71.      * @private
  72.      */
  73.     _getKeyArray: function(key, data)
  74.     {
  75.         var i = 0,
  76.             obj,
  77.             keyArr = [],
  78.             labels = [],
  79.             len = data.length;
  80.         if(!this._indices)
  81.         {
  82.             this._indices = {};
  83.         }
  84.         for(; i < len; ++i)
  85.         {
  86.             obj = data[i];
  87.             keyArr[i] = i;
  88.             labels[i] = obj[key];
  89.         }
  90.         this._indices[key] = keyArr;
  91.         return labels;
  92.     },

  93.     /**
  94.      * Sets data by key
  95.      *
  96.      * @method _setDataByKey
  97.      * @param {String} key Key value to use.
  98.      * @param {Array} data Array to use.
  99.      * @private
  100.      */
  101.     _setDataByKey: function(key)
  102.     {
  103.         var i,
  104.             obj,
  105.             arr = [],
  106.             labels = [],
  107.             dv = this._dataClone.concat(),
  108.             len = dv.length;
  109.         if(!this._indices)
  110.         {
  111.             this._indices = {};
  112.         }
  113.         for(i = 0; i < len; ++i)
  114.         {
  115.             obj = dv[i];
  116.             arr[i] = i;
  117.             labels[i] = obj[key];
  118.         }
  119.         this._indices[key] = arr;
  120.         this.get("keys")[key] = labels.concat();
  121.         this._updateTotalDataFlag = true;
  122.     },

  123.     /**
  124.      * Returns an array of values based on an identifier key.
  125.      *
  126.      * @method getDataByKey
  127.      * @param {String} value value used to identify the array
  128.      * @return Array
  129.      */
  130.     getDataByKey: function (value)
  131.     {
  132.         if(!this._indices)
  133.         {
  134.             this.get("keys");
  135.         }
  136.         var keys = this._indices;
  137.         if(keys[value])
  138.         {
  139.             return keys[value];
  140.         }
  141.         return null;
  142.     },

  143.     /**
  144.      * Returns the total number of majorUnits that will appear on an axis.
  145.      *
  146.      * @method getTotalMajorUnits
  147.      * @param {Object} majorUnit Object containing properties related to the majorUnit.
  148.      * @param {Number} len Length of the axis.
  149.      * @return Number
  150.      */
  151.     getTotalMajorUnits: function(majorUnit, len)
  152.     {
  153.         return this.get("data").length;
  154.     },

  155.     /**
  156.      * Returns the distance between major units on an axis.
  157.      *
  158.      * @method getMajorUnitDistance
  159.      * @param {Number} len Number of ticks
  160.      * @param {Number} uiLen Size of the axis.
  161.      * @param {Object} majorUnit Hash of properties used to determine the majorUnit
  162.      * @return Number
  163.      */
  164.     getMajorUnitDistance: function(len, uiLen, majorUnit)
  165.     {
  166.         var dist;
  167.         if(majorUnit.determinant === "count")
  168.         {
  169.             dist = uiLen/len;
  170.         }
  171.         else if(majorUnit.determinant === "distance")
  172.         {
  173.             dist = majorUnit.distance;
  174.         }
  175.         return dist;
  176.     },

  177.     /**
  178.      * Gets the distance that the first and last ticks are offset from there respective
  179.      * edges.
  180.      *
  181.      * @method getEdgeOffset
  182.      * @param {Number} ct Number of ticks on the axis.
  183.      * @param {Number} l Length (in pixels) of the axis.
  184.      * @return Number
  185.      */
  186.     getEdgeOffset: function(ct, l)
  187.     {
  188.         return l/ct;
  189.     },

  190.     /**
  191.      * Returns a value based of a key value and an index.
  192.      *
  193.      * @method getKeyValueAt
  194.      * @param {String} key value used to look up the correct array
  195.      * @param {Number} index within the array
  196.      * @return String
  197.      */
  198.     getKeyValueAt: function(key, index)
  199.     {
  200.         var value = NaN,
  201.             keys = this.get("keys");
  202.         if(keys[key] && keys[key][index])
  203.         {
  204.             value = keys[key][index];
  205.         }
  206.         return value;
  207.     },

  208.     /**
  209.      * Calculates and returns a value based on the number of labels and the index of
  210.      * the current label.
  211.      *
  212.      * @method getLabelByIndex
  213.      * @param {Number} i Index of the label.
  214.      * @param {Number} l Total number of labels.
  215.      * @return String
  216.      */
  217.     getLabelByIndex: function(i, l)
  218.     {
  219.         var label,
  220.             data = this.get("data"),
  221.             position = this.get("position");
  222.         if(position == "bottom" || position == "top")
  223.         {
  224.             label = data[i];
  225.         }
  226.         else
  227.         {
  228.             label = data[l - (i + 1)];
  229.         }
  230.         return label;
  231.     },

  232.     /**
  233.      * Returns a string corresponding to the first label on an
  234.      * axis.
  235.      *
  236.      * @method getMinimumValue
  237.      * @return String
  238.      */
  239.     getMinimumValue: function()
  240.     {
  241.         var data = this.get("data"),
  242.             label = data[0];
  243.         return label;
  244.     },

  245.     /**
  246.      * Returns a string corresponding to the last label on an
  247.      * axis.
  248.      *
  249.      * @method getMaximumValue
  250.      * @return String
  251.      */
  252.     getMaximumValue: function()
  253.     {
  254.         var data = this.get("data"),
  255.             len = data.length - 1,
  256.             label = data[len];
  257.         return label;
  258.     }
  259. });

  260. Y.CategoryAxis = CategoryAxis;


  261.