Skip to content
This repository was archived by the owner on Nov 25, 2021. It is now read-only.

Commit 62c68cb

Browse files
author
sluger
committed
ensure positive plus and negative minus values; minor refactoring
1 parent 7132d18 commit 62c68cb

1 file changed

Lines changed: 42 additions & 41 deletions

File tree

src/plugin.js

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,46 +12,10 @@ const defaultOptions = {
1212
const ErrorBarsPlugin = {
1313
id: 'chartJsPluginErrorBars',
1414

15-
/**
16-
* draw error bar mark
17-
* @param chart chartjs instance
18-
* @param ctx canvas context
19-
* @param model bar base coords
20-
* @param plus positive error bar length
21-
* @param minus negative error bar length
22-
* @param color error bar stroke color
23-
* @param width error bar width in pixel
24-
* @param horizontal orientation
25-
* @private
26-
*/
27-
_drawErrorBar(chart, ctx, model, plus, minus, color, width, horizontal) {
28-
ctx.save();
29-
ctx.strokeStyle = color;
30-
ctx.beginPath();
31-
if (horizontal) {
32-
ctx.moveTo(model.x + minus, model.y - width / 2);
33-
ctx.lineTo(model.x + minus, model.y + width / 2);
34-
ctx.moveTo(model.x + minus, model.y);
35-
ctx.lineTo(model.x + plus, model.y);
36-
ctx.moveTo(model.x + plus, model.y - width / 2);
37-
ctx.lineTo(model.x + plus, model.y + width / 2);
38-
ctx.stroke();
39-
} else {
40-
ctx.moveTo(model.x - width / 2, model.y - plus);
41-
ctx.lineTo(model.x + width / 2, model.y - plus);
42-
ctx.moveTo(model.x, model.y - plus);
43-
ctx.lineTo(model.x, model.y - minus);
44-
ctx.moveTo(model.x - width / 2, model.y - minus);
45-
ctx.lineTo(model.x + width / 2, model.y - minus);
46-
ctx.stroke();
47-
}
48-
ctx.restore();
49-
},
50-
5115
/**
5216
* get original barchart base bar coords
5317
* @param chart chartjs instance
54-
* @returns {Array}
18+
* @returns {Array} containing label, x, y and color
5519
* @private
5620
*/
5721
_getBarchartBaseCoords(chart) {
@@ -91,7 +55,9 @@ const ErrorBarsPlugin = {
9155
/**
9256
* compute error bars width in pixel or percent
9357
* @param chart chartjs instance
58+
* @param horizontal orientation
9459
* @param options plugin options
60+
* @returns {*} width in pixel as number
9561
* @private
9662
*/
9763
_computeWidth(chart, horizontal, options) {
@@ -130,6 +96,42 @@ const ErrorBarsPlugin = {
13096
return widthInPx;
13197
},
13298

99+
/**
100+
* draw error bar mark
101+
* @param chart chartjs instance
102+
* @param ctx canvas context
103+
* @param model bar base coords
104+
* @param plus positive error bar length
105+
* @param minus negative error bar length
106+
* @param color error bar stroke color
107+
* @param width error bar width in pixel
108+
* @param horizontal orientation
109+
* @private
110+
*/
111+
_drawErrorBar(chart, ctx, model, plus, minus, color, width, horizontal) {
112+
ctx.save();
113+
ctx.strokeStyle = color;
114+
ctx.beginPath();
115+
if (horizontal) {
116+
ctx.moveTo(model.x + minus, model.y - width / 2);
117+
ctx.lineTo(model.x + minus, model.y + width / 2);
118+
ctx.moveTo(model.x + minus, model.y);
119+
ctx.lineTo(model.x + plus, model.y);
120+
ctx.moveTo(model.x + plus, model.y - width / 2);
121+
ctx.lineTo(model.x + plus, model.y + width / 2);
122+
ctx.stroke();
123+
} else {
124+
ctx.moveTo(model.x - width / 2, model.y - plus);
125+
ctx.lineTo(model.x + width / 2, model.y - plus);
126+
ctx.moveTo(model.x, model.y - plus);
127+
ctx.lineTo(model.x, model.y - minus);
128+
ctx.moveTo(model.x - width / 2, model.y - minus);
129+
ctx.lineTo(model.x + width / 2, model.y - minus);
130+
ctx.stroke();
131+
}
132+
ctx.restore();
133+
},
134+
133135
/**
134136
* plugin hook to draw the error bars
135137
* @param chart chartjs instance
@@ -172,9 +174,8 @@ const ErrorBarsPlugin = {
172174
// common scale such as categorical
173175
if (hasLabelProperty) {
174176
errorBarData = cur[bar.label];
175-
}
176-
// hierarchical scale has its label property nested in b.label object as b.label.label
177-
if (!hasLabelProperty && bar.label && bar.label.label && cur.hasOwnProperty(bar.label.label)) {
177+
} else if (!hasLabelProperty && bar.label && bar.label.label && cur.hasOwnProperty(bar.label.label)) {
178+
// hierarchical scale has its label property nested in b.label object as b.label.label
178179
errorBarData = cur[bar.label.label];
179180
}
180181

@@ -184,7 +185,7 @@ const ErrorBarsPlugin = {
184185
const plus = vScale.getRightValue(errorBarData.plus);
185186
const minus = vScale.getRightValue(errorBarData.minus);
186187
if (chart.__renderedOnce) {
187-
this._drawErrorBar(chart, ctx, bar, plus, minus, errorBarColor, errorBarWidth, horizontal);
188+
this._drawErrorBar(chart, ctx, bar, Math.abs(plus), (Math.abs(minus) * -1), errorBarColor, errorBarWidth, horizontal);
188189
}
189190
}
190191
});

0 commit comments

Comments
 (0)