Skip to content
This repository was archived by the owner on Aug 29, 2021. It is now read-only.

Commit 5fcc124

Browse files
committed
Fix some grid/ticks being draw too many/too few
1 parent 7ea581f commit 5fcc124

File tree

4 files changed

+33
-18
lines changed

4 files changed

+33
-18
lines changed

src/goat-plot.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -366,16 +366,23 @@ static gboolean draw (GtkWidget *widget, cairo_t *cr)
366366
const gint width = allocation.width - padding.left - padding.right;
367367
const gint height = allocation.height - padding.top - padding.bottom;
368368

369+
g_printf ("--- w=%i h=%i\n", width, height);
369370
gdouble ref_x_min = +G_MAXDOUBLE;
370371
gdouble ref_x_max = -G_MAXDOUBLE;
371372
gdouble ref_y_min = +G_MAXDOUBLE;
372373
gdouble ref_y_max = -G_MAXDOUBLE;
373-
goat_scale_get_range (priv->scale_x, &ref_x_min, &ref_x_max);
374-
goat_scale_get_range (priv->scale_y, &ref_y_min, &ref_y_max);
375374

376-
// draw the actual data
375+
// get the common extends of all data sets if any scale used is set to autorange
376+
//
377+
// vfunc, we use it in a loop, so caching is good idea
377378
const gboolean register autorange_x = goat_scale_is_auto_range (priv->scale_x);
378379
const gboolean register autorange_y = goat_scale_is_auto_range (priv->scale_y);
380+
if (!autorange_x) {
381+
goat_scale_get_range (priv->scale_x, &ref_x_min, &ref_x_max);
382+
}
383+
if (!autorange_y) {
384+
goat_scale_get_range (priv->scale_y, &ref_y_min, &ref_y_max);
385+
}
379386
if (autorange_x || autorange_y) {
380387

381388
for (i = 0; i < priv->array->len; i++) {
@@ -399,19 +406,17 @@ static gboolean draw (GtkWidget *widget, cairo_t *cr)
399406
goat_scale_update_range (priv->scale_x, ref_x_min, ref_x_max);
400407
goat_scale_update_range (priv->scale_y, ref_y_min, ref_y_max);
401408

402-
g_printf ("x range %lf %lf\n", ref_x_min, ref_x_max);
403-
g_printf ("y range %lf %lf\n", ref_y_min, ref_y_max);
404-
405409
// TODO add some fixup if x_min is very close to x_max
406410
// TODO add some additional padding for niceness :)
411+
// TODO think about extra padding to compensate marker sizes
407412
}
408413
gboolean draw = TRUE;
409414
if (!get_unit_to_pixel_factor (width, ref_x_min, ref_x_max, &x_unit_to_pixel)) {
410-
g_warning ("Bad x range. This is too boring to plot. %lf", x_unit_to_pixel);
415+
g_warning ("Bad x range");
411416
draw = FALSE;
412417
}
413418
if (!get_unit_to_pixel_factor (height, ref_y_min, ref_y_max, &y_unit_to_pixel)) {
414-
g_warning ("Bad y range. This is too boring to plot. %lf", y_unit_to_pixel);
419+
g_warning ("Bad y range");
415420
draw = FALSE;
416421
}
417422

src/goat-scale-linear.c

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ void goat_scale_linear_set_ticks (GoatScaleLinear *scale, gdouble major, gint mi
173173
}
174174

175175

176+
static inline gboolean is_major_tick (gint i, gint minors_per_major)
177+
{
178+
return (i == 0) || (i % minors_per_major == 0);
179+
}
176180
/**
177181
* @param x/y-nil in pixel
178182
* @param x/y-factor convert unit to pixel
@@ -184,11 +188,7 @@ static void draw (GoatScale *scale, cairo_t *cr, gint left, gint right, gint top
184188
GoatScaleLinearPrivate *priv = goat_scale_linear_get_instance_private (self);
185189

186190
const double step_minor = (priv->major_delta / priv->minors_per_major);
187-
const int register start = (top - nil) / step_minor / factor;
188-
const int register end = (bottom - nil) / step_minor / factor;
189-
g_print ("[] bottom %i top %i _nil %lf\n", bottom, top, nil);
190-
g_print ("> start=%i end=%i %lf _factor\n", start, end, factor);
191-
int register i;
191+
gint register i;
192192
const gint width_major = priv->width_major;
193193
const gint width_minor = priv->width_minor;
194194
GdkRGBA color_minor = priv->color_minor;
@@ -202,8 +202,10 @@ static void draw (GoatScale *scale, cairo_t *cr, gint left, gint right, gint top
202202
cairo_set_line_width (cr, 1.);
203203

204204
if (where == GOAT_POSITION_LEFT) {
205+
const int register start = (top - nil) / step_minor / factor;
206+
const int register end = (bottom - nil) / step_minor / factor;
205207
for (i = start; i <= end; i++) {
206-
const gboolean register majorstip = (i % priv->minors_per_major == 0);
208+
const gboolean register majorstip = is_major_tick (i, priv->minors_per_major);
207209
const double register y = nil + top + step_minor * factor * i;
208210
if (grid) {
209211
cairo_move_to (cr, right, y);
@@ -231,8 +233,10 @@ static void draw (GoatScale *scale, cairo_t *cr, gint left, gint right, gint top
231233
}
232234
}
233235
if (where == GOAT_POSITION_RIGHT) {
236+
const int register start = (top - nil) / step_minor / factor;
237+
const int register end = (bottom - nil) / step_minor / factor;
234238
for (i = start; i <= end; i++) {
235-
const gboolean register majorstip = (i % priv->minors_per_major == 0);
239+
const gboolean register majorstip = is_major_tick (i, priv->minors_per_major);
236240
const double register y = nil + top + step_minor * factor * i;
237241
if (grid) {
238242
cairo_move_to (cr, left, y);
@@ -260,8 +264,10 @@ static void draw (GoatScale *scale, cairo_t *cr, gint left, gint right, gint top
260264
}
261265
}
262266
if (where == GOAT_POSITION_BOTTOM) {
267+
const int register start = (left - nil) / step_minor / factor;
268+
const int register end = (right - nil) / step_minor / factor;
263269
for (i = start; i <= end; i++) {
264-
const gboolean register majorstip = (i % priv->minors_per_major == 0);
270+
const gboolean register majorstip = is_major_tick (i, priv->minors_per_major);
265271
const double register x = nil + left + step_minor * factor * i;
266272
if (grid) {
267273
cairo_move_to (cr, x, top);
@@ -289,8 +295,10 @@ static void draw (GoatScale *scale, cairo_t *cr, gint left, gint right, gint top
289295
}
290296
}
291297
if (where == GOAT_POSITION_TOP) {
298+
const int register start = (left - nil) / step_minor / factor;
299+
const int register end = (right - nil) / step_minor / factor;
292300
for (i = start; i <= end; i++) {
293-
const gboolean register majorstip = (i % priv->minors_per_major == 0);
301+
const gboolean register majorstip = is_major_tick (i, priv->minors_per_major);
294302
const double register x = nil + left + step_minor * factor * i;
295303
if (grid) {
296304
cairo_move_to (cr, x, bottom);

tests/dynamic.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ int main (int argc, char *argv[])
3838
GoatScale *scale_x = GOAT_SCALE (goat_scale_linear_new (GOAT_POSITION_BOTTOM, GOAT_ORIENTATION_HORIZONTAL));
3939
GoatScale *scale_y = GOAT_SCALE (goat_scale_linear_new (GOAT_POSITION_RIGHT, GOAT_ORIENTATION_VERTICAL));
4040

41+
goat_scale_linear_set_ticks (GOAT_SCALE_LINEAR (scale_y), 100, 2);
42+
4143
plot = goat_plot_new (scale_x, scale_y);
4244

4345
GoatDataset *dataset;

tests/screenshot.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ int main (int argc, char *argv[])
4949
box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
5050
scale_x = GOAT_SCALE (goat_scale_linear_new (GOAT_POSITION_TOP, GOAT_ORIENTATION_HORIZONTAL));
5151
goat_scale_set_range (scale_x, -44., +30.);
52-
goat_scale_linear_set_ticks (GOAT_SCALE_LINEAR (scale_x), 50, 5);
52+
goat_scale_linear_set_ticks (GOAT_SCALE_LINEAR (scale_x), 100, 5);
5353
scale_y = GOAT_SCALE (goat_scale_linear_new (GOAT_POSITION_LEFT, GOAT_ORIENTATION_VERTICAL));
5454
goat_scale_set_range_auto (scale_y);
5555
goat_scale_linear_set_ticks (GOAT_SCALE_LINEAR (scale_y), 50, 5);

0 commit comments

Comments
 (0)