Refactor a bit the code that draws SVG polylines in circles
This commit is contained in:
parent
eb47988464
commit
02a4fad443
|
@ -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%'/>")
|
||||
sb.WriteString("<polyline points='")
|
||||
for i, v := range values {
|
||||
sb.WriteString(fmt.Sprintf(" %f,%f", float64(i)/dataPoints*width, height-v.sales/max*height))
|
||||
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-value(v)/max*height))
|
||||
}
|
||||
sb.WriteString("'/>")
|
||||
}
|
||||
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))
|
||||
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("'/>")
|
||||
sb.WriteString("<polyline points='")
|
||||
for i, v := range values {
|
||||
sb.WriteString(fmt.Sprintf(" %f,%f", float64(i)/dataPoints*width, height-v.expenses/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())
|
||||
|
|
Loading…
Reference in New Issue