Skip to content

Commit

Permalink
fix(Arc|Pie|PieChart): Consider width when responsively setting outer…
Browse files Browse the repository at this point in the history
…Radius (non-fixed value)
  • Loading branch information
techniq committed Feb 5, 2025
1 parent 4f77d23 commit 54581a4
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 63 deletions.
5 changes: 5 additions & 0 deletions .changeset/mean-mails-glow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'layerchart': patch
---

fix(Arc|Pie|PieChart): Consider width when responsively setting outerRadius (non-fixed value)
5 changes: 5 additions & 0 deletions .changeset/silent-knives-move.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'layerchart': patch
---

docs(Arc|Pie|PieChart): Allow all examples to resize to demonsrtrate responsiveness
9 changes: 3 additions & 6 deletions packages/layerchart/src/lib/components/Arc.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,7 @@
export let innerRadius: number | undefined = undefined;
/**
* Define outerRadius. Defaults to yRange max / 2 (ie. chart height / 2)
*/
/**
* Define outerRadius. Defaults to yRange max (ie. chart height / 2)
* Define outerRadius. Defaults to smallest width (xRange) or height (yRange) dimension (/ 2)
* • value >= 1: discrete value
* • value < 1: percent of chart height / 2
* • value < 0: offset of chart height / 2
Expand All @@ -100,7 +97,7 @@
export let onpointermove: ((e: PointerEvent) => void) | undefined = undefined;
export let onpointerleave: ((e: PointerEvent) => void) | undefined = undefined;
const { yRange } = chartContext();
const { xRange, yRange } = chartContext();
$: scale = scaleLinear().domain(domain).range(range);
Expand All @@ -122,7 +119,7 @@
}
}
$: _outerRadius = getOuterRadius(outerRadius, (max<number>($yRange) ?? 0) / 2);
$: _outerRadius = getOuterRadius(outerRadius, (Math.min($xRange[1], $yRange[0]) ?? 0) / 2);
function getInnerRadius(innerRadius: number | undefined, outerRadius: number) {
if (innerRadius == null) {
Expand Down
2 changes: 1 addition & 1 deletion packages/layerchart/src/lib/components/Pie.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
export let sort: ((a: any, b: any) => number) | null | undefined = undefined;
const { data: contextData, x, y, xRange, c, cScale, config, width, height } = chartContext();
const { data: contextData, x, y, xRange, c, cScale, config } = chartContext();
// @ts-expect-error
$: resolved_endAngle = endAngle ?? degreesToRadians($config.xRange ? max($xRange) : max(range));
Expand Down
40 changes: 20 additions & 20 deletions packages/layerchart/src/routes/docs/components/Pie/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<h2>Basic</h2>

<Preview {data}>
<div class="h-[300px] p-4 border rounded">
<div class="h-[300px] p-4 border rounded resize overflow-auto">
<Chart {data} x="value" c="date" cRange={keyColors}>
<Svg center>
<Pie />
Expand All @@ -49,7 +49,7 @@
<h2>Disable sorting</h2>

<Preview {data}>
<div class="h-[300px] p-4 border rounded">
<div class="h-[300px] p-4 border rounded resize overflow-auto">
<Chart {data} x="value" c="date" cRange={keyColors}>
<Svg center>
<Pie sort={null} />
Expand All @@ -61,7 +61,7 @@
<h2>Partial range (Chart xRange)</h2>

<Preview {data}>
<div class="h-[300px] p-4 border rounded">
<div class="h-[300px] p-4 border rounded resize overflow-auto">
<Chart {data} x="value" xRange={[-90, 90]} c="date" cRange={keyColors}>
<Svg center>
<Pie />
Expand All @@ -73,7 +73,7 @@
<h2>Partial range (range prop)</h2>

<Preview {data}>
<div class="h-[300px] p-4 border rounded">
<div class="h-[300px] p-4 border rounded resize overflow-auto">
<Chart {data} x="value" c="date" cRange={keyColors}>
<Svg center>
<Pie range={[-90, 90]} />
Expand All @@ -85,7 +85,7 @@
<h2>Pad angle</h2>

<Preview {data}>
<div class="h-[300px] p-4 border rounded">
<div class="h-[300px] p-4 border rounded resize overflow-auto">
<Chart {data} x="value" c="date" cRange={keyColors}>
<Svg center>
<Pie padAngle={0.05} />
Expand All @@ -97,7 +97,7 @@
<h2>Pad angle</h2>

<Preview {data}>
<div class="h-[300px] p-4 border rounded">
<div class="h-[300px] p-4 border rounded resize overflow-auto">
<Chart {data} x="value" c="date" cRange={keyColors}>
<Svg center>
<Pie innerRadius={100} padAngle={0.03} />
Expand All @@ -111,7 +111,7 @@
<h3>If value >= 1, value will be treated as discrete</h3>

<Preview {data}>
<div class="h-[300px] p-4 border rounded">
<div class="h-[300px] p-4 border rounded resize overflow-auto">
<Chart {data} x="value" c="date" cRange={keyColors}>
<Svg center>
<Pie innerRadius={100} />
Expand All @@ -123,7 +123,7 @@
<h3>If value >= 0 and less than 1, value will be treated as a percentage of outerRadius</h3>

<Preview {data}>
<div class="h-[300px] p-4 border rounded">
<div class="h-[300px] p-4 border rounded resize overflow-auto">
<Chart {data} x="value" c="date" cRange={keyColors}>
<Svg center>
<Pie innerRadius={0.9} />
Expand All @@ -135,7 +135,7 @@
<h3>If value less than 0, value will be treated as a offset of outerRadius</h3>

<Preview {data}>
<div class="h-[300px] p-4 border rounded">
<div class="h-[300px] p-4 border rounded resize overflow-auto">
<Chart {data} x="value" c="date" cRange={keyColors}>
<Svg center>
<Pie innerRadius={-30} />
Expand All @@ -147,7 +147,7 @@
<h2>Outer radius</h2>

<Preview {data}>
<div class="h-[300px] p-4 border rounded">
<div class="h-[300px] p-4 border rounded resize overflow-auto">
<Chart {data} x="value" c="date" cRange={keyColors}>
<Svg center>
<Pie outerRadius={100} />
Expand All @@ -159,7 +159,7 @@
<h2>Multiple (data prop)</h2>

<Preview {data}>
<div class="h-[300px] p-4 border rounded">
<div class="h-[300px] p-4 border rounded resize overflow-auto">
<Chart {data} x="value" c="date" cRange={keyColors}>
<Svg center>
<Pie innerRadius={100} {data} />
Expand All @@ -178,7 +178,7 @@
</div>

<Preview {data}>
<div class="h-[300px] p-4 border rounded">
<div class="h-[300px] p-4 border rounded resize overflow-auto">
<Chart {data} x="value" c="date" cRange={keyColors}>
<Svg center>
{#if show}
Expand All @@ -193,7 +193,7 @@
<h2>Offset</h2>

<Preview {data}>
<div class="h-[300px] p-4 border rounded">
<div class="h-[300px] p-4 border rounded resize overflow-auto">
<Chart {data} x="value" c="date" cRange={keyColors}>
<Svg center>
<Pie offset={4} />
Expand All @@ -205,7 +205,7 @@
<h2>default slot / render each `Arc`</h2>

<Preview {data}>
<div class="h-[300px] p-4 border rounded">
<div class="h-[300px] p-4 border rounded resize overflow-auto">
<Chart {data} x="value" c="date" cRange={keyColors}>
<Svg center>
<Pie let:arcs>
Expand All @@ -227,7 +227,7 @@
<h2>Centroid labels</h2>

<Preview {data}>
<div class="h-[300px] p-4 border rounded">
<div class="h-[300px] p-4 border rounded resize overflow-auto">
<Chart {data} x="value" c="date">
<Svg center>
<Pie let:arcs>
Expand Down Expand Up @@ -269,7 +269,7 @@
<h2>Tooltip</h2>

<Preview {data}>
<div class="h-[300px] p-4 border rounded">
<div class="h-[300px] p-4 border rounded resize overflow-auto">
<Chart {data} x="value" c="date" cRange={keyColors} let:tooltip>
<Svg center>
<Pie {tooltip} />
Expand All @@ -293,7 +293,7 @@
<h2>Tooltip with Arcs (slot)</h2>

<Preview {data}>
<div class="h-[300px] p-4 border rounded">
<div class="h-[300px] p-4 border rounded resize overflow-auto">
<Chart {data} x="value" c="date" cRange={keyColors} let:tooltip>
<Svg center>
<Pie let:arcs>
Expand Down Expand Up @@ -354,7 +354,7 @@
<h3>left</h3>

<Preview {data}>
<div class="h-[300px] p-4 border rounded">
<div class="h-[300px] p-4 border rounded resize overflow-auto">
<Chart {data} x="value" c="date" cRange={keyColors} let:height>
<Svg>
<Group x={height / 2} center="y">
Expand All @@ -368,7 +368,7 @@
<h3>center</h3>

<Preview {data}>
<div class="h-[300px] p-4 border rounded">
<div class="h-[300px] p-4 border rounded resize overflow-auto">
<Chart {data} x="value" c="date" cRange={keyColors}>
<Svg center>
<Pie />
Expand All @@ -380,7 +380,7 @@
<h3>right</h3>

<Preview {data}>
<div class="h-[300px] p-4 border rounded">
<div class="h-[300px] p-4 border rounded resize overflow-auto">
<Chart {data} x="value" c="date" cRange={keyColors} let:width let:height>
<Svg>
<Group x={width - height / 2} center="y">
Expand Down
Loading

0 comments on commit 54581a4

Please sign in to comment.