From 3de8a0a99f74cef8032d718e4b51d5c9c364db56 Mon Sep 17 00:00:00 2001 From: "Thomas F. K. Jorna" Date: Thu, 15 Jul 2021 13:54:55 +0200 Subject: made graph interactively tweakable --- app/components/graph/graph.tsx | 43 +++++++++-------------- app/components/index.ts | 1 + app/components/tweaks/tweaks.story.tsx | 15 ++++++++ app/components/tweaks/tweaks.tsx | 62 ++++++++++++++++++++++++++++++++++ 4 files changed, 94 insertions(+), 27 deletions(-) create mode 100644 app/components/tweaks/tweaks.story.tsx create mode 100644 app/components/tweaks/tweaks.tsx (limited to 'app/components') diff --git a/app/components/graph/graph.tsx b/app/components/graph/graph.tsx index 592e00b..934901f 100644 --- a/app/components/graph/graph.tsx +++ b/app/components/graph/graph.tsx @@ -6,13 +6,12 @@ import { color, typography } from "../../theme" import { Text } from "../" import { flatten } from "ramda" -import data from "../../data/miserables.json" -import genRandomTree from "../../data/randomdata"; +//import data from "../../data/miserables.json" +//import genRandomTree from "../../data/randomdata"; +import rando from "../../data/rando.json" import { ForceGraph2D, ForceGraph3D, ForceGraphVR, ForceGraphAR } from 'react-force-graph'; -import { GraphData, ForceGraphMethods } from "react-force-graph-2d"; import * as d3 from "d3-force"; -import Slider from '@react-native-community/slider'; const CONTAINER: ViewStyle = { justifyContent: "center", @@ -29,53 +28,43 @@ export interface GraphProps { * An optional style override useful for padding & margin. */ style?: StyleProp + physics + gData } /** * Describe your component here */ export const Graph = observer(function Graph(props: GraphProps) { - const { style } = props + const { style, physics, gData } = props const styles = flatten([CONTAINER, style]) const fgRef= useRef(); const GROUPS: number =12; - const gData = genRandomTree(); + //const gData = genRandomTree(200); - const [charge, setCharge] = useState(-30); - const [link, setLink] = useState(-30); + //const [charge, setCharge] = useState(-30); + //const [link, setLink] = useState(-30); useEffect(()=> { const fg = fgRef.current; - fg.d3Force('charge').strength(charge); - fg.d3Force('center').strength(0.05); - fg.d3Force('link').strength(0.1); - fg.d3Force('link').iterations(4); - fg.d3Force('collide', d3.forceCollide().radius(20)); + //fg.d3Force('center').strength(0.05); + fg.d3Force('link').strength(physics.linkStrength); + fg.d3Force('link').iterations(physics.linkIts); + physics.collision ? fg.d3Force('collide', d3.forceCollide().radius(20)) : fg.d3Force('collide',null); + fg.d3Force('charge').strength(physics.charge); }); return ( - {setCharge(value)}} - value={charge} - /> - {setCharge(value)}} - value={charge} - /> d.id%GROUPS} - linkAutoColorBy={d => gData.nodes[d.source].id%GROUPS} + linkAutoColorBy={d => rando.nodes[d.source].id%GROUPS} linkColor={"#ffffff"} linkWidth={2} //d3VelocityDecay={visco} diff --git a/app/components/index.ts b/app/components/index.ts index 9feec07..b770c09 100644 --- a/app/components/index.ts +++ b/app/components/index.ts @@ -11,3 +11,4 @@ export * from "./text-field/text-field" export * from "./wallpaper/wallpaper" export * from "./auto-image/auto-image" export * from "./graph/graph" +export * from "./tweaks/tweaks" diff --git a/app/components/tweaks/tweaks.story.tsx b/app/components/tweaks/tweaks.story.tsx new file mode 100644 index 0000000..7ff70d6 --- /dev/null +++ b/app/components/tweaks/tweaks.story.tsx @@ -0,0 +1,15 @@ +import * as React from "react" +import { storiesOf } from "@storybook/react-native" +import { StoryScreen, Story, UseCase } from "../../../storybook/views" +import { color } from "../../theme" +import { Tweaks } from "./tweaks" + +storiesOf("Tweaks", module) + .addDecorator((fn) => {fn()}) + .add("Style Presets", () => ( + + + + + + )) diff --git a/app/components/tweaks/tweaks.tsx b/app/components/tweaks/tweaks.tsx new file mode 100644 index 0000000..d1c8c2a --- /dev/null +++ b/app/components/tweaks/tweaks.tsx @@ -0,0 +1,62 @@ +import * as React from "react" +import { StyleProp, Switch, TextStyle, View, ViewStyle } from "react-native" +import { observer } from "mobx-react-lite" +import { color, typography } from "../../theme" +import { Text } from "../" +import { flatten } from "ramda" +import Slider from "@react-native-community/slider" +import { useState } from "react" + +const CONTAINER: ViewStyle = { + justifyContent: "center", +} + +const TEXT: TextStyle = { + fontFamily: typography.primary, + fontSize: 14, + color: color.primary, +} + +export interface TweaksProps { + /** + * An optional style override useful for padding & margin. + */ + style?: StyleProp + physics + setPhysics +} + +/** + * Describe your component here + */ +export const Tweaks = observer(function Tweaks(props: TweaksProps) { + const { style, physics, setPhysics } = props + const styles = flatten([CONTAINER, style]) + + return ( + <> + { setPhysics({...physics, charge: value}) }} + value={physics.charge} + step={1}/> + { setPhysics({...physics, linkStrength: value}) }} + value={physics.linkStrength} + step={1}/> + { setPhysics({...physics, linkIts: value}) }} + value={physics.linkIts} + step={1}/> + {setPhysics({...physics, collision: !physics.collision})}} + /> + + ) +}) -- cgit v1.2.3