By Daniel · December 27, 2008
Draws a pie chart illustrating the given list of data. The list does not need to be normalized; this is done automatically. Note that this function only draws the shape of the chart, not the labels.
// draw_piechart(dataList)
// Draws a pie chart illustrating the given list of data
// The list does not need to be normalized; this is done automatically
var prec, sat, val, _x, _y, r, data, sum, i, j, n, _frac, acc, parts, ang1,
range, ang;
// Adjustable settings
prec = 24; // precision
sat = 80; // saturation
val = 220; // value
c_out = c_dkgray; // outline color
// Load input
_x = argument0;
_y = argument1;
r = argument2;
data = argument3;
// Compute sum
sum = 0;
for (i = 0; i < ds_list_size(data); i += 1)
sum += ds_list_find_value(data, i);
// Draw each slice
acc = 0;
for (i = 0; i < ds_list_size(data); i += 1) {
// Compute the parts, starting angle and angular range for this slice
n = ds_list_find_value(data, i);
_frac = n / sum;
parts = ceil(_frac * prec);
ang1 = 360 * acc;
range = 360 * _frac;
// Pick a color and start constructing the primitive
draw_set_color(make_color_hsv(256 * (acc + _frac / 2), sat, val));
draw_primitive_begin(pr_trianglefan);
draw_vertex(_x, _y); // center
// Draw each part of the primitive
for (j = 0; j <= parts; j += 1) {
ang = ang1 + range * j / parts;
draw_vertex(_x + lengthdir_x(r, ang), _y + lengthdir_y(r, ang));
}
// Finish up
draw_primitive_end();
acc += _frac;
}
// Draw outline
draw_set_color(c_out);
draw_circle(_x, _y, r, true);
Categories: 2D graphics, Data processing, Geometry, Mathematics
There are no comments to display.
You must be signed in to post comments.