forked from ajstarks/svgo
/
barchart.go
executable file
·472 lines (433 loc) · 11.6 KB
/
barchart.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
// barchart - bar chart
package main
import (
"encoding/xml"
"flag"
"fmt"
"github.com/ajstarks/svgo"
"io"
"math"
"os"
"strconv"
"strings"
)
var (
width, height, iscale, fontsize, barheight, gutter, cornerRadius, labelimit int
bgcolor, barcolor, title, inbar, valformat string
showtitle, showdata, showgrid, showscale, endtitle, trace bool
)
const (
gstyle = "font-family:Calibri,sans-serif;font-size:%dpx"
borderstyle = "stroke:lightgray;stroke-width:1px"
scalestyle = "text-anchor:middle;font-size:75%"
btitlestyle = "font-style:italic;font-size:150%;text-anchor:"
notestyle = "font-style:italic;text-anchor:"
datastyle = "text-anchor:end;fill:"
titlestyle = "text-anchor:start;font-size:300%"
labelstyle = "fill:black;baseline-shift:-25%"
)
// a Barchart Defintion
// <barchart title="Bullet Graph" top="50" left="250" right="50">
// <note>This is a note</note>
// <note>More expository text</note>
// <bdata title="Browser Market Share" scale="0,100,20" showdata="true" color="red" unit="%"/>
// <bitem name="Firefox" value="22.5" color="green"/>
// <bitem name="Chrome" value="12.3"/>
// <bitem name="IE8" value="63.5"/>
// <bdata>
// </barchart>
type Barchart struct {
Top int `xml:"top,attr"`
Left int `xml:"left,attr"`
Right int `xml:"right,attr"`
Title string `xml:"title,attr"`
Bdata []bdata `xml:"bdata"`
Note []note `xml:"note"`
}
type bdata struct {
Title string `xml:"title,attr"`
Scale string `xml:"scale,attr"`
Color string `xml:"color,attr"`
Unit string `xml:"unit,attr"`
Showdata bool `xml:"showdata,attr"`
Showgrid bool `xml:"showgrid,attr"`
Samebar bool `xml:"samebar,attr"`
Bitem []bitem `xml:"bitem"`
Bstack []bstack `xml:"bstack"`
Note []note `xml:"note"`
}
type bitem struct {
Name string `xml:"name,attr"`
Value float64 `xml:"value,attr"`
Color string `xml:"color,attr"`
Samebar bool `xml:"samebar,attr"`
}
type bstack struct {
Name string `xml:"name,attr"`
Value string `xml:"value,attr"`
Color string `xml:"color,attr"`
}
type note struct {
Text string `xml:",chardata"`
}
// dobc does file i/o
func dobc(location string, s *svg.SVG) {
var f *os.File
var err error
if len(location) > 0 {
f, err = os.Open(location)
} else {
f = os.Stdin
}
if err == nil {
readbc(f, s)
f.Close()
} else {
fmt.Fprintf(os.Stderr, "%v\n", err)
}
}
// readbc reads and parses the XML specification
func readbc(r io.Reader, s *svg.SVG) {
var bc Barchart
if err := xml.NewDecoder(r).Decode(&bc); err == nil {
drawbc(bc, s)
} else {
fmt.Fprintf(os.Stderr, "%v\n", err)
}
}
// drawbc draws the bar chart
func drawbc(bg Barchart, canvas *svg.SVG) {
if bg.Left == 0 {
bg.Left = 250
}
if bg.Right == 0 {
bg.Right = 50
}
if bg.Top == 0 {
bg.Top = 50
}
if len(title) > 0 {
bg.Title = title
}
labelimit = bg.Left/8
cr := cornerRadius
maxwidth := width - (bg.Left + bg.Right)
x := bg.Left
y := bg.Top
sep := 10
color := barcolor
scfmt := "%v"
canvas.Title(bg.Title)
// for each bdata element...
for _, b := range bg.Bdata {
if trace {
fmt.Fprintf(os.Stderr, "# %s\n", b.Title)
}
// overide the color if specified
if len(b.Color) > 0 {
color = b.Color
} else {
color = barcolor
}
// extract the scale data from the XML attributes
// if not specified, compute the scale factors
sc := strings.Split(b.Scale, ",")
var scalemin, scalemax, scaleincr float64
if len(sc) != 3 {
if len(b.Bitem) > 0 {
scalemin, scalemax, scaleincr = scalevalues(b.Bitem)
}
if len(b.Bstack) > 0 {
scalemin, scalemax, scaleincr = scalestack(b.Bstack)
}
} else {
scalemin, _ = strconv.ParseFloat(sc[0], 64)
scalemax, _ = strconv.ParseFloat(sc[1], 64)
scaleincr, _ = strconv.ParseFloat(sc[2], 64)
}
// label the graph
canvas.Text(x, y, b.Title, btitlestyle+anchor())
y += sep * 2
chartop := y
// draw the data items
canvas.Gstyle(datastyle + color)
// stacked bars
for _, stack := range b.Bstack {
if trace {
fmt.Fprintf(os.Stderr, "%s~%s\n", stack.Value, stack.Name)
}
stackdata := stackvalues(stack.Value)
if len(stackdata) < 1 {
continue
}
sx := x
canvas.Text(x-sep, y+barheight/2, textlimit(stack.Name, labelimit), labelstyle)
barop := colorange(1.0, 0.3, len(stackdata))
for ns, sd := range stackdata {
dw := vmap(sd, scalemin, scalemax, 0, float64(maxwidth))
if len(stack.Color) > 0 {
canvas.Roundrect(sx, y, int(dw), barheight, cr, cr, fmt.Sprintf("fill:%s;fill-opacity:%.2f", stack.Color, barop[ns]))
} else {
canvas.Roundrect(sx, y, int(dw), barheight, cr, cr, fmt.Sprintf("fill-opacity:%.2f", barop[ns]))
}
if (showdata || b.Showdata) && sd > 0 {
var valuestyle = "fill-opacity:1;font-style:italic;font-size:75%;text-anchor:middle;baseline-shift:-25%;"
var ditem string
var datax int
if len(b.Unit) > 0 {
ditem = fmt.Sprintf(valformat+"%s", sd, b.Unit)
} else {
ditem = fmt.Sprintf(valformat, sd)
}
if len(inbar) > 0 {
valuestyle += inbar
} else {
valuestyle += "fill:black"
}
datax = sx + int(dw)/2
canvas.Text(datax, y+barheight/2, ditem, valuestyle)
}
sx += int(dw)
}
y += barheight + gutter
}
// plain bars
for _, d := range b.Bitem {
if trace {
fmt.Fprintf(os.Stderr, "%.2f~%s\n", d.Value, d.Name)
}
canvas.Text(x-sep, y+barheight/2, textlimit(d.Name, labelimit), labelstyle)
dw := vmap(d.Value, scalemin, scalemax, 0, float64(maxwidth))
var barop float64
if b.Samebar {
barop = 0.3
} else {
barop = 1.0
}
if len(d.Color) > 0 {
canvas.Roundrect(x, y, int(dw), barheight, cr, cr, fmt.Sprintf("fill:%s;fill-opacity:%.2f", d.Color, barop))
} else {
canvas.Roundrect(x, y, int(dw), barheight, cr, cr, fmt.Sprintf("fill-opacity:%.2f", barop))
}
if showdata || b.Showdata {
var valuestyle = "fill-opacity:1;font-style:italic;font-size:75%;text-anchor:start;baseline-shift:-25%;"
var ditem string
var datax int
if len(b.Unit) > 0 {
ditem = fmt.Sprintf(valformat+"%s", d.Value, b.Unit)
} else {
ditem = fmt.Sprintf(valformat, d.Value)
}
if len(inbar) > 0 {
valuestyle += inbar
datax = x + fontsize/2
} else {
valuestyle += "fill:black"
datax = x + int(dw) + fontsize/2
}
canvas.Text(datax, y+barheight/2, ditem, valuestyle)
}
if !d.Samebar {
y += barheight + gutter
}
}
canvas.Gend()
// draw the scale and borders
chartbot := y + gutter
if showgrid || b.Showgrid {
canvas.Line(x, chartop, x+maxwidth, chartop, borderstyle) // top border
canvas.Line(x, chartbot-gutter, x+maxwidth, chartbot-gutter, borderstyle) // bottom border
}
if showscale {
if scaleincr < 1 {
scfmt = "%.1f"
} else {
scfmt = "%0.f"
}
canvas.Gstyle(scalestyle)
for sc := scalemin; sc <= scalemax; sc += scaleincr {
scx := vmap(sc, scalemin, scalemax, 0, float64(maxwidth))
canvas.Text(x+int(scx), chartbot+fontsize, fmt.Sprintf(scfmt, sc))
if showgrid || b.Showgrid {
canvas.Line(x+int(scx), chartbot, x+int(scx), chartop, borderstyle) // grid line
}
}
canvas.Gend()
}
// apply the note if present
if len(b.Note) > 0 {
canvas.Gstyle(notestyle + anchor())
y += fontsize * 2
leading := 3
for _, note := range b.Note {
canvas.Text(bg.Left, y, note.Text)
y += fontsize + leading
}
canvas.Gend()
}
y += sep * 7 // advance vertically for the next chart
}
// if requested, place the title below the last chart
if showtitle && len(bg.Title) > 0 {
y += fontsize * 2
canvas.Text(bg.Left, y, bg.Title, titlestyle)
}
// apply overall note if present
if len(bg.Note) > 0 {
canvas.Gstyle(notestyle + anchor())
y += fontsize * 2
leading := 3
for _, note := range bg.Note {
canvas.Text(bg.Left, y, note.Text)
y += fontsize + leading
}
canvas.Gend()
}
}
func anchor() string {
if endtitle {
return "end"
}
return "start"
}
// vmap maps one interval to another
func vmap(value float64, low1 float64, high1 float64, low2 float64, high2 float64) float64 {
return low2 + (high2-low2)*(value-low1)/(high1-low1)
}
// maxitem finds the maxima is a collection of bar items
func maxitem(data []bitem) float64 {
max := -math.SmallestNonzeroFloat64
for _, d := range data {
if d.Value > max {
max = d.Value
}
}
return max
}
// maxstack finds the maxima is a stack of bars
func maxstack(stacks []bstack) float64 {
max := -math.SmallestNonzeroFloat64
for _, s := range stacks {
sv := stackvalues(s.Value)
sum := 0.0
for _, d := range sv {
sum += d
}
if sum > max {
max = sum
}
}
return max
}
// scale values returns the min, max, increment from a set of bar items
func scalevalues(data []bitem) (float64, float64, float64) {
var m, max, increment float64
rui := 5
m = maxitem(data)
max = roundup(m, 100)
if max > 2 {
increment = roundup(max/float64(rui), 10)
} else {
increment = 0.4
}
return 0, max, increment
}
// scalestack returns the min, max, increment from a stack of bars
func scalestack(data []bstack) (float64, float64, float64) {
var m, max, increment float64
rui := 5
m = maxstack(data)
max = roundup(m, 100)
if max > 2 {
increment = roundup(max/float64(rui), 10)
} else {
increment = 0.4
}
return 0, max, increment
}
// roundup rouds a floating point number up
func roundup(n float64, m int) float64 {
i := int(n)
if i <= 2 {
return 2
}
for ; i%m != 0; i++ {
}
return float64(i)
}
// stack value returns the values from the value string of a stack
func stackvalues(s string) []float64 {
v := strings.Split(s, "/")
if len(v) <= 0 {
return nil
}
vals := make([]float64, len(v))
for i, x := range v {
f, err := strconv.ParseFloat(x, 64)
if err != nil {
vals[i] = 0
} else {
vals[i] = f
}
}
return vals
}
// colorange evenly distributes opacity across a range of values
func colorange(start, end float64, n int) []float64 {
v := make([]float64, n)
v[0] = start
v[n-1] = end
if n == 2 {
return v
}
incr := (end-start)/float64(n-1)
for i:=1; i < n-1; i++ {
v[i] = v[i-1] + incr
}
return v
}
func textlimit(s string, n int) string {
l := len(s)
if l <= n {
return s
}
return s[0:n-3]+"..."
}
// init sets up the command flags
func init() {
flag.StringVar(&bgcolor, "bg", "white", "background color")
flag.StringVar(&barcolor, "bc", "rgb(200,200,200)", "bar color")
flag.StringVar(&valformat, "vfmt", "%v", "value format")
flag.IntVar(&width, "w", 1024, "width")
flag.IntVar(&height, "h", 800, "height")
flag.IntVar(&barheight, "bh", 20, "bar height")
flag.IntVar(&gutter, "g", 5, "gutter")
flag.IntVar(&cornerRadius, "cr", 0, "corner radius")
flag.IntVar(&fontsize, "f", 18, "fontsize (px)")
flag.BoolVar(&showscale, "showscale", true, "show scale")
flag.BoolVar(&showgrid, "showgrid", false, "show grid")
flag.BoolVar(&showdata, "showdata", false, "show data values")
flag.BoolVar(&showtitle, "showtitle", false, "show title")
flag.BoolVar(&endtitle, "endtitle", false, "align title to the end")
flag.BoolVar(&trace, "trace", false, "show name/value pairs")
flag.StringVar(&inbar, "inbar", "", "data in bar format")
flag.StringVar(&title, "t", "", "title")
}
// for every input file (or stdin), draw a bar graph
// as specified by command flags
func main() {
flag.Parse()
canvas := svg.New(os.Stdout)
canvas.Start(width, height)
canvas.Rect(0, 0, width, height, "fill:"+bgcolor)
canvas.Gstyle(fmt.Sprintf(gstyle, fontsize))
if len(flag.Args()) == 0 {
dobc("", canvas)
} else {
for _, f := range flag.Args() {
dobc(f, canvas)
}
}
canvas.Gend()
canvas.End()
}