Skip to content

Commit db6845c

Browse files
committed
add max range
1 parent e0e1630 commit db6845c

File tree

2 files changed

+48
-17
lines changed

2 files changed

+48
-17
lines changed

index.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@ <h1 class="title">Angular D3 Chart Directive</h1>
193193
labels: {
194194
y: 'Number of people'
195195
},
196+
max: {
197+
y: 400000
198+
},
196199
data: [
197200
{
198201
x: 'Jacob',
@@ -296,6 +299,7 @@ <h1 class="title">Angular D3 Chart Directive</h1>
296299
});
297300

298301
function drawBarGraph(isHorizontal) {
302+
299303
$scope.drawBarVH({
300304
colors: {
301305
axes: '#222',
@@ -304,7 +308,7 @@ <h1 class="title">Angular D3 Chart Directive</h1>
304308
focusedBar: '#8A8FC0'
305309
},
306310
orientation: isHorizontal ? 'horizontal' : 'vertical',
307-
infoFormat: '{x} - {y}',
311+
infoFormat: '{y} - {x}',
308312
labels: {
309313
y: 'Number of people'
310314
},

src/angular-graphs.bar.js

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ angular.module('picardy.graphs.bar', ['picardy.graphs.common'])
5353
info = common.newLayer(svg, 'info');
5454

5555
margin = {
56+
top: 10,
5657
info: 50
5758
};
5859

@@ -76,20 +77,34 @@ angular.module('picardy.graphs.bar', ['picardy.graphs.common'])
7677

7778
function drawYAxis (done) {
7879
var axisLength = options.height,
79-
yAxis;
80+
yAxis, range;
8081

8182
if (isVertical()) {
8283
axisLength -= margin.info;
84+
axisLength -= margin.top;
8385
y = d3.scale.linear().range([axisLength, 0]);
84-
y.domain([0, d3.max(options.data, function (d) {
85-
return d.y;
86-
})]);
86+
87+
if (options.max && options.max.y) {
88+
range = [0, options.max.y];
89+
} else {
90+
range = [0, d3.max(options.data, function (d) {
91+
return d.y;
92+
})];
93+
}
94+
95+
y.domain(range);
8796
} else {
8897
axisLength -= margin.info + margin.label + 21;
8998
y = d3.scale.ordinal().rangeRoundBands([0, axisLength], 0.2);
90-
y.domain(options.data.map(function (d) {
91-
return d.x;
92-
}));
99+
100+
if (options.max && options.max.y) {
101+
range = [0, options.max.y];
102+
} else {
103+
range = options.data.map(function (d) {
104+
return d.x;
105+
});
106+
}
107+
y.domain(range);
93108
}
94109

95110
yAxis = d3.svg.axis()
@@ -112,6 +127,7 @@ angular.module('picardy.graphs.bar', ['picardy.graphs.common'])
112127
if (isVertical()) {
113128
margin.yAxis = this.getBBox().width;
114129
xPos = margin.label + margin.yAxis + 20;
130+
yPos += margin.top;
115131
} else {
116132
xPos += 20;
117133
yPos += margin.info + margin.label + 20;
@@ -126,19 +142,30 @@ angular.module('picardy.graphs.bar', ['picardy.graphs.common'])
126142

127143
function drawXAxis () {
128144
var axisLength = options.width - 21,
129-
xAxis;
145+
xAxis, range;
130146

131147
if (isVertical()) {
132148
axisLength -= margin.label + margin.yAxis;
133149
x = d3.scale.ordinal().rangeRoundBands([0, axisLength], 0.2);
134-
x.domain(options.data.map(function (d) {
135-
return d.x;
136-
}));
150+
if (options.max && options.max.x) {
151+
range = [0, options.max.x];
152+
} else {
153+
range = options.data.map(function (d) {
154+
return d.x;
155+
});
156+
}
157+
x.domain(range);
137158
} else {
138159
x = d3.scale.linear().range([axisLength, 0]);
139-
x.domain([0, d3.max(options.data, function (d) {
140-
return d.y;
141-
})]);
160+
if (options.max && options.max.y) {
161+
range = [0, options.max.y];
162+
} else {
163+
range = [0, d3.max(options.data, function (d) {
164+
return d.y;
165+
})];
166+
}
167+
168+
x.domain(range);
142169
}
143170

144171
xAxis = d3.svg.axis()
@@ -239,14 +266,14 @@ angular.module('picardy.graphs.bar', ['picardy.graphs.common'])
239266
var attrTransitions = {
240267
transform: function (d) {
241268
return isVertical()
242-
? common.translate(margin.label + margin.yAxis + 20 + x(d.x), y(d.y) + 1)
269+
? common.translate(margin.label + margin.yAxis + 20 + x(d.x), margin.top + y(d.y) + 1)
243270
: common.translate(20, margin.info + margin.label + y(d.x) + 21);
244271
}
245272
};
246273

247274
if (isVertical()) {
248275
attrTransitions.height = function (d) {
249-
return options.height - margin.info - y(d.y);
276+
return options.height - margin.top - margin.info - y(d.y);
250277
};
251278
} else {
252279
attrTransitions.width = function (d) {

0 commit comments

Comments
 (0)