From 12483e997841af289986d3d9b547fe2d6ab28631 Mon Sep 17 00:00:00 2001 From: Nishi Date: Mon, 14 Sep 2026 06:01:54 +0900 Subject: [PATCH] pie graph --- examples/basic/periodic.c | 2 +- include/Mw/Constants.h | 1 + milsko.xml | 1 + src/widget/chart.c | 55 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 1 deletion(-) diff --git a/examples/basic/periodic.c b/examples/basic/periodic.c index c17cfd0..e53d66b 100644 --- a/examples/basic/periodic.c +++ b/examples/basic/periodic.c @@ -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); diff --git a/include/Mw/Constants.h b/include/Mw/Constants.h index 6df447d..42f1471 100644 --- a/include/Mw/Constants.h +++ b/include/Mw/Constants.h @@ -116,6 +116,7 @@ enum MwCLIPBOARD { enum MwCHART { MwCHART_BAR = 0, MwCHART_3D_BAR, + MwCHART_PIE }; enum MwMOUSE { diff --git a/milsko.xml b/milsko.xml index 6bf802b..0652a55 100644 --- a/milsko.xml +++ b/milsko.xml @@ -233,6 +233,7 @@ 0 + 1 diff --git a/src/widget/chart.c b/src/widget/chart.c index 83e3a5e..4079e8b 100644 --- a/src/widget/chart.c +++ b/src/widget/chart.c @@ -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);