Debian Codegraph @

src/js/Rickshaw.Graph.Axis.X.js

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