From e6063b79aa399bb908a51fc40dd8a33d55bcfe01 Mon Sep 17 00:00:00 2001 From: vurtun Date: Wed, 25 May 2016 10:58:00 +0200 Subject: [PATCH] Added chart line/column dependend coloring API It's now possible to define a specific color for each line or column entry inside a chart. This purely extends the API and all previous implementation still work. --- demo/overview.c | 15 +++++++++++++++ nuklear.h | 41 ++++++++++++++++++++++++++++++----------- 2 files changed, 45 insertions(+), 11 deletions(-) diff --git a/demo/overview.c b/demo/overview.c index e440c6d7..d946a73b 100644 --- a/demo/overview.c +++ b/demo/overview.c @@ -618,6 +618,21 @@ overview(struct nk_context *ctx) } } nk_chart_end(ctx); + + /* mixed colored chart */ + nk_layout_row_dynamic(ctx, 100, 1); + bounds = nk_widget_bounds(ctx); + if (nk_chart_begin_colored(ctx, NK_CHART_LINES, nk_rgb(255,0,0), nk_rgb(150,0,0), 32, 0.0f, 1.0f)) { + nk_chart_add_slot_colored(ctx, NK_CHART_LINES, nk_rgb(0,0,255), nk_rgb(0,0,150),32, -1.0f, 1.0f); + nk_chart_add_slot_colored(ctx, NK_CHART_LINES, nk_rgb(0,255,0), nk_rgb(0,150,0), 32, -1.0f, 1.0f); + for (id = 0, i = 0; i < 32; ++i) { + nk_chart_push_slot(ctx, (float)fabs(sin(id)), 0); + nk_chart_push_slot(ctx, (float)cos(id), 1); + nk_chart_push_slot(ctx, (float)sin(id), 2); + id += step; + } + } + nk_chart_end(ctx); nk_tree_pop(ctx); } diff --git a/nuklear.h b/nuklear.h index 317b1144..12566098 100644 --- a/nuklear.h +++ b/nuklear.h @@ -674,7 +674,9 @@ NK_API nk_flags nk_edit_buffer(struct nk_context*, nk_flags, str /* Chart */ NK_API int nk_chart_begin(struct nk_context*, enum nk_chart_type, int num, float min, float max); -NK_API void nk_chart_add_slot(struct nk_context *ctx, const enum nk_chart_type type, int count, float min_value, float max_value); +NK_API int nk_chart_begin_colored(struct nk_context*, enum nk_chart_type, struct nk_color, struct nk_color active, int num, float min, float max); +NK_API void nk_chart_add_slot(struct nk_context *ctx, const enum nk_chart_type, int count, float min_value, float max_value); +NK_API void nk_chart_add_slot_colored(struct nk_context *ctx, const enum nk_chart_type, struct nk_color, struct nk_color active, int count, float min_value, float max_value); NK_API nk_flags nk_chart_push(struct nk_context*, float); NK_API nk_flags nk_chart_push_slot(struct nk_context*, float, int); NK_API void nk_chart_end(struct nk_context*); @@ -2190,11 +2192,12 @@ NK_API struct nk_style_item nk_style_item_hide(void); struct nk_chart_slot { enum nk_chart_type type; + struct nk_color color; + struct nk_color highlight; float min, max, range; int count; struct nk_vec2 last; int index; - }; struct nk_chart { @@ -17902,7 +17905,8 @@ nk_color_picker(struct nk_context *ctx, struct nk_color color, * * --------------------------------------------------------------*/ NK_API int -nk_chart_begin(struct nk_context *ctx, const enum nk_chart_type type, +nk_chart_begin_colored(struct nk_context *ctx, enum nk_chart_type type, + struct nk_color color, struct nk_color highlight, int count, float min_value, float max_value) { struct nk_window *win; @@ -17941,6 +17945,8 @@ nk_chart_begin(struct nk_context *ctx, const enum nk_chart_type type, {struct nk_chart_slot *slot = &chart->slots[chart->slot++]; slot->type = type; slot->count = count; + slot->color = color; + slot->highlight = highlight; slot->min = NK_MIN(min_value, max_value); slot->max = NK_MAX(min_value, max_value); slot->range = slot->max - slot->min;} @@ -17957,8 +17963,14 @@ nk_chart_begin(struct nk_context *ctx, const enum nk_chart_type type, return 1; } +NK_API int +nk_chart_begin(struct nk_context *ctx, const enum nk_chart_type type, + int count, float min_value, float max_value) +{return nk_chart_begin_colored(ctx, type, ctx->style.chart.color, ctx->style.chart.selected_color, count, min_value, max_value);} + NK_API void -nk_chart_add_slot(struct nk_context *ctx, const enum nk_chart_type type, +nk_chart_add_slot_colored(struct nk_context *ctx, const enum nk_chart_type type, + struct nk_color color, struct nk_color highlight, int count, float min_value, float max_value) { NK_ASSERT(ctx); @@ -17973,11 +17985,18 @@ nk_chart_add_slot(struct nk_context *ctx, const enum nk_chart_type type, struct nk_chart_slot *slot = &chart->slots[chart->slot++]; slot->type = type; slot->count = count; + slot->color = color; + slot->highlight = highlight; slot->min = NK_MIN(min_value, max_value); slot->max = NK_MAX(min_value, max_value); slot->range = slot->max - slot->min;} } +NK_API void +nk_chart_add_slot(struct nk_context *ctx, const enum nk_chart_type type, + int count, float min_value, float max_value) +{nk_chart_add_slot_colored(ctx, type, ctx->style.chart.color, ctx->style.chart.selected_color, count, min_value, max_value);} + NK_INTERN nk_flags nk_chart_push_line(struct nk_context *ctx, struct nk_window *win, struct nk_chart *g, float value, int slot) @@ -18011,13 +18030,13 @@ nk_chart_push_line(struct nk_context *ctx, struct nk_window *win, bounds.w = 4; bounds.h = 4; - color = style->color; + color = g->slots[slot].color; if (!(layout->flags & NK_WINDOW_ROM) && NK_INBOX(i->mouse.pos.x,i->mouse.pos.y, g->slots[slot].last.x-3, g->slots[slot].last.y-3, 6, 6)){ ret = nk_input_is_mouse_hovering_rect(i, bounds) ? NK_CHART_HOVERING : 0; ret |= (i->mouse.buttons[NK_BUTTON_LEFT].down && i->mouse.buttons[NK_BUTTON_LEFT].clicked) ? NK_CHART_CLICKED: 0; - color = style->selected_color; + color = g->slots[slot].highlight; } nk_fill_rect(out, bounds, 0, color); g->slots[slot].index += 1; @@ -18025,9 +18044,10 @@ nk_chart_push_line(struct nk_context *ctx, struct nk_window *win, } /* draw a line between the last data point and the new one */ + color = g->slots[slot].color; cur.x = g->x + (float)(step * (float)g->slots[slot].index); cur.y = (g->y + g->h) - (ratio * (float)g->h); - nk_stroke_line(out, g->slots[slot].last.x, g->slots[slot].last.y, cur.x, cur.y, 1.0f, style->color); + nk_stroke_line(out, g->slots[slot].last.x, g->slots[slot].last.y, cur.x, cur.y, 1.0f, color); bounds.x = cur.x - 3; bounds.y = cur.y - 3; @@ -18035,13 +18055,12 @@ nk_chart_push_line(struct nk_context *ctx, struct nk_window *win, bounds.h = 6; /* user selection of current data point */ - color = style->color; if (!(layout->flags & NK_WINDOW_ROM)) { if (nk_input_is_mouse_hovering_rect(i, bounds)) { ret = NK_CHART_HOVERING; ret |= (!i->mouse.buttons[NK_BUTTON_LEFT].down && i->mouse.buttons[NK_BUTTON_LEFT].clicked) ? NK_CHART_CLICKED: 0; - color = style->selected_color; + color = g->slots[slot].highlight; } } nk_fill_rect(out, nk_rect(cur.x - 2, cur.y - 2, 4, 4), 0, color); @@ -18077,7 +18096,7 @@ nk_chart_push_column(const struct nk_context *ctx, struct nk_window *win, /* calculate bounds of current bar chart entry */ style = &ctx->style.chart; - color = style->color; + color = chart->slots[slot].color;; item.h = chart->h * NK_ABS((value/chart->slots[slot].range)); if (value >= 0) { ratio = (value + NK_ABS(chart->slots[slot].min)) / NK_ABS(chart->slots[slot].range); @@ -18095,7 +18114,7 @@ nk_chart_push_column(const struct nk_context *ctx, struct nk_window *win, ret = NK_CHART_HOVERING; ret |= (!in->mouse.buttons[NK_BUTTON_LEFT].down && in->mouse.buttons[NK_BUTTON_LEFT].clicked) ? NK_CHART_CLICKED: 0; - color = style->selected_color; + color = chart->slots[slot].highlight; } nk_fill_rect(out, item, 0, color); chart->slots[slot].index += 1;