Skip to content

Commit

Permalink
[storybook] Adds a story to show to handle nodes and edges opacity
Browse files Browse the repository at this point in the history
  • Loading branch information
jacomyal committed May 27, 2024
1 parent 8e52503 commit 68da672
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
16 changes: 16 additions & 0 deletions packages/storybook/stories/nodes-edges-opacity/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<style>
body {
font-family: sans-serif;
}
html,
body,
#storybook-root,
#sigma-container {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
overflow: hidden;
}
</style>
<div id="sigma-container"></div>
44 changes: 44 additions & 0 deletions packages/storybook/stories/nodes-edges-opacity/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* This example shows how to render nodes and edges with various opacities
*/
import chroma from "chroma-js";
import Graph from "graphology";
import Sigma from "sigma";

import data from "../../public/data.json";
import { onStoryDown } from "../utils";

const DEFAULT_ARGS = {
nodesAlpha: 0.5,
edgesAlpha: 0.5,
};

export default (input: { args: typeof DEFAULT_ARGS }) => {
const args = {
...DEFAULT_ARGS,
...input.args,
};

const container = document.getElementById("sigma-container") as HTMLElement;

const graph = new Graph();
graph.import(data);
const renderer = new Sigma(graph, container, {
enableEdgeEvents: true,
edgeReducer: (_, attr) => {
attr.color = chroma(attr.color || "#000000")
.alpha(args.edgesAlpha)
.hex();
return attr;
},
nodeReducer: (_, attr) => {
attr.color = chroma(attr.color).alpha(args.nodesAlpha).hex();
attr.label = null;
return attr;
},
});

onStoryDown(() => {
renderer?.kill();
});
};
45 changes: 45 additions & 0 deletions packages/storybook/stories/nodes-edges-opacity/stories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import type { Meta, StoryObj } from "@storybook/web-components";

import play from "./index";
import template from "./index.html?raw";
import source from "./index?raw";

type StoryArgs = {
nodesAlpha: number;
edgesAlpha: number;
};

const meta: Meta<StoryArgs> = {
id: "nodes-edges-opacity",
title: "Examples",
argTypes: {
nodesAlpha: {
name: "Nodes alpha",
defaultValue: 0.5,
control: { type: "number", step: "0.05", min: "0", max: "1" },
},
edgesAlpha: {
name: "Edges alpha",
defaultValue: 0.5,
control: { type: "number", step: "0.05", min: "0", max: "1" },
},
},
};
export default meta;

type Story = StoryObj<StoryArgs>;

export const story: Story = {
name: "Display nodes and edges with opacity",
render: () => template,
play,
args: {
nodesAlpha: 0.5,
edgesAlpha: 0.5,
},
parameters: {
storySource: {
source,
},
},
};

0 comments on commit 68da672

Please sign in to comment.