Refactor a bit the code that draws SVG polylines in circles

This commit is contained in:
jordi fita mas 2023-05-21 18:59:42 +02:00
parent eb47988464
commit 02a4fad443
1 changed files with 15 additions and 16 deletions

View File

@ -240,25 +240,24 @@ func buildDashboardChart(ctx context.Context, conn *Conn, company *Company, peri
var sb strings.Builder var sb strings.Builder
sb.WriteString(fmt.Sprintf("<svg id='income-chart' viewBox='-10 -10 %d %d'>", int(width)+20, int(height)+20)) sb.WriteString(fmt.Sprintf("<svg id='income-chart' viewBox='-10 -10 %d %d'>", int(width)+20, int(height)+20))
sb.WriteString("<rect x='-10' y='-10' width='100%', height='100%'/>") sb.WriteString("<rect x='-10' y='-10' width='100%', height='100%'/>")
sb.WriteString("<polyline points='") writePolyline := func(value func(value) float64) {
for i, v := range values { sb.WriteString("<polyline points='")
sb.WriteString(fmt.Sprintf(" %f,%f", float64(i)/dataPoints*width, height-v.sales/max*height)) for i, v := range values {
sb.WriteString(fmt.Sprintf(" %f,%f", float64(i)/dataPoints*width, height-value(v)/max*height))
}
sb.WriteString("'/>")
} }
sb.WriteString("'/>") writePolyline(func(v value) float64 { return v.sales })
sb.WriteString("<polyline points='") writePolyline(func(v value) float64 { return v.income })
for i, v := range values { writePolyline(func(v value) float64 { return v.expenses })
sb.WriteString(fmt.Sprintf(" %f,%f", float64(i)/dataPoints*width, height-v.income/max*height))
writeCircle := func(i int, v float64) {
sb.WriteString(fmt.Sprintf("<circle cx='%f' cy='%f' r='4'></circle>", float64(i)/dataPoints*width, height-v/max*height))
} }
sb.WriteString("'/>")
sb.WriteString("<polyline points='")
for i, v := range values { for i, v := range values {
sb.WriteString(fmt.Sprintf(" %f,%f", float64(i)/dataPoints*width, height-v.expenses/max*height)) writeCircle(i, v.sales)
} writeCircle(i, v.income)
sb.WriteString("'/>") writeCircle(i, v.expenses)
for i, v := range values {
sb.WriteString(fmt.Sprintf("<circle cx='%f' cy='%f' r='4'/>", float64(i)/dataPoints*width, height-v.sales/max*height))
sb.WriteString(fmt.Sprintf("<circle cx='%f' cy='%f' r='4'/>", float64(i)/dataPoints*width, height-v.income/max*height))
sb.WriteString(fmt.Sprintf("<circle cx='%f' cy='%f' r='4'/>", float64(i)/dataPoints*width, height-v.expenses/max*height))
} }
sb.WriteString("</svg>") sb.WriteString("</svg>")
return template.HTML(sb.String()) return template.HTML(sb.String())