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);
58 smallfont = openmemsubfont(unsharp("#9/font/lucidasans/lstr.10"));
60 blue = allocrepl(DBlue);
61 red = allocrepl(DRed);
62 grid = allocrepl(0x77777777);
63 for(i=0; i<nelem(fill)/2 && i<nelem(lofill) && i<nelem(hifill); i++){
64 lofill[i] = allocrepl(fill[2*i]);
65 hifill[i] = allocrepl(fill[2*i+1]);
70 mklabel(char *str, int v)
79 sprint(str, "%dk", v/1000);
81 sprint(str, "%dM", v/1000000);
85 drawlabel(Memimage *m, Point p, int n)
91 w = memsubfontwidth(smallfont, buf);
92 memimagestring(m, Pt(p.x-5-w.x, p.y), memblack, ZP, smallfont, buf);
96 scalept(int val, int valmin, int valmax, int ptmin, int ptmax)
104 return ptmin + (vlong)(val-valmin)*(ptmax-ptmin)/(valmax-valmin);
110 int i, lastlo, nbin, x, lo, hi, min, max, first;
113 Statbin *b, bin[2000]; /* 32 kB, but whack is worse */
115 needstack(8192); /* double check that bin didn't kill us */
117 if(g->wid <= MinWidth)
118 g->wid = DefaultWidth;
119 if(g->ht <= MinHeight)
120 g->ht = DefaultHeight;
121 if(g->wid > nelem(bin))
124 g->fill = ((uint)g->arg>>8)%nelem(lofill);
125 if(g->fill > nelem(lofill))
126 g->fill %= nelem(lofill);
128 nbin = g->wid - (Left+Right);
129 binstats(g->fn, g->arg, g->t0, g->t1, bin, nbin);
136 if(min < 0 || max <= min){
139 for(i=0; i<nbin; i++){
143 if(first || b->min < min)
145 if(first || b->max > max)
153 if(smallfont==nil || black==nil || blue==nil || red==nil || hifill==nil || lofill==nil){
154 werrstr("graphics initialization failed");
155 qunlock(&memdrawlock);
160 m = allocmemimage(Rect(0,0,g->wid,g->ht), ABGR32);
162 qunlock(&memdrawlock);
165 r = Rect(Left, Top, g->wid-Right, g->ht-Bottom);
166 memfillcolor(m, DTransparent);
169 memimagedraw(m, Rect(r.min.x, r.max.y, r.max.x, r.max.y+1), black, ZP, memopaque, ZP, S);
172 drawlabel(m, r.min, max);
174 drawlabel(m, Pt(r.min.x, r.max.y-smallfont->height), min);
178 for(i=0; i<nbin; i++){
182 lo = scalept(b->min, min, max, r.max.y, r.min.y);
183 hi = scalept(b->max, min, max, r.max.y, r.min.y);
189 memimagedraw(m, Rect(x-1, lastlo, x, lo), hifill[g->fill], ZP, memopaque, ZP, S);
191 memimagedraw(m, Rect(x-1, lo, x, lastlo), hifill[g->fill], ZP, memopaque, ZP, S);
193 memimagedraw(m, Rect(x, hi, x+1,lo), hifill[g->fill], ZP, memopaque, ZP, S);
194 memimagedraw(m, Rect(x, lo, x+1, r.max.y), lofill[g->fill], ZP, memopaque, ZP, S);
198 if(bin[nbin-1].nsamp)
199 drawlabel(m, Pt(r.max.x, r.min.y+(Dy(r)-smallfont->height)/2), bin[nbin-1].avg);
200 qunlock(&memdrawlock);