| 1 |
Rickshaw.namespace('Rickshaw.Graph.Axis.Y.Scaled'); |
| 2 |
|
| 3 |
Rickshaw.Graph.Axis.Y.Scaled = Rickshaw.Class.create( Rickshaw.Graph.Axis.Y, { |
| 4 |
|
| 5 |
initialize: function($super, args) { |
| 6 |
|
| 7 |
if (typeof(args.scale) === 'undefined') { |
| 8 |
throw new Error('Scaled requires scale'); |
| 9 |
} |
| 10 |
|
| 11 |
this.scale = args.scale; |
| 12 |
|
| 13 |
if (typeof(args.grid) === 'undefined') { |
| 14 |
this.grid = true; |
| 15 |
} else { |
| 16 |
this.grid = args.grid; |
| 17 |
} |
| 18 |
|
| 19 |
$super(args); |
| 20 |
|
| 21 |
}, |
| 22 |
|
| 23 |
_drawAxis: function($super, scale) { |
| 24 |
// Adjust scale's domain to compensate for adjustments to the |
| 25 |
// renderer's domain (e.g. padding). |
| 26 |
var domain = this.scale.domain(); |
| 27 |
var renderDomain = this.graph.renderer.domain().y; |
| 28 |
|
| 29 |
var extents = [ |
| 30 |
Math.min.apply(Math, domain), |
| 31 |
Math.max.apply(Math, domain)]; |
| 32 |
|
| 33 |
// A mapping from the ideal render domain [0, 1] to the extent |
| 34 |
// of the original scale's domain. This is used to calculate |
| 35 |
// the extents of the adjusted domain. |
| 36 |
var extentMap = d3.scale.linear().domain([0, 1]).range(extents); |
| 37 |
|
| 38 |
var adjExtents = [ |
| 39 |
extentMap(renderDomain[0]), |
| 40 |
extentMap(renderDomain[1])]; |
| 41 |
|
| 42 |
// A mapping from the original domain to the adjusted domain. |
| 43 |
var adjustment = d3.scale.linear().domain(extents).range(adjExtents); |
| 44 |
|
| 45 |
// Make a copy of the custom scale, apply the adjusted domain, and |
| 46 |
// copy the range to match the graph's scale. |
| 47 |
var adjustedScale = this.scale.copy() |
| 48 |
.domain(domain.map(adjustment)) |
| 49 |
.range(scale.range()); |
| 50 |
|
| 51 |
return $super(adjustedScale); |
| 52 |
}, |
| 53 |
|
| 54 |
_drawGrid: function($super, axis) { |
| 55 |
if (this.grid) { |
| 56 |
// only draw the axis if the grid option is true |
| 57 |
$super(axis); |
| 58 |
} |
| 59 |
} |
| 60 |
} ); |