| 1 |
Rickshaw.namespace('Rickshaw.Graph.Axis.Y'); |
| 2 |
|
| 3 |
Rickshaw.Graph.Axis.Y = Rickshaw.Class.create( { |
| 4 |
|
| 5 |
initialize: function(args) { |
| 6 |
|
| 7 |
this.graph = args.graph; |
| 8 |
this.orientation = args.orientation || 'right'; |
| 9 |
|
| 10 |
this.pixelsPerTick = args.pixelsPerTick || 75; |
| 11 |
if (args.ticks) this.staticTicks = args.ticks; |
| 12 |
if (args.tickValues) this.tickValues = args.tickValues; |
| 13 |
|
| 14 |
this.tickSize = args.tickSize || 4; |
| 15 |
this.ticksTreatment = args.ticksTreatment || 'plain'; |
| 16 |
|
| 17 |
this.tickFormat = args.tickFormat || function(y) { return y }; |
| 18 |
|
| 19 |
this.berthRate = 0.10; |
| 20 |
|
| 21 |
if (args.element) { |
| 22 |
|
| 23 |
this.element = args.element; |
| 24 |
this.vis = d3.select(args.element) |
| 25 |
.append("svg:svg") |
| 26 |
.attr('class', 'rickshaw_graph y_axis'); |
| 27 |
|
| 28 |
this.element = this.vis[0][0]; |
| 29 |
this.element.style.position = 'relative'; |
| 30 |
|
| 31 |
this.setSize({ width: args.width, height: args.height }); |
| 32 |
|
| 33 |
} else { |
| 34 |
this.vis = this.graph.vis; |
| 35 |
} |
| 36 |
|
| 37 |
var self = this; |
| 38 |
this.graph.onUpdate( function() { self.render() } ); |
| 39 |
}, |
| 40 |
|
| 41 |
setSize: function(args) { |
| 42 |
|
| 43 |
args = args || {}; |
| 44 |
|
| 45 |
if (!this.element) return; |
| 46 |
|
| 47 |
if (typeof window !== 'undefined') { |
| 48 |
|
| 49 |
var style = window.getComputedStyle(this.element.parentNode, null); |
| 50 |
var elementWidth = parseInt(style.getPropertyValue('width'), 10); |
| 51 |
|
| 52 |
if (!args.auto) { |
| 53 |
var elementHeight = parseInt(style.getPropertyValue('height'), 10); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
this.width = args.width || elementWidth || this.graph.width * this.berthRate; |
| 58 |
this.height = args.height || elementHeight || this.graph.height; |
| 59 |
|
| 60 |
this.vis |
| 61 |
.attr('width', this.width) |
| 62 |
.attr('height', this.height * (1 + this.berthRate)); |
| 63 |
|
| 64 |
var berth = this.height * this.berthRate; |
| 65 |
|
| 66 |
if (this.orientation == 'left') { |
| 67 |
this.element.style.top = -1 * berth + 'px'; |
| 68 |
} |
| 69 |
}, |
| 70 |
|
| 71 |
render: function() { |
| 72 |
|
| 73 |
if (this._renderHeight !== undefined && this.graph.height !== this._renderHeight) this.setSize({ auto: true }); |
| 74 |
|
| 75 |
this.ticks = this.staticTicks || Math.floor(this.graph.height / this.pixelsPerTick); |
| 76 |
|
| 77 |
var axis = this._drawAxis(this.graph.y); |
| 78 |
|
| 79 |
this._drawGrid(axis); |
| 80 |
|
| 81 |
this._renderHeight = this.graph.height; |
| 82 |
}, |
| 83 |
|
| 84 |
_drawAxis: function(scale) { |
| 85 |
var axis = d3.svg.axis().scale(scale).orient(this.orientation); |
| 86 |
axis.tickFormat(this.tickFormat); |
| 87 |
if (this.tickValues) axis.tickValues(this.tickValues); |
| 88 |
|
| 89 |
if (this.orientation == 'left') { |
| 90 |
var berth = this.height * this.berthRate; |
| 91 |
var transform = 'translate(' + this.width + ', ' + berth + ')'; |
| 92 |
} |
| 93 |
|
| 94 |
if (this.element) { |
| 95 |
this.vis.selectAll('*').remove(); |
| 96 |
} |
| 97 |
|
| 98 |
this.vis |
| 99 |
.append("svg:g") |
| 100 |
.attr("class", ["y_ticks", this.ticksTreatment].join(" ")) |
| 101 |
.attr("transform", transform) |
| 102 |
.call(axis.ticks(this.ticks).tickSubdivide(0).tickSize(this.tickSize)); |
| 103 |
|
| 104 |
return axis; |
| 105 |
}, |
| 106 |
|
| 107 |
_drawGrid: function(axis) { |
| 108 |
var gridSize = (this.orientation == 'right' ? 1 : -1) * this.graph.width; |
| 109 |
|
| 110 |
this.graph.vis |
| 111 |
.append("svg:g") |
| 112 |
.attr("class", "y_grid") |
| 113 |
.call(axis.ticks(this.ticks).tickSubdivide(0).tickSize(gridSize)) |
| 114 |
.selectAll('text') |
| 115 |
.each(function() { this.parentNode.setAttribute('data-y-value', this.textContent) }); |
| 116 |
} |
| 117 |
} ); |