Fossil

Diff
Login

Differences From Artifact [b5e85c0be1]:

To Artifact [2c3f0e5bdf]:


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
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







+
+
+
+
+
+
+
+








-
+















-
+

+
+
+
+
+
+










-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+


















+
+
-
+






-
+






+
-
-
-
+
+
+
+
+
+


-
+







      default: r=v; g=A; b=B;  break;
    }
  }
  sqlite3_snprintf(sizeof(zColor),zColor,"#%02x%02x%02x",r,g,b);
  return zColor;  
}

/*
** Flags that can be passed into the pie-chart generator
*/
#if INTERFACE
#define PIE_OTHER     0x0001    /* No wedge less than 1/60th of the circle */
#define PIE_CHROMATIC 0x0002    /* Wedge colors are in chromatic order */
#endif

/*
** Output HTML that will render a pie chart using data from
** the PIECHART temporary table.
**
** The schema for the PIECHART table should be:
**
**     CREATE TEMP TABLE piechart(amt REAL, label TEXT);
*/
void piechart_render(int width, int height){
void piechart_render(int width, int height, unsigned int pieFlags){
  Stmt q;
  double cx, cy;          /* center of the pie */
  double r, r2;           /* Radius of the pie */
  double x1,y1;           /* Start of the slice */
  double x2,y2;           /* End of the slice */
  double x3,y3;           /* Middle point of the slice */
  double x4,y4;           /* End of line extending from x3,y3 */
  double x5,y5;           /* Text anchor */
  double d1;              /* radius to x4,y4 */
  const char *zAnc;       /* Anchor point for text */
  double a1 = 0.0;        /* Angle for first edge of slice */
  double a2;              /* Angle for second edge */
  double a3;              /* Angle at middle of slice */
  int rot;                /* Text rotation angle */
  double sina3, cosa3;    /* sin(a3) and cos(a3) */
  unsigned char h, h2;    /* Hue */
  unsigned char h;        /* Hue */
  const char *zClr;       /* Color */
  int l;                  /* Large arc flag */
  int j;                  /* Wedge number */
  double rTotal;          /* Total piechart.amt */
  double rTooSmall;       /* Sum of pieChart.amt entries less than 1/60th */
  int nTotal;             /* Total number of entries in piechart */
  int nTooSmall;          /* Number of pieChart.amt entries less than 1/60th */

# define SATURATION 128
# define VALUE      192

  cx = 0.5*width;
  cy = 0.5*height;
  r2 = cx<cy ? cx : cy;
  r = r2 - 60.0;
  if( r<0.33333*r2 ) r = 0.33333*r2;
  h = 0;
  

  db_prepare(&q, "SELECT amt/(SELECT sum(amt) FROM piechart), label"
                 " FROM piechart ORDER BY rowid");
  while( db_step(&q)==SQLITE_ROW ){
    double x = db_column_double(&q,0);

  db_prepare(&q, "SELECT sum(amt), count(*) FROM piechart");
  if( db_step(&q)!=SQLITE_ROW ) return;
  rTotal = db_column_double(&q, 0);
  nTotal = db_column_int(&q, 1);
  db_finalize(&q);
  rTooSmall = 0.0;
  nTooSmall = 0;
  if( (pieFlags & PIE_OTHER)!=0 && nTotal>1 ){
    db_prepare(&q, "SELECT sum(amt), count(*) FROM piechart WHERE amt<:amt");
    db_bind_double(&q, ":amt", rTotal/60.0);
    if( db_step(&q)==SQLITE_ROW ){
      rTooSmall = db_column_double(&q, 0);
      nTooSmall = db_column_double(&q, 1);
    }
    db_finalize(&q);
  }
  if( nTooSmall>1 ){
    db_prepare(&q, "SELECT amt, label FROM piechart WHERE amt>=:limit"
                   " UNION ALL SELECT %.17g, '(%d others)';",
                    rTooSmall, nTooSmall);
    db_bind_double(&q, ":limit", rTotal/60.0);
    nTotal += 1 - nTooSmall;
  }else{
    db_prepare(&q, "SELECT amt, label FROM piechart");
  }
  for(j=0; db_step(&q)==SQLITE_ROW; j++){
    double x = db_column_double(&q,0)/rTotal;
    const char *zLbl = db_column_text(&q,1);
    /* @ <!-- x=%g(x) zLbl="%h(zLbl)" h=%d(h) --> */
    if( x<=0.0 ) continue;
    x1 = cx + sin(a1)*r;
    y1 = cy - cos(a1)*r;
    a2 = a1 + x*2.0*M_PI;
    x2 = cx + sin(a2)*r;
    y2 = cy - cos(a2)*r;
    a3 = 0.5*(a1+a2);
    sina3 = sin(a3);
    cosa3 = cos(a3);
    x3 = cx + sina3*r;
    y3 = cy - cosa3*r;
    d1 = r*1.1;
    x4 = cx + sina3*d1;
    y4 = cy - cosa3*d1;
    y5 = y4 - 3.0 + 6.0*(1.0 -cosa3);
    rot = ((int)(a3*180/M_PI))%180;
    if( a2-a1 > 0.6 ){
      rot = 0;  /* Never rotate text on fat slices */
    if( rot<60 ){
    }else if( rot<60 ){
      rot = (rot - 60)/2;
    }else if( rot>120 ){
      rot = (rot - 120)/2;
    }else{
      rot = 0;
    }
    if( x4<cx ){
    if( x4<=cx ){
      x5 = x4 - 5.0;
      zAnc = "end";
    }else{
      x5 = x4 + 4.0;
      zAnc = "start";
    }
    if( (j&1)==0 || (pieFlags & PIE_CHROMATIC)!=0 ){
    h2 = h + (int)128.0*x;
    zClr = rgbName(h2,SATURATION,VALUE);
    h += (int)256.0*x;
      h = 256*j/nTotal;
    }else{
      h = 256*((j+1+nTotal/2)%nTotal)/nTotal;
    }
    zClr = rgbName(h,SATURATION,VALUE);
    l = x>=0.5;
    a1 = a2;
    @ <path stroke="black" stroke-width="1" fill="%s(zClr)"
    @  d='M%g(cx),%g(cy) L%g(x1),%g(y1) A%g(r),%g(r) 0 0,1 %g(x2),%g(y2) z'/>
    @  d='M%g(cx),%g(cy)L%g(x1),%g(y1)A%g(r),%g(r) 0 %d(l),1 %g(x2),%g(y2)z'/>
    @ <line stroke='black' stroke-width='1'
    @  x1='%g(x3)' y1='%g(y3)' x2='%g(x4)' y2='%g(y4)''/>
    if( rot!=0 ){
      @ <text text-anchor="%s(zAnc)" transform='rotate(%d(rot),%g(x5),%g(y5))'
    }else{
      @ <text text-anchor="%s(zAnc)" transform='rotate(%d(rot),%g(x5),%g(y5))'
    }
180
181
182
183
184
185
186
187

188
189
190
191
192
193
194
222
223
224
225
226
227
228

229
230
231
232
233
234
235
236







-
+







    db_reset(&ins);
    n++;
  }
  db_finalize(&ins);
  blob_reset(&all);
  if( n>0 ){
    @ <svg width=%d(width) height=%d(height) style="border:1px solid #d3d3d3;">
    piechart_render(width,height);
    piechart_render(width,height, PIE_OTHER|PIE_CHROMATIC);
    @ </svg>
    @ <hr>
  }
  @ <form method="post" action='%R/test-piechart'>
  @ <p>One slice per line.  Value and then Label.<p>
  @ <textarea name='data' rows='20' cols='80'>%h(zData)</textarea><br/>
  @ Width: <input type='text' size='8' name='width' value='%d(width)'/>