@@ -290,14 +290,17 @@ window.Chart = function(context){
290
290
scaleSteps : null ,
291
291
scaleStepWidth : null ,
292
292
scaleStartValue : null ,
293
+ scaleShowStartValue : false ,
293
294
scaleLineColor : "rgba(0,0,0,.1)" ,
294
295
scaleLineWidth : 1 ,
295
296
scaleShowLabels : true ,
296
297
scaleLabel : "<%=value%>" ,
298
+ scaleLabelPosition : 'left' ,
297
299
scaleFontFamily : "'Arial'" ,
298
300
scaleFontSize : 12 ,
299
301
scaleFontStyle : "normal" ,
300
302
scaleFontColor : "#666" ,
303
+ labelsFontColor : "#666" ,
301
304
scaleShowGridLines : true ,
302
305
scaleGridLineColor : "rgba(0,0,0,.05)" ,
303
306
scaleGridLineWidth : 1 ,
@@ -313,9 +316,9 @@ window.Chart = function(context){
313
316
animationEasing : "easeOutQuart" ,
314
317
onAnimationComplete : null
315
318
} ;
316
- var config = ( options ) ? mergeChartConfig ( chart . Line . defaults , options ) : chart . Line . defaults ;
319
+ var config = ( options ) ? mergeChartConfig ( chart . Line . defaults , options ) : chart . Line . defaults ;
317
320
318
- return new Line ( data , config , context ) ;
321
+ return new Line ( data , config , context ) ;
319
322
}
320
323
321
324
this . Bar = function ( data , options ) {
@@ -789,8 +792,8 @@ window.Chart = function(context){
789
792
790
793
}
791
794
792
- var Line = function ( data , config , ctx ) {
793
- var maxSize , scaleHop , calculatedScale , labelHeight , scaleHeight , valueBounds , labelTemplateString , valueHop , widestXLabel , xAxisLength , yAxisPosX , xAxisPosY , rotateLabels = 0 ;
795
+ var Line = function ( data , config , ctx ) {
796
+ var maxSize , scaleHop , calculatedScale , labelHeight , scaleHeight , valueBounds , labelTemplateString , valueHop , widestXLabel , widestYLabel , xAxisLength , yAxisPosX , xAxisPosY , rotateLabels = 0 ;
794
797
795
798
calculateDrawingSizes ( ) ;
796
799
@@ -808,7 +811,7 @@ window.Chart = function(context){
808
811
graphMin : config . scaleStartValue ,
809
812
labels : [ ]
810
813
}
811
- populateLabels ( labelTemplateString , calculatedScale . labels , calculatedScale . steps , config . scaleStartValue , config . scaleStepWidth ) ;
814
+ populateLabels ( labelTemplateString , calculatedScale . labels , calculatedScale . steps , config . scaleStartValue , config . scaleStepWidth , config . scaleShowStartValue ) ;
812
815
}
813
816
814
817
scaleHop = Math . floor ( scaleHeight / calculatedScale . steps ) ;
@@ -879,7 +882,7 @@ window.Chart = function(context){
879
882
else {
880
883
ctx . textAlign = "center" ;
881
884
}
882
- ctx . fillStyle = config . scaleFontColor ;
885
+ ctx . fillStyle = config . labelsFontColor ;
883
886
for ( var i = 0 ; i < data . labels . length ; i ++ ) {
884
887
ctx . save ( ) ;
885
888
if ( rotateLabels > 0 ) {
@@ -897,7 +900,7 @@ window.Chart = function(context){
897
900
ctx . moveTo ( yAxisPosX + i * valueHop , xAxisPosY + 3 ) ;
898
901
899
902
//Check i isnt 0, so we dont go over the Y axis twice.
900
- if ( config . scaleShowGridLines && i > 0 ) {
903
+ if ( ( config . scaleShowGridLines == 'vertical' || config . scaleShowGridLines === true ) && i > 0 ) {
901
904
ctx . lineWidth = config . scaleGridLineWidth ;
902
905
ctx . strokeStyle = config . scaleGridLineColor ;
903
906
ctx . lineTo ( yAxisPosX + i * valueHop , 5 ) ;
@@ -911,29 +914,38 @@ window.Chart = function(context){
911
914
//Y axis
912
915
ctx . lineWidth = config . scaleLineWidth ;
913
916
ctx . strokeStyle = config . scaleLineColor ;
917
+ ctx . fillStyle = config . scaleFontColor ;
914
918
ctx . beginPath ( ) ;
915
919
ctx . moveTo ( yAxisPosX , xAxisPosY + 5 ) ;
916
920
ctx . lineTo ( yAxisPosX , 5 ) ;
917
921
ctx . stroke ( ) ;
918
922
919
- ctx . textAlign = "right" ;
923
+ ctx . textAlign = config . scaleLabelPosition == "right" ? 'left' : 'right' ;
920
924
ctx . textBaseline = "middle" ;
921
- for ( var j = 0 ; j < calculatedScale . steps ; j ++ ) {
925
+
926
+ var k = calculatedScale . steps ;
927
+ if ( config . scaleShowStartValue ) {
928
+ ++ k ;
929
+ }
930
+ for ( var j = + ! config . scaleShowStartValue ; j < k ; j ++ ) {
922
931
ctx . beginPath ( ) ;
923
- ctx . moveTo ( yAxisPosX - 3 , xAxisPosY - ( ( j + 1 ) * scaleHop ) ) ;
924
- if ( config . scaleShowGridLines ) {
932
+ ctx . moveTo ( yAxisPosX - 3 , xAxisPosY - ( j * scaleHop ) ) ;
933
+ if ( config . scaleShowGridLines == 'horizontal' || config . scaleShowGridLines === true ) {
925
934
ctx . lineWidth = config . scaleGridLineWidth ;
926
935
ctx . strokeStyle = config . scaleGridLineColor ;
927
- ctx . lineTo ( yAxisPosX + xAxisLength + 5 , xAxisPosY - ( ( j + 1 ) * scaleHop ) ) ;
928
- }
929
- else {
930
- ctx . lineTo ( yAxisPosX - 0.5 , xAxisPosY - ( ( j + 1 ) * scaleHop ) ) ;
936
+ ctx . lineTo ( yAxisPosX + xAxisLength + 5 , xAxisPosY - ( j * scaleHop ) ) ;
937
+ } else {
938
+ ctx . lineTo ( yAxisPosX - 0.5 , xAxisPosY - ( j * scaleHop ) ) ;
931
939
}
932
940
933
941
ctx . stroke ( ) ;
934
942
935
- if ( config . scaleShowLabels ) {
936
- ctx . fillText ( calculatedScale . labels [ j ] , yAxisPosX - 8 , xAxisPosY - ( ( j + 1 ) * scaleHop ) ) ;
943
+ if ( config . scaleShowLabels ) {
944
+ ctx . fillText (
945
+ calculatedScale . labels [ j ] ,
946
+ yAxisPosX - 8 + ( config . scaleLabelPosition == 'right' ? xAxisLength + 20 : 0 ) ,
947
+ xAxisPosY - ( j * scaleHop )
948
+ ) ;
937
949
}
938
950
}
939
951
@@ -951,10 +963,11 @@ window.Chart = function(context){
951
963
//Add a little extra padding from the y axis
952
964
longestText += 10 ;
953
965
}
966
+ widestYLabel = longestText ;
954
967
xAxisLength = width - longestText - widestXLabel ;
955
968
valueHop = Math . floor ( xAxisLength / ( data . labels . length - 1 ) ) ;
956
969
957
- yAxisPosX = width - widestXLabel / 2 - xAxisLength ;
970
+ yAxisPosX = width - widestXLabel / 2 - xAxisLength - ( config . scaleLabelPosition == "right" ? longestText : 0 ) ;
958
971
xAxisPosY = scaleHeight + config . scaleFontSize / 2 ;
959
972
}
960
973
function calculateDrawingSizes ( ) {
@@ -1332,11 +1345,19 @@ window.Chart = function(context){
1332
1345
}
1333
1346
1334
1347
//Populate an array of all the labels by interpolating the string.
1335
- function populateLabels ( labelTemplateString , labels , numberOfSteps , graphMin , stepValue ) {
1336
- if ( labelTemplateString ) {
1348
+ function populateLabels ( labelTemplate , labels , numberOfSteps , graphMin , stepValue , showStartValue ) {
1349
+ if ( labelTemplate ) {
1350
+ var labelFn = (
1351
+ typeof labelTemplate == 'function'
1352
+ ? function ( labelTemplate , options ) {
1353
+ return labelTemplate ( options . value ) ;
1354
+ }
1355
+ : tmpl
1356
+ ) ;
1357
+
1337
1358
//Fix floating point errors by setting to fixed the on the same decimal as the stepValue.
1338
- for ( var i = 1 ; i < numberOfSteps + 1 ; i ++ ) {
1339
- labels . push ( tmpl ( labelTemplateString , { value : ( graphMin + ( stepValue * i ) ) . toFixed ( getDecimalPlaces ( stepValue ) ) } ) ) ;
1359
+ for ( var i = + ! showStartValue ; i < numberOfSteps + 1 ; i ++ ) {
1360
+ labels . push ( labelFn ( labelTemplate , { value : ( graphMin + ( stepValue * i ) ) . toFixed ( getDecimalPlaces ( stepValue ) ) } ) ) ;
1340
1361
}
1341
1362
}
1342
1363
}
0 commit comments