Skip to content

Commit 8cc20bf

Browse files
committed
feat(core): add overlay operators * and /; demo OperatorPlayground showing multi-series overlay
1 parent 5a7ddc3 commit 8cc20bf

File tree

2 files changed

+49
-7
lines changed

2 files changed

+49
-7
lines changed

LeanPlot/Core.lean

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,29 @@ The result is a `Plot` containing all layers from both operands. -/
7070
instance (priority := 2000) [ToPlot α] [ToPlot β] : HAdd α β Plot where
7171
hAdd a b := Plot.overlay (toPlot a) (toPlot b)
7272

73+
/-- Generic `*` overlay via `[ToPlot]`. -/
74+
instance (priority := 2000) [ToPlot α] [ToPlot β] : HMul α β Plot where
75+
hMul a b := Plot.overlay (toPlot a) (toPlot b)
76+
77+
/-- Generic `/` overlay via `[ToPlot]`. Provided for API symmetry. -/
78+
instance (priority := 2000) [ToPlot α] [ToPlot β] : HDiv α β Plot where
79+
hDiv a b := Plot.overlay (toPlot a) (toPlot b)
80+
7381
/-! ### Render instance ---------------------------------------------------- -/
7482

7583
instance : Render Layer where
7684
render := Layer.html
7785

78-
/-- Render a plot by vertically stacking each layer inside a `<div>` container.
86+
/-- Render a plot by overlaying all layers in a single relative container.
7987
For line/scatter overlays we ideally want a *single* combined Recharts chart;
80-
that is a future optimisation. The vertical stack is "correct" though perhaps
81-
not visually perfect. -/
88+
that is a future optimisation. -/
8289
instance : Render Plot where
8390
render p :=
84-
-- simple flex column
85-
<div style={Json.mkObj [("display", "flex"), ("flex-direction", "column")]}>
86-
{... p.layers.map (fun l => l.html)}
87-
</div>
91+
let rows := (List.range p.layers.size).toArray.map (fun idx =>
92+
let l := p.layers[idx]!
93+
Html.element "div"
94+
#[("key", Json.str (toString idx))]
95+
#[l.html])
96+
Html.element "div"
97+
#[("style", Json.str "display:flex; flex-direction:column;")]
98+
rows
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import LeanPlot.Components
2+
import LeanPlot.Palette
3+
import LeanPlot.Plot
4+
import LeanPlot.Core
5+
import LeanPlot
6+
open LeanPlot.Components LeanPlot.Palette LeanPlot
7+
open Lean ProofWidgets
8+
open scoped ProofWidgets.Jsx
9+
10+
/-! # Operator playground demo – revised
11+
12+
React complained when stacking multiple full Recharts charts in one
13+
panel. The most reliable demonstration of an *overlay* is therefore to
14+
build **one** chart that already contains every series we want to show.
15+
16+
Below we construct a three-series chart (id, sqr, sqrt) using
17+
`sampleMany`. Render it with `#plot multiSeries`. -/
18+
19+
namespace LeanPlot.OperatorDemo
20+
21+
private def multiSeries : Html :=
22+
mkLineChartFull
23+
(sampleMany #[
24+
("id", fun x => x),
25+
("sqr", fun x => x*x),
26+
("sqrt", fun x => Float.sqrt x)])
27+
(autoColours #["id", "sqr", "sqrt"])
28+
29+
#plot multiSeries
30+
#plot line (fun x => x^3)
31+
end LeanPlot.OperatorDemo

0 commit comments

Comments
 (0)