Skip to content

Instantly share code, notes, and snippets.

@auycro
Last active February 13, 2022 04:15
Show Gist options
  • Save auycro/d64574a41b6da75b31a377ffcfb4e643 to your computer and use it in GitHub Desktop.
Save auycro/d64574a41b6da75b31a377ffcfb4e643 to your computer and use it in GitHub Desktop.
Svelte with Cytoscape Example
<!--
---Created by Gumpanat Keardkeawfa on 2022Feb13.---
---Copyright © 2022 Auycro. All rights reserved.---
-->
<script>
import cytoscape from 'cytoscape'
import { onMount } from 'svelte';
const endpoint = "https://js.cytoscape.org/demos/grid-layout/data.json";
let data = [];
let cyInstance;
let chartCanvas;
onMount(async (promise) => {
const res = await fetch(endpoint);
data = await res.json();
//console.log(data);
cyInstance = cytoscape({
container: chartCanvas,
elements: data,
layout: {
name: 'grid',
},
boxSelectionEnabled: false,
autounselectify: true,
style: [
{
selector: 'node',
style: {
'height': 20,
'width': 20,
'background-color': '#18e018',
'opacity': 0.5,
}
},
{
selector: 'edge',
style: {
'curve-style': 'haystack',
'haystack-radius': 0,
'width': 5,
'opacity': 0.5,
'line-color': '#a2efa2'
}
}
],
});
//console.log(cyInstance.container());
});
</script>
<!--
<div class="foobar">
{#each data as item}
{#if item.group=="nodes"}
{item.position.x},{item.position.y}<br/>
{/if}
{:else}
<p>loading...</p>
{/each}
</div>
-->
<div bind:this={chartCanvas} class="foo"></div>
<style>
.foo {
width: 300px;
height: 300px;
display: block;
}
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment