Skip to content
This repository has been archived by the owner on Sep 17, 2021. It is now read-only.

Commit

Permalink
Fix #28 - rounding floats to fix abnormal axis display issues (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
marzolfb authored Feb 22, 2017
1 parent bbafba9 commit d3c95b2
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/Axis.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,23 @@ class AxisStruct {
magMsd = 10.0
else if (magMsd > 2.0)
magMsd = 5.0
else if (magMsd > 1.0)
magMsd = 2.0
else if (magMsd > 1.0)
magMsd = 2.0

return magMsd * magPow
}

static getTickValues(axis, tickCount) {
static roundFloat(floatVal, decimalPlaces) {
return Math.round(parseFloat((floatVal * Math.pow(10, decimalPlaces)).toFixed(decimalPlaces))) / Math.pow(10, decimalPlaces)
}

static getTickValues(axis, tickCount, decimalPlaces) {
const tickStep = AxisStruct.calcStepSize((axis.maxValue - axis.minValue),tickCount)
return _.range(axis.minValue, axis.maxValue + 1,tickStep)
let tickValues = _.range(axis.minValue, axis.maxValue + 1,tickStep)
tickValues = tickValues.map(tickValue => {
return AxisStruct.roundFloat(tickValue, decimalPlaces)
})
return tickValues
}

axis() {
Expand All @@ -61,7 +69,8 @@ class AxisStruct {
const yAxis = this.chartArea.y
const currentAxis = horizontal?xAxis:yAxis
const tickInterval = this.options.tickCount || 10
const ticks = this.options.tickValues !== undefined && this.options.tickValues.length !== 0? _.map(this.options.tickValues,function(v){return v.value }):AxisStruct.getTickValues(currentAxis, tickInterval)
const decimalPlaces = this.options.decimalPlaces || 2
const ticks = this.options.tickValues !== undefined && this.options.tickValues.length !== 0? _.map(this.options.tickValues,function(v){return v.value }):AxisStruct.getTickValues(currentAxis, tickInterval, decimalPlaces)
const fixed = this.options.zeroAxis?this.scale(0):horizontal?yAxis.min:xAxis.min
const start = {x: horizontal?xAxis.min:fixed, y: horizontal?fixed:yAxis.min}
const end = {x:horizontal?xAxis.max:fixed,y: horizontal?fixed:yAxis.max}
Expand Down

0 comments on commit d3c95b2

Please sign in to comment.