Skip to content

Commit

Permalink
add ScatterPlot into TableInset showing periodicity trends when heat …
Browse files Browse the repository at this point in the history
…map selected

inspired by https://ptable.netlify.app (github.com/kadinzhang/Periodicity)
  • Loading branch information
janosh committed Jun 4, 2022
1 parent 35c512b commit efd2ed6
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 1 deletion.
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,16 @@
"devDependencies": {
"@sveltejs/adapter-static": "^1.0.0-next.32",
"@sveltejs/kit": "^1.0.0-next.345",
"@types/d3-array": "^3.0.3",
"@types/d3-interpolate-path": "^2.0.0",
"@types/d3-scale": "^4.0.2",
"@types/d3-shape": "^3.1.0",
"@typescript-eslint/eslint-plugin": "^5.26.0",
"@typescript-eslint/parser": "^5.26.0",
"d3-array": "^3.1.6",
"d3-interpolate-path": "^2.2.3",
"d3-scale": "^4.0.2",
"d3-shape": "^3.1.0",
"eslint": "^8.16.0",
"eslint-plugin-svelte3": "^4.0.0",
"hastscript": "^7.0.2",
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Periodic Table

[![Netlify Status](https://api.netlify.com/api/v1/badges/0238699e-17a8-4423-85de-a5ca30baff0d/deploy-status)](https://app.netlify.com/sites/periodic-table/deploys)
[![Netlify Status](https://api.netlify.com/api/v1/badges/42b5fd04-c538-4e3c-bd69-73e383989cfd/deploy-status)](https://app.netlify.com/sites/ptable-elements/deploys)
[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/janosh/periodic-table/main.svg?badge_token=nUqJfPCFS4uyMwcFSDIfdQ)](https://results.pre-commit.ci/latest/github/janosh/periodic-table/main?badge_token=nUqJfPCFS4uyMwcFSDIfdQ)

A dynamic Periodic Table component written in Svelte.
Expand Down
30 changes: 30 additions & 0 deletions src/lib/graph/DataPoint.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<script lang="ts">
import { cubicOut } from 'svelte/easing'
import { tweened } from 'svelte/motion'
export let x: number
export let y: number
export let fill = `gray`
const tX = tweened(0, { duration: 400, easing: cubicOut })
const tY = tweened(0, { duration: 400, easing: cubicOut })
$: tX.set(x)
$: tY.set(y)
</script>

<g transform="translate({$tX} {$tY})" {fill} on:mouseenter>
<circle cx="0" cy="0" r="3" />
</g>

<style>
circle {
transition: 0.3s;
stroke: transparent;
stroke-width: 5px;
}
g:hover circle {
fill: orange;
transform: scale(2);
}
</style>
33 changes: 33 additions & 0 deletions src/lib/graph/Line.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<script lang="ts">
import { interpolatePath } from 'd3-interpolate-path'
import { curveMonotoneX, line } from 'd3-shape'
import { cubicOut } from 'svelte/easing'
import { tweened } from 'svelte/motion'
export let data: [number, number, ...unknown[]][]
const lineGenerator = line()
.x((point) => point[0])
.y((point) => point[1])
.curve(curveMonotoneX)
const tLinePath = tweened(``, {
duration: 400,
easing: cubicOut,
interpolate: interpolatePath,
})
$: linePath = lineGenerator(data)
$: tLinePath.set(linePath)
</script>

<path d={$tLinePath} />

<style>
path {
fill: none;
stroke: teal;
stroke-width: 2;
}
</style>
50 changes: 50 additions & 0 deletions src/lib/graph/ScatterPlot.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<script lang="ts">
import { extent } from 'd3-array'
import { scaleLinear } from 'd3-scale'
import { active_element } from '../../stores'
import { Element } from '../../types'
import Datapoint from './Datapoint.svelte'
import Line from './Line.svelte'
export let data: [number, number, Element][]
export let colorscale: (y: number) => string | undefined = () => undefined
const padding = 10 // pixels
let width: number
let height: number
$: xrange = extent(data.map((point) => point[0]))
$: yrange = extent(data.map((point) => point[1]))
$: x_scale = scaleLinear()
.domain(xrange)
.range([padding, width - padding])
$: y_scale = scaleLinear()
.domain(yrange)
.range([height - padding, padding])
$: scaled_data = data
.filter(([x, y]) => !(isNaN(x) || isNaN(y) || x === null || y === null))
.map(([x, y, ...rest]) => [x_scale(x), y_scale(y), colorscale(y), ...rest])
</script>

<div class="scatter" bind:clientWidth={width} bind:clientHeight={height}>
{#if width && height}
<svg {width} {height}>
<Line data={scaled_data} />
{#each scaled_data as [x, y, fill, element]}
<Datapoint {x} {y} {fill} on:mouseenter={() => ($active_element = element)} />
{/each}
</svg>
{/if}
</div>

<style>
.scatter {
width: 100%;
height: 100%;
display: flex;
overflow: hidden;
}
</style>
51 changes: 51 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,40 @@
magic-string "^0.26.2"
svelte-hmr "^0.14.11"

"@types/d3-array@^3.0.3":
version "3.0.3"
resolved "https://registry.yarnpkg.com/@types/d3-array/-/d3-array-3.0.3.tgz#87d990bf504d14ad6b16766979d04e943c046dac"
integrity sha512-Reoy+pKnvsksN0lQUlcH6dOGjRZ/3WRwXR//m+/8lt1BXeI4xyaUZoqULNjyXXRuh0Mj4LNpkCvhUpQlY3X5xQ==

"@types/d3-interpolate-path@^2.0.0":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@types/d3-interpolate-path/-/d3-interpolate-path-2.0.0.tgz#5c2fbe0ad1072576b7d9e16227422919c68be503"
integrity sha512-9YpwRqlDWrEanka86O4xEqFd1xVUgm+bD6yEayp8GxxcLIiax58TgWcCUdpLibSSLzYLv7Yuu4YL3Btwvzhsow==

"@types/d3-path@*":
version "3.0.0"
resolved "https://registry.yarnpkg.com/@types/d3-path/-/d3-path-3.0.0.tgz#939e3a784ae4f80b1fde8098b91af1776ff1312b"
integrity sha512-0g/A+mZXgFkQxN3HniRDbXMN79K3CdTpLsevj+PXiTcb2hVyvkZUBg37StmgCQkaD84cUJ4uaDAWq7UJOQy2Tg==

"@types/d3-scale@^4.0.2":
version "4.0.2"
resolved "https://registry.yarnpkg.com/@types/d3-scale/-/d3-scale-4.0.2.tgz#41be241126af4630524ead9cb1008ab2f0f26e69"
integrity sha512-Yk4htunhPAwN0XGlIwArRomOjdoBFXC3+kCxK2Ubg7I9shQlVSJy/pG/Ht5ASN+gdMIalpk8TJ5xV74jFsetLA==
dependencies:
"@types/d3-time" "*"

"@types/d3-shape@^3.1.0":
version "3.1.0"
resolved "https://registry.yarnpkg.com/@types/d3-shape/-/d3-shape-3.1.0.tgz#1d87a6ddcf28285ef1e5c278ca4bdbc0658f3505"
integrity sha512-jYIYxFFA9vrJ8Hd4Se83YI6XF+gzDL1aC5DCsldai4XYYiVNdhtpGbA/GM6iyQ8ayhSp3a148LY34hy7A4TxZA==
dependencies:
"@types/d3-path" "*"

"@types/d3-time@*":
version "3.0.0"
resolved "https://registry.yarnpkg.com/@types/d3-time/-/d3-time-3.0.0.tgz#e1ac0f3e9e195135361fa1a1d62f795d87e6e819"
integrity sha512-sZLCdHvBUcNby1cB6Fd3ZBrABbjz3v1Vm90nysCQ6Vt7vd6e/h9Lt7SiJUoEX0l4Dzc7P5llKyhqSi1ycSf1Hg==

"@types/hast@^2.0.0":
version "2.3.4"
resolved "https://registry.yarnpkg.com/@types/hast/-/hast-2.3.4.tgz#8aa5ef92c117d20d974a82bdfb6a648b08c0bafc"
Expand Down Expand Up @@ -382,13 +416,23 @@ cross-spawn@^7.0.2:
resolved "https://registry.yarnpkg.com/d3-format/-/d3-format-3.1.0.tgz#9260e23a28ea5cb109e93b21a06e24e2ebd55641"
integrity sha512-YyUI6AEuY/Wpt8KWLgZHsIU86atmikuoOmCfommt0LYHiQSPjvX2AcFc38PX0CBpr2RCyZhjex+NS/LPOv6YqA==

d3-interpolate-path@^2.2.3:
version "2.2.3"
resolved "https://registry.yarnpkg.com/d3-interpolate-path/-/d3-interpolate-path-2.2.3.tgz#7a2a906231c316c5166872c13400aae918e604fa"
integrity sha512-1i1WUmOzSHI7nYqhjnL9v/gA5VQNgMMfX4u1Emv2vKmUUQsRPx/Ghl/AvcyOq3GRINW8iSz5elCdoHX0imyWCg==

"d3-interpolate@1.2.0 - 3":
version "3.0.1"
resolved "https://registry.yarnpkg.com/d3-interpolate/-/d3-interpolate-3.0.1.tgz#3c47aa5b32c5b3dfb56ef3fd4342078a632b400d"
integrity sha512-3bYs1rOD33uo8aqJfKP3JWPAibgw8Zm2+L9vBKEHJ2Rg+viTR7o5Mmv5mZcieN+FRYaAOWX5SJATX6k1PWz72g==
dependencies:
d3-color "1 - 3"

"d3-path@1 - 3":
version "3.0.1"
resolved "https://registry.yarnpkg.com/d3-path/-/d3-path-3.0.1.tgz#f09dec0aaffd770b7995f1a399152bf93052321e"
integrity sha512-gq6gZom9AFZby0YLduxT1qmrp4xpBA1YZr19OI717WIdKE2OM5ETq5qrHLb301IgxhLwcuxvGZVLeeWc/k1I6w==

d3-scale@^4.0.2:
version "4.0.2"
resolved "https://registry.yarnpkg.com/d3-scale/-/d3-scale-4.0.2.tgz#82b38e8e8ff7080764f8dcec77bd4be393689396"
Expand All @@ -400,6 +444,13 @@ d3-scale@^4.0.2:
d3-time "2.1.1 - 3"
d3-time-format "2 - 4"

d3-shape@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/d3-shape/-/d3-shape-3.1.0.tgz#c8a495652d83ea6f524e482fca57aa3f8bc32556"
integrity sha512-tGDh1Muf8kWjEDT/LswZJ8WF85yDZLvVJpYU9Nq+8+yW1Z5enxrmXOhTArlkaElU+CTn0OTVNli+/i+HP45QEQ==
dependencies:
d3-path "1 - 3"

"d3-time-format@2 - 4":
version "4.1.0"
resolved "https://registry.yarnpkg.com/d3-time-format/-/d3-time-format-4.1.0.tgz#7ab5257a5041d11ecb4fe70a5c7d16a195bb408a"
Expand Down

0 comments on commit efd2ed6

Please sign in to comment.