Skip to content
This repository has been archived by the owner on Feb 19, 2022. It is now read-only.

Commit

Permalink
Merge pull request #469 from FormidableLabs/cursor
Browse files Browse the repository at this point in the history
VictoryCursorContainer
  • Loading branch information
boygirl authored May 12, 2017
2 parents 9a89a74 + 5855a02 commit 8c73ecb
Show file tree
Hide file tree
Showing 8 changed files with 513 additions and 3 deletions.
3 changes: 3 additions & 0 deletions demo/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import VoronoiDemo from "./components/victory-voronoi-demo";
import TooltipDemo from "./components/victory-tooltip-demo";
import ZoomContainerDemo from "./components/victory-zoom-container-demo";
import VoronoiContainerDemo from "./components/victory-voronoi-container-demo";
import CursorContainerDemo from "./components/victory-cursor-container-demo";
import CreateContainerDemo from "./components/create-container-demo";
import BrushContainerDemo from "./components/victory-brush-container-demo";
import AnimationDemo from "./components/animation-demo";
Expand Down Expand Up @@ -63,6 +64,7 @@ class App extends React.Component {
case "/tooltip": Child = TooltipDemo; break;
case "/zoom-container": Child = ZoomContainerDemo; break;
case "/voronoi-container": Child = VoronoiContainerDemo; break;
case "/cursor-container": Child = CursorContainerDemo; break;
case "/brush-container": Child = BrushContainerDemo; break;
case "/animation": Child = AnimationDemo; break;
case "/selection-container": Child = SelectionDemo; break;
Expand Down Expand Up @@ -92,6 +94,7 @@ class App extends React.Component {
<li><a href="#/tooltip">Victory Tooltip Demo</a></li>
<li><a href="#/zoom-container">Victory Zoom Container Demo</a></li>
<li><a href="#/voronoi-container">Victory Voronoi Container Demo</a></li>
<li><a href="#/cursor-container">Victory Cursor Container Demo</a></li>
<li><a href="#/brush-container">Victory Brush Container Demo</a></li>
<li><a href="#/animation">Animation Demo</a></li>
<li><a href="#/selection-container">Victory Selection Container Demo</a></li>
Expand Down
22 changes: 20 additions & 2 deletions demo/components/create-container-demo.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*eslint-disable no-magic-numbers,react/no-multi-comp */
import React from "react";
import { round } from "lodash";
import {
VictoryChart, VictoryGroup, VictoryStack, VictoryScatter, VictoryBar, VictoryLine,
createContainer
Expand All @@ -21,6 +22,7 @@ const Charts = ({ CustomContainer }) => { // eslint-disable-line react/prop-type
return (
<div className="demo">
<div style={containerStyle}>
{/* A */}
<VictoryChart style={chartStyle}
domainPadding={{ y: 2 }}
containerComponent={
Expand Down Expand Up @@ -69,6 +71,7 @@ const Charts = ({ CustomContainer }) => { // eslint-disable-line react/prop-type
/>
</VictoryChart>

{/* B */}
<VictoryScatter
style={{
parent: chartStyle.parent,
Expand All @@ -78,10 +81,13 @@ const Charts = ({ CustomContainer }) => { // eslint-disable-line react/prop-type
}}
containerComponent={
<CustomContainer
dimension="x"
cursorLabel={(d) => round(d.x, 2)}
selectionStyle={{
stroke: "tomato", strokeWidth: 2, fill: "tomato", fillOpacity: 0.1
}}
selectedDomain={{ x: [0.4, 0.95], y: [0.5, 0.8] }}
defaultCursorValue={0.99}
/>
}
size={(datum, active) => active ? 5 : 3}
Expand All @@ -90,7 +96,12 @@ const Charts = ({ CustomContainer }) => { // eslint-disable-line react/prop-type

<VictoryChart
style={chartStyle}
containerComponent={<CustomContainer selectedDomain={{ x: [0, 0] }} />}
containerComponent={
<CustomContainer
selectedDomain={{ x: [0, 0] }}
dimension="y"
/>
}
>
<VictoryGroup style={chartStyle}>
<VictoryScatter
Expand Down Expand Up @@ -144,9 +155,14 @@ const Charts = ({ CustomContainer }) => { // eslint-disable-line react/prop-type
</VictoryGroup>
</VictoryChart>

{/* C */}
<VictoryStack
style={chartStyle}
containerComponent={<CustomContainer selectedDomain={{ x: [1.5, 2.5], y: [-3, 4] }} />}
containerComponent={
<CustomContainer
selectedDomain={{ x: [1.5, 2.5], y: [-3, 4] }}
/>
}
>
<VictoryBar
style={{
Expand Down Expand Up @@ -218,6 +234,8 @@ class App extends React.Component {
<Charts CustomContainer={createContainer("brush", "voronoi")} />
<pre>createContainer("zoom", "voronoi")</pre>
<Charts CustomContainer={createContainer("zoom", "voronoi")} />
<pre>createContainer("cursor", "voronoi")</pre>
<Charts CustomContainer={createContainer("cursor", "voronoi")} />
</div>
);
}
Expand Down
283 changes: 283 additions & 0 deletions demo/components/victory-cursor-container-demo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,283 @@
/*global window:false*/
/*eslint-disable no-magic-numbers */
import React from "react";
import { random, range, round } from "lodash";
import {
VictoryChart, VictoryGroup, VictoryStack, VictoryScatter, VictoryBar, VictoryLine,
VictoryCursorContainer
} from "../../src/index";
import { VictoryTooltip, VictoryTheme, VictoryLabel } from "victory-core";

class App extends React.Component {

constructor() {
super();
this.defaultCursorValue = { x: 2.25, y: 1.75 };
this.state = {
data: this.getData(),
cursorValue: this.defaultCursorValue
};
}

componentDidMount() {
/* eslint-disable react/no-did-mount-set-state */
this.setStateInterval = window.setInterval(() => {
this.setState({
data: this.getData()
});
}, 3000);
}

componentWillUnmount() {
window.clearInterval(this.setStateInterval);
}


getData() {
const bars = random(6, 10);
return range(bars).map((bar) => {
return { a: bar + 1, b: random(2, 10) };
});
}

render() {
const containerStyle = {
display: "flex",
flexDirection: "row",
flexWrap: "wrap",
alignItems: "center",
justifyContent: "center"
};

const chartStyle = { parent: { border: "1px solid #ccc", margin: "2%", maxWidth: "40%" } };

const cursorLabel = (d) => (
`${round(d.x, 2)} , ${round(d.y, 2)}`
);

return (
<div className="demo">
<div style={containerStyle}>

<VictoryChart style={chartStyle}
theme={VictoryTheme.material}
domainPadding={{ y: 2 }}
containerComponent={
<VictoryCursorContainer
cursorLabel={cursorLabel}
defaultCursorValue={this.defaultCursorValue}
onChange={(cursorValue) => this.setState({ cursorValue })}
/>
}
>
<VictoryLabel
x={50} y={310}
text={
`x: ${round(this.state.cursorValue.x, 2)} y: ${round(this.state.cursorValue.y, 2)}`
}
/>
<VictoryLine
data={[
{ x: 1, y: 5, l: "one" },
{ x: 1.5, y: 5, l: "one point five" },
{ x: 2, y: 4, l: "two" },
{ x: 3, y: -2, l: "three" }
]}
style={{
data: { stroke: "tomato", strokeWidth: (d, active) => active ? 4 : 2 },
labels: { fill: "tomato" }
}}
/>

<VictoryLine
data={[
{ x: 1, y: -3, l: "red" },
{ x: 2, y: 5, l: "green" },
{ x: 3, y: 3, l: "blue" }
]}
style={{
data: { stroke: "blue", strokeWidth: (d, active) => active ? 4 : 2 },
labels: { fill: "blue" }
}}
/>

<VictoryLine
data={[
{ x: 1, y: 5, l: "cat" },
{ x: 2, y: -4, l: "dog" },
{ x: 3, y: -2, l: "bird" }
]}
style={{
data: { stroke: "black", strokeWidth: (d, active) => active ? 4 : 2 },
labels: { fill: "black" }
}}
/>
</VictoryChart>

<VictoryScatter
animate={{ duration: 1000 }}
style={{
parent: chartStyle.parent,
data: {
fill: (datum, active) => active ? "tomato" : "black"
}
}}
containerComponent={
<VictoryCursorContainer
cursorLabel={(d) => `${round(d.x, 2)}`}
dimension="x"
defaultCursorValue={1}
/>
}
size={(datum, active) => active ? 5 : 3}
data={this.state.data}
x="a"
y="b"
/>

<VictoryScatter
style={{
parent: chartStyle.parent,
data: {
fill: (datum, active) => active ? "tomato" : "black"
}
}}
containerComponent={
<VictoryCursorContainer
selectionStyle={{
stroke: "tomato", strokeWidth: 2, fill: "tomato", fillOpacity: 0.1
}}
/>
}
size={(datum, active) => active ? 5 : 3}
y={(d) => d.x * d.x}
/>

<VictoryChart
style={chartStyle}
containerComponent={
<VictoryCursorContainer
defaultCursorValue={2}
dimension="x"
cursorLabel={(datum) => round(datum.x, 2)}
cursorLabelOffset={15}
/>
}
>
<VictoryGroup style={chartStyle}>
<VictoryScatter
style={{
data: { fill: "tomato" }
}}
size={(datum, active) => active ? 5 : 3}
labels={(d) => d.y}
labelComponent={<VictoryTooltip/>}
data={[
{ x: 1, y: -5 },
{ x: 2, y: 4 },
{ x: 3, y: 2 },
{ x: 4, y: 0 },
{ x: 5, y: 1 },
{ x: 6, y: -3 },
{ x: 7, y: 3 }
]}
/>
<VictoryScatter
style={{
data: { fill: "blue" }
}}
size={(datum, active) => active ? 5 : 3}
labels={(d) => d.y}
labelComponent={<VictoryTooltip/>}
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 }
]}
labels={(d) => d.y}
labelComponent={<VictoryTooltip/>}
size={(datum, active) => active ? 5 : 3}
/>
</VictoryGroup>
</VictoryChart>

<VictoryStack style={chartStyle} containerComponent={<VictoryCursorContainer/>}>
<VictoryBar
style={{
data: {
fill: "tomato",
stroke: (d, active) => active ? "black" : "none",
strokeWidth: 2
}
}}
size={(datum, 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 }
]}
/>
<VictoryBar
style={{
data: {
fill: "orange",
stroke: (d, active) => active ? "black" : "none",
strokeWidth: 2
}
}}
size={(datum, 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 }
]}
/>
<VictoryBar
style={{
data: {
fill: "gold",
stroke: (d, 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>
</div>
</div>
);
}
}

export default App;
1 change: 1 addition & 0 deletions src/components/containers/brush-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ const Helpers = {
};

export default {
...Helpers,
onMouseDown: Helpers.onMouseDown.bind(Helpers),
onMouseUp: Helpers.onMouseUp.bind(Helpers),
onMouseLeave: Helpers.onMouseLeave.bind(Helpers),
Expand Down
Loading

0 comments on commit 8c73ecb

Please sign in to comment.