-
Notifications
You must be signed in to change notification settings - Fork 715
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[shape][scale] add <BarGroup />, add tickFormat to scaleBand
- Loading branch information
Showing
8 changed files
with
280 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import React from 'react'; | ||
import { BarGroup } from '@vx/shape'; | ||
import { Group } from '@vx/group'; | ||
import { AxisBottom } from '@vx/axis'; | ||
import { cityTemperature } from '@vx/mock-data'; | ||
import { scaleBand, scaleLinear, scaleOrdinal } from '@vx/scale'; | ||
import { timeParse, timeFormat } from 'd3-time-format'; | ||
import { extent, max } from 'd3-array'; | ||
|
||
const data = cityTemperature.slice(0, 8); | ||
const keys = Object.keys(data[0]).filter(d => d !== 'date'); | ||
const parseDate = timeParse("%Y%m%d"); | ||
const format = timeFormat("%b %d"); | ||
const formatDate = (date) => format(parseDate(date)); | ||
|
||
// accessors | ||
const x0 = d => d.date; | ||
const y = d => d.value; | ||
|
||
export default ({ | ||
width, | ||
height, | ||
margin = { | ||
top: 40 | ||
} | ||
}) => { | ||
if (width < 10) return null; | ||
|
||
// bounds | ||
const xMax = width; | ||
const yMax = height - margin.top - 100; | ||
|
||
// // scales | ||
const x0Scale = scaleBand({ | ||
rangeRound: [0, xMax], | ||
domain: data.map(x0), | ||
padding: 0.2, | ||
tickFormat: () => (val) => formatDate(val) | ||
}); | ||
const x1Scale = scaleBand({ | ||
rangeRound: [0, x0Scale.bandwidth()], | ||
domain: keys, | ||
padding: .1 | ||
}); | ||
const yScale = scaleLinear({ | ||
rangeRound: [yMax, 0], | ||
domain: [0, max(data, (d) => { | ||
return max(keys, (key) => d[key]) | ||
})], | ||
}); | ||
const zScale = scaleOrdinal({ | ||
domain: keys, | ||
range: ['#aeeef8', '#e5fd3d', '#9caff6'] | ||
}) | ||
|
||
return ( | ||
<svg width={width} height={height}> | ||
<rect | ||
x={0} | ||
y={0} | ||
width={width} | ||
height={height} | ||
fill={`#612efb`} | ||
rx={14} | ||
/> | ||
<BarGroup | ||
top={margin.top} | ||
data={data} | ||
keys={keys} | ||
height={yMax} | ||
x0={x0} | ||
y={y} | ||
x0Scale={x0Scale} | ||
x1Scale={x1Scale} | ||
yScale={yScale} | ||
zScale={zScale} | ||
/> | ||
<AxisBottom | ||
scale={x0Scale} | ||
top={yMax + margin.top} | ||
stroke='#e5fd3d' | ||
tickStroke='#e5fd3d' | ||
hideAxisLine | ||
tickLabelComponent={( | ||
<text | ||
fill='#e5fd3d' | ||
fontSize={11} | ||
textAnchor="middle" | ||
/> | ||
)} | ||
/> | ||
</svg> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import React from 'react'; | ||
import Show from '../components/show'; | ||
import BarGroup from '../components/tiles/bargroup'; | ||
|
||
export default () => { | ||
return ( | ||
<Show margin={{top: 80}} component={BarGroup} title="Bar Group"> | ||
{`import React from 'react'; | ||
import { BarGroup } from '@vx/shape'; | ||
import { Group } from '@vx/group'; | ||
import { AxisBottom } from '@vx/axis'; | ||
import { cityTemperature } from '@vx/mock-data'; | ||
import { scaleBand, scaleLinear, scaleOrdinal } from '@vx/scale'; | ||
import { timeParse, timeFormat } from 'd3-time-format'; | ||
import { extent, max } from 'd3-array'; | ||
const data = cityTemperature.slice(0, 4); | ||
const keys = Object.keys(data[0]).filter(d => d !== 'date'); | ||
const parseDate = timeParse("%Y%m%d"); | ||
const format = timeFormat("%b %d"); | ||
const formatDate = (date) => format(parseDate(date)); | ||
// accessors | ||
const x0 = d => d.date; | ||
const y = d => d.value; | ||
export default ({ | ||
width, | ||
height, | ||
margin = { | ||
top: 40 | ||
} | ||
}) => { | ||
if (width < 10) return null; | ||
// bounds | ||
const xMax = width; | ||
const yMax = height - margin.top - 100; | ||
// // scales | ||
const x0Scale = scaleBand({ | ||
rangeRound: [0, xMax], | ||
domain: data.map(x0), | ||
padding: 0.2, | ||
tickFormat: () => (val) => formatDate(val) | ||
}); | ||
const x1Scale = scaleBand({ | ||
rangeRound: [0, x0Scale.bandwidth()], | ||
domain: keys, | ||
padding: .1 | ||
}); | ||
const yScale = scaleLinear({ | ||
rangeRound: [yMax, 0], | ||
domain: [0, max(data, (d) => { | ||
return max(keys, (key) => d[key]) | ||
})], | ||
}); | ||
const zScale = scaleOrdinal({ | ||
domain: keys, | ||
range: ['#aeeef8', '#e5fd3d', '#9caff6'] | ||
}) | ||
return ( | ||
<svg width={width} height={height}> | ||
<rect | ||
x={0} | ||
y={0} | ||
width={width} | ||
height={height} | ||
fill='#612efb' | ||
rx={14} | ||
/> | ||
<BarGroup | ||
top={margin.top} | ||
data={data} | ||
keys={keys} | ||
height={yMax} | ||
x0={x0} | ||
y={y} | ||
x0Scale={x0Scale} | ||
x1Scale={x1Scale} | ||
yScale={yScale} | ||
zScale={zScale} | ||
/> | ||
<AxisBottom | ||
scale={x0Scale} | ||
top={yMax + margin.top} | ||
stroke='#e5fd3d' | ||
tickStroke='#e5fd3d' | ||
hideAxisLine | ||
tickLabelComponent={( | ||
<text | ||
fill='#e5fd3d' | ||
fontSize={11} | ||
textAnchor="middle" | ||
/> | ||
)} | ||
/> | ||
</svg> | ||
); | ||
}`} | ||
</Show> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters