Skip to content

Commit

Permalink
Merge pull request #285 from hshoff/harry-threshold
Browse files Browse the repository at this point in the history
[threshold] add @vx/threshold. fixes #245
  • Loading branch information
hshoff authored Apr 30, 2018
2 parents 01b64c6 + 41242f9 commit a5daa00
Show file tree
Hide file tree
Showing 15 changed files with 415 additions and 10 deletions.
30 changes: 29 additions & 1 deletion packages/vx-demo/components/gallery.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import Responsive from '../components/tiles/responsive';
import DragI from '../components/tiles/drag-i';
import DragII from '../components/tiles/drag-ii';
import LinkTypes from '../components/tiles/linkTypes';
import Threshold from '../components/tiles/threshold';

const items = [
'#242424',
Expand Down Expand Up @@ -801,7 +802,34 @@ export default class Gallery extends React.Component {
</div>
</Link>
</Tilt>
<div className="gallery-item placeholder" />
<Tilt className="tilt" options={{ max: 8, scale: 1 }}>
<Link prefetch href="/threshold">
<div className="gallery-item" style={{ background: '#f3f3f3' }}>
<div className="image">
<ParentSize>
{({ width, height }) => (
<Threshold
width={width}
height={height}
margin={{
top: 40,
left: 40,
right: 20,
bottom: 30
}}
/>
)}
</ParentSize>
</div>
<div className="details" style={{ color: '#111' }}>
<div className="title">Threshold</div>
<div className="description">
<pre>{`<Threshold />`}</pre>
</div>
</div>
</div>
</Link>
</Tilt>
<div className="gallery-item placeholder" />
</div>

Expand Down
4 changes: 3 additions & 1 deletion packages/vx-demo/components/show.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export default withScreenSize(
component,
shadow = false,
events = false,
margin = { top: 0, left: 0, right: 0, bottom: 80 }
margin = { top: 0, left: 0, right: 0, bottom: 80 },
description
}) => {
const padding = 40;
let width = screenWidth - padding;
Expand Down Expand Up @@ -45,6 +46,7 @@ export default withScreenSize(
events
})}
</div>
{description && React.createElement(description, { width, height })}
{children && (
<div style={{ width: width }}>
<h2>Code</h2>
Expand Down
101 changes: 101 additions & 0 deletions packages/vx-demo/components/tiles/threshold.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import React from 'react';
import { Group } from '@vx/group';
import { curveBasis } from '@vx/curve';
import { LinePath } from '@vx/shape';
import { Threshold } from '@vx/threshold';
import { scaleTime, scaleLinear } from '@vx/scale';
import { AxisLeft, AxisBottom } from '@vx/axis';
import { GridRows, GridColumns } from '@vx/grid';
import { cityTemperature as data } from '@vx/mock-data';
import { timeFormat, timeParse } from 'd3-time-format';

const parseDate = timeParse('%Y%m%d');

// accessors
const date = d => parseDate(d.date);
const ny = d => d['New York'];
const sf = d => d['San Francisco'];

export default class Thresholds extends React.Component {
render() {
const { width, height, margin, events } = this.props;
if (width < 10) return null;

// bounds
const xMax = width - margin.left - margin.right;
const yMax = height - margin.top - margin.bottom;

// scales
const xScale = scaleTime({
range: [0, xMax],
domain: [Math.min(...data.map(date)), Math.max(...data.map(date))]
});
const yScale = scaleLinear({
range: [yMax, 0],
domain: [
Math.min(...data.map(d => Math.min(ny(d), sf(d)))),
Math.max(...data.map(d => Math.max(ny(d), sf(d))))
],
nice: true
});

return (
<div>
<svg width={width} height={height}>
<rect x={0} y={0} width={width} height={height} fill="#f3f3f3" rx={14} />
<Group left={margin.left} top={margin.top}>
<GridRows scale={yScale} width={xMax} height={yMax} stroke="#e0e0e0" />
<GridColumns scale={xScale} width={xMax} height={yMax} stroke="#e0e0e0" />
<line x1={xMax} x2={xMax} y1={0} y2={yMax} stroke="#e0e0e0" />
<AxisBottom top={yMax} scale={xScale} numTicks={width > 520 ? 10 : 5} />
<AxisLeft scale={yScale} />
<text x="-70" y="15" transform="rotate(-90)" fontSize={10}>
Temperature (°F)
</text>
<Threshold
data={data}
x={date}
y0={ny}
y1={sf}
xScale={xScale}
yScale={yScale}
clipAboveTo={0}
clipBelowTo={yMax}
curve={curveBasis}
belowAreaProps={{
fill: 'red',
fillOpacity: 0.4
}}
aboveAreaProps={{
fill: 'green',
fillOpacity: 0.4
}}
/>
<LinePath
data={data}
curve={curveBasis}
x={date}
y={sf}
xScale={xScale}
yScale={yScale}
stroke="#000"
strokeWidth={1.5}
strokeOpacity={0.8}
strokeDasharray="1,2"
/>
<LinePath
data={data}
curve={curveBasis}
x={date}
y={ny}
xScale={xScale}
yScale={yScale}
stroke="#000"
strokeWidth={1.5}
/>
</Group>
</svg>
</div>
);
}
}
11 changes: 3 additions & 8 deletions packages/vx-demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@
"start": "next start",
"test": "jest"
},
"keywords": [
"vx",
"demo",
"examples"
],
"keywords": ["vx", "demo", "examples"],
"author": "@hshoff",
"license": "MIT",
"dependencies": {
Expand Down Expand Up @@ -43,6 +39,7 @@
"@vx/shape": "0.0.161",
"@vx/stats": "0.0.161",
"@vx/text": "0.0.161",
"@vx/threshold": "0.0.1",
"@vx/tooltip": "0.0.161",
"@vx/voronoi": "0.0.161",
"classnames": "^2.2.5",
Expand All @@ -67,9 +64,7 @@
"topojson-client": "^3.0.0"
},
"browserify": {
"transform": [
"babelify"
]
"transform": ["babelify"]
},
"publishConfig": {
"access": "public"
Expand Down
132 changes: 132 additions & 0 deletions packages/vx-demo/pages/threshold.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import React from 'react';
import Show from '../components/show';
import Threshold from '../components/tiles/threshold';

function Description({ width }) {
return (
<div style={{ width, fontSize: 14 }}>
Based on Mike Bostock's{' '}
<a href="https://bl.ocks.org/mbostock/3894205" target="_blank">
Difference Chart
</a>
</div>
);
}

export default () => {
return (
<Show
component={Threshold}
title="Threshold"
description={Description}
margin={{
top: 40,
left: 40,
right: 20,
bottom: 50
}}
>
{`import React from 'react';
import { Group } from '@vx/group';
import { curveBasis } from '@vx/curve';
import { LinePath } from '@vx/shape';
import { Threshold } from '@vx/threshold';
import { scaleTime, scaleLinear } from '@vx/scale';
import { AxisLeft, AxisBottom } from '@vx/axis';
import { GridRows, GridColumns } from '@vx/grid';
import { cityTemperature as data } from '@vx/mock-data';
import { timeFormat, timeParse } from 'd3-time-format';
const parseDate = timeParse('%Y%m%d');
// accessors
const date = d => parseDate(d.date);
const ny = d => d['New York'];
const sf = d => d['San Francisco'];
export default class Thresholds extends React.Component {
render() {
const { width, height, margin, events } = this.props;
if (width < 10) return null;
// bounds
const xMax = width - margin.left - margin.right;
const yMax = height - margin.top - margin.bottom;
// scales
const xScale = scaleTime({
range: [0, xMax],
domain: [Math.min(...data.map(date)), Math.max(...data.map(date))]
});
const yScale = scaleLinear({
range: [yMax, 0],
domain: [
Math.min(...data.map(d => Math.min(ny(d), sf(d)))),
Math.max(...data.map(d => Math.max(ny(d), sf(d))))
],
nice: true
});
return (
<div>
<svg width={width} height={height}>
<rect x={0} y={0} width={width} height={height} fill="#f3f3f3" rx={14} />
<Group left={margin.left} top={margin.top}>
<GridRows scale={yScale} width={xMax} height={yMax} stroke="#e0e0e0" />
<GridColumns scale={xScale} width={xMax} height={yMax} stroke="#e0e0e0" />
<line x1={xMax} x2={xMax} y1={0} y2={yMax} stroke="#e0e0e0" />
<AxisBottom top={yMax} scale={xScale} numTicks={width > 520 ? 10 : 5} />
<AxisLeft scale={yScale} />
<text x="-70" y="15" transform="rotate(-90)" fontSize={10}>
Temperature (°F)
</text>
<Threshold
data={data}
x={date}
y0={ny}
y1={sf}
xScale={xScale}
yScale={yScale}
clipAboveTo={0}
clipBelowTo={yMax}
curve={curveBasis}
belowAreaProps={{
fill: 'red',
fillOpacity: 0.4
}}
aboveAreaProps={{
fill: 'green',
fillOpacity: 0.4
}}
/>
<LinePath
data={data}
curve={curveBasis}
x={date}
y={sf}
xScale={xScale}
yScale={yScale}
stroke="#000"
strokeWidth={1.5}
strokeOpacity={0.8}
strokeDasharray="1,2"
/>
<LinePath
data={data}
curve={curveBasis}
x={date}
y={ny}
xScale={xScale}
yScale={yScale}
stroke="#000"
strokeWidth={1.5}
/>
</Group>
</svg>
</div>
);
}
}`}
</Show>
);
};
2 changes: 2 additions & 0 deletions packages/vx-shape/src/shapes/Area.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Area.propTypes = {
};

export default function Area({
children,
x,
x0,
x1,
Expand Down Expand Up @@ -58,6 +59,7 @@ export default function Area({
if (y1) path.y1(d => yScale(y1(d)));
if (defined) path.defined(defined);
if (curve) path.curve(curve);
if (children) return children({ path });
return (
<g>
<path
Expand Down
19 changes: 19 additions & 0 deletions packages/vx-threshold/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"presets": ["es2015", "react", "stage-0"],
"plugins": [],
"env": {
"development": {
"plugins": [
["react-transform", {
"transforms": [{
"transform": "react-transform-hmr",
"imports": ["react"],
"locals": ["module"]
}]
}],
"transform-runtime",
"transform-decorators-legacy"
]
}
}
}
1 change: 1 addition & 0 deletions packages/vx-threshold/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
1 change: 1 addition & 0 deletions packages/vx-threshold/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include node_modules/react-fatigue-dev/Makefile
5 changes: 5 additions & 0 deletions packages/vx-threshold/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# @vx/threshold

```
npm install --save @vx/threshold
```
Loading

0 comments on commit a5daa00

Please sign in to comment.