diff --git a/packages/vx-demo/components/tiles/bargroup.js b/packages/vx-demo/components/tiles/bargroup.js new file mode 100644 index 000000000..5ccf3c91b --- /dev/null +++ b/packages/vx-demo/components/tiles/bargroup.js @@ -0,0 +1,94 @@ +import React from 'react'; +import { BarGroup } from '@vx/shape'; +import { Group } from '@vx/group'; +import { AxisBottom } 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, 8); +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 x0 = d => d.date; +const y = d => d.value; + +export default ({ + width, + height, + margin = { + top: 40 + } +}) => { + if (width < 10) return null; + + // bounds + const xMax = width; + const yMax = height - margin.top - 100; + + // // scales + const x0Scale = scaleBand({ + rangeRound: [0, xMax], + domain: data.map(x0), + padding: 0.2, + tickFormat: () => (val) => formatDate(val) + }); + const x1Scale = scaleBand({ + rangeRound: [0, x0Scale.bandwidth()], + domain: keys, + padding: .1 + }); + const yScale = scaleLinear({ + rangeRound: [yMax, 0], + domain: [0, max(data, (d) => { + return max(keys, (key) => d[key]) + })], + }); + const zScale = scaleOrdinal({ + domain: keys, + range: ['#aeeef8', '#e5fd3d', '#9caff6'] + }) + + return ( + + + + + )} + /> + + ); +} diff --git a/packages/vx-demo/components/tiles/barstack.js b/packages/vx-demo/components/tiles/barstack.js new file mode 100644 index 000000000..63f6e23b0 --- /dev/null +++ b/packages/vx-demo/components/tiles/barstack.js @@ -0,0 +1,95 @@ +import React from 'react'; +import { BarStack } from '@vx/shape'; +import { Group } from '@vx/group'; +import { AxisBottom } 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, 12); +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 x = d => d.date; +const y = d => d.value; + +const totals = data.reduce((ret, cur) => { + const t = keys.reduce((dailyTotal, k) => { + dailyTotal += +cur[k] + return dailyTotal; + }, 0) + ret.push(t) + return ret; +}, []) + +export default ({ + width, + height, + margin = { + top: 40 + } +}) => { + if (width < 10) return null; + + // bounds + const xMax = width; + const yMax = height - margin.top - 100; + + // // scales + const xScale = scaleBand({ + rangeRound: [0, xMax], + domain: data.map(x), + padding: 0.2, + tickFormat: () => (val) => formatDate(val) + }); + const yScale = scaleLinear({ + rangeRound: [yMax, 0], + nice: true, + domain: [0, max(totals)], + }); + const zScale = scaleOrdinal({ + domain: keys, + range: ['#6c5efb', '#c998ff', '#a44afe'] + }) + + return ( + + + + + )} + /> + + ); +} diff --git a/packages/vx-demo/pages/bargroup.js b/packages/vx-demo/pages/bargroup.js new file mode 100644 index 000000000..0defb2d76 --- /dev/null +++ b/packages/vx-demo/pages/bargroup.js @@ -0,0 +1,104 @@ +import React from 'react'; +import Show from '../components/show'; +import BarGroup from '../components/tiles/bargroup'; + +export default () => { + return ( + +{`import React from 'react'; +import { BarGroup } from '@vx/shape'; +import { Group } from '@vx/group'; +import { AxisBottom } 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 x0 = d => d.date; +const y = d => d.value; + +export default ({ + width, + height, + margin = { + top: 40 + } +}) => { + if (width < 10) return null; + + // bounds + const xMax = width; + const yMax = height - margin.top - 100; + + // // scales + const x0Scale = scaleBand({ + rangeRound: [0, xMax], + domain: data.map(x0), + padding: 0.2, + tickFormat: () => (val) => formatDate(val) + }); + const x1Scale = scaleBand({ + rangeRound: [0, x0Scale.bandwidth()], + domain: keys, + padding: .1 + }); + const yScale = scaleLinear({ + rangeRound: [yMax, 0], + domain: [0, max(data, (d) => { + return max(keys, (key) => d[key]) + })], + }); + const zScale = scaleOrdinal({ + domain: keys, + range: ['#aeeef8', '#e5fd3d', '#9caff6'] + }) + + return ( + + + + + )} + /> + + ); +}`} + + ); +} diff --git a/packages/vx-demo/pages/barstack.js b/packages/vx-demo/pages/barstack.js new file mode 100644 index 000000000..b27ead461 --- /dev/null +++ b/packages/vx-demo/pages/barstack.js @@ -0,0 +1,105 @@ +import React from 'react'; +import Show from '../components/show'; +import BarStack from '../components/tiles/barstack'; + +export default () => { + return ( + +{`import React from 'react'; +import { BarStack } from '@vx/shape'; +import { Group } from '@vx/group'; +import { AxisBottom } 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, 12); +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 x = d => d.date; +const y = d => d.value; + +const totals = data.reduce((ret, cur) => { + const t = keys.reduce((dailyTotal, k) => { + dailyTotal += +cur[k] + return dailyTotal; + }, 0) + ret.push(t) + return ret; +}, []) + +export default ({ + width, + height, + margin = { + top: 40 + } +}) => { + if (width < 10) return null; + + // bounds + const xMax = width; + const yMax = height - margin.top - 100; + + // // scales + const xScale = scaleBand({ + rangeRound: [0, xMax], + domain: data.map(x), + padding: 0.2, + tickFormat: () => (val) => formatDate(val) + }); + const yScale = scaleLinear({ + rangeRound: [yMax, 0], + nice: true, + domain: [0, max(totals)], + }); + const zScale = scaleOrdinal({ + domain: keys, + range: ['#6c5efb', '#c998ff', '#a44afe'] + }) + + return ( + + + + + )} + /> + + ); +}`} + + ); +} diff --git a/packages/vx-demo/pages/gallery.js b/packages/vx-demo/pages/gallery.js index 402702608..f4ced70e5 100644 --- a/packages/vx-demo/pages/gallery.js +++ b/packages/vx-demo/pages/gallery.js @@ -15,6 +15,8 @@ import Area from '../components/tiles/area'; 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 BarStack from '../components/tiles/barstack'; const items = [ "#242424", @@ -69,6 +71,8 @@ export default class Gallery extends React.Component { const t7 = this.state.dimensions[6] || [8, 300]; const t8 = this.state.dimensions[7] || [8, 300]; const t9 = this.state.dimensions[8] || [8, 300]; + const t10 = this.state.dimensions[9] || [8, 300]; + const t11 = this.state.dimensions[10] || [8, 300]; return ( @@ -310,12 +314,54 @@ export default class Gallery extends React.Component { -
-
-

- More on the way! -

-
+ + +
this.nodes.add(d)}> +
+ +
+
+
Bar Group
+
+
{``}
+
+
+
+ +
+ + +
this.nodes.add(d)}> +
+ +
+
+
Bar Stack
+
+
{``}
+
+
+
+ +
+ + +
+

+ More on the way! +