Skip to content
Snippets Groups Projects
Commit 5e695300 authored by Tom Greig's avatar Tom Greig
Browse files

Add legend and v keybind to toggle it

parent 37a3df65
No related branches found
No related tags found
No related merge requests found
...@@ -32,5 +32,8 @@ $ e-plot --help ...@@ -32,5 +32,8 @@ $ e-plot --help
Keybinding: Keybinding:
h,l - Move left and right ←, h - Move left
H,L - Zoom out and in →, l - Move right
Shift+←, H - Zoom out
Shift+→, L - Zoom in
v - Toggle legend
...@@ -42,6 +42,7 @@ struct context { ...@@ -42,6 +42,7 @@ struct context {
struct { struct {
char* header; char* header;
PangoLayout* legend;
enum { XAXIS, LEFT_AXIS, RIGHT_AXIS, IGNORE, } type; enum { XAXIS, LEFT_AXIS, RIGHT_AXIS, IGNORE, } type;
} * columns; } * columns;
size_t n_columns; size_t n_columns;
...@@ -93,6 +94,8 @@ struct context { ...@@ -93,6 +94,8 @@ struct context {
int n; int n;
} xtick_layouts, yltick_layouts, yrtick_layouts; } xtick_layouts, yltick_layouts, yrtick_layouts;
bool show_legend;
}; };
struct fd_source { struct fd_source {
...@@ -133,6 +136,7 @@ int main( int argc, char** argv ) { ...@@ -133,6 +136,7 @@ int main( int argc, char** argv ) {
context->xfit = true; context->xfit = true;
context->xfollow = true; context->xfollow = true;
context->font_size = 12; context->font_size = 12;
context->show_legend = true;
static const char* colours[] = { static const char* colours[] = {
"#005C84", "#FCBC00", "#0C838C", "#E63037", "#005C84", "#FCBC00", "#0C838C", "#E63037",
...@@ -479,6 +483,18 @@ static int key_callback( GtkEventControllerKey* event_controller, ...@@ -479,6 +483,18 @@ static int key_callback( GtkEventControllerKey* event_controller,
} }
case 'v': {
if ( 0 == mods ) {
context->show_legend = !context->show_legend;
gtk_widget_queue_draw( context->plot );
return true;
}
return false;
}
default: { default: {
return false; return false;
...@@ -1424,6 +1440,109 @@ static void plot_draw( GtkDrawingArea* plot, ...@@ -1424,6 +1440,109 @@ static void plot_draw( GtkDrawingArea* plot,
cairo_restore( cr ); cairo_restore( cr );
/* Legend */
if ( context->show_legend ) {
double w = 0;
double h = 0.5 * context->font_size;
for ( size_t i = 0; i < context->n_columns; i++ ) {
if ( XAXIS == context->columns[i].type ||
IGNORE == context->columns[i].type ) {
continue;
}
if ( NULL == context->columns[i].legend ) {
context->columns[i].legend =
pango_cairo_create_layout( cr );
pango_layout_set_font_description(
context->columns[i].legend,
context->tick_font_description );
pango_layout_set_text( context->columns[i].legend,
context->columns[i].header, -1 );
}
int lw, lh;
pango_layout_get_pixel_size( context->columns[i].legend,
&lw, &lh );
if ( w < lw + 5 * context->font_size ) {
w = lw + 5 * context->font_size;
}
h += lh + 0.5 * context->font_size;
}
cairo_save( cr );
cairo_set_line_width( cr, 1 );
cairo_move_to( cr, m[1], m[2] );
cairo_line_to( cr, m[1] + w, m[2] );
cairo_line_to( cr, m[1] + w, m[2] + h );
cairo_line_to( cr, m[1], m[2] + h );
cairo_line_to( cr, m[1], m[2] );
cairo_set_source_rgb( cr, 1, 1, 1 );
cairo_fill_preserve( cr );
cairo_set_source_rgb( cr, 0, 0, 0 );
cairo_stroke( cr );
cairo_restore( cr );
h = 0.5 * context->font_size + m[2];
size_t c = 0;
for ( size_t i = 0; i < context->n_columns; i++ ) {
if ( XAXIS == context->columns[i].type ||
IGNORE == context->columns[i].type ) {
continue;
}
int lw, lh;
pango_layout_get_pixel_size( context->columns[i].legend,
&lw, &lh );
if ( context->columns[i].type == LEFT_AXIS ) {
cairo_move_to( cr, m[1] + context->font_size * 4, h );
} else {
cairo_move_to( cr, m[1] + context->font_size * 1, h );
}
cairo_save( cr );
cairo_set_source_rgb( cr, 0, 0, 0 );
pango_cairo_show_layout( cr,
context->columns[i].legend );
cairo_restore( cr );
if ( context->columns[i].type == LEFT_AXIS ) {
cairo_move_to( cr,
m[1] + context->font_size * 1, h + lh / 2 );
cairo_line_to( cr,
m[1] + context->font_size * 3, h + lh / 2 );
} else {
cairo_move_to( cr,
m[1] + w - context->font_size * 3, h + lh / 2 );
cairo_line_to( cr,
m[1] + w - context->font_size * 1, h + lh / 2 );
}
cairo_save( cr );
cairo_set_source_rgb( cr, context->colours[c][0],
context->colours[c][1], context->colours[c][2]);
cairo_set_line_width( cr, 1 );
cairo_stroke( cr );
cairo_restore( cr );
h += lh + 0.5 * context->font_size;
c = ( c + 1 ) % context->n_colours;
}
}
} }
static int pretty( double* lo, double* up, int ndiv ) { static int pretty( double* lo, double* up, int ndiv ) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment