diff --git a/internal/apps/inflation/main.go b/internal/apps/inflation/main.go index 77e9bb4..bf10f50 100644 --- a/internal/apps/inflation/main.go +++ b/internal/apps/inflation/main.go @@ -28,22 +28,30 @@ var rawData = make(map[string]map[time.Time]float32) var datas = map[string]Datas{ "CPIAUCSL": { Name: "CPI USA BASE", - ProcessFunction: toPercent, + ProcessFunction: toPercentNeg, }, "PCEPI": { Name: "PCECPI USA", - ProcessFunction: toPercent, + ProcessFunction: toPercentNeg, }, "M2SL": { Name: "M2 USA", - ProcessFunction: toPercent, + ProcessFunction: toPercentNeg, }, "CP0000EZ19M086NEST": { Name: "HICP ECB", - ProcessFunction: toPercent, + ProcessFunction: toPercentNeg, }, "MABMM301EZM189S": { Name: "M3 ECB", + ProcessFunction: toPercentNeg, + }, + "CBBTCUSD": { + Name: "Bitcoin", + ProcessFunction: toPercent, + }, + "APU0000708111": { + Name: "Eggs", ProcessFunction: toPercent, }, } @@ -60,7 +68,59 @@ func Setup() { i18n.RegisterTranslations(Embed, "locales") web.RegisterAppSetupFunc(func(a *fiber.App) { - a.Get("/app/inflation", func(c *fiber.Ctx) error { + a.Get("app/inflation", func(c *fiber.Ctx) error { + processedData := make(map[string]map[time.Time]float32) + for id, data := range datas { + processedData[id] = data.ProcessFunction(rawData[id]) + } + + line := charts.NewLine() + + startYear, endYear := getYearRange(processedData) + yearList := createYearList(startYear, endYear) + for seriesID, timeData := range processedData { + + aggregatedData := aggregateDataByYear(timeData) + + yAxisT := []opts.LineData{} + var useAmount float32 = 1 + for _, year := range yearList { + if value, exists := aggregatedData[year]; exists { + + current := (value / 100) * useAmount + useAmount = useAmount + current + yAxisT = append(yAxisT, opts.LineData{Value: useAmount}) + } else { + yAxisT = append(yAxisT, opts.LineData{Value: nil}) + } + } + + line.AddSeries(datas[seriesID].Name, yAxisT) + } + + line.SetXAxis(yearList) + + line.SetGlobalOptions( + charts.WithTitleOpts(opts.Title{ + Title: "Inflation", + Subtitle: "Copyright Tijl 2024", + }), + charts.WithDataZoomOpts(opts.DataZoom{ + Orient: "horizontal", + Type: "slider", + }), + ) + + rendered := line.RenderSnippet() + + data := *web.Common(c) + data["Title"] = "tmp" + data["RenderedScript"] = template.JS(handleScriptElement(rendered.Script)) + data["RenderedElement"] = template.HTML(rendered.Element) + return c.Render("apps/inflation/index", data, "layouts/base") + + }) + a.Get("/app/inflation/base", func(c *fiber.Ctx) error { processedData := make(map[string]map[time.Time]float32) for id, data := range datas { diff --git a/internal/apps/inflation/util.go b/internal/apps/inflation/util.go index b4ace20..506ef45 100644 --- a/internal/apps/inflation/util.go +++ b/internal/apps/inflation/util.go @@ -52,6 +52,30 @@ func aggregateDataByYear(data map[time.Time]float32) map[int32]float32 { return aggregatedData } +func toPercentNeg(m map[time.Time]float32) map[time.Time]float32 { + var times []time.Time + for t := range m { + times = append(times, t) + } + sort.Slice(times, func(i, j int) bool { + return times[i].Before(times[j]) + }) + percentageChanges := make(map[time.Time]float32) + var previousValue float32 + for i, t := range times { + currentValue := m[t] + if i == 0 { + percentageChanges[t] = 0 + } else { + percentageChange := ((currentValue - previousValue) / previousValue) * 100 + percentageChanges[t] = -percentageChange + } + previousValue = currentValue + } + + return percentageChanges +} + func toPercent(m map[time.Time]float32) map[time.Time]float32 { var times []time.Time for t := range m { diff --git a/web/views/apps/inflation/index.html b/web/views/apps/inflation/index.html index 17e7b3e..8cdedf1 100644 --- a/web/views/apps/inflation/index.html +++ b/web/views/apps/inflation/index.html @@ -1,4 +1,4 @@ -
+
{{.RenderedElement}}
@@ -8,4 +8,15 @@ const script = `{{.RenderedScript}}`; eval(script); }; + window.onresize = function () { + document.querySelectorAll('canvas, div').forEach( + function (e) { + const instance = echarts.getInstanceByDom(e); + if (instance) { + instance.resize() + console.log(instance, "resize") + } + } + ); + } diff --git a/web/views/projects.html b/web/views/projects.html index 21e8570..e0dbf27 100644 --- a/web/views/projects.html +++ b/web/views/projects.html @@ -1,2 +1,4 @@
flags
uploader
+
inflation (full time based)
+
inflation