Skip to content

Commit

Permalink
Merge pull request #320 from spiderbites/bar-group-horizontal
Browse files Browse the repository at this point in the history
[shape] Add <BarGroupHorizontal /> component
  • Loading branch information
hshoff committed Jul 12, 2018
2 parents 2767d02 + ac2bf07 commit 1363640
Show file tree
Hide file tree
Showing 7 changed files with 416 additions and 1 deletion.
23 changes: 22 additions & 1 deletion packages/vx-demo/components/gallery.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import Stacked from '../components/tiles/stacked';
import MultiLine from '../components/tiles/multiline';
import Axis from '../components/tiles/axis';
import BarGroup from '../components/tiles/bargroup';
import BarGroupHorizontal from '../components/tiles/bargrouphorizontal';
import BarStack from '../components/tiles/barstack';
import BarStackHorizontal from '../components/tiles/barstackhorizontal';
import Heatmap from '../components/tiles/heatmap';
Expand Down Expand Up @@ -860,7 +861,27 @@ export default class Gallery extends React.Component {
</div>
</Link>
</Tilt>
{false && <div className="gallery-item placeholder" />}
<Tilt className="tilt" options={{ max: 8, scale: 1 }}>
<Link prefetch href="/bargrouphorizontal">
<div className="gallery-item" style={{ background: '#612efb' }}>
<div className="image">
<ParentSize>
{({ width, height }) => (
<BarGroupHorizontal width={width} height={height + detailsHeight} />
)}
</ParentSize>
</div>
<div className="details" style={{ color: '#e5fd3d' }}>
<div className="title">Bar Group Horizontal</div>
<div className="description">
<pre>{`<Shape.BarGroupHorizontal />`}</pre>
</div>
</div>
</div>
</Link>
</Tilt>
<div className="gallery-item placeholder" />
<div className="gallery-item placeholder" />
</div>

<div>
Expand Down
100 changes: 100 additions & 0 deletions packages/vx-demo/components/tiles/bargrouphorizontal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import React from 'react';
import { BarGroupHorizontal } from '@vx/shape';
import { Group } from '@vx/group';
import { AxisLeft } 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 y0 = d => d.date;
const x = d => d.value;

export default ({
width,
height,
events = false,
margin = {
top: 20,
left: 50,
right: 10,
bottom: 0
}
}) => {
if (width < 10) return null;

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

// // scales
const y0Scale = scaleBand({
rangeRound: [0, yMax],
domain: data.map(y0),
padding: 0.2,
tickFormat: () => val => formatDate(val)
});

const y1Scale = scaleBand({
rangeRound: [0, y0Scale.bandwidth()],
domain: keys,
padding: 0.1
});

const xScale = scaleLinear({
rangeRound: [xMax, 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} />
<Group top={margin.top} left={margin.left}>
<BarGroupHorizontal
data={data}
keys={keys}
width={xMax}
y0={y0}
y0Scale={y0Scale}
y1Scale={y1Scale}
xScale={xScale}
zScale={zScale}
rx={4}
onClick={data => event => {
if (!events) return;
alert(`clicked: ${JSON.stringify(data)}`);
}}
/>
<AxisLeft
scale={y0Scale}
stroke="#e5fd3d"
tickStroke="#e5fd3d"
hideAxisLine
tickLabelProps={(value, index) => ({
fill: '#e5fd3d',
fontSize: 11,
textAnchor: 'end',
dy: '0.33em'
})}
/>
</Group>
</svg>
);
};
116 changes: 116 additions & 0 deletions packages/vx-demo/pages/bargrouphorizontal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import React from 'react';
import Show from '../components/show';
import BarGroupHorizontal from '../components/tiles/bargrouphorizontal';

export default () => {
return (
<Show
events={true}
margin={{ top: 45, left: 60, right: 20, bottom: 0 }}
component={BarGroupHorizontal}
title="Bar Group Horizontal"
>
{`import React from 'react';
import { BarGroupHorizontal } from '@vx/shape';
import { Group } from '@vx/group';
import { AxisLeft } 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 y0 = d => d.date;
const x = d => d.value;
export default ({
width,
height,
events = false,
margin = {
top: 20,
left: 50,
right: 10,
bottom: 0
},
}) => {
if (width < 10) return null;
// bounds
const xMax = width - margin.left - margin.right;
const yMax = height - 100
// // scales
const y0Scale = scaleBand({
rangeRound: [0, yMax],
domain: data.map(y0),
padding: 0.2,
tickFormat: () => val => formatDate(val)
});
const y1Scale = scaleBand({
rangeRound: [0, y0Scale.bandwidth()],
domain: keys,
padding: 0.1
});
const xScale = scaleLinear({
rangeRound: [xMax, 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} />
<Group top={margin.top} left={margin.left}>
<BarGroupHorizontal
data={data}
keys={keys}
width={xMax}
y0={y0}
y0Scale={y0Scale}
y1Scale={y1Scale}
xScale={xScale}
zScale={zScale}
rx={4}
onClick={data => event => {
if (!events) return;
alert(\`clicked: \${JSON.stringify(data)}\`);
}}
/>
<AxisLeft
scale={y0Scale}
stroke="#e5fd3d"
tickStroke="#e5fd3d"
hideAxisLine
tickLabelProps={(value, index) => ({
fill: '#e5fd3d',
fontSize: 11,
textAnchor: 'end',
dy: '0.33em'
})}
/>
</Group>
</svg>
);
};
`}
</Show>
);
};
69 changes: 69 additions & 0 deletions packages/vx-shape/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,73 @@ A simple rectangle (a `<rect>` element) to use in your graphs.
| strokeMiterlimit | | number | The svg [Miterlimit](https://mzl.la/2pLVE13) of the stroke. |
| strokeOpacity | | number | The svg opacity. |

## `<BarGroup />`

![BarGroup Example](https://i.imgur.com/Ef9fVqe.png)

```js
<BarGroup
data={data}
keys={keys}
height={yMax}
x0={x0}
x0Scale={x0Scale}
x1Scale={x1Scale}
yScale={yScale}
zScale={zScale}
rx={4}
/>
```

### Properties
| Name | Default | Type | Description |
|:---------------- |:--------- |:-------- |:------------------------------------------------------------------------------------------------------------------------------- |
| data | | array | An array of data elements. |
| className | | string | The class name for the `path` element. |
| top | | number | The margin on top. |
| left | | number | The margin on the left. |
| x0 | | function | xs accessor function. |
| x0Scale | | function | A [scale band function](https://github.com/hshoff/vx/tree/master/packages/vx-scale#band-scaling) for the bar group. |
| x1Scale | | function | A [scale band function](https://github.com/hshoff/vx/tree/master/packages/vx-scale#band-scaling) for each bar within the group. |
| yScale | | function | A [scale function](https://github.com/hshoff/vx/tree/master/packages/vx-scale) for the ys. |
| zScale | | function | A [scale function](https://github.com/hshoff/vx/tree/master/packages/vx-scale) for the keys. |
| keys | | array | A list of data keys |
| height | | number | The pixel height of the bar group. |


## `<BarGroupHorizontal />`

![BarGroupHorizontal Example](https://i.imgur.com/RDBJewO.png)

```js
<BarGroupHorizontal
data={data}
keys={keys}
width={xMax}
y0={y0}
y0Scale={y0Scale}
y1Scale={y1Scale}
xScale={xScale}
zScale={zScale}
rx={4}
/>
```

### Properties
| Name | Default | Type | Description |
|:---------------- |:--------- |:-------- |:------------------------------------------------------------------------------------------------------------------------------- |
| data | | array | An array of data elements. |
| className | | string | The class name for the `path` element. |
| top | | number | The margin on top. |
| left | | number | The margin on the left. |
| y0 | | function | ys accessor function. |
| y0Scale | | function | A [scale band function](https://github.com/hshoff/vx/tree/master/packages/vx-scale#band-scaling) for the bar group. |
| y1Scale | | function | A [scale band function](https://github.com/hshoff/vx/tree/master/packages/vx-scale#band-scaling) for each bar within the group. |
| xScale | | function | A [scale function](https://github.com/hshoff/vx/tree/master/packages/vx-scale) for the xs. |
| zScale | | function | A [scale function](https://github.com/hshoff/vx/tree/master/packages/vx-scale) for the keys. |
| keys | | array | A list of data keys |
| height | | number | The pixel height of the bar group. |

## `<Line />`

A simple line. Good for drawing in the sand.
Expand Down Expand Up @@ -274,6 +341,8 @@ A more complicated line path. A `<LinePath />` is useful for making line graphs
+ [`<AreaClosed />`](https://github.com/hshoff/vx/blob/master/packages/vx-shape/src/shapes/AreaClosed.js)
+ [`<AreaStack />`](https://github.com/hshoff/vx/blob/master/packages/vx-shape/src/shapes/AreaStack.js)
+ [`<Bar />`](https://github.com/hshoff/vx/blob/master/packages/vx-shape/src/shapes/Bar.js)
+ [`<BarGroup />`](https://github.com/hshoff/vx/blob/master/packages/vx-shape/src/shapes/BarGroup.js)
+ [`<BarGroupHorizontal />`](https://github.com/hshoff/vx/blob/master/packages/vx-shape/src/shapes/BarGroupHorizontal.js)
+ [`<Line />`](https://github.com/hshoff/vx/blob/master/packages/vx-shape/src/shapes/Line.js)
+ [`<LinePath />`](https://github.com/hshoff/vx/blob/master/packages/vx-shape/src/shapes/LinePath.js)
+ [`<LineRadial />`](https://github.com/hshoff/vx/blob/master/packages/vx-shape/src/shapes/LineRadial.js)
Expand Down
1 change: 1 addition & 0 deletions packages/vx-shape/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export { default as AreaClosed } from './shapes/AreaClosed';
export { default as AreaStack } from './shapes/AreaStack';
export { default as Bar } from './shapes/Bar';
export { default as BarGroup } from './shapes/BarGroup';
export { default as BarGroupHorizontal } from './shapes/BarGroupHorizontal';
export { default as BarStack } from './shapes/BarStack';
export { default as BarStackHorizontal } from './shapes/BarStackHorizontal';
export { default as Stack } from './shapes/Stack';
Expand Down
Loading

0 comments on commit 1363640

Please sign in to comment.