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

Zoom and Brush container fixes #475

Merged
merged 3 commits into from
Jun 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions demo/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import CreateContainerDemo from "./components/create-container-demo";
import BrushContainerDemo from "./components/victory-brush-container-demo";
import AnimationDemo from "./components/animation-demo";
import SelectionDemo from "./components/selection-demo";
import DebugDemo from "./components/debug-demo";

class Home extends React.Component {
render() {
Expand Down Expand Up @@ -69,6 +70,7 @@ class App extends React.Component {
case "/animation": Child = AnimationDemo; break;
case "/selection-container": Child = SelectionDemo; break;
case "/create-container": Child = CreateContainerDemo; break;
case "/debug": Child = DebugDemo; break;
default: Child = Home;
}
return Child;
Expand Down
66 changes: 66 additions & 0 deletions demo/components/debug-demo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*eslint-disable no-magic-numbers */
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A demo to work through bug reproductions. Intensionally not shown on the demo table of contents

import React from "react";
import {
VictoryChart, VictoryLine, VictoryGroup, VictoryZoomContainer
} from "../../src/index";

const edata = [
{ 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 }
];

class App extends React.Component {
constructor() {
super();
this.state = { data: edata.slice(3) };
}


handleDomainChange(domain) {
this.setState({ selectedDomain: domain });
}
changeDataSet(data) {
this.setState({
data,
selectedDomain: { x: [data[0].x, data[data.length - 1].x] }
});
}

render() {
return (
<div style={{ width: 300 }}>
<button onClick={() => this.changeDataSet(edata.slice(3))}>Part</button>
<button onClick={() => this.changeDataSet(edata)}>All</button>
<VictoryChart
containerComponent={
<VictoryZoomContainer
dimension="x"
onDomainChange={this.handleDomainChange.bind(this)}
zoomDomain={this.state.selectedDomain}
/>
}
style={{ parent: { cursor: "pointer" } }}
>
<VictoryGroup

data={this.state.data}
>
<VictoryLine/>
</VictoryGroup>
</VictoryChart>
<p>
currentDomain (domain),
domain,
cachedZoomDomain
</p>
</div>
);
}
}

export default App;
10 changes: 4 additions & 6 deletions demo/components/victory-brush-container-demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ class App extends React.Component {
}

handleZoom(domain) {
this.setState({ selectedDomain: domain });
}

handleBrush(domain) {
this.setState({ zoomDomain: domain });
}

Expand All @@ -37,6 +33,7 @@ class App extends React.Component {
containerComponent={
<VictoryZoomContainer responsive={false}
zoomDomain={this.state.zoomDomain}
dimension="x"
onDomainChange={this.handleZoom.bind(this)}
/>
}
Expand All @@ -63,8 +60,9 @@ class App extends React.Component {
width={800} height={100} scale={{ x: "time" }}
containerComponent={
<VictoryBrushContainer responsive={false}
selectedDomain={this.state.selectedDomain}
onDomainChange={this.handleBrush.bind(this)}
selectedDomain={this.state.zoomDomain}
dimension="x"
onDomainChange={this.handleZoom.bind(this)}
/>
}
>
Expand Down
3 changes: 2 additions & 1 deletion demo/components/victory-zoom-container-demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export default class App extends React.Component {
/>

<VictoryGroup
containerComponent={<VictoryZoomContainer/>}
containerComponent={<VictoryZoomContainer dimension="y"/>}
style={{ parent: parentStyle }} data={this.state.transitionData}
>
<VictoryLine animate={{ duration: 1500 }} style={{ data: this.state.style }} />
Expand All @@ -99,6 +99,7 @@ export default class App extends React.Component {
containerComponent={
<VictoryZoomContainer
zoomDomain={{ x: [new Date(1993, 1, 1), new Date(2005, 1, 1)] }}
dimension="x"
/>
}
scale={{
Expand Down
13 changes: 8 additions & 5 deletions src/components/containers/brush-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,11 +250,14 @@ const Helpers = {
}];
},

onMouseLeave() {
return [{
target: "parent",
mutation: () => ({ isPanning: false, isSelecting: false })
}];
onMouseLeave(evt) {
if (evt.target.nodeName === "svg") {
return [{
target: "parent",
mutation: () => ({ isPanning: false, isSelecting: false })
}];
}
return [];
}
};

Expand Down
36 changes: 19 additions & 17 deletions src/components/containers/zoom-helpers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*eslint no-magic-numbers: ["error", { "ignore": [-1, 0, 1, 2, 1000] }]*/
import { Selection, Collection } from "victory-core";
import { throttle, isFunction, defaults } from "lodash";
import { throttle, isFunction, defaults, isEqual } from "lodash";
import { attachId } from "../../helpers/event-handlers.js";

const Helpers = {
Expand Down Expand Up @@ -128,26 +128,32 @@ const Helpers = {
return undefined;
},

getLastDomain(targetProps, originalDomain) {
const { zoomDomain, cachedZoomDomain, currentDomain, domain } = targetProps;
if (zoomDomain && !isEqual(zoomDomain, cachedZoomDomain)) {
return defaults(
{}, zoomDomain, domain
);
}
return defaults(
{}, currentDomain || zoomDomain || originalDomain, domain
);
},

getDomain(props) {
const { originalDomain, domain, scale } = props;
const scaleDomain = { x: scale.x.domain(), y: scale.y.domain() };
return defaults({}, originalDomain, domain, scaleDomain);
},

onMouseDown(evt, targetProps) {
onMouseDown(evt) {
evt.preventDefault();
const { domain, zoomDomain } = targetProps;
const originalDomain = this.getDomain(targetProps);
const currentDomain = defaults(
{}, targetProps.currentDomain || zoomDomain || originalDomain, domain
);
const { x, y } = Selection.getSVGEventCoordinates(evt);
return [{
target: "parent",
mutation: () => {
return {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no reason why onMouseDown needs to squash values for the various *domain props. this makes the this.getLastDomain fix work.

startX: x, startY: y, domain: currentDomain, cachedZoomDomain: zoomDomain,
originalDomain, currentDomain, panning: true,
startX: x, startY: y, panning: true,
parentControlledProps: ["domain"]
};
}
Expand All @@ -174,12 +180,10 @@ const Helpers = {

onMouseMove(evt, targetProps, eventKey, ctx) { // eslint-disable-line max-params
if (targetProps.panning) {
const { scale, startX, startY, onDomainChange, dimension, domain, zoomDomain } = targetProps;
const { scale, startX, startY, onDomainChange, dimension, zoomDomain } = targetProps;
const { x, y } = Selection.getSVGEventCoordinates(evt);
const originalDomain = this.getDomain(targetProps);
const lastDomain = defaults(
{}, targetProps.currentDomain || zoomDomain || originalDomain, domain
);
const lastDomain = this.getLastDomain(targetProps, originalDomain);
const dx = (startX - x) / this.getDomainScale(lastDomain, scale, "x");
const dy = (y - startY) / this.getDomainScale(lastDomain, scale, "y");
const currentDomain = {
Expand Down Expand Up @@ -208,11 +212,9 @@ const Helpers = {
if (!targetProps.allowZoom) {
return {};
}
const { onDomainChange, dimension, domain, zoomDomain } = targetProps;
const { onDomainChange, dimension, zoomDomain } = targetProps;
const originalDomain = this.getDomain(targetProps);
const lastDomain = defaults(
{}, targetProps.currentDomain || zoomDomain || originalDomain, domain
);
const lastDomain = this.getLastDomain(targetProps, originalDomain);
const { x, y } = lastDomain;
const currentDomain = {
x: dimension === "y" ? lastDomain.x : this.scale(x, evt, targetProps, "x"),
Expand Down