pie graph

This commit is contained in:
Nishi 2026-09-14 06:01:54 +09:00
commit 12483e9978
4 changed files with 58 additions and 1 deletions

View file

@ -269,7 +269,7 @@ int main() {
wclock = child(f);
f = frame("Chart", -PaddingContent, -PaddingContent, MwChartClass,
MwNtype, MwCHART_3D_BAR,
MwNtype, MwCHART_PIE,
MwNcolumnSpan, 2,
NULL);
w = child(f);

View file

@ -116,6 +116,7 @@ enum MwCLIPBOARD {
enum MwCHART {
MwCHART_BAR = 0,
MwCHART_3D_BAR,
MwCHART_PIE
};
enum MwMOUSE {

View file

@ -233,6 +233,7 @@
<enumeration name="MwCHART">
<integer name="MwCHART_BAR">0</integer>
<integer name="MwCHART_3D_BAR" />
<integer name="MwCHART_PIE" />
</enumeration>
<enumeration name="MwMOUSE">
<integer name="MwMOUSE_LEFT">1</integer>

View file

@ -13,6 +13,7 @@ static const char* palette0[] = {
NULL};
#define NUMFMT "%.2f"
#define PIE_DIV 4
static int wcreate(MwWidget handle) {
MwChart c = malloc(sizeof(*c));
@ -239,6 +240,60 @@ static void draw(MwWidget handle) {
MwLLFreeColor(colorl);
MwLLFreeColor(color);
}
} else if(type == MwCHART_PIE) {
double sum = 0;
int radius = r.width < r.height ? r.width : r.height;
double cangle = 0;
int k;
for(i = 0; i < arrlen(c->entries); i++) {
if(c->entries[i].value > 0) sum += c->entries[i].value;
}
for(k = 0; k < 2; k++) {
for(i = 0; i < arrlen(c->entries); i++) {
MwLLColor color;
double angle = c->entries[i].value / sum * 360;
MwPoint p[360 + 1 + 1];
int div = angle / PIE_DIV;
int j;
int count;
if(c->entries[i].value <= 0) continue;
color = MwParseColor(handle, c->entries[i].color == NULL ? colors[i % n] : c->entries[i].color);
p[0].x = r.width / 2;
p[0].y = r.height / 2;
for(j = 0; j <= div; j++) {
double a = angle / div * j;
p[div - j + 1].x = p[0].x + cos((cangle + a - 90) / 180 * M_PI) * radius / 2;
p[div - j + 1].y = p[0].y + sin((cangle + a - 90) / 180 * M_PI) * radius / 2;
}
count = j + 1;
if(k == 0) {
MwLLPolygon(handle->lowlevel, p, count, color);
} else {
int j;
p[count] = p[0];
for(j = 0; j < count; j++) {
MwLLLine(handle->lowlevel, &p[j], border);
}
p[0].x += cos((cangle + angle / 2 - 90) / 180 * M_PI) * radius / 4;
p[0].y += sin((cangle + angle / 2 - 90) / 180 * M_PI) * radius / 4;
MwDrawText(handle, NULL, p, c->entries[i].name, MwALIGNMENT_CENTER, border);
}
cangle += angle;
MwLLFreeColor(color);
}
}
}
MwLLFreeColor(border);