-
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.
refactor(chart): ♻️ optimize chart init and updates (#53)
* refactor(chart): ♻️ optimize chart init and updates * more coverage * test(chart): ✅ test bar chart
- Loading branch information
Showing
14 changed files
with
356 additions
and
263 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { ChartColor } from '$lib/constants' | ||
import type * as echarts from 'echarts' | ||
import zipObject from 'lodash/zipObject' | ||
import { secPerHour } from '$lib/constants' | ||
|
||
export const createBarChartOption = ( | ||
xValues: string[], | ||
series: echarts.SeriesOption[], | ||
): echarts.EChartsOption => ({ | ||
tooltip: { | ||
valueFormatter: (value) => `${value}h`, | ||
}, | ||
grid: { left: 50, right: 20, top: 50, bottom: 50 }, | ||
legend: { | ||
type: 'scroll', | ||
textStyle: { | ||
color: ChartColor.Text, | ||
}, | ||
pageIconColor: ChartColor.Icon, | ||
pageTextStyle: { | ||
color: ChartColor.Text, | ||
}, | ||
}, | ||
xAxis: { | ||
type: 'category', | ||
data: xValues, | ||
}, | ||
yAxis: { | ||
type: 'value', | ||
axisLabel: { | ||
formatter: (value) => `${value}h`, | ||
showMinLabel: false, | ||
}, | ||
}, | ||
series, | ||
}) | ||
|
||
export const createBarChartSeries = <T>( | ||
xValues: string[], | ||
itemsByXValues: T[][], | ||
itemNames: string[], | ||
) => { | ||
const yDataByItem = zipObject( | ||
itemNames, | ||
JSON.parse(JSON.stringify(Array(itemNames.length).fill(Array(xValues.length).fill(0)))), | ||
) | ||
|
||
itemsByXValues.forEach((items, index) => { | ||
items.forEach((item) => { | ||
// @ts-expect-error tough type | ||
yDataByItem[item.name][index] = Number((item.total_seconds / secPerHour).toFixed(1)) | ||
}) | ||
}) | ||
|
||
return itemNames.map((key) => { | ||
return { | ||
data: yDataByItem[key], | ||
type: 'bar', | ||
stack: 'total', | ||
emphasis: { | ||
focus: 'series', | ||
}, | ||
name: key, | ||
} | ||
}) as echarts.SeriesOption[] | ||
} |
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<script lang="ts"> | ||
import { DateFormat } from '$lib/constants' | ||
import type { | ||
SummariesResult, | ||
WakaCategory, | ||
} from '$src/routes/api/wakatime/current/summaries/+server' | ||
import dayjs from 'dayjs' | ||
import * as echarts from 'echarts' | ||
import { afterUpdate, onMount } from 'svelte' | ||
import ChartTitle from '../ChartTitle.svelte' | ||
import ChartContainer from '../ChartContainer.svelte' | ||
import { createBarChartOption, createBarChartSeries } from './ barChartHelpers' | ||
export let summaries: SummariesResult | ||
export let title = 'Category vs Time' | ||
let chartRef: HTMLDivElement | ||
let chart: echarts.ECharts | ||
let option: echarts.EChartsOption | ||
$: xValues = summaries.data.map((item) => dayjs(item.range.date).format(DateFormat.Short)) | ||
$: categoriesByDate = summaries.data.map((item) => item.categories) | ||
$: categoryNames = [ | ||
...new Set( | ||
summaries.data.map((item) => item.categories.map((category) => category.name)).flat(), | ||
), | ||
] | ||
$: series = createBarChartSeries<WakaCategory>(xValues, categoriesByDate, categoryNames) | ||
$: option = createBarChartOption(xValues, series) | ||
onMount(() => { | ||
const handleResize = () => chart.resize() | ||
chart = echarts.init(chartRef, 'dark', { renderer: 'svg' }) | ||
window.addEventListener('resize', handleResize, { passive: true }) | ||
return () => window.removeEventListener('resize', handleResize) | ||
}) | ||
afterUpdate(() => { | ||
chart.setOption(option) | ||
}) | ||
</script> | ||
|
||
<ChartContainer> | ||
<ChartTitle>{title}</ChartTitle> | ||
<div class="h-96 w-full" bind:this={chartRef} /> | ||
</ChartContainer> |
167 changes: 0 additions & 167 deletions
167
src/lib/components/CodingActivityChartByCategory.svelte
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.
252ea6b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
wakastats – ./
wakastats-cesarmejia.vercel.app
wakastats.vercel.app
wakastats-git-main-cesarmejia.vercel.app