-
Notifications
You must be signed in to change notification settings - Fork 524
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1550 from FormidableLabs/task/audit-selection-demo
Audits and adds demo: SelectionDemo
- Loading branch information
Showing
3 changed files
with
356 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,354 @@ | ||
/*eslint-disable no-magic-numbers */ | ||
import React from "react"; | ||
import { VictoryChart } from "@packages/victory-chart"; | ||
import { VictoryStack } from "@packages/victory-stack"; | ||
import { VictoryGroup } from "@packages/victory-group"; | ||
import { VictoryBar } from "@packages/victory-bar"; | ||
import { VictoryLine } from "@packages/victory-line"; | ||
import { VictoryScatter } from "@packages/victory-scatter"; | ||
import { VictorySelectionContainer } from "@packages/victory-selection-container"; | ||
import { VictoryLegend } from "@packages/victory-legend"; | ||
import { VictoryTooltip } from "@packages/victory-tooltip"; | ||
|
||
interface SelectionDemoState { | ||
points: { x: number; y: number }[]; | ||
} | ||
|
||
interface DataSet { | ||
data?: { x: number; y: number }[]; | ||
} | ||
|
||
export default class SelectionDemo extends React.Component<any, SelectionDemoState> { | ||
constructor(props: any) { | ||
super(props); | ||
this.state = { | ||
points: [] | ||
}; | ||
} | ||
|
||
handleSelection(datasets: DataSet[]) { | ||
const points = datasets.reduce((memo: any, dataset: DataSet) => { | ||
memo = memo.concat(dataset.data); | ||
return memo; | ||
}, []); | ||
this.setState({ points }); | ||
} | ||
|
||
handleClearSelection() { | ||
this.setState({ points: [] }); | ||
} | ||
|
||
listData() { | ||
const points = this.state.points.map((point: { x: number; y: number }, index: number) => { | ||
return <li key={index}>{`${point.x}, ${point.y}`}</li>; | ||
}); | ||
|
||
return ( | ||
<div> | ||
<p>Points</p> | ||
<ul>{points}</ul> | ||
</div> | ||
); | ||
} | ||
|
||
render() { | ||
const containerStyle: React.CSSProperties = { | ||
display: "flex", | ||
flexDirection: "row", | ||
flexWrap: "wrap", | ||
alignItems: "center", | ||
justifyContent: "center" | ||
}; | ||
|
||
const chartStyle = { parent: { border: "1px solid #ccc", margin: "2%", maxWidth: "40%" } }; | ||
|
||
return ( | ||
<div className="demo"> | ||
<div style={containerStyle}> | ||
{this.listData()} | ||
<VictoryChart | ||
style={chartStyle} | ||
height={400} | ||
padding={{ top: 100, bottom: 50, left: 50, right: 50 }} | ||
containerComponent={ | ||
<VictorySelectionContainer | ||
selectionDimension="x" | ||
selectionStyle={{ | ||
stroke: "tomato", | ||
strokeWidth: 2, | ||
fill: "tomato", | ||
fillOpacity: 0.1 | ||
}} | ||
onSelection={this.handleSelection.bind(this)} | ||
onSelectionCleared={this.handleClearSelection.bind(this)} | ||
/> | ||
} | ||
> | ||
<VictoryLegend | ||
x={120} | ||
y={20} | ||
title="Legend" | ||
centerTitle | ||
orientation="horizontal" | ||
gutter={20} | ||
style={{ border: { stroke: "black" }, title: { fontSize: 20 } }} | ||
data={[ | ||
{ name: "One", symbol: { fill: "tomato" } }, | ||
{ name: "Two", symbol: { fill: "orange" } }, | ||
{ name: "Three", symbol: { fill: "gold" } } | ||
]} | ||
/> | ||
<VictoryLine | ||
style={{ | ||
data: { stroke: "tomato" } | ||
}} | ||
data={[ | ||
{ x: 1, y: -5 }, | ||
{ x: 2, y: 4 }, | ||
{ x: 3, y: 2 }, | ||
{ x: 4, y: 3 }, | ||
{ x: 5, y: 1 }, | ||
{ x: 6, y: -3 }, | ||
{ x: 7, y: 3 } | ||
]} | ||
/> | ||
<VictoryLine | ||
style={{ | ||
data: { stroke: "blue" } | ||
}} | ||
data={[ | ||
{ x: 1, y: -3 }, | ||
{ x: 2, y: 5 }, | ||
{ x: 3, y: 3 }, | ||
{ x: 4, y: 0 }, | ||
{ x: 5, y: -2 }, | ||
{ x: 6, y: -2 }, | ||
{ x: 7, y: 5 } | ||
]} | ||
/> | ||
<VictoryLine | ||
data={[ | ||
{ x: 1, y: 5 }, | ||
{ x: 2, y: -4 }, | ||
{ x: 3, y: -2 }, | ||
{ x: 4, y: -3 }, | ||
{ x: 5, y: -1 }, | ||
{ x: 6, y: 3 }, | ||
{ x: 7, y: -3 } | ||
]} | ||
/> | ||
</VictoryChart> | ||
|
||
<VictoryChart style={chartStyle} containerComponent={<VictorySelectionContainer />}> | ||
<VictoryGroup data={[{ x: 1, y: 5 }, { x: 2, y: 4 }, { x: 3, y: -2 }]}> | ||
<VictoryLine style={{ data: { stroke: "tomato" } }} /> | ||
<VictoryScatter | ||
style={{ data: { fill: ({ active }) => (active ? "tomato" : "gray") } }} | ||
labels={({ datum }) => datum.y} | ||
labelComponent={<VictoryTooltip />} | ||
/> | ||
</VictoryGroup> | ||
|
||
<VictoryGroup data={[{ x: 1, y: -3 }, { x: 2, y: 5 }, { x: 3, y: 3 }]}> | ||
<VictoryLine style={{ data: { stroke: "blue" } }} /> | ||
<VictoryScatter | ||
style={{ data: { fill: ({ active }) => (active ? "blue" : "gray") } }} | ||
labels={({ datum }) => datum.y} | ||
labelComponent={<VictoryTooltip />} | ||
/> | ||
</VictoryGroup> | ||
|
||
<VictoryGroup data={[{ x: 1, y: 5 }, { x: 2, y: -4 }, { x: 3, y: -2 }]}> | ||
<VictoryLine style={{ data: { stroke: "black" } }} /> | ||
<VictoryScatter | ||
style={{ data: { fill: ({ active }) => (active ? "black" : "gray") } }} | ||
labels={({ datum }) => datum.y} | ||
labelComponent={<VictoryTooltip />} | ||
/> | ||
</VictoryGroup> | ||
</VictoryChart> | ||
|
||
<VictoryScatter | ||
style={{ | ||
parent: chartStyle.parent, | ||
data: { | ||
fill: ({ active }) => (active ? "tomato" : "black") | ||
} | ||
}} | ||
containerComponent={ | ||
<VictorySelectionContainer | ||
selectionStyle={{ | ||
stroke: "tomato", | ||
strokeWidth: 2, | ||
fill: "tomato", | ||
fillOpacity: 0.1 | ||
}} | ||
/> | ||
} | ||
size={({ active }) => (active ? 5 : 3)} | ||
data={[ | ||
{ x: 1, y: -5 }, | ||
{ x: 2, y: 4 }, | ||
{ x: 3, y: 2 }, | ||
{ x: 4, y: 3 }, | ||
{ x: 5, y: 1 }, | ||
{ x: 6, y: -3 }, | ||
{ x: 7, y: 3 } | ||
]} | ||
/> | ||
|
||
<VictoryScatter | ||
style={{ | ||
parent: chartStyle.parent, | ||
data: { | ||
fill: ({ active }) => (active ? "tomato" : "black") | ||
} | ||
}} | ||
containerComponent={ | ||
<VictorySelectionContainer | ||
selectionStyle={{ | ||
stroke: "tomato", | ||
strokeWidth: 2, | ||
fill: "tomato", | ||
fillOpacity: 0.1 | ||
}} | ||
/> | ||
} | ||
size={({ active }) => (active ? 5 : 3)} | ||
y={(d) => d.x * d.x} | ||
/> | ||
|
||
<VictoryGroup | ||
style={chartStyle} | ||
containerComponent={ | ||
<VictorySelectionContainer | ||
selectionStyle={{ | ||
stroke: "tomato", | ||
strokeWidth: 2, | ||
fill: "tomato", | ||
fillOpacity: 0.1 | ||
}} | ||
/> | ||
} | ||
> | ||
<VictoryScatter | ||
style={{ | ||
data: { fill: "tomato" } | ||
}} | ||
size={({ active }) => (active ? 5 : 3)} | ||
data={[ | ||
{ x: 1, y: -5 }, | ||
{ x: 2, y: 4 }, | ||
{ x: 3, y: 2 }, | ||
{ x: 4, y: 3 }, | ||
{ x: 5, y: 1 }, | ||
{ x: 6, y: -3 }, | ||
{ x: 7, y: 3 } | ||
]} | ||
/> | ||
<VictoryScatter | ||
style={{ | ||
data: { fill: "blue" } | ||
}} | ||
size={({ active }) => (active ? 5 : 3)} | ||
data={[ | ||
{ x: 1, y: -3 }, | ||
{ x: 2, y: 5 }, | ||
{ x: 3, y: 3 }, | ||
{ x: 4, y: 0 }, | ||
{ x: 5, y: -2 }, | ||
{ x: 6, y: -2 }, | ||
{ x: 7, y: 5 } | ||
]} | ||
/> | ||
<VictoryScatter | ||
data={[ | ||
{ x: 1, y: 5 }, | ||
{ x: 2, y: -4 }, | ||
{ x: 3, y: -2 }, | ||
{ x: 4, y: -3 }, | ||
{ x: 5, y: -1 }, | ||
{ x: 6, y: 3 }, | ||
{ x: 7, y: -3 } | ||
]} | ||
size={({ active }) => (active ? 5 : 3)} | ||
/> | ||
</VictoryGroup> | ||
|
||
<VictoryChart | ||
horizontal | ||
style={chartStyle} | ||
containerComponent={ | ||
<VictorySelectionContainer | ||
selectionDimension="x" | ||
selectionStyle={{ | ||
stroke: "tomato", | ||
strokeWidth: 2, | ||
fill: "tomato", | ||
fillOpacity: 0.1 | ||
}} | ||
/> | ||
} | ||
> | ||
<VictoryStack> | ||
<VictoryBar | ||
style={{ | ||
data: { | ||
fill: "tomato", | ||
stroke: ({ active }) => (active ? "black" : "none"), | ||
strokeWidth: 2 | ||
} | ||
}} | ||
data={[ | ||
{ x: 1, y: -5 }, | ||
{ x: 2, y: 4 }, | ||
{ x: 3, y: 2 }, | ||
{ x: 4, y: 3 }, | ||
{ x: 5, y: 1 }, | ||
{ x: 6, y: -3 }, | ||
{ x: 7, y: 3 } | ||
]} | ||
/> | ||
<VictoryBar | ||
style={{ | ||
data: { | ||
fill: "orange", | ||
stroke: ({ active }) => (active ? "black" : "none"), | ||
strokeWidth: 2 | ||
} | ||
}} | ||
data={[ | ||
{ x: 1, y: -3 }, | ||
{ x: 2, y: 5 }, | ||
{ x: 3, y: 3 }, | ||
{ x: 4, y: 0 }, | ||
{ x: 5, y: -2 }, | ||
{ x: 6, y: -2 }, | ||
{ x: 7, y: 5 } | ||
]} | ||
/> | ||
<VictoryBar | ||
style={{ | ||
data: { | ||
fill: "gold", | ||
stroke: ({ active }) => (active ? "black" : "none"), | ||
strokeWidth: 2 | ||
} | ||
}} | ||
data={[ | ||
{ x: 1, y: 5 }, | ||
{ x: 2, y: -4 }, | ||
{ x: 3, y: -2 }, | ||
{ x: 4, y: -3 }, | ||
{ x: 5, y: -1 }, | ||
{ x: 6, y: 3 }, | ||
{ x: 7, y: -3 } | ||
]} | ||
/> | ||
</VictoryStack> | ||
</VictoryChart> | ||
</div> | ||
</div> | ||
); | ||
} | ||
} |