f69d245bb21be88752420e834a6b6be37e9b525fTripp/**
f69d245bb21be88752420e834a6b6be37e9b525fTripp * Gridlines draws gridlines on a Graph.
f69d245bb21be88752420e834a6b6be37e9b525fTripp *
a75ebc38c1de401b679953a9b87bd323f0f48d02Tripp * @module charts
8648721e29bb657dd5c5ff20f03e86fe50628ce6Tripp * @class Gridlines
8648721e29bb657dd5c5ff20f03e86fe50628ce6Tripp * @constructor
8648721e29bb657dd5c5ff20f03e86fe50628ce6Tripp * @extends Base
8648721e29bb657dd5c5ff20f03e86fe50628ce6Tripp * @uses Renderer
f69d245bb21be88752420e834a6b6be37e9b525fTripp */
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTrippY.Gridlines = Y.Base.create("gridlines", Y.Base, [Y.Renderer], {
8209f3939e32e0e5bde64192267fdaf9db6f4fbcTripp /**
8209f3939e32e0e5bde64192267fdaf9db6f4fbcTripp * Reference to the `Path` element used for drawing Gridlines.
8209f3939e32e0e5bde64192267fdaf9db6f4fbcTripp *
8209f3939e32e0e5bde64192267fdaf9db6f4fbcTripp * @property _path
8209f3939e32e0e5bde64192267fdaf9db6f4fbcTripp * @type Path
8209f3939e32e0e5bde64192267fdaf9db6f4fbcTripp * @private
8209f3939e32e0e5bde64192267fdaf9db6f4fbcTripp */
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp _path: null,
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp
f69d245bb21be88752420e834a6b6be37e9b525fTripp /**
8209f3939e32e0e5bde64192267fdaf9db6f4fbcTripp * Removes the Gridlines.
8209f3939e32e0e5bde64192267fdaf9db6f4fbcTripp *
8209f3939e32e0e5bde64192267fdaf9db6f4fbcTripp * @method remove
f69d245bb21be88752420e834a6b6be37e9b525fTripp * @private
f69d245bb21be88752420e834a6b6be37e9b525fTripp */
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp remove: function()
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp {
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp var path = this._path;
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp if(path)
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp {
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp path.destroy();
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp }
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp },
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp /**
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp * Draws the gridlines
f69d245bb21be88752420e834a6b6be37e9b525fTripp *
f69d245bb21be88752420e834a6b6be37e9b525fTripp * @method draw
8209f3939e32e0e5bde64192267fdaf9db6f4fbcTripp * @protected
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp */
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp draw: function()
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp {
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp if(this.get("axis") && this.get("graph"))
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp {
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp this._drawGridlines();
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp }
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp },
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp /**
8209f3939e32e0e5bde64192267fdaf9db6f4fbcTripp * Algorithm for drawing gridlines
8209f3939e32e0e5bde64192267fdaf9db6f4fbcTripp *
8209f3939e32e0e5bde64192267fdaf9db6f4fbcTripp * @method _drawGridlines
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp * @private
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp */
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp _drawGridlines: function()
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp {
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp var path,
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp axis = this.get("axis"),
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp axisPosition = axis.get("position"),
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp points,
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp i = 0,
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp l,
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp direction = this.get("direction"),
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp graph = this.get("graph"),
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp w = graph.get("width"),
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp h = graph.get("height"),
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp line = this.get("styles").line,
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp color = line.color,
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp weight = line.weight,
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp alpha = line.alpha,
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp lineFunction = direction == "vertical" ? this._verticalLine : this._horizontalLine;
d79066bfa31eec6909a20ccabb5227cc1e1ea2d0Tripp if(isFinite(w) && isFinite(h) && w > 0 && h > 0)
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp {
d79066bfa31eec6909a20ccabb5227cc1e1ea2d0Tripp if(axisPosition != "none" && axis && axis.get("tickPoints"))
d79066bfa31eec6909a20ccabb5227cc1e1ea2d0Tripp {
d79066bfa31eec6909a20ccabb5227cc1e1ea2d0Tripp points = axis.get("tickPoints");
d79066bfa31eec6909a20ccabb5227cc1e1ea2d0Tripp l = points.length;
d79066bfa31eec6909a20ccabb5227cc1e1ea2d0Tripp }
d79066bfa31eec6909a20ccabb5227cc1e1ea2d0Tripp else
d79066bfa31eec6909a20ccabb5227cc1e1ea2d0Tripp {
d79066bfa31eec6909a20ccabb5227cc1e1ea2d0Tripp points = [];
d79066bfa31eec6909a20ccabb5227cc1e1ea2d0Tripp l = axis.get("styles").majorUnit.count;
d79066bfa31eec6909a20ccabb5227cc1e1ea2d0Tripp for(; i < l; ++i)
d79066bfa31eec6909a20ccabb5227cc1e1ea2d0Tripp {
d79066bfa31eec6909a20ccabb5227cc1e1ea2d0Tripp points[i] = {
d79066bfa31eec6909a20ccabb5227cc1e1ea2d0Tripp x: w * (i/(l-1)),
d79066bfa31eec6909a20ccabb5227cc1e1ea2d0Tripp y: h * (i/(l-1))
d79066bfa31eec6909a20ccabb5227cc1e1ea2d0Tripp };
d79066bfa31eec6909a20ccabb5227cc1e1ea2d0Tripp }
d79066bfa31eec6909a20ccabb5227cc1e1ea2d0Tripp i = 0;
d79066bfa31eec6909a20ccabb5227cc1e1ea2d0Tripp }
d79066bfa31eec6909a20ccabb5227cc1e1ea2d0Tripp path = graph.get("gridlines");
d79066bfa31eec6909a20ccabb5227cc1e1ea2d0Tripp path.set("width", w);
d79066bfa31eec6909a20ccabb5227cc1e1ea2d0Tripp path.set("height", h);
d79066bfa31eec6909a20ccabb5227cc1e1ea2d0Tripp path.set("stroke", {
d79066bfa31eec6909a20ccabb5227cc1e1ea2d0Tripp weight: weight,
d79066bfa31eec6909a20ccabb5227cc1e1ea2d0Tripp color: color,
d79066bfa31eec6909a20ccabb5227cc1e1ea2d0Tripp opacity: alpha
d79066bfa31eec6909a20ccabb5227cc1e1ea2d0Tripp });
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp for(; i < l; ++i)
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp {
d79066bfa31eec6909a20ccabb5227cc1e1ea2d0Tripp lineFunction(path, points[i], w, h);
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp }
d79066bfa31eec6909a20ccabb5227cc1e1ea2d0Tripp path.end();
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp }
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp },
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp
f69d245bb21be88752420e834a6b6be37e9b525fTripp /**
8209f3939e32e0e5bde64192267fdaf9db6f4fbcTripp * Algorithm for horizontal lines.
8209f3939e32e0e5bde64192267fdaf9db6f4fbcTripp *
8209f3939e32e0e5bde64192267fdaf9db6f4fbcTripp * @method _horizontalLine
8209f3939e32e0e5bde64192267fdaf9db6f4fbcTripp * @param {Path} path Reference to path element
8209f3939e32e0e5bde64192267fdaf9db6f4fbcTripp * @param {Object} pt Coordinates corresponding to a major unit of an axis.
8209f3939e32e0e5bde64192267fdaf9db6f4fbcTripp * @param {Number} w Width of the Graph
8209f3939e32e0e5bde64192267fdaf9db6f4fbcTripp * @param {Number} h Height of the Graph
f69d245bb21be88752420e834a6b6be37e9b525fTripp * @private
f69d245bb21be88752420e834a6b6be37e9b525fTripp */
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp _horizontalLine: function(path, pt, w, h)
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp {
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp path.moveTo(0, pt.y);
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp path.lineTo(w, pt.y);
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp },
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp /**
8209f3939e32e0e5bde64192267fdaf9db6f4fbcTripp * Algorithm for vertical lines.
8209f3939e32e0e5bde64192267fdaf9db6f4fbcTripp *
8209f3939e32e0e5bde64192267fdaf9db6f4fbcTripp * @method _verticalLine
8209f3939e32e0e5bde64192267fdaf9db6f4fbcTripp * @param {Path} path Reference to path element
8209f3939e32e0e5bde64192267fdaf9db6f4fbcTripp * @param {Object} pt Coordinates corresponding to a major unit of an axis.
8209f3939e32e0e5bde64192267fdaf9db6f4fbcTripp * @param {Number} w Width of the Graph
8209f3939e32e0e5bde64192267fdaf9db6f4fbcTripp * @param {Number} h Height of the Graph
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp * @private
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp */
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp _verticalLine: function(path, pt, w, h)
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp {
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp path.moveTo(pt.x, 0);
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp path.lineTo(pt.x, h);
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp },
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp /**
8209f3939e32e0e5bde64192267fdaf9db6f4fbcTripp * Gets the default value for the `styles` attribute. Overrides
8648721e29bb657dd5c5ff20f03e86fe50628ce6Tripp * base implementation.
8648721e29bb657dd5c5ff20f03e86fe50628ce6Tripp *
8648721e29bb657dd5c5ff20f03e86fe50628ce6Tripp * @method _getDefaultStyles
8648721e29bb657dd5c5ff20f03e86fe50628ce6Tripp * @return Object
8209f3939e32e0e5bde64192267fdaf9db6f4fbcTripp * @protected
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp */
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp _getDefaultStyles: function()
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp {
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp var defs = {
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp line: {
c8497af565e3869417da55c16f0afc9fafb7d79aTripp color:"#f0efe9",
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp weight: 1,
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp alpha: 1
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp }
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp };
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp return defs;
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp }
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp},
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp{
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp ATTRS: {
f69d245bb21be88752420e834a6b6be37e9b525fTripp /**
f69d245bb21be88752420e834a6b6be37e9b525fTripp * Indicates the direction of the gridline.
f69d245bb21be88752420e834a6b6be37e9b525fTripp *
f69d245bb21be88752420e834a6b6be37e9b525fTripp * @attribute direction
f69d245bb21be88752420e834a6b6be37e9b525fTripp * @type String
f69d245bb21be88752420e834a6b6be37e9b525fTripp */
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp direction: {},
f69d245bb21be88752420e834a6b6be37e9b525fTripp
f69d245bb21be88752420e834a6b6be37e9b525fTripp /**
8209f3939e32e0e5bde64192267fdaf9db6f4fbcTripp * Indicate the `Axis` in which to bind
f69d245bb21be88752420e834a6b6be37e9b525fTripp * the gridlines.
f69d245bb21be88752420e834a6b6be37e9b525fTripp *
f69d245bb21be88752420e834a6b6be37e9b525fTripp * @attribute axis
f69d245bb21be88752420e834a6b6be37e9b525fTripp * @type Axis
f69d245bb21be88752420e834a6b6be37e9b525fTripp */
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp axis: {},
f69d245bb21be88752420e834a6b6be37e9b525fTripp
f69d245bb21be88752420e834a6b6be37e9b525fTripp /**
8209f3939e32e0e5bde64192267fdaf9db6f4fbcTripp * Indicates the `Graph` in which the gridlines
f69d245bb21be88752420e834a6b6be37e9b525fTripp * are drawn.
f69d245bb21be88752420e834a6b6be37e9b525fTripp *
f69d245bb21be88752420e834a6b6be37e9b525fTripp * @attribute graph
f69d245bb21be88752420e834a6b6be37e9b525fTripp * @type Graph
f69d245bb21be88752420e834a6b6be37e9b525fTripp */
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp graph: {}
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp }
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp});