-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
24542a0
commit 5f6a692
Showing
2 changed files
with
50 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,44 @@ | ||
/** @jsx h */ | ||
/** @jsxFrag Fragment */ | ||
import {h, Fragment} from 'preact'; | ||
import {useEffect, useRef, useState} from 'preact/hooks'; | ||
import { h, Fragment } from 'preact'; | ||
import { useEffect, useRef, useState } from 'preact/hooks'; | ||
import register from "preact-custom-element"; | ||
|
||
import * as Plot from "@observablehq/plot"; | ||
// import * as d3 from "d3"; | ||
|
||
function RfPlotBar({dataSelector = ""}) { | ||
function RfPlotBar({ dataJsonSelector = "", dataJsonField = "" }) { | ||
const containerRef = useRef(); | ||
const [data, setData] = useState(); | ||
const [data, setData] = useState(); | ||
|
||
useEffect(() => { | ||
// d3.csv("/-_-/ui/static/Get-Fit-History-Daily-overview.csv", d3.autoType).then(setData); | ||
const jsonStr = document.querySelector(dataSelector).text; | ||
const dataJson = JSON.parse(jsonStr); | ||
setData(dataJson); | ||
}, []); | ||
useEffect(() => { | ||
if (!dataJsonSelector || !dataJsonField) { | ||
return; | ||
} | ||
const jsonStr = document.querySelector(dataJsonSelector).text; | ||
const dataJson = JSON.parse(jsonStr); | ||
setData(dataJson[dataJsonField]); | ||
}, []); | ||
|
||
useEffect(() => { | ||
if (data === undefined) return; | ||
const plot = Plot.plot({ | ||
y: {grid: true}, | ||
color: {scheme: "burd"}, | ||
marks: [ | ||
Plot.ruleY([0]), | ||
Plot.dot(data, {x: "tsHourMs", y: "totalVisits", stroke: "ruleUrl"}) | ||
] | ||
}); | ||
containerRef.current.append(plot); | ||
return () => plot.remove(); | ||
}, [data]); | ||
useEffect(() => { | ||
if (data === undefined) return; | ||
|
||
return <div ref={containerRef} />; | ||
const plot = Plot.plot({ | ||
height: 200, | ||
y: { label: "Total Visits", grid: true }, | ||
x: { label: "Date UTC (1h)", type: "utc" }, | ||
marks: [ | ||
// Make the zero-line bold. | ||
Plot.ruleY([0]), | ||
Plot.rectY(data, { x: "tsHourMs", y: "totalVisits", r: 2, fill: "var(--pico-primary)", interval: "hour" }), | ||
Plot.tip(data, Plot.pointerX({x: "tsHourMs", y: "totalVisits" })), | ||
Plot.crosshair(data, {x: "tsHourMs", y: "totalVisits"}), | ||
] | ||
}); | ||
|
||
containerRef.current.append(plot); | ||
return () => plot.remove(); | ||
}, [data]); | ||
|
||
return <div ref={containerRef} />; | ||
} | ||
register(RfPlotBar, "rf-plot-bar", ["data-selector"], { shadow: false }); | ||
register(RfPlotBar, "rf-plot-bar", ["data-json-selector", "data-json-field"], { shadow: false }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters