Skip to content

Commit

Permalink
Merge pull request #282 from hshoff/harry-band-grid
Browse files Browse the repository at this point in the history
[grid] support band scales. fixes #268
  • Loading branch information
hshoff authored Apr 28, 2018
2 parents 6f56f97 + f5a9fa0 commit 01b64c6
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 77 deletions.
12 changes: 12 additions & 0 deletions packages/vx-demo/components/tiles/barstack.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import { BarStack } from '@vx/shape';
import { Group } from '@vx/group';
import { Grid } from '@vx/grid';
import { AxisBottom } from '@vx/axis';
import { cityTemperature } from '@vx/mock-data';
import { scaleBand, scaleLinear, scaleOrdinal } from '@vx/scale';
Expand Down Expand Up @@ -73,6 +74,17 @@ export default withTooltip(
<div style={{ position: 'relative' }}>
<svg width={width} height={height}>
<rect x={0} y={0} width={width} height={height} fill={`#eaedff`} rx={14} />
<Grid
top={margin.top}
left={margin.left}
xScale={xScale}
yScale={yScale}
width={xMax}
height={yMax}
stroke={'black'}
strokeOpacity={0.1}
xOffset={xScale.bandwidth() / 2}
/>
<BarStack
top={margin.top}
data={data}
Expand Down
50 changes: 22 additions & 28 deletions packages/vx-demo/pages/barstack.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default () => {
{`import React from 'react';
import { BarStack } from '@vx/shape';
import { Group } from '@vx/group';
import { Grid } from '@vx/grid';
import { AxisBottom } from '@vx/axis';
import { cityTemperature } from '@vx/mock-data';
import { scaleBand, scaleLinear, scaleOrdinal } from '@vx/scale';
Expand Down Expand Up @@ -79,13 +80,17 @@ export default withTooltip(
return (
<div style={{ position: 'relative' }}>
<svg width={width} height={height}>
<rect
x={0}
y={0}
width={width}
height={height}
fill='#eaedff'
rx={14}
<rect x={0} y={0} width={width} height={height} fill={\`#eaedff\`} rx={14} />
<Grid
top={margin.top}
left={margin.left}
xScale={xScale}
yScale={yScale}
width={xMax}
height={yMax}
stroke={'black'}
strokeOpacity={0.1}
xOffset={xScale.bandwidth() / 2}
/>
<BarStack
top={margin.top}
Expand All @@ -108,8 +113,7 @@ export default withTooltip(
onMouseMove={data => event => {
if (tooltipTimeout) clearTimeout(tooltipTimeout);
const top = event.clientY - margin.top - data.height;
const left =
xScale(data.x) + data.width + data.paddingInner * data.step / 2;
const left = xScale(data.x) + data.width + data.paddingInner * data.step / 2;
showTooltip({
tooltipData: data,
tooltipTop: top,
Expand All @@ -125,7 +129,7 @@ export default withTooltip(
tickLabelProps={(value, index) => ({
fill: '#a44afe',
fontSize: 11,
textAnchor: 'middle',
textAnchor: 'middle'
})}
/>
</svg>
Expand All @@ -139,13 +143,9 @@ export default withTooltip(
fontSize: '14px'
}}
>
<LegendOrdinal
scale={zScale}
direction="row"
labelMargin="0 15px 0 0"
/>
<LegendOrdinal scale={zScale} direction="row" labelMargin="0 15px 0 0" />
</div>
{tooltipOpen &&
{tooltipOpen && (
<Tooltip
top={tooltipTop}
left={tooltipLeft}
Expand All @@ -156,24 +156,18 @@ export default withTooltip(
}}
>
<div style={{ color: zScale(tooltipData.key) }}>
<strong>
{tooltipData.key}
</strong>
</div>
<div>
{tooltipData.data[tooltipData.key]}℉
<strong>{tooltipData.key}</strong>
</div>
<div>{tooltipData.data[tooltipData.key]}℉</div>
<div>
<small>
{tooltipData.xFormatted}
</small>
<small>{tooltipData.xFormatted}</small>
</div>
</Tooltip>}
</Tooltip>
)}
</div>
);
}
);
`}
);`}
</Show>
);
};
49 changes: 25 additions & 24 deletions packages/vx-grid/src/grids/Columns.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,35 @@ export default function Columns({
className,
numTicks = 10,
lineStyle,
offset,
...restProps
}) {
const ticks = scale.ticks ? scale.ticks(numTicks) : scale.domain();
return (
<Group className={cx('vx-columns', className)} top={top} left={left}>
{scale.ticks &&
scale.ticks(numTicks).map((d, i) => {
const x = scale(d);
const fromPoint = new Point({
x,
y: 0
});
const toPoint = new Point({
x,
y: height
});
return (
<Line
key={`column-line-${d}-${i}`}
from={fromPoint}
to={toPoint}
stroke={stroke}
strokeWidth={strokeWidth}
strokeDasharray={strokeDasharray}
style={lineStyle}
{...restProps}
/>
);
})}
{ticks.map((d, i) => {
const x = offset ? scale(d) + offset : scale(d);
const fromPoint = new Point({
x,
y: 0
});
const toPoint = new Point({
x,
y: height
});
return (
<Line
key={`column-line-${d}-${i}`}
from={fromPoint}
to={toPoint}
stroke={stroke}
strokeWidth={strokeWidth}
strokeDasharray={strokeDasharray}
style={lineStyle}
{...restProps}
/>
);
})}
</Group>
);
}
9 changes: 8 additions & 1 deletion packages/vx-grid/src/grids/Grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ export default function Grid({
numTicksRows,
numTicksColumns,
rowLineStyle,
columnLineStyle
columnLineStyle,
xOffset,
yOffset,
...restProps
}) {
return (
<Group className={cx('vx-grid', className)} top={top} left={left}>
Expand All @@ -31,6 +34,8 @@ export default function Grid({
strokeDasharray={strokeDasharray}
numTicks={numTicksRows}
style={rowLineStyle}
offset={yOffset}
{...restProps}
/>
<Columns
className={className}
Expand All @@ -41,6 +46,8 @@ export default function Grid({
strokeDasharray={strokeDasharray}
numTicks={numTicksColumns}
style={columnLineStyle}
offset={xOffset}
{...restProps}
/>
</Group>
);
Expand Down
49 changes: 25 additions & 24 deletions packages/vx-grid/src/grids/Rows.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,35 @@ export default function Rows({
className,
numTicks = 10,
lineStyle,
offset,
...restProps
}) {
const ticks = scale.ticks ? scale.ticks(numTicks) : scale.domain();
return (
<Group className={cx('vx-rows', className)} top={top} left={left}>
{scale.ticks &&
scale.ticks(numTicks).map((d, i) => {
const y = scale(d);
const fromPoint = new Point({
x: 0,
y
});
const toPoint = new Point({
x: width,
y
});
return (
<Line
key={`row-line-${d}-${i}`}
from={fromPoint}
to={toPoint}
stroke={stroke}
strokeWidth={strokeWidth}
strokeDasharray={strokeDasharray}
style={lineStyle}
{...restProps}
/>
);
})}
{ticks.map((d, i) => {
const y = offset ? scale(d) + offset : scale(d);
const fromPoint = new Point({
x: 0,
y
});
const toPoint = new Point({
x: width,
y
});
return (
<Line
key={`row-line-${d}-${i}`}
from={fromPoint}
to={toPoint}
stroke={stroke}
strokeWidth={strokeWidth}
strokeDasharray={strokeDasharray}
style={lineStyle}
{...restProps}
/>
);
})}
</Group>
);
}

0 comments on commit 01b64c6

Please sign in to comment.