Skip to content

Commit

Permalink
Merge pull request #1258 from FormidableLabs/experiment/horizontal
Browse files Browse the repository at this point in the history
Improve horizontal charts
  • Loading branch information
boygirl authored Feb 27, 2019
2 parents f1e2daa + 8efc260 commit 8973408
Show file tree
Hide file tree
Showing 79 changed files with 1,790 additions and 923 deletions.
4 changes: 3 additions & 1 deletion demo/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import VictoryLegendDemo from "./components/victory-legend-demo";
import VictoryPieDemo from "./components/victory-pie-demo";
import VictoryDemo from "./components/victory-demo";
import HorizontalDemo from "./components/horizontal-demo";
import DraggableDemo from "./components/draggable-demo";

const MAP = {
"/axis": { component: AxisDemo, name: "AxisDemo" },
Expand Down Expand Up @@ -67,7 +68,8 @@ const MAP = {
"/legend": { component: VictoryLegendDemo, name: "LegendDemo" },
"/pie": { component: VictoryPieDemo, name: "PieDemo" },
"/victory": { component: VictoryDemo, name: "VictoryDemo" },
"/horizontal": { component: HorizontalDemo, name: "HorizontalDemo" }
"/horizontal": { component: HorizontalDemo, name: "HorizontalDemo" },
"/draggable": { component: DraggableDemo, name: "DraggableDemo" }
};

class Home extends React.Component {
Expand Down
13 changes: 12 additions & 1 deletion demo/components/debug-demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ class App extends React.Component {
</VictoryChart>

<VictoryChart
horizontal
style={chartStyle}
containerComponent={<VictoryVoronoiContainer labels={() => "HELLO"} />}
>
<VictoryBar
horizontal
style={{ data: { fill: (d, a) => (a ? "red" : "black") } }}
data={[
{ x: 0, y: 3.5, y0: 2.5 },
Expand All @@ -69,6 +69,17 @@ class App extends React.Component {
{ x: 2, y: 12, y0: 7 }
]}
/>
<VictoryScatter
style={{ data: { fill: (d, a) => (a ? "black" : "red") } }}
data={[
{ x: 0, y: 3.5 },
{ x: 0, y: 11 },
{ x: 1, y: 2 },
{ x: 1, y: 7 },
{ x: 2, y: 4 },
{ x: 2, y: 12 }
]}
/>
</VictoryChart>

<VictoryChart
Expand Down
240 changes: 240 additions & 0 deletions demo/components/draggable-demo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
/*eslint-disable no-magic-numbers,react/no-multi-comp */

import React from "react";
import { VictoryChart } from "../../packages/victory-chart/src/index";
import { VictoryAxis } from "../../packages/victory-axis/src/index";
import { VictoryBar } from "../../packages/victory-bar/src/index";
import { VictoryBrushLine } from "../../packages/victory-brush-line/src/index";
import { VictoryScatter } from "../../packages/victory-scatter/src/index";
import { VictoryClipContainer, Point, Selection } from "../../packages/victory-core/src/index";
import { VictoryZoomContainer } from "../../packages/victory-zoom-container/src/index";
import { VictoryBrushContainer } from "../../packages/victory-brush-container/src/index";

const bars = [
{ name: "SEA", range: [new Date(2013, 1, 1), new Date(2019, 1, 1)] },
{ name: "HKG", range: [new Date(2015, 1, 1), new Date(2015, 5, 1)] },
{ name: "LHR", range: [new Date(2016, 5, 1), new Date(2019, 1, 1)] },
{ name: "DEN", range: [new Date(2018, 8, 1), new Date(2019, 1, 1)] }
];

const points = [
{ name: "SEA", date: new Date(2012, 9, 1) },
{ name: "HKG", date: new Date(2014, 3, 1) },
{ name: "LHR", date: new Date(2015, 6, 1) },
{ name: "DEN", date: new Date(2018, 3, 1) }
];

class DraggablePoint extends React.Component {
static defaultEvents = [
{
target: "data",
eventHandlers: {
onMouseOver: (evt, targetProps) => {
return [
{
mutation: () => Object.assign({}, targetProps, { active: true })
}
];
},
onMouseDown: (evt, targetProps) => {
return [
{
mutation: () => Object.assign({}, targetProps, { dragging: true })
}
];
},
onMouseMove: (evt, targetProps) => {
const { scale, onPointChange, datum } = targetProps;
if (targetProps.dragging) {
const { x } = Selection.getSVGEventCoordinates(evt);
const point = scale.y.invert(x);
const name = datum.name;
onPointChange({ name, date: point });
return [
{
mutation: () => Object.assign({}, targetProps, { x })
}
];
}
return null;
},
onMouseUp: (evt, targetProps) => {
return [
{
mutation: () => Object.assign({}, targetProps, { dragging: false, active: false })
}
];
},
onMouseLeave: (evt, targetProps) => {
return [
{
mutation: () => Object.assign({}, targetProps, { dragging: false, active: false })
}
];
}
}
}
];

render() {
return <Point {...this.props} />;
}
}

class App extends React.Component {
constructor() {
super();
this.state = { bars, points };
}

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

onDomainChange(domain, props) {
const { name } = props;
const newBars = this.state.bars.map((bar) =>
bar.name === name ? { name, range: domain } : bar
);
this.setState({ bars: newBars });
}

onPointChange(point) {
const newPoints = this.state.points.map((p) => (p.name === point.name ? point : p));
this.setState({ points: newPoints });
}

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

const sharedProps = {
width: 800,
domain: { y: [new Date(2012, 1, 1), new Date(2019, 1, 1)], x: [0.5, 4.5] }
};

return (
<div style={containerStyle}>
<VictoryChart
horizontal
{...sharedProps}
height={400}
padding={{ top: 50, left: 50, right: 30, bottom: 0 }}
containerComponent={
<VictoryZoomContainer
responsive={false}
allowPan={false}
zoomDomain={this.state.zoomDomain}
zoomDimension="y"
onZoomDomainChange={this.handleZoom.bind(this)}
clipContainerComponent={
<VictoryClipContainer clipPadding={{ top: 15, bottom: 15 }} />
}
/>
}
>
<VictoryAxis
style={{
axis: { stroke: "none" }
}}
/>

{bars.map((bar, index) => (
<VictoryAxis
dependentAxis
key={index}
axisComponent={
<VictoryBrushLine
name={bar.name}
width={20}
allowDraw={false}
brushDomain={bar.range}
onBrushDomainChange={this.onDomainChange.bind(this)}
brushStyle={{
fill: "skyBlue",
opacity: (d, a) => (a ? 1 : 0.5)
}}
/>
}
style={{
axis: { stroke: "none" }
}}
axisValue={bar.name}
tickFormat={() => ""}
/>
))}
<VictoryScatter
data={points}
dataComponent={<DraggablePoint onPointChange={this.onPointChange.bind(this)} />}
style={{
data: {
fill: "skyBlue",
opacity: (d, a) => (a ? 1 : 0.5),
cursor: "move"
}
}}
x="name"
y="date"
size={10}
/>
</VictoryChart>
<VictoryChart
horizontal
{...sharedProps}
padding={{ top: 30, left: 50, right: 30, bottom: 0 }}
scale={{ y: "time" }}
height={120}
containerComponent={
<VictoryBrushContainer
responsive={false}
brushDomain={this.state.zoomDomain}
brushDimension="y"
onBrushDomainChange={this.handleZoom.bind(this)}
/>
}
>
<VictoryAxis
style={{
axis: { stroke: "none" }
}}
/>
<VictoryAxis
dependentAxis
orientation="top"
style={{
axis: { stroke: "none" },
tickLabels: { fontSize: 20 }
}}
tickCount={3}
tickFormat={(t) => t.getFullYear()}
/>
<VictoryScatter
data={this.state.points}
size={5}
style={{
data: { fill: "skyBlue" }
}}
x="name"
y="date"
/>
<VictoryBar
style={{
data: { fill: "skyBlue" }
}}
data={this.state.bars}
x="name"
y={(d) => d.range[0]}
y0={(d) => d.range[1]}
/>
</VictoryChart>
</div>
);
}
}

export default App;
Loading

0 comments on commit 8973408

Please sign in to comment.