Skip to content

Commit

Permalink
feat: add todos line chart
Browse files Browse the repository at this point in the history
  • Loading branch information
azat-io committed Oct 18, 2024
1 parent 4ad57c2 commit 5ea706b
Show file tree
Hide file tree
Showing 14 changed files with 663 additions and 65 deletions.
2 changes: 1 addition & 1 deletion preview/blocks/footer.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<style>
.footer {
margin-block: var(--space-xl);
margin-block: var(--space-2xl) var(--space-xl);
}
.wrapper {
Expand Down
30 changes: 30 additions & 0 deletions preview/blocks/graph.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<script lang="ts">
import Typography from '~/elements/typography.svelte'
import ChartLine from '~/elements/chart-line.svelte'
import Container from '~/elements/container.svelte'
import { data } from '~/stores/data'
$: history =
$data.history?.sort(
(a, b) => new Date(a.date).getTime() - new Date(b.date).getTime(),
) ?? []
$: labels = history?.map(({ date }) => date)
$: values = history?.map(({ count }) => count)
</script>

<div class="graph-wrapper">
<Container>
<Typography size="l" tag="h2" mbe="l">Todos Graph</Typography>
<div>
{#if values.length && labels.length}
<ChartLine {values} {labels} />
{/if}
</div>
</Container>
</div>

<style>
.graph-wrapper {
margin-block: var(--space-xl) var(--space-2xl);
}
</style>
3 changes: 2 additions & 1 deletion preview/blocks/header.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@

<style>
.header {
padding-block: var(--space-xl);
padding-block: var(--space-xl) var(--space-xs);
margin-block-end: var(--space-xl);
}
.wrapper {
Expand Down
62 changes: 35 additions & 27 deletions preview/blocks/info.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
$: finalTodosEntries = (() => {
if (todosEntries.length > 5) {
let topFive = todosEntries.slice(0, 4)
let topFive = todosEntries.slice(0, 5)
let remainingSum = todosEntries
.slice(4)
.slice(5)
.reduce((sum, [, value]) => sum + value, 0)
topFive.push(['TOTAL', remainingSum])
topFive.push(['OTHER', remainingSum])
return topFive
}
return todosEntries
Expand All @@ -40,41 +40,49 @@
]
</script>

<Container>
<div class="wrapper">
<div></div>
<div class="chart">
<Typography size="l" tag="h2" mbe="l" align="center">
Todos by Kind
</Typography>
{#if values.length}
<ChartDoughnut {values} />
{/if}
</div>
</div>
<div class="legend">
{#each finalTodosEntries as [kind], index}
<div class="legend-element">
<div
style={`--color: var(--color-additional-${colors[index]});`}
class="legend-color"
></div>
<Typography size="s" tag="span">{kind}</Typography>
<div class="wrapper">
<Container>
<div class="grid">
<div>
<Typography size="l" tag="h2" mbe="l">Did You Know That?</Typography>
</div>
<div class="chart">
<Typography size="l" tag="h2" mbe="l" align="center">
Todos by Kind
</Typography>
{#if values.length}
<ChartDoughnut {values} />
{/if}
</div>
{/each}
</div>
</Container>
</div>
<div class="legend">
{#each finalTodosEntries as [kind], index}
<div class="legend-element">
<div
style={`--color: var(--color-additional-${colors[index]});`}
class="legend-color"
></div>
<Typography size="s" tag="span">{kind}</Typography>
</div>
{/each}
</div>
</Container>
</div>

<style>
.wrapper {
margin-block: var(--space-2xl);
}
.grid {
display: grid;
grid-template-columns: 1fr 420px;
gap: var(--space-l);
margin-block-end: var(--space-xl);
}
.legend {
display: flex;
flex-wrap: wrap;
gap: var(--space-xl);
justify-content: center;
max-inline-size: 800px;
Expand Down
4 changes: 2 additions & 2 deletions preview/blocks/list.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@

<div class="list">
<Container>
<Typography size="l" tag="h2" mbe="l">List of Commits</Typography>
<Typography size="l" tag="h2" mbe="l">List of Todos</Typography>
<Table data={preparedData} {columns} />
</Container>
</div>

<style>
.list {
margin-block: var(--space-xl);
margin-block: var(--space-2xl);
}
</style>
38 changes: 35 additions & 3 deletions preview/elements/chart-doughnut.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,46 @@
computedStyles = getComputedStyle(document.body)
})
let options: ChartOptions<'doughnut'> = {
$: options = {
plugins: {
tooltip: {
callbacks: {
labelColor: context => ({
backgroundColor: (context.dataset.backgroundColor as string[])[
context.dataIndex
],
borderColor: computedStyles.getPropertyValue(
'--color-border-primary',
),
borderRadius: 2,
borderWidth: 0,
}),
},
titleFont: {
family: computedStyles.getPropertyValue('--font-family-base'),
size: 16,
},
bodyFont: {
family: computedStyles.getPropertyValue('--font-family-base'),
size: 16,
},
backgroundColor: computedStyles.getPropertyValue(
'--color-background-secondary',
),
footerFont: {
family: computedStyles.getPropertyValue('--font-family-base'),
},
borderColor: computedStyles.getPropertyValue('--color-border-primary'),
titleColor: computedStyles.getPropertyValue('--color-content-primary'),
bodyColor: computedStyles.getPropertyValue('--color-content-primary'),
borderWidth: 1,
},
legend: {
display: false,
},
},
responsive: true,
}
} as ChartOptions<'doughnut'>
</script>

<Chart type="doughnut" {options} {data} />
<Chart type="doughnut" height={null} {options} {data} />
140 changes: 140 additions & 0 deletions preview/elements/chart-line.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<script lang="ts">
import type { ChartOptions, ChartData } from 'chart.js'
import Chart from '~/elements/chart.svelte'
import { theme } from '~/stores/theme'
export let values: number[]
export let labels: string[]
let max = Math.max(...values)
let min = Math.min(...values)
$: computedStyles = getComputedStyle(document.body)
$: data = {
datasets: [
{
pointBackgroundColor: computedStyles.getPropertyValue(
'--color-additional-primary',
),
backgroundColor: computedStyles.getPropertyValue(
'--color-additional-secondary',
),
pointBorderColor: computedStyles.getPropertyValue(
'--color-additional-primary',
),
borderColor: computedStyles.getPropertyValue(
'--color-additional-primary',
),
borderJoinStyle: 'miter',
pointBorderWidth: 3,
borderWidth: 4,
tension: 0.3,
data: values,
fill: false,
},
],
labels,
} as ChartData<'line'>
theme.subscribe(() => {
computedStyles = getComputedStyle(document.body)
})
$: options = {
scales: {
x: {
ticks: {
callback: (_value, index) => {
let date = new Date(labels[index])
let monthNames = [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec',
]
if (date.getDate() === 1) {
return monthNames[date.getMonth()]
}
return null
},
font: {
family: computedStyles.getPropertyValue('--font-family-base'),
size: 16,
},
color: computedStyles.getPropertyValue('--color-content-primary'),
autoSkip: false,
maxRotation: 0,
},
grid: {
color: computedStyles.getPropertyValue('--color-border-primary'),
},
},
y: {
ticks: {
font: {
family: computedStyles.getPropertyValue('--font-family-base'),
size: 16,
},
color: computedStyles.getPropertyValue('--color-content-primary'),
},
grid: {
color: computedStyles.getPropertyValue('--color-border-primary'),
},
min: min === 0 ? 0 : min / 2,
max: max + min / 2,
},
},
plugins: {
tooltip: {
callbacks: {
labelColor: () => ({
backgroundColor: computedStyles.getPropertyValue(
'--color-additional-primary',
),
borderColor: computedStyles.getPropertyValue(
'--color-border-primary',
),
borderRadius: 2,
borderWidth: 0,
}),
},
titleFont: {
family: computedStyles.getPropertyValue('--font-family-base'),
size: 16,
},
bodyFont: {
family: computedStyles.getPropertyValue('--font-family-base'),
size: 16,
},
backgroundColor: computedStyles.getPropertyValue(
'--color-background-secondary',
),
footerFont: {
family: computedStyles.getPropertyValue('--font-family-base'),
},
borderColor: computedStyles.getPropertyValue('--color-border-primary'),
titleColor: computedStyles.getPropertyValue('--color-content-primary'),
bodyColor: computedStyles.getPropertyValue('--color-content-primary'),
borderWidth: 1,
},
legend: {
display: false,
},
},
maintainAspectRatio: false,
responsive: true,
} as ChartOptions<'line'>
</script>

<Chart type="line" height={600} {options} {data} />
9 changes: 8 additions & 1 deletion preview/elements/chart.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
export let type: T
export let data: ChartData<T>
export let options: ChartOptions<T>
export let height: number | null
let props: Record<string, unknown> = {}
if (height) {
props = { height }
}
let canvasRef: HTMLCanvasElement
Expand Down Expand Up @@ -49,4 +56,4 @@
})
</script>

<canvas bind:this={canvasRef}></canvas>
<canvas bind:this={canvasRef} {...props}></canvas>
2 changes: 1 addition & 1 deletion preview/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<meta name="theme-color" content="#2c7f50" />
<link href="./favicon.ico" sizes="any" rel="icon" />
<link type="image/svg+xml" href="./icon.svg" rel="icon" />
<!-- <link href="./apple-touch-icon.png" rel="apple-touch-icon" /> -->
<link href="./apple-touch-icon.png" rel="apple-touch-icon" />

<style>
@import './styles/base.css';
Expand Down
Loading

0 comments on commit 5ea706b

Please sign in to comment.