Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix scaleBand equality (updated) #481

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/x-axis.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ class XAxis extends PureComponent {
width: 0,
height: 0,
}
_isScaleBand(scale) {
return scale.name === 'band'
}

_onLayout(event) {
const {
Expand Down Expand Up @@ -37,7 +40,7 @@ class XAxis extends PureComponent {
.domain(domain)
.range([left, width - right])

if (scale === d3Scale.scaleBand) {
if (this._isScaleBand(scale)) {
x.paddingInner([spacingInner]).paddingOuter([spacingOuter])

//add half a bar to center label
Expand All @@ -58,7 +61,7 @@ class XAxis extends PureComponent {

const values = data.map((item, index) => xAccessor({ item, index }))
const extent = array.extent(values)
const domain = scale === d3Scale.scaleBand ? values : [min || extent[0], max || extent[1]]
const domain = this._isScaleBand(scale) ? values : [min || extent[0], max || extent[1]]

const x = this._getX(domain)
const ticks = numberOfTicks ? x.ticks(numberOfTicks) : values
Expand Down
23 changes: 9 additions & 14 deletions src/y-axis.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ class YAxis extends PureComponent {
width: 0,
}

_isScaleBand(scale) {
return scale.name === 'band'
}

_onLayout(event) {
const {
nativeEvent: {
Expand All @@ -34,7 +38,7 @@ class YAxis extends PureComponent {
.domain(domain)
.range([height - bottom, top])

if (scale === d3Scale.scaleBand) {
if (this._isScaleBand(scale)) {
// use index as domain identifier instead of value since
// same value can occur at several places in dataPoints
y
Expand Down Expand Up @@ -65,12 +69,12 @@ class YAxis extends PureComponent {

const { min = extent[0], max = extent[1] } = this.props

const domain = scale === d3Scale.scaleBand ? values : [min, max]
const domain = this._isScaleBand(scale) ? values : [min, max]

//invert range to support svg coordinate system
const y = this.getY(domain)

const ticks = scale === d3Scale.scaleBand ? values : y.ticks(numberOfTicks)
const ticks = this._isScaleBand(scale) ? values : y.ticks(numberOfTicks)

const longestValue = ticks
.map((value, index) => formatLabel(value, index))
Expand All @@ -88,16 +92,7 @@ class YAxis extends PureComponent {
<View style={[style]}>
<View style={{ flexGrow: 1 }} onLayout={(event) => this._onLayout(event)}>
{/*invisible text to allow for parent resizing*/}
<Text
style={{
opacity: 0,
fontSize: svg.fontSize,
fontFamily: svg.fontFamily,
fontWeight: svg.fontWeight,
}}
>
{longestValue}
</Text>
<Text style={{ opacity: 0, fontSize: svg.fontSize }}>{longestValue}</Text>
Comment on lines -91 to +95
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why were these changed? I'm pretty sure fontFamily and fontWeight affects the size.

{height > 0 && width > 0 && (
<Svg
style={{
Expand All @@ -123,7 +118,7 @@ class YAxis extends PureComponent {
x={'50%'}
alignmentBaseline={'middle'}
{...svg}
key={y(value)}
key={index}
y={y(value)}
>
{formatLabel(value, index, ticks.length)}
Expand Down