Skip to content

Commit

Permalink
Merge 0818538 into ac7f39f
Browse files Browse the repository at this point in the history
  • Loading branch information
williaster committed Sep 14, 2020
2 parents ac7f39f + 0818538 commit 9fb48bb
Show file tree
Hide file tree
Showing 29 changed files with 725 additions and 137 deletions.
2 changes: 1 addition & 1 deletion packages/vx-axis/src/types.ts
Expand Up @@ -68,7 +68,7 @@ export type CommonProps<Scale extends AxisScale> = {
/** The color for the stroke of the lines. */
stroke?: string;
/** The pixel value for the width of the lines. */
strokeWidth?: number;
strokeWidth?: number | string;
/** The pattern of dashes in the stroke. */
strokeDasharray?: string;
/** The class name applied to each tick group. */
Expand Down
55 changes: 0 additions & 55 deletions packages/vx-demo/src/sandboxes/vx-xychart/Controls.tsx

This file was deleted.

Expand Up @@ -6,7 +6,6 @@ const patternId = 'xy-chart-pattern';

export default function CustomChartBackground() {
const { theme, margin, width, height } = useContext(DataContext);
const textStyles = { ...theme?.axisStyles.x.bottom.axisLabel, textAnchor: 'start' };

// early return values not available in context
if (width == null || height == null || margin == null || theme == null) return null;
Expand All @@ -15,8 +14,8 @@ export default function CustomChartBackground() {
<>
<PatternLines
id={patternId}
width={10}
height={10}
width={16}
height={16}
orientation={['diagonal']}
stroke={theme?.gridStyles?.stroke}
strokeWidth={1}
Expand All @@ -28,13 +27,8 @@ export default function CustomChartBackground() {
width={width - margin.left - margin.right}
height={height - margin.top - margin.bottom}
fill={`url(#${patternId})`}
fillOpacity={0.3}
/>
<text x={margin.left} y={height - margin.top + margin.bottom / 2} {...textStyles}>
width {width}px
</text>
<g transform={`translate(${margin.left / 1.3}, ${height - margin.bottom})rotate(-90)`}>
<text {...textStyles}>height {height}px</text>
</g>
</>
);
}
69 changes: 50 additions & 19 deletions packages/vx-demo/src/sandboxes/vx-xychart/Example.tsx
@@ -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 packages/vx-demo/src/sandboxes/vx-xychart/ExampleControls.tsx
@@ -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>
&nbsp;&nbsp;&nbsp;&nbsp;
<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>
</>
);
}
1 change: 1 addition & 0 deletions packages/vx-demo/src/sandboxes/vx-xychart/package.json
Expand Up @@ -9,6 +9,7 @@
"@types/react-dom": "^16",
"@vx/mock-data": "latest",
"@vx/pattern": "latest",
"@vx/react-spring": "latest",
"@vx/responsive": "latest",
"@vx/xychart": "latest",
"react": "^16",
Expand Down
3 changes: 2 additions & 1 deletion packages/vx-grid/src/types.ts
@@ -1,4 +1,5 @@
import { D3Scale, NumberLike } from '@vx/scale';
import { CSSProperties } from 'react';

// In order to plot values on an axis, output of the scale must be number.
// Some scales return undefined.
Expand Down Expand Up @@ -29,7 +30,7 @@ export type CommonGridProps = {
/** Approximate number of grid lines. Approximate due to d3 alogrithm, specify `tickValues` for precise control. */
numTicks?: number;
/** Styles to apply as grid line style. */
lineStyle?: React.CSSProperties;
lineStyle?: CSSProperties;
/** Pixel offset to apply as a translation (y- for Rows, x- for Columns) to each grid lines. */
offset?: number;
};

0 comments on commit 9fb48bb

Please sign in to comment.