Skip to content

Commit 7a55a16

Browse files
committed
0.2 Update
- Fixes hard coded scales now longer not one step out of sync
1 parent 75f4d21 commit 7a55a16

File tree

2 files changed

+53
-71
lines changed

2 files changed

+53
-71
lines changed

Chart.js

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -376,11 +376,7 @@ var Chart = function(context){
376376
graphMin : config.scaleStartValue,
377377
labels : []
378378
}
379-
for (var i=0; i<calculatedScale.steps; i++){
380-
if(labelTemplateString){
381-
calculatedScale.labels.push(tmpl(labelTemplateString,{value:(config.scaleStartValue + (config.scaleStepWidth * i)).toFixed(getDecimalPlaces (config.scaleStepWidth))}));
382-
}
383-
}
379+
populateLabels(labelTemplateString, calculatedScale.labels,calculatedScale.steps,config.scaleStartValue,config.scaleStepWidth);
384380
}
385381

386382
scaleHop = maxSize/(calculatedScale.steps);
@@ -516,11 +512,7 @@ var Chart = function(context){
516512
graphMin : config.scaleStartValue,
517513
labels : []
518514
}
519-
for (var i=0; i<calculatedScale.steps; i++){
520-
if(labelTemplateString){
521-
calculatedScale.labels.push(tmpl(labelTemplateString,{value:(config.scaleStartValue + (config.scaleStepWidth * i)).toFixed(getDecimalPlaces (config.scaleStepWidth))}));
522-
}
523-
}
515+
populateLabels(labelTemplateString, calculatedScale.labels,calculatedScale.steps,config.scaleStartValue,config.scaleStepWidth);
524516
}
525517

526518
scaleHop = maxSize/(calculatedScale.steps);
@@ -813,11 +805,7 @@ var Chart = function(context){
813805
graphMin : config.scaleStartValue,
814806
labels : []
815807
}
816-
for (var i=0; i<calculatedScale.steps; i++){
817-
if(labelTemplateString){
818-
calculatedScale.labels.push(tmpl(labelTemplateString,{value:(config.scaleStartValue + (config.scaleStepWidth * i)).toFixed(getDecimalPlaces (config.scaleStepWidth))}));
819-
}
820-
}
808+
populateLabels(labelTemplateString, calculatedScale.labels,calculatedScale.steps,config.scaleStartValue,config.scaleStepWidth);
821809
}
822810

823811
scaleHop = Math.floor(scaleHeight/calculatedScale.steps);
@@ -1049,11 +1037,7 @@ var Chart = function(context){
10491037
graphMin : config.scaleStartValue,
10501038
labels : []
10511039
}
1052-
for (var i=0; i<calculatedScale.steps; i++){
1053-
if(labelTemplateString){
1054-
calculatedScale.labels.push(tmpl(labelTemplateString,{value:(config.scaleStartValue + (config.scaleStepWidth * i)).toFixed(getDecimalPlaces (config.scaleStepWidth))}));
1055-
}
1056-
}
1040+
populateLabels(labelTemplateString, calculatedScale.labels,calculatedScale.steps,config.scaleStartValue,config.scaleStepWidth);
10571041
}
10581042

10591043
scaleHop = Math.floor(scaleHeight/calculatedScale.steps);
@@ -1324,19 +1308,9 @@ var Chart = function(context){
13241308
numberOfSteps = Math.round(graphRange/stepValue);
13251309
}
13261310
};
1327-
13281311

1329-
1330-
//Create an array of all the labels by interpolating the string.
1331-
13321312
var labels = [];
1333-
1334-
if(labelTemplateString){
1335-
//Fix floating point errors by setting to fixed the on the same decimal as the stepValue.
1336-
for (var i=1; i<numberOfSteps+1; i++){
1337-
labels.push(tmpl(labelTemplateString,{value:(graphMin + (stepValue*i)).toFixed(getDecimalPlaces (stepValue))}));
1338-
}
1339-
}
1313+
populateLabels(labelTemplateString, labels, numberOfSteps, graphMin, stepValue);
13401314

13411315
return {
13421316
steps : numberOfSteps,
@@ -1352,6 +1326,16 @@ var Chart = function(context){
13521326

13531327

13541328
}
1329+
1330+
//Populate an array of all the labels by interpolating the string.
1331+
function populateLabels(labelTemplateString, labels, numberOfSteps, graphMin, stepValue) {
1332+
if (labelTemplateString) {
1333+
//Fix floating point errors by setting to fixed the on the same decimal as the stepValue.
1334+
for (var i = 1; i < numberOfSteps + 1; i++) {
1335+
labels.push(tmpl(labelTemplateString, {value: (graphMin + (stepValue * i)).toFixed(getDecimalPlaces(stepValue))}));
1336+
}
1337+
}
1338+
}
13551339

13561340
//Max value from array
13571341
function Max( array ){
@@ -1440,4 +1424,3 @@ var Chart = function(context){
14401424
}
14411425

14421426

1443-

0 commit comments

Comments
 (0)