Skip to content

Commit

Permalink
opt: opt yAxis.type: 'log' display
Browse files Browse the repository at this point in the history
  • Loading branch information
liihuu committed Sep 9, 2022
1 parent 5459249 commit 936db31
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
12 changes: 9 additions & 3 deletions src/component/axis/Axis.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ export default class Axis {
this._minValue = 0
this._maxValue = 0
this._range = 0
this._realMinValue = 0
this._realMaxValue = 0
this._realRange = 0
this._ticks = []
this._initMeasureCanvas()
}
Expand Down Expand Up @@ -78,6 +81,9 @@ export default class Axis {
this._minValue = minMax.min
this._maxValue = minMax.max
this._range = minMax.range
this._realMinValue = minMax.realMin
this._realMaxValue = minMax.realMax
this._realRange = minMax.realRange
if (this._cacheMinValue !== minMax.min || this._cacheMaxValue !== minMax.max || forceCompute) {
this._cacheMinValue = minMax.min
this._cacheMaxValue = minMax.max
Expand Down Expand Up @@ -106,11 +112,11 @@ export default class Axis {
_computeTicks () {
const ticks = []
if (this._range >= 0) {
const intervalPrecision = this._computeInterval(this._range)
const intervalPrecision = this._computeInterval(this._realRange)
const interval = intervalPrecision.interval
const precision = intervalPrecision.precision
const first = round(Math.ceil(this._minValue / interval) * interval, precision)
const last = round(Math.floor(this._maxValue / interval) * interval, precision)
const first = round(Math.ceil(this._realMinValue / interval) * interval, precision)
const last = round(Math.floor(this._realMaxValue / interval) * interval, precision)
let n = 0
let f = first

Expand Down
5 changes: 2 additions & 3 deletions src/component/axis/XAxis.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ export default class XAxis extends Axis {
}

_optimalMinMax ({ min, max }) {
const range = max - min + 1
return {
min,
max,
range: max - min + 1
min, max, range, realMin: min, realMax: max, realRange: range
}
}

Expand Down
29 changes: 16 additions & 13 deletions src/component/axis/YAxis.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ import { CandleType, YAxisPosition, YAxisType } from '../../options/styleOptions
import { isNumber, isValid } from '../../utils/typeChecks'
import { calcTextWidth, createFont } from '../../utils/canvas'
import { formatBigNumber, formatPrecision } from '../../utils/format'
import { round, log10, index10 } from '../../utils/number'
import { log10, index10 } from '../../utils/number'

export default class YAxis extends Axis {
constructor (chartStore, isCandleYAxis, paneId) {
super(chartStore)
this._realRange = 0
this._isCandleYAxis = isCandleYAxis
this._paneId = paneId
}
Expand Down Expand Up @@ -186,17 +185,26 @@ export default class YAxis extends Axis {
// 保证每次图形绘制上下都留间隙
minValue = minValue - range * bottomRate
maxValue = maxValue + range * topRate

range = Math.abs(maxValue - minValue)
let realMinValue
let realMaxValue
let realRange
if (yAxisType === YAxisType.LOG) {
this._realRange = Math.abs(index10(maxValue) - index10(minValue))
realMinValue = index10(minValue)
realMaxValue = index10(maxValue)
realRange = Math.abs(realMaxValue - realMinValue)
} else {
this._realRange = range
realMinValue = minValue
realMaxValue = maxValue
realRange = range
}
return {
min: minValue,
max: maxValue,
range
range,
realMin: realMinValue,
realMax: realMaxValue,
realRange
}
}

Expand All @@ -217,10 +225,6 @@ export default class YAxis extends Axis {
})
}
const textHeight = this._chartStore.styleOptions().xAxis.tickText.size
let intervalPrecision
if (yAxisType === YAxisType.LOG) {
intervalPrecision = this._computeInterval(this._realRange)
}
let validY
ticks.forEach(({ v }) => {
let value
Expand All @@ -231,9 +235,8 @@ export default class YAxis extends Axis {
break
}
case YAxisType.LOG: {
value = round(index10(v), intervalPrecision.precision)
y = this._innerConvertToPixel(log10(value))
value = formatPrecision(value, precision)
y = this._innerConvertToPixel(log10(v))
value = formatPrecision(v, precision)
break
}
default: {
Expand Down

0 comments on commit 936db31

Please sign in to comment.