[READ-ONLY] Mirror of https://github.com/probablykasper/starchart. GitHub star history graph starchart.kasper.space
chart github github-api github-star graph history star star-history stargazers stars
0

Configure Feed

Select the types of activity you want to include in your feed.

Clearer code

Kasper (Mar 3, 2023, 4:47 AM +0100) 46883982 9bf2f1c8

+13 -15
+13 -15
src/routes/chart.ts
··· 4 4 type DeepPartial, 5 5 type IChartApi, 6 6 type ISeriesApi, 7 - type SeriesDataItemTypeMap, 7 + type SingleValueData, 8 8 type UTCTimestamp, 9 9 } from 'lightweight-charts' 10 10 import { writable } from 'svelte/store' ··· 40 40 41 41 function loadJsonLines(): LineJson[] { 42 42 try { 43 - const seriesLocalStorage = JSON.parse(localStorage.getItem('starchart-series') || '[]') 44 - if (seriesLocalStorage?.v !== jsonTypeVersion || seriesLocalStorage?.expiry < Date.now()) { 45 - localStorage.removeItem('starchart-series') 46 - } else { 43 + const seriesLocalStorage = JSON.parse(localStorage.getItem('starchart-series') || '{}') 44 + if (seriesLocalStorage?.v === jsonTypeVersion || seriesLocalStorage?.expiry > Date.now()) { 47 45 return (seriesLocalStorage as Json).lines 46 + } else { 47 + localStorage.removeItem('starchart-series') 48 48 } 49 - } catch (_e) { 50 - // ignore 49 + } catch (e) { 50 + console.log('Could not load lines', e) 51 51 } 52 52 return [] 53 53 } ··· 96 96 if (lineJson.data.length >= 1) { 97 97 const start = lineJson.data[0] 98 98 const end = lineJson.final ?? lineJson.data[lineJson.data.length - 1] 99 - const chartData = toChartData(lineJson.data, start, end) 99 + const chartData = toChartSeries(lineJson.data, start, end) 100 100 series.setData(chartData) 101 101 } 102 102 const line: Line = { ··· 126 126 } 127 127 const start = line.data[line.data.length - 1] ?? data[0] 128 128 const end = line.final ?? data[data.length - 1] 129 - const chartData = toChartData(data, start, end) 129 + const chartData = toChartSeries(data, start, end) 130 130 const fresh = line.data.length === 0 131 131 132 132 if (fresh) { ··· 215 215 localStorage.setItem('starchart-series', JSON.stringify(json)) 216 216 } 217 217 218 - type Series = SeriesDataItemTypeMap['Area'][] 219 - function toChartData(data: DataPoint[], start: DataPoint, final: DataPoint): Series { 220 - const dataPoints: { date: Date; value?: number }[] = [] 218 + function toChartSeries(data: DataPoint[], start: DataPoint, final: DataPoint) { 219 + const dataPoints: { date: Date; value: number }[] = [] 221 220 222 221 for (const dataPoint of [start, ...data, final]) { 223 222 const dt = new Date(dataPoint.t * 1000) ··· 248 247 } 249 248 250 249 return dataPoints.map((dataPoint) => { 251 - const dp: SeriesDataItemTypeMap['Area'] = { 250 + return { 252 251 time: (dataPoint.date.getTime() / 1000) as UTCTimestamp, 253 252 value: dataPoint.value, 254 - } 255 - return dp 253 + } satisfies SingleValueData 256 254 }) 257 255 }