Skip to content

Commit c5df9c9

Browse files
committed
option C
1 parent 4081f06 commit c5df9c9

File tree

8 files changed

+61
-66
lines changed

8 files changed

+61
-66
lines changed

src/elements/element.rectangle.js

Lines changed: 61 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ defaults._set('global', {
1717
}
1818
});
1919

20-
function isVertical(bar) {
21-
return bar._view.width !== undefined;
20+
function isVertical(vm) {
21+
return vm.width !== undefined;
2222
}
2323

2424
/**
@@ -27,11 +27,10 @@ function isVertical(bar) {
2727
* @return {Bounds} bounds of the bar
2828
* @private
2929
*/
30-
function getBarBounds(bar) {
31-
var vm = bar._view;
30+
function getBarBounds(vm) {
3231
var x1, x2, y1, y2, half;
3332

34-
if (isVertical(bar)) {
33+
if (isVertical(vm)) {
3534
half = vm.width / 2;
3635
x1 = vm.x - half;
3736
x2 = vm.x + half;
@@ -57,29 +56,29 @@ function swap(orig, v1, v2) {
5756
return orig === v1 ? v2 : orig === v2 ? v1 : orig;
5857
}
5958

60-
function parseBorderSkipped(bar) {
61-
var vm = bar._view;
59+
function parseBorderSkipped(vm) {
6260
var edge = vm.borderSkipped;
6361
var res = {};
6462

6563
if (!edge) {
6664
return res;
6765
}
6866

69-
if (isVertical(bar)) {
70-
if (vm.base < vm.y) {
71-
edge = swap(edge, 'bottom', 'top');
67+
if (vm.horizontal) {
68+
if (vm.base > vm.x) {
69+
edge = swap(edge, 'left', 'right');
7270
}
73-
} else if (vm.base > vm.x) {
74-
edge = swap(edge, 'left', 'right');
71+
} else if (vm.base < vm.y) {
72+
edge = swap(edge, 'bottom', 'top');
7573
}
7674

7775
res[edge] = true;
7876
return res;
7977
}
8078

81-
function parseBorderWidth(value, bar, maxWidth, maxHeight) {
82-
var _skip = parseBorderSkipped(bar);
79+
function parseBorderWidth(vm, maxW, maxH) {
80+
var value = vm.borderWidth;
81+
var _skip = parseBorderSkipped(vm);
8382
var t, r, b, l;
8483

8584
if (helpers.isObject(value)) {
@@ -92,62 +91,57 @@ function parseBorderWidth(value, bar, maxWidth, maxHeight) {
9291
}
9392

9493
return {
95-
top: _skip.top || (t < 0) ? 0 : t > maxHeight ? maxHeight : t,
96-
right: _skip.right || (r < 0) ? 0 : r > maxWidth ? maxWidth : r,
97-
bottom: _skip.bottom || (b < 0) ? 0 : b > maxWidth ? maxWidth : b,
98-
left: _skip.left || (l < 0) ? 0 : l > maxWidth ? maxWidth : l
94+
t: _skip.top || (t < 0) ? 0 : t > maxH ? maxH : t,
95+
r: _skip.right || (r < 0) ? 0 : r > maxW ? maxW : r,
96+
b: _skip.bottom || (b < 0) ? 0 : b > maxH ? maxH : b,
97+
l: _skip.left || (l < 0) ? 0 : l > maxW ? maxW : l
98+
};
99+
}
100+
101+
function boundingRects(vm) {
102+
var bounds = getBarBounds(vm);
103+
var width = bounds.right - bounds.left;
104+
var height = bounds.bottom - bounds.top;
105+
var border = parseBorderWidth(vm, width / 2, height / 2);
106+
107+
return {
108+
outer: {
109+
x: bounds.left,
110+
y: bounds.top,
111+
w: width,
112+
h: height
113+
},
114+
inner: {
115+
x: bounds.left + border.l,
116+
y: bounds.top + border.t,
117+
w: width - border.l - border.r,
118+
h: height - border.t - border.b
119+
}
99120
};
100121
}
101122

102123
module.exports = Element.extend({
103124
draw: function() {
104-
var me = this;
105-
var ctx = me._chart.ctx;
106-
var vm = me._view;
107-
var bounds = getBarBounds(me);
108-
var left = bounds.left;
109-
var top = bounds.top;
110-
var width = bounds.right - left;
111-
var height = bounds.bottom - top;
112-
var border = parseBorderWidth(vm.borderWidth, me, width / 2, height / 2);
113-
var bLeft = border.left;
114-
var bTop = border.top;
115-
var bRight = border.right;
116-
var bBottom = border.bottom;
117-
var maxBorder = Math.max(bLeft, bTop, bRight, bBottom);
118-
var halfBorder = maxBorder / 2;
119-
var inner = {
120-
left: left + bLeft,
121-
top: top + bTop,
122-
width: width - bLeft - bRight,
123-
height: height - bBottom - bTop
124-
};
125+
var ctx = this._chart.ctx;
126+
var vm = this._view;
127+
var rects = boundingRects(vm);
128+
var outer = rects.outer;
129+
var inner = rects.inner;
125130

126131
ctx.fillStyle = vm.backgroundColor;
132+
ctx.fillRect(outer.x, outer.y, outer.w, outer.h);
127133

128-
if (!maxBorder) {
129-
ctx.fillRect(left, top, width, height);
134+
if (outer.w === inner.w && outer.h === inner.h) {
130135
return;
131136
}
132137

133-
ctx.fillRect(inner.left, inner.top, inner.width, inner.height);
134-
135-
// offset inner rectangle by half of widest border
136-
// move edges additional 1px out, where there is no border, to prevent artifacts
137-
inner.left -= halfBorder + (bLeft ? 0 : 1);
138-
inner.top -= halfBorder + (bTop ? 0 : 1);
139-
inner.width += maxBorder + (bLeft ? 0 : 1) + (bRight ? 0 : 1);
140-
inner.height += maxBorder + (bTop ? 0 : 1) + (bBottom ? 0 : 1);
141-
142138
ctx.save();
143139
ctx.beginPath();
144-
ctx.rect(left, top, width, height);
140+
ctx.rect(outer.x, outer.y, outer.w, outer.h);
145141
ctx.clip();
146-
ctx.beginPath();
147-
ctx.rect(inner.left, inner.top, inner.width, inner.height);
148-
ctx.lineWidth = maxBorder + 1; // + 1 to prevent artifacts
149-
ctx.strokeStyle = vm.borderColor;
150-
ctx.stroke();
142+
ctx.fillStyle = vm.borderColor;
143+
ctx.rect(inner.x, inner.y, inner.w, inner.h);
144+
ctx.fill('evenodd');
151145
ctx.restore();
152146
},
153147

@@ -158,25 +152,26 @@ module.exports = Element.extend({
158152

159153
inRange: function(mouseX, mouseY) {
160154
var inRange = false;
155+
var vm = this._view;
161156

162-
if (this._view) {
163-
var bounds = getBarBounds(this);
157+
if (vm) {
158+
var bounds = getBarBounds(vm);
164159
inRange = mouseX >= bounds.left && mouseX <= bounds.right && mouseY >= bounds.top && mouseY <= bounds.bottom;
165160
}
166161

167162
return inRange;
168163
},
169164

170165
inLabelRange: function(mouseX, mouseY) {
171-
var me = this;
172-
if (!me._view) {
166+
var vm = this._view;
167+
if (!vm) {
173168
return false;
174169
}
175170

176171
var inRange = false;
177-
var bounds = getBarBounds(me);
172+
var bounds = getBarBounds(vm);
178173

179-
if (isVertical(me)) {
174+
if (isVertical(vm)) {
180175
inRange = mouseX >= bounds.left && mouseX <= bounds.right;
181176
} else {
182177
inRange = mouseY >= bounds.top && mouseY <= bounds.bottom;
@@ -186,19 +181,19 @@ module.exports = Element.extend({
186181
},
187182

188183
inXRange: function(mouseX) {
189-
var bounds = getBarBounds(this);
184+
var bounds = getBarBounds(this._view);
190185
return mouseX >= bounds.left && mouseX <= bounds.right;
191186
},
192187

193188
inYRange: function(mouseY) {
194-
var bounds = getBarBounds(this);
189+
var bounds = getBarBounds(this._view);
195190
return mouseY >= bounds.top && mouseY <= bounds.bottom;
196191
},
197192

198193
getCenterPoint: function() {
199194
var vm = this._view;
200195
var x, y;
201-
if (isVertical(this)) {
196+
if (isVertical(vm)) {
202197
x = vm.x;
203198
y = (vm.y + vm.base) / 2;
204199
} else {
@@ -212,7 +207,7 @@ module.exports = Element.extend({
212207
getArea: function() {
213208
var vm = this._view;
214209

215-
return isVertical(this)
210+
return isVertical(vm)
216211
? vm.width * Math.abs(vm.y - vm.base)
217212
: vm.height * Math.abs(vm.x - vm.base);
218213
},
-178 Bytes
Loading
Loading
Loading
-156 Bytes
Loading
Loading
-158 Bytes
Loading
-118 Bytes
Loading

0 commit comments

Comments
 (0)