11 MinWidth = Left+Right+2,
12 MinHeight = Top+Bottom+2,
13 DefaultWidth = Left+Right+500,
14 DefaultHeight = Top+Bottom+40
18 static Memsubfont *smallfont;
19 static Memimage *black;
20 static Memimage *blue;
22 static Memimage *lofill[6];
23 static Memimage *hifill[6];
24 static Memimage *grid;
26 static ulong fill[] = {
27 0xFFAAAAFF, 0xBB5D5DFF, /* peach */
28 DPalegreygreen, DPurpleblue, /* aqua */
29 DDarkyellow, DYellowgreen, /* yellow */
30 DMedgreen, DDarkgreen, /* green */
31 0x00AAFFFF, 0x0088CCFF, /* blue */
32 0xCCCCCCFF, 0x888888FF, /* grey */
36 allocrepl(ulong color)
40 m = allocmemimage(Rect(0,0,1,1), RGB24);
41 memfillcolor(m, color);
43 m->clipr = Rect(-1000000, -1000000, 1000000, 1000000);
59 smallfont = openmemsubfont(unsharp("#9/font/lucsans/lstr.10"));
61 smallfont = openmemsubfont("/lib/font/bit/lucidasans/lstr.10");
64 blue = allocrepl(DBlue);
65 red = allocrepl(DRed);
66 grid = allocrepl(0x77777777);
67 for(i=0; i<nelem(fill)/2 && i<nelem(lofill) && i<nelem(hifill); i++){
68 lofill[i] = allocrepl(fill[2*i]);
69 hifill[i] = allocrepl(fill[2*i+1]);
74 mklabel(char *str, int v)
83 sprint(str, "%dk", v/1000);
85 sprint(str, "%dM", v/1000000);
89 drawlabel(Memimage *m, Point p, int n)
95 w = memsubfontwidth(smallfont, buf);
96 memimagestring(m, Pt(p.x-5-w.x, p.y), memblack, ZP, smallfont, buf);
100 scalept(int val, int valmin, int valmax, int ptmin, int ptmax)
108 return ptmin + (vlong)(val-valmin)*(ptmax-ptmin)/(valmax-valmin);
114 int i, nbin, x, lo, hi, min, max, first;
117 Statbin *b, bin[2000]; /* 32 kB, but whack is worse */
119 needstack(8192); /* double check that bin didn't kill us */
121 if(g->wid <= MinWidth)
122 g->wid = DefaultWidth;
123 if(g->ht <= MinHeight)
124 g->ht = DefaultHeight;
125 if(g->wid > nelem(bin))
128 g->fill = ((uint)(uintptr)g->arg>>8)%nelem(lofill);
129 if(g->fill > nelem(lofill))
130 g->fill %= nelem(lofill);
132 nbin = g->wid - (Left+Right);
133 binstats(g->fn, g->arg, g->t0, g->t1, bin, nbin);
140 if(min < 0 || max <= min){
143 for(i=0; i<nbin; i++){
147 if(first || b->min < min)
149 if(first || b->max > max)
157 if(smallfont==nil || black==nil || blue==nil || red==nil || hifill[0]==nil || lofill[0]==nil){
158 werrstr("graphics initialization failed: %r");
159 qunlock(&memdrawlock);
164 m = allocmemimage(Rect(0,0,g->wid,g->ht), ABGR32);
166 qunlock(&memdrawlock);
169 r = Rect(Left, Top, g->wid-Right, g->ht-Bottom);
170 memfillcolor(m, DTransparent);
173 memimagedraw(m, Rect(r.min.x, r.max.y, r.max.x, r.max.y+1), black, ZP, memopaque, ZP, S);
176 drawlabel(m, r.min, max);
178 drawlabel(m, Pt(r.min.x, r.max.y-smallfont->height), min);
181 for(i=0; i<nbin; i++){
185 lo = scalept(b->min, min, max, r.max.y, r.min.y);
186 hi = scalept(b->max, min, max, r.max.y, r.min.y);
189 memimagedraw(m, Rect(x, hi, x+1,lo), hifill[g->fill%nelem(hifill)], ZP, memopaque, ZP, S);
190 memimagedraw(m, Rect(x, lo, x+1, r.max.y), lofill[g->fill%nelem(lofill)], ZP, memopaque, ZP, S);
193 if(bin[nbin-1].nsamp)
194 drawlabel(m, Pt(r.max.x, r.min.y+(Dy(r)-smallfont->height)/2), bin[nbin-1].avg);
195 qunlock(&memdrawlock);