11'use strict' ;
22
3- module . exports = function ( Chart ) {
4- var helpers = Chart . helpers ;
5- var globalDefaults = Chart . defaults . global ;
3+ var defaults = require ( '../core/core.defaults' ) ;
4+ var helpers = require ( '../ helpers/index' ) ;
5+ var MODEL_KEY = '$gridlines' ;
66
7- // Stores all gridLines that should be displayed
8- var lines = [ ] ;
7+ // This is copied from core.scale.js, which is also required here and im not sure where it should be placed for both of them to access it
8+ function getLineValue ( scale , index , offsetGridLines ) {
9+ var lineValue = scale . getPixelForTick ( index ) ;
910
11+ if ( offsetGridLines ) {
12+ if ( index === 0 ) {
13+ lineValue -= ( scale . getPixelForTick ( 1 ) - lineValue ) / 2 ;
14+ } else {
15+ lineValue -= ( lineValue - scale . getPixelForTick ( index - 1 ) ) / 2 ;
16+ }
17+ }
18+ return lineValue ;
19+ }
20+
21+ module . exports = function ( ) {
1022 function getGridLine ( chart , scale , position , tickIndex ) {
1123 var chartArea = chart . chartArea ;
1224 var scaleOptions = scale . options ;
1325 var gridLines = scaleOptions . gridLines ;
1426
1527 var lineWidth , lineColor , borderDash , borderDashOffset ;
1628
17- if ( tickIndex === ( typeof scale . zeroLineIndex !== 'undefined' ? scale . zeroLineIndex : 0 ) ) {
29+ if ( tickIndex !== undefined && tickIndex === scale . zeroLineIndex ) {
1830 lineWidth = gridLines . zeroLineWidth ;
1931 lineColor = gridLines . zeroLineColor ;
2032 borderDash = gridLines . zeroLineBorderDash ;
2133 borderDashOffset = gridLines . zeroLineBorderDashOffset ;
2234 } else {
2335 lineWidth = helpers . getValueAtIndexOrDefault ( gridLines . lineWidth , tickIndex ) ;
2436 lineColor = helpers . getValueAtIndexOrDefault ( gridLines . color , tickIndex ) ;
25- borderDash = helpers . getValueOrDefault ( gridLines . borderDash , globalDefaults . borderDash ) ;
26- borderDashOffset = helpers . getValueOrDefault ( gridLines . borderDashOffset , globalDefaults . borderDashOffset ) ;
37+ borderDash = helpers . getValueOrDefault ( gridLines . borderDash , defaults . global . borderDash ) ;
38+ borderDashOffset = helpers . getValueOrDefault ( gridLines . borderDashOffset , defaults . global . borderDashOffset ) ;
2739 }
2840
2941 var x1 , x2 , y1 , y2 ;
@@ -51,9 +63,9 @@ module.exports = function(Chart) {
5163 }
5264
5365 function collectVisibleGridLines ( chart ) {
54- lines = [ ] ;
66+ var lines = [ ] ;
5567
56- // Temporary object that stores already collected gridLine positions to check and prevent gridLines from overlapping
68+ // Temporary object that stores already collected gridLine positions to prevent gridLines from overlapping
5769 var gridLinesHash = {
5870 horizontal : { } ,
5971 vertical : { }
@@ -62,12 +74,11 @@ module.exports = function(Chart) {
6274 // Marks scale border positions to prevent overlapping of gridLines and scale borders
6375 helpers . each ( chart . scales , function ( scale ) {
6476 var scaleOptions = scale . options ;
77+ var glHashByOrientantion = gridLinesHash [ ! scale . isHorizontal ( ) ? 'horizontal' : 'vertical' ] ;
78+ var borderPosition ;
6579
6680 // gridLines.drawBorder is deprecated
6781 if ( scaleOptions . gridLines . drawBorder && scaleOptions . borderColor !== null && scaleOptions . borderWidth !== 0 && scaleOptions . borderWidth !== null ) {
68- var glHashByOrientantion = gridLinesHash [ ! scale . isHorizontal ( ) ? 'horizontal' : 'vertical' ] ;
69- var borderPosition ;
70-
7182 if ( scale . isHorizontal ( ) ) {
7283 borderPosition = scale . position === 'top' ? scale . bottom : scale . top ;
7384 } else {
@@ -81,19 +92,16 @@ module.exports = function(Chart) {
8192 // Collects gridLines
8293 helpers . each ( chart . scales , function ( scale ) {
8394 var scaleOptions = scale . options ;
95+ var glHashByOrientantion = gridLinesHash [ scale . isHorizontal ( ) ? 'horizontal' : 'vertical' ] ;
96+ var position ;
8497
8598 if ( scaleOptions . display && scaleOptions . gridLines . display && scaleOptions . gridLines . drawOnChartArea ) {
86- var glHashByOrientantion = gridLinesHash [ scale . isHorizontal ( ) ? 'horizontal' : 'vertical' ] ;
87- var position ;
88-
89- for ( var tickIndex = 0 ; tickIndex < scale . ticks . length ; tickIndex ++ ) {
90- var tick = scale . ticks [ tickIndex ] ;
91-
92- if ( tick === null || tick === undefined ) {
99+ for ( var tickIndex = 0 , ticksCount = scale . ticks . length ; tickIndex < ticksCount ; tickIndex ++ ) {
100+ if ( helpers . isNullOrUndef ( scale . ticks [ tickIndex ] ) ) {
93101 continue ;
94102 }
95103
96- position = scale . getPixelForTick ( tickIndex ) ;
104+ position = getLineValue ( scale , tickIndex , scaleOptions . gridLines . offsetGridLines && ticksCount > 1 ) ;
97105
98106 if ( glHashByOrientantion [ position ] === undefined ) {
99107 glHashByOrientantion [ position ] = true ;
@@ -102,7 +110,7 @@ module.exports = function(Chart) {
102110 }
103111
104112 // When offsetGridLines is enabled, there should be one more gridLine than there
105- // are ticks in scale.ticks array, thefore the missing gridLine has to be manually added
113+ // are ticks in scale.ticks array, therefore the missing gridLine has to be manually added
106114 if ( scaleOptions . gridLines . offsetGridLines ) {
107115 position = Math . round ( ! scale . isHorizontal ( ) ? scale . bottom : scale . right ) ;
108116
@@ -113,26 +121,29 @@ module.exports = function(Chart) {
113121 }
114122 }
115123 } ) ;
124+
125+ return lines ;
116126 }
117127
118- function drawGridLines ( chart ) {
119- var context = chart . ctx ;
128+ function drawGridLines ( ctx , lines ) {
129+ var aliasPixel ;
130+ var x1 , x2 , y1 , y2 ;
120131
121- for ( var i = 0 ; i < lines . length ; i ++ ) {
132+ for ( var i = 0 , ilen = lines . length ; i < ilen ; i ++ ) {
122133 var line = lines [ i ] ;
123134
124- context . lineWidth = line . width ;
125- context . strokeStyle = line . color ;
126- if ( context . setLineDash ) {
127- context . setLineDash ( line . borderDash ) ;
128- context . lineDashOffset = line . borderDashOffset ;
135+ ctx . lineWidth = line . width ;
136+ ctx . strokeStyle = line . color ;
137+ if ( ctx . setLineDash ) {
138+ ctx . setLineDash ( line . borderDash ) ;
139+ ctx . lineDashOffset = line . borderDashOffset ;
129140 }
130141
131- var aliasPixel = helpers . aliasPixel ( context . lineWidth ) ;
132- var x1 = line . x1 ;
133- var x2 = line . x2 ;
134- var y1 = line . y1 ;
135- var y2 = line . y2 ;
142+ aliasPixel = helpers . aliasPixel ( ctx . lineWidth ) ;
143+ x1 = line . x1 ;
144+ x2 = line . x2 ;
145+ y1 = line . y1 ;
146+ y2 = line . y2 ;
136147
137148 if ( y1 === y2 ) {
138149 y1 += aliasPixel ;
@@ -142,25 +153,30 @@ module.exports = function(Chart) {
142153 x2 += aliasPixel ;
143154 }
144155
145- context . beginPath ( ) ;
156+ ctx . beginPath ( ) ;
146157
147- context . moveTo ( x1 , y1 ) ;
148- context . lineTo ( x2 , y2 ) ;
158+ ctx . moveTo ( x1 , y1 ) ;
159+ ctx . lineTo ( x2 , y2 ) ;
149160
150- context . stroke ( ) ;
151- context . restore ( ) ;
161+ ctx . stroke ( ) ;
162+ ctx . restore ( ) ;
152163 }
153164 }
154165
155166 return {
156167 id : 'gridlines' ,
157168
158169 afterUpdate : function ( chart ) {
159- collectVisibleGridLines ( chart ) ;
170+ chart [ MODEL_KEY ] = {
171+ lines : collectVisibleGridLines ( chart )
172+ } ;
160173 } ,
161174
162175 beforeDraw : function ( chart ) {
163- drawGridLines ( chart ) ;
176+ var model = chart [ MODEL_KEY ] ;
177+ if ( model ) {
178+ drawGridLines ( chart . ctx , model . lines ) ;
179+ }
164180 }
165181 } ;
166182} ;
0 commit comments