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

Commit 8182212

Browse files
committed
fix #1 and fix #2 scaling + absolute
1 parent 87d92f4 commit 8182212

1 file changed

Lines changed: 45 additions & 26 deletions

File tree

src/plugin.js

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,20 @@
33
import Chart from 'chart.js';
44

55
const defaultOptions = {
6+
/**
7+
* stroke color
8+
* @default: derived from borderColor
9+
*/
10+
color: undefined,
611
/**
712
* with as number, or as string with pixel (px) ending, or as string with percentage (%) ending
813
*/
9-
width: 10
14+
width: 10,
15+
16+
/**
17+
* whether the error values are given in absolute values or relative (default)
18+
*/
19+
absoluteValues: false
1020
};
1121

1222
const ErrorBarsPlugin = {
@@ -22,6 +32,7 @@ const ErrorBarsPlugin = {
2232
const coords = [];
2333
chart.data.datasets.forEach((d, i) => {
2434
const bars = chart.getDatasetMeta(i).data;
35+
const values = d.data;
2536
coords.push(bars.map((b, j) => {
2637

2738
// line charts do not have labels in their meta data, access global label array instead
@@ -33,6 +44,7 @@ const ErrorBarsPlugin = {
3344
}
3445
return {
3546
label: barLabel,
47+
value: values[j],
3648
x: b._model.x,
3749
y: b._model.y,
3850
color: b._model.borderColor
@@ -98,37 +110,35 @@ const ErrorBarsPlugin = {
98110

99111
/**
100112
* draw error bar mark
101-
* @param chart chartjs instance
102113
* @param ctx canvas context
103114
* @param model bar base coords
104-
* @param plus positive error bar length
105-
* @param minus negative error bar length
115+
* @param plus positive error bar position
116+
* @param minus negative error bar position
106117
* @param color error bar stroke color
107118
* @param width error bar width in pixel
108119
* @param horizontal orientation
109120
* @private
110121
*/
111-
_drawErrorBar(chart, ctx, model, plus, minus, color, width, horizontal) {
122+
_drawErrorBar(ctx, model, plus, minus, color, width, horizontal) {
112123
ctx.save();
113124
ctx.strokeStyle = color;
114125
ctx.beginPath();
115126
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();
127+
ctx.moveTo(minus, model.y - width / 2);
128+
ctx.lineTo(minus, model.y + width / 2);
129+
ctx.moveTo(minus, model.y);
130+
ctx.lineTo(plus, model.y);
131+
ctx.moveTo(plus, model.y - width / 2);
132+
ctx.lineTo(plus, model.y + width / 2);
123133
} 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();
134+
ctx.moveTo(model.x - width / 2, plus);
135+
ctx.lineTo(model.x + width / 2, plus);
136+
ctx.moveTo(model.x, plus);
137+
ctx.lineTo(model.x, minus);
138+
ctx.moveTo(model.x - width / 2, minus);
139+
ctx.lineTo(model.x + width / 2, minus);
131140
}
141+
ctx.stroke();
132142
ctx.restore();
133143
},
134144

@@ -139,11 +149,15 @@ const ErrorBarsPlugin = {
139149
* @param options plugin options
140150
*/
141151
afterDraw(chart, easingValue, options) {
142-
options = Object.assign({}, defaultOptions, options);
143-
144152
// wait for easing value to reach 1 at the first render, after that draw immediately
145153
chart.__renderedOnce = chart.__renderedOnce || easingValue === 1;
146154

155+
if (!chart.__renderedOnce) {
156+
return;
157+
}
158+
159+
options = Object.assign({}, defaultOptions, options);
160+
147161
// error bar and barchart bar coords
148162
const errorBarCoords = chart.data.datasets.map((d) => d.errorBars);
149163
const barchartCoords = this._getBarchartBaseCoords(chart);
@@ -182,11 +196,16 @@ const ErrorBarsPlugin = {
182196
// error bar data for the barchart bar or point in linechart
183197
if (errorBarData) {
184198
const errorBarColor = options.color ? options.color : bar.color;
185-
const plus = vScale.getRightValue(errorBarData.plus);
186-
const minus = vScale.getRightValue(errorBarData.minus);
187-
if (chart.__renderedOnce) {
188-
this._drawErrorBar(chart, ctx, bar, Math.abs(plus), (Math.abs(minus) * -1), errorBarColor, errorBarWidth, horizontal);
189-
}
199+
const value = vScale.getRightValue(bar.value);
200+
const base = horizontal ? bar.x : bar.y;
201+
202+
const plusValue = options.absoluteValues ? Math.abs(errorBarData.plus) : (value + Math.abs(errorBarData.plus));
203+
const minusValue = options.absoluteValues ? Math.abs(errorBarData.minus) : (value - Math.abs(errorBarData.minus));
204+
205+
const plus = vScale.getPixelForValue(plusValue);
206+
const minus = vScale.getPixelForValue(minusValue);
207+
208+
this._drawErrorBar(ctx, bar, plus, minus, errorBarColor, errorBarWidth, horizontal);
190209
}
191210
});
192211
});

0 commit comments

Comments
 (0)