@@ -8,11 +8,17 @@ const defaultOptions = {
88 * @default : derived from borderColor
99 */
1010 color : undefined ,
11+
1112 /**
1213 * with as number, or as string with pixel (px) ending, or as string with percentage (%) ending
1314 */
1415 width : 10 ,
1516
17+ /**
18+ * lineWidth as number, or as string with pixel (px) ending, or array of such definition
19+ */
20+ lineWidth : 2 ,
21+
1622 /**
1723 * whether the error values are given in absolute values or relative (default)
1824 */
@@ -68,12 +74,11 @@ const ErrorBarsPlugin = {
6874 * compute error bars width in pixel or percent
6975 * @param chart chartjs instance
7076 * @param horizontal orientation
71- * @param options plugin options
77+ * @param width plugin option width
7278 * @returns {* } width in pixel as number
7379 * @private
7480 */
75- _computeWidth ( chart , horizontal , options ) {
76- const width = options . width ;
81+ _computeWidth ( chart , horizontal , width ) {
7782 let widthInPx = width ;
7883
7984 try {
@@ -102,7 +107,7 @@ const ErrorBarsPlugin = {
102107 console . error ( e ) ;
103108 } finally {
104109 if ( Number . isNaN ( widthInPx ) ) {
105- widthInPx = options . width ;
110+ widthInPx = width ;
106111 }
107112 }
108113 return widthInPx ;
@@ -119,9 +124,10 @@ const ErrorBarsPlugin = {
119124 * @param horizontal orientation
120125 * @private
121126 */
122- _drawErrorBar ( ctx , model , plus , minus , color , width , horizontal ) {
127+ _drawErrorBar ( ctx , model , plus , minus , color , lineWidth , width , horizontal ) {
123128 ctx . save ( ) ;
124129 ctx . strokeStyle = color ;
130+ ctx . lineWidth = lineWidth ;
125131 ctx . beginPath ( ) ;
126132 if ( horizontal ) {
127133 ctx . moveTo ( minus , model . y - width / 2 ) ;
@@ -170,7 +176,10 @@ const ErrorBarsPlugin = {
170176 const horizontal = this . _isHorizontal ( chart ) ;
171177 const vScale = horizontal ? chart . scales [ 'x-axis-0' ] : chart . scales [ 'y-axis-0' ] ;
172178
173- const errorBarWidth = this . _computeWidth ( chart , horizontal , options ) ;
179+ const errorBarWidths = ( Array . isArray ( options . width ) ? options . width : [ options . width ] ) . map ( ( w ) => this . _computeWidth ( chart , horizontal , w ) ) ;
180+ const errorBarLineWidths = Array . isArray ( options . lineWidth ) ? options . lineWidth : [ options . lineWidth ] ;
181+ const errorBarColors = Array . isArray ( options . color ) ? options . color : [ options . color ] ;
182+
174183
175184 const ctx = chart . ctx ;
176185 ctx . save ( ) ;
@@ -182,7 +191,7 @@ const ErrorBarsPlugin = {
182191 if ( ! cur ) {
183192 return ;
184193 }
185- let hasLabelProperty = cur . hasOwnProperty ( bar . label ) ;
194+ const hasLabelProperty = cur . hasOwnProperty ( bar . label ) ;
186195 let errorBarData = null ;
187196
188197 // common scale such as categorical
@@ -193,19 +202,27 @@ const ErrorBarsPlugin = {
193202 errorBarData = cur [ bar . label . label ] ;
194203 }
195204
196- // error bar data for the barchart bar or point in linechart
197- if ( errorBarData ) {
198- const errorBarColor = options . color ? options . color : bar . color ;
199- const value = vScale . getRightValue ( bar . value ) ;
205+ if ( ! errorBarData ) {
206+ return ;
207+ }
208+
209+ const errorBars = Array . isArray ( errorBarData ) ? errorBarData : [ errorBarData ] ;
210+ const value = vScale . getRightValue ( bar . value ) ;
200211
201- const plusValue = options . absoluteValues ? Math . abs ( errorBarData . plus ) : ( value + Math . abs ( errorBarData . plus ) ) ;
202- const minusValue = options . absoluteValues ? Math . abs ( errorBarData . minus ) : ( value - Math . abs ( errorBarData . minus ) ) ;
212+ errorBars . forEach ( ( errorBar , ei ) => {
213+ // error bar data for the barchart bar or point in linechart
214+ const errorBarColor = errorBarColors [ ei % errorBarColors . length ] ? errorBarColors [ ei % errorBarColors . length ] : bar . color ;
215+ const errorBarLineWidth = errorBarLineWidths [ ei % errorBarLineWidths . length ] ;
216+ const errorBarWidth = errorBarWidths [ ei % errorBarWidths . length ] ;
217+
218+ const plusValue = options . absoluteValues ? errorBar . plus : ( value + Math . abs ( errorBar . plus ) ) ;
219+ const minusValue = options . absoluteValues ? errorBar . minus : ( value - Math . abs ( errorBar . minus ) ) ;
203220
204221 const plus = vScale . getPixelForValue ( plusValue ) ;
205222 const minus = vScale . getPixelForValue ( minusValue ) ;
206223
207- this . _drawErrorBar ( ctx , bar , plus , minus , errorBarColor , errorBarWidth , horizontal ) ;
208- }
224+ this . _drawErrorBar ( ctx , bar , plus , minus , errorBarColor , errorBarLineWidth , errorBarWidth , horizontal ) ;
225+ } ) ;
209226 } ) ;
210227 } ) ;
211228
0 commit comments