-
Notifications
You must be signed in to change notification settings - Fork 3
/
pmap.go
executable file
·244 lines (224 loc) · 5.86 KB
/
pmap.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
// pmap percentage maps
package main
import (
"encoding/xml"
"flag"
"fmt"
"io"
"os"
"github.com/ajstarks/gensvg"
)
// Pmap defines a porportional map
type Pmap struct {
Top float64 `xml:"top,attr"`
Left float64 `xml:"left,attr"`
Title string `xml:"title,attr"`
Pdata []Pdata `xml:"pdata"`
}
// Pdata defines data with a portpotional map
type Pdata struct {
Legend string `xml:"legend,attr"`
Stagger string `xml:"stagger,attr"`
Alternate string `xml:"alternate,attr"`
Item []Item `xml:"item"`
}
// Item defines an item with porpotional map data
type Item struct {
Name string `xml:",chardata"`
Value float64 `xml:"value,attr"`
}
var (
width, height, fontsize, fontscale, round, gutter, oflen float64
bgcolor, olcolor, colorspec, title string
pred, pgreen, pblue int
showpercent, showdata, alternate, showtitle, stagger, showlegend, showtotal bool
ofpct float64
leftmargin = 40.0
topmargin = 40.0
canvas = gensvg.New(os.Stdout)
)
const (
globalfmt = "stroke-width:1;font-family:Calibri,sans-serif;text-anchor:middle;font-size:%vpt"
legendstyle = "text-anchor:start;font-size:150%"
linefmt = "stroke:%s"
)
func dopmap(location string) {
var f *os.File
var err error
if len(location) > 0 {
f, err = os.Open(location)
} else {
f = os.Stdin
}
if err == nil {
readpmap(f)
f.Close()
} else {
fmt.Fprintf(os.Stderr, "%v\n", err)
}
}
func readpmap(r io.Reader) {
var pm Pmap
if err := xml.NewDecoder(r).Decode(&pm); err == nil {
drawpmap(pm)
} else {
fmt.Fprintf(os.Stderr, "Unable to parse pmap (%v)\n", err)
}
}
func drawpmap(m Pmap) {
fs := fontsize
if m.Left > 0 {
leftmargin = m.Left
}
if m.Top > 0 {
topmargin = m.Top
} else {
topmargin = fs * fontscale
}
x := leftmargin
y := topmargin
if len(m.Title) > 0 {
title = m.Title
}
canvas.Title(title)
if showtitle {
dotitle(title)
}
for _, p := range m.Pdata {
pmap(x, y, fs, p)
y += fs*fontscale + (gutter + fs*2)
}
}
func pmap(x, y, fs float64, m Pdata) {
var tfill, vfmt, oc string
var up bool
var slen float64
h := fs * fontscale
fw := fs * 80
up = false
sum := 0.0
for _, v := range m.Item {
sum += v.Value
}
if len(olcolor) > 0 {
oc = olcolor
} else {
oc = bgcolor
}
loffset := (fs * fontscale) + fs
gline := fmt.Sprintf(linefmt, "gray")
wline := fmt.Sprintf(linefmt, oc)
if len(m.Legend) > 0 && showlegend {
if showtotal {
canvas.Text(x, y-fs, fmt.Sprintf("%s (total: "+floatfmt(sum)+")", m.Legend, sum), legendstyle)
} else {
canvas.Text(x, y-fs, m.Legend, legendstyle)
}
}
for i, p := range m.Item {
k := p.Name
v := p.Value
if v == 0.0 {
continue
}
pct := v / sum
pw := pct * fw
xw := x + (pw / 2)
yh := y + (h / 2)
if pct >= .4 {
tfill = "fill:white"
} else {
tfill = "fill:black"
}
if round > 0 {
canvas.Roundrect(x, y, pw, h, round, round, canvas.RGBA(pred, pgreen, pblue, pct))
} else {
canvas.Rect(x, y, pw, h, canvas.RGBA(pred, pgreen, pblue, pct))
}
dy := yh + fs + (fs / 2)
if pct <= ofpct || float64(len(k)) > oflen { // overflow label
if up {
dy -= loffset
yh -= loffset
canvas.Line(xw, y, xw, dy+(fs/2), gline)
} else {
dy += loffset
yh += loffset
canvas.Line(xw, y+h, xw, dy-(fs*3), gline)
}
if alternate {
up = !up
slen = fs * 2
} else {
slen = fs * 3
}
if stagger {
loffset += slen
}
tfill = "fill:black"
}
canvas.Text(xw, yh, k, tfill)
dpfmt := tfill + ";font-size:75%"
vfmt = floatfmt(v)
switch {
case showpercent && !showdata:
canvas.Text(xw, dy, fmt.Sprintf("%.1f%%", pct*100), dpfmt)
case showpercent && showdata:
canvas.Text(xw, dy, fmt.Sprintf(vfmt+", %.1f%%", v, pct*100), dpfmt)
case showdata && !showpercent:
canvas.Text(xw, dy, fmt.Sprintf(vfmt, v), dpfmt)
}
x += pw
if i < len(m.Item) {
canvas.Line(x, y, x, y+h, wline)
}
}
}
func floatfmt(v float64) string {
var vfmt = "%.1f"
if v-float64(int(v)) == 0.0 {
vfmt = "%.0f"
}
return vfmt
}
func dotitle(s string) {
offset := 40.0
canvas.Text(leftmargin, height-offset, s, "text-anchor:start;font-size:250%")
}
func init() {
flag.Float64Var(&width, "w", 1024, "width")
flag.Float64Var(&height, "h", 768, "height")
flag.Float64Var(&fontsize, "f", 12, "font size (pt)")
flag.Float64Var(&fontscale, "s", 5, "font scaling factor")
flag.Float64Var(&round, "r", 0, "rounded corner size")
flag.Float64Var(&gutter, "g", 100, "gutter")
flag.Float64Var(&oflen, "ol", 20, "overflow length")
flag.StringVar(&bgcolor, "bg", "white", "background color")
flag.StringVar(&olcolor, "oc", "", "outline color")
flag.StringVar(&colorspec, "c", "0,0,0", "color (r,g,b)")
flag.StringVar(&title, "t", "Proportions", "title")
flag.BoolVar(&showpercent, "p", false, "show percentage")
flag.BoolVar(&showdata, "d", false, "show data")
flag.BoolVar(&alternate, "a", false, "alternate overflow labels")
flag.BoolVar(&stagger, "stagger", false, "stagger labels")
flag.BoolVar(&showlegend, "showlegend", true, "show the legend")
flag.BoolVar(&showtitle, "showtitle", false, "show the title")
flag.BoolVar(&showtotal, "showtotal", false, "show totals in the legend")
flag.Float64Var(&ofpct, "op", 0.05, "overflow percentage")
flag.Parse()
fmt.Sscanf(colorspec, "%d,%d,%d", &pred, &pgreen, &pblue)
}
func main() {
canvas.Start(width, height)
canvas.Rect(0, 0, width, height, "fill:"+bgcolor)
canvas.Gstyle(fmt.Sprintf(globalfmt, fontsize))
if len(flag.Args()) == 0 {
dopmap("")
} else {
for _, f := range flag.Args() {
dopmap(f)
}
}
canvas.Gend()
canvas.End()
}