Skip to content

A self-hosted dashboard that puts all your feeds in one place

fix: render flat market histories with valid chart coordinates

HocVoNgThai5256bb554fe6
diff --git a/internal/glance/utils.go b/internal/glance/utils.go
index 21cd69b..55abdf0 100644
--- a/internal/glance/utils.go
+++ b/internal/glance/utils.go
@@ -54,10 +54,14 @@ func svgPolylineCoordsFromYValues(width float64, height float64, values []float6
max := slices.Max(values)
for i := range values {
+ normalizedY := 0.5
+ if max != min {
+ normalizedY = (max - values[i]) / (max - min)
+ }
coordinates[i] = fmt.Sprintf(
"%.2f,%.2f",
float64(i)*distanceBetweenPoints,
- ((max-values[i])/(max-min))*height+verticalPadding,
+ normalizedY*height+verticalPadding,
)
}
diff --git a/internal/glance/utils_test.go b/internal/glance/utils_test.go
new file mode 100644
index 0000000..99ecf11
--- /dev/null
+++ b/internal/glance/utils_test.go
@@ -0,0 +1,26 @@
+package glance
+
+import "testing"
+
+func TestSVGPolylineCoordsFromYValues(t *testing.T) {
+ tests := []struct {
+ name string
+ values []float64
+ want string
+ }{
+ {"empty", nil, ""},
+ {"single value", []float64{10}, ""},
+ {"increasing", []float64{10, 20, 30}, "0.00,49.00 50.00,25.00 100.00,1.00"},
+ {"decreasing", []float64{30, 20, 10}, "0.00,1.00 50.00,25.00 100.00,49.00"},
+ {"flat positive", []float64{42, 42, 42}, "0.00,25.00 50.00,25.00 100.00,25.00"},
+ {"flat zero", []float64{0, 0}, "0.00,25.00 100.00,25.00"},
+ {"flat negative", []float64{-5, -5}, "0.00,25.00 100.00,25.00"},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ if got := svgPolylineCoordsFromYValues(100, 50, tt.values); got != tt.want {
+ t.Errorf("svgPolylineCoordsFromYValues() = %q, want %q", got, tt.want)
+ }
+ })
+ }
+}