| 1 |
<script lang="ts"> |
| 2 |
import type { Snippet } from "svelte"; |
| 3 |
|
| 4 |
import { |
| 5 |
barChartMode, |
| 6 |
chartToggledCurrencies, |
| 7 |
hierarchyChartMode, |
| 8 |
lineChartMode, |
| 9 |
showCharts, |
| 10 |
} from "../stores/chart"; |
| 11 |
import type { FavaChart } from "."; |
| 12 |
import BarChart from "./BarChart.svelte"; |
| 13 |
import ChartLegend from "./ChartLegend.svelte"; |
| 14 |
import HierarchyContainer from "./HierarchyContainer.svelte"; |
| 15 |
import LineChart from "./LineChart.svelte"; |
| 16 |
import ModeSwitch from "./ModeSwitch.svelte"; |
| 17 |
import ScatterPlot from "./ScatterPlot.svelte"; |
| 18 |
|
| 19 |
interface Props { |
| 20 |
/** The chart to render. */ |
| 21 |
chart: FavaChart; |
| 22 |
/** Additional elements to render in the top right. */ |
| 23 |
children?: Snippet; |
| 24 |
} |
| 25 |
|
| 26 |
let { chart, children }: Props = $props(); |
| 27 |
|
| 28 |
/** Width of the chart. */ |
| 29 |
let width: number | undefined = $state(); |
| 30 |
</script> |
| 31 |
|
| 32 |
<div class="flex-row"> |
| 33 |
{#if $showCharts} |
| 34 |
{#if chart.type === "barchart"} |
| 35 |
<ChartLegend |
| 36 |
legend={chart.currencies} |
| 37 |
color={!($barChartMode === "stacked" && chart.hasStackedData)} |
| 38 |
toggled={chartToggledCurrencies} |
| 39 |
/> |
| 40 |
{/if} |
| 41 |
{#if chart.type === "linechart"} |
| 42 |
<ChartLegend |
| 43 |
legend={chart.series_names} |
| 44 |
color={true} |
| 45 |
toggled={chartToggledCurrencies} |
| 46 |
/> |
| 47 |
{/if} |
| 48 |
{#if chart.type === "hierarchy" && $hierarchyChartMode === "treemap" && chart.treemap_currency} |
| 49 |
<ChartLegend |
| 50 |
legend={chart.currencies} |
| 51 |
color={false} |
| 52 |
active={chart.treemap_currency} |
| 53 |
/> |
| 54 |
{/if} |
| 55 |
<span class="spacer"></span> |
| 56 |
{#if chart.type === "hierarchy"} |
| 57 |
<ModeSwitch store={hierarchyChartMode} /> |
| 58 |
{:else if chart.type === "linechart"} |
| 59 |
<ModeSwitch store={lineChartMode} /> |
| 60 |
{:else if chart.type === "barchart" && chart.hasStackedData} |
| 61 |
<ModeSwitch store={barChartMode} /> |
| 62 |
{/if} |
| 63 |
{:else}<span class="spacer"></span>{/if} |
| 64 |
{@render children?.()} |
| 65 |
<button |
| 66 |
type="button" |
| 67 |
class="show-charts" |
| 68 |
onclick={() => { |
| 69 |
showCharts.update((v) => !v); |
| 70 |
}} |
| 71 |
> |
| 72 |
{$showCharts ? "▼" : "◀"} |
| 73 |
</button> |
| 74 |
</div> |
| 75 |
<div hidden={!$showCharts} bind:clientWidth={width}> |
| 76 |
{#if width} |
| 77 |
{#if chart.type === "barchart"} |
| 78 |
<BarChart {chart} {width} /> |
| 79 |
{:else if chart.type === "hierarchy"} |
| 80 |
<HierarchyContainer {chart} {width} /> |
| 81 |
{:else if chart.type === "linechart"} |
| 82 |
<LineChart {chart} {width} /> |
| 83 |
{:else if chart.type === "scatterplot"} |
| 84 |
<ScatterPlot {chart} {width} /> |
| 85 |
{/if} |
| 86 |
{/if} |
| 87 |
</div> |
| 88 |
|
| 89 |
<style> |
| 90 |
@media print { |
| 91 |
button.show-charts { |
| 92 |
display: none; |
| 93 |
} |
| 94 |
} |
| 95 |
</style> |