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
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%'/>")
writePolyline := func(value func(value) float64) {
sb.WriteString("<polyline points='")
for i, v := range values {
sb.WriteString(fmt.Sprintf(" %f,%f", float64(i)/dataPoints*width, height-v.sales/max*height))
sb.WriteString(fmt.Sprintf(" %f,%f", float64(i)/dataPoints*width, height-value(v)/max*height))
}
sb.WriteString("'/>")
sb.WriteString("<polyline points='")
for i, v := range values {
sb.WriteString(fmt.Sprintf(" %f,%f", float64(i)/dataPoints*width, height-v.income/max*height))
}
sb.WriteString("'/>")
sb.WriteString("<polyline points='")
for i, v := range values {
sb.WriteString(fmt.Sprintf(" %f,%f", float64(i)/dataPoints*width, height-v.expenses/max*height))
writePolyline(func(v value) float64 { return v.sales })
writePolyline(func(v value) float64 { return v.income })
writePolyline(func(v value) float64 { return v.expenses })
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("'/>")
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))
writeCircle(i, v.sales)
writeCircle(i, v.income)
writeCircle(i, v.expenses)
}
sb.WriteString("</svg>")
return template.HTML(sb.String())