forked from airbnb/visx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request airbnb#797 from hshoff/chris--xychart-axis-grid-demo
new(demo/xychart): add Axis + Grid to demo
- Loading branch information
Showing
5 changed files
with
247 additions
and
83 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,64 @@ | ||
import React, { useState } from 'react'; | ||
import React from 'react'; | ||
import cityTemperature, { CityTemperature } from '@vx/mock-data/lib/mocks/cityTemperature'; | ||
import { XYChart, LineSeries, DataProvider, darkTheme, XYChartTheme } from '@vx/xychart'; | ||
|
||
import Controls from './Controls'; | ||
import { AnimatedAxis, AnimatedGrid, DataProvider, LineSeries, XYChart } from '@vx/xychart'; | ||
import ExampleControls from './ExampleControls'; | ||
import CustomChartBackground from './CustomChartBackground'; | ||
|
||
type Props = { | ||
width: number; | ||
height: number; | ||
}; | ||
|
||
const xScaleConfig = { type: 'linear' } as const; | ||
const yScaleConfig = xScaleConfig; | ||
const data = cityTemperature.slice(50, 80); | ||
const xScaleConfig = { type: 'time' } as const; | ||
const yScaleConfig = { type: 'linear' } as const; | ||
const numTicks = 4; | ||
const data = cityTemperature.slice(0, 100); | ||
const getDate = (d: CityTemperature) => new Date(d.date); | ||
const getSfTemperature = (d: CityTemperature) => Number(d['San Francisco']); | ||
|
||
export default function Example(_: Props) { | ||
const [theme, setTheme] = useState<XYChartTheme>(darkTheme); | ||
|
||
export default function Example({ height }: Props) { | ||
return ( | ||
<> | ||
<DataProvider theme={theme} xScale={xScaleConfig} yScale={yScaleConfig}> | ||
<XYChart height={400}> | ||
<CustomChartBackground /> | ||
<LineSeries dataKey="line" data={data} xAccessor={getDate} yAccessor={getSfTemperature} /> | ||
</XYChart> | ||
</DataProvider> | ||
<Controls theme={theme} setTheme={setTheme} /> | ||
</> | ||
<ExampleControls> | ||
{({ | ||
theme, | ||
xAxisOrientation, | ||
yAxisOrientation, | ||
showGridRows, | ||
showGridColumns, | ||
animationTrajectory, | ||
}) => ( | ||
<DataProvider theme={theme} xScale={xScaleConfig} yScale={yScaleConfig}> | ||
<XYChart height={Math.min(400, height)}> | ||
<CustomChartBackground /> | ||
<AnimatedAxis | ||
key={`xaxis-${animationTrajectory}`} // force animate on update | ||
orientation={xAxisOrientation} | ||
numTicks={numTicks} | ||
animationTrajectory={animationTrajectory} | ||
/> | ||
<AnimatedAxis | ||
key={`yaxis-${animationTrajectory}`} | ||
label="Temperature (°F)" | ||
orientation={yAxisOrientation} | ||
numTicks={numTicks} | ||
animationTrajectory={animationTrajectory} | ||
/> | ||
<AnimatedGrid | ||
key={`grid-${animationTrajectory}`} | ||
rows={showGridRows} | ||
columns={showGridColumns} | ||
animationTrajectory={animationTrajectory} | ||
numTicks={numTicks} | ||
/> | ||
<LineSeries | ||
dataKey="line" | ||
data={data} | ||
xAccessor={getDate} | ||
yAccessor={getSfTemperature} | ||
/> | ||
</XYChart> | ||
</DataProvider> | ||
)} | ||
</ExampleControls> | ||
); | ||
} |
193 changes: 193 additions & 0 deletions
193
packages/vx-demo/src/sandboxes/vx-xychart/ExampleControls.tsx
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,193 @@ | ||
/* eslint-disable jsx-a11y/label-has-associated-control */ | ||
import React, { useState } from 'react'; | ||
import { lightTheme, darkTheme, XYChartTheme } from '@vx/xychart'; | ||
import { AnimationTrajectory } from '@vx/react-spring/lib/types'; | ||
import customTheme from './customTheme'; | ||
|
||
type ProvidedProps = { | ||
animationTrajectory: AnimationTrajectory; | ||
showGridColumns: boolean; | ||
showGridRows: boolean; | ||
theme: XYChartTheme; | ||
xAxisOrientation: 'top' | 'bottom'; | ||
yAxisOrientation: 'left' | 'right'; | ||
}; | ||
|
||
type ControlsProps = { | ||
children: (props: ProvidedProps) => React.ReactNode; | ||
}; | ||
|
||
export default function ExampleControls({ children }: ControlsProps) { | ||
const [theme, setTheme] = useState<XYChartTheme>(darkTheme); | ||
const [animationTrajectory, setAnimationTrajectory] = useState<AnimationTrajectory>('center'); | ||
const [gridProps, setGridProps] = useState<[boolean, boolean]>([true, true]); | ||
const [showGridRows, showGridColumns] = gridProps; | ||
const [xAxisOrientation, setXAxisOrientation] = useState<'top' | 'bottom'>('bottom'); | ||
const [yAxisOrientation, setYAxisOrientation] = useState<'left' | 'right'>('right'); | ||
|
||
return ( | ||
<> | ||
{children({ | ||
animationTrajectory, | ||
showGridColumns, | ||
showGridRows, | ||
theme, | ||
xAxisOrientation, | ||
yAxisOrientation, | ||
})} | ||
<div className="controls"> | ||
{/** theme */} | ||
<div> | ||
<strong>theme</strong> | ||
<label> | ||
<input | ||
type="radio" | ||
onChange={() => setTheme(lightTheme)} | ||
checked={theme === lightTheme} | ||
/>{' '} | ||
light | ||
</label> | ||
<label> | ||
<input | ||
type="radio" | ||
onChange={() => setTheme(darkTheme)} | ||
checked={theme === darkTheme} | ||
/>{' '} | ||
dark | ||
</label> | ||
<label> | ||
<input | ||
type="radio" | ||
onChange={() => setTheme(customTheme)} | ||
checked={theme === customTheme} | ||
/>{' '} | ||
custom | ||
</label> | ||
</div> | ||
|
||
{/** axes */} | ||
<div> | ||
<strong>axes</strong> | ||
<label> | ||
<input | ||
type="radio" | ||
onChange={() => setXAxisOrientation('bottom')} | ||
checked={xAxisOrientation === 'bottom'} | ||
/>{' '} | ||
bottom | ||
</label> | ||
<label> | ||
<input | ||
type="radio" | ||
onChange={() => setXAxisOrientation('top')} | ||
checked={xAxisOrientation === 'top'} | ||
/>{' '} | ||
top | ||
</label> | ||
| ||
<label> | ||
<input | ||
type="radio" | ||
onChange={() => setYAxisOrientation('left')} | ||
checked={yAxisOrientation === 'left'} | ||
/>{' '} | ||
left | ||
</label> | ||
<label> | ||
<input | ||
type="radio" | ||
onChange={() => setYAxisOrientation('right')} | ||
checked={yAxisOrientation === 'right'} | ||
/>{' '} | ||
right | ||
</label> | ||
</div> | ||
|
||
{/** grid */} | ||
<div> | ||
<strong>grid</strong> | ||
<label> | ||
<input | ||
type="radio" | ||
onChange={() => setGridProps([true, false])} | ||
checked={showGridRows && !showGridColumns} | ||
/>{' '} | ||
rows | ||
</label> | ||
<label> | ||
<input | ||
type="radio" | ||
onChange={() => setGridProps([false, true])} | ||
checked={!showGridRows && showGridColumns} | ||
/>{' '} | ||
columns | ||
</label> | ||
<label> | ||
<input | ||
type="radio" | ||
onChange={() => setGridProps([true, true])} | ||
checked={showGridRows && showGridColumns} | ||
/>{' '} | ||
both | ||
</label> | ||
<label> | ||
<input | ||
type="radio" | ||
onChange={() => setGridProps([false, false])} | ||
checked={!showGridRows && !showGridColumns} | ||
/>{' '} | ||
none | ||
</label> | ||
</div> | ||
{/** animation trajectory */} | ||
<div> | ||
<strong>axis + grid animation</strong> | ||
<label> | ||
<input | ||
type="radio" | ||
onChange={() => setAnimationTrajectory('center')} | ||
checked={animationTrajectory === 'center'} | ||
/>{' '} | ||
from center | ||
</label> | ||
<label> | ||
<input | ||
type="radio" | ||
onChange={() => setAnimationTrajectory('outside')} | ||
checked={animationTrajectory === 'outside'} | ||
/>{' '} | ||
from outside | ||
</label> | ||
<label> | ||
<input | ||
type="radio" | ||
onChange={() => setAnimationTrajectory('min')} | ||
checked={animationTrajectory === 'min'} | ||
/>{' '} | ||
from min | ||
</label> | ||
<label> | ||
<input | ||
type="radio" | ||
onChange={() => setAnimationTrajectory('max')} | ||
checked={animationTrajectory === 'max'} | ||
/>{' '} | ||
from max | ||
</label> | ||
</div> | ||
</div> | ||
<style jsx>{` | ||
.controls { | ||
font-size: 13px; | ||
line-height: 1.5em; | ||
} | ||
label { | ||
font-size: 11px; | ||
} | ||
input[type='radio'] { | ||
height: 10px; | ||
} | ||
`}</style> | ||
</> | ||
); | ||
} |
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