diff mbox

[i-g-t,09/16] plot: Draw a grid in the background

Message ID 1436186144-19665-10-git-send-email-damien.lespiau@intel.com (mailing list archive)
State New, archived
Headers show

Commit Message

Lespiau, Damien July 6, 2015, 12:35 p.m. UTC
Signed-off-by: Damien Lespiau <damien.lespiau@intel.com>
---
 lib/igt_plot.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
diff mbox

Patch

diff --git a/lib/igt_plot.c b/lib/igt_plot.c
index 126f160..c8d6dcb 100644
--- a/lib/igt_plot.c
+++ b/lib/igt_plot.c
@@ -452,6 +452,37 @@  igt_plot_draw_text(igt_plot_t *plot, double x, double y, igt_label_t *label)
 	cairo_show_text(plot->cr, label->text);
 }
 
+static void igt_plot_draw_grid(igt_plot_t *plot, flush_t *flush)
+{
+	unsigned int i, n_ticks;
+	igt_box_t *area = &flush->plot_area;
+	double area_width, area_height;
+
+	area_width = area->x2 - area->x1;
+	area_height = area->y2 - area->y1;
+
+	cairo_set_source_rgb(plot->cr, 0.9, 0.9, 0.9);
+	cairo_set_line_width(plot->cr, 1.0);
+
+	n_ticks = plot->x_axis.n_ticks;
+	for (i = 0; i < n_ticks; i++) {
+		double x = area->x1 + i * area_width / (n_ticks - 1);
+
+		cairo_move_to(plot->cr, SNAP(x), area->y2);
+		cairo_line_to(plot->cr, SNAP(x), area->y1);
+		cairo_stroke(plot->cr);
+	}
+
+	n_ticks = plot->y_axis.n_ticks;
+	for (i = 0; i < n_ticks; i++) {
+		double y = area->y2 - i * area_height / (n_ticks - 1);
+
+		cairo_move_to(plot->cr, area->x1, SNAP(y));
+		cairo_line_to(plot->cr, area->x2, SNAP(y));
+		cairo_stroke(plot->cr);
+	}
+}
+
 static double fit(double p, double start, double range, double scale)
 {
 	return start + (range / 2 + p) * scale;
@@ -508,6 +539,7 @@  static void igt_plot_draw_ticks(igt_plot_t *plot, igt_plot_axis_t *axis,
 	area_height = area->y2 - area->y1;
 
 	cairo_set_font_size(plot->cr, flush->tick_label_font_size);
+	cairo_set_source_rgb(plot->cr, 0.0, 0.0, 0.0);
 	cairo_set_line_cap(plot->cr, CAIRO_LINE_CAP_SQUARE);
 	cairo_set_line_width(plot->cr, 1.0);
 
@@ -560,6 +592,8 @@  static void igt_plot_draw_axis(igt_plot_t *plot, flush_t *flush)
 	igt_box_t *area = &flush->plot_area;
 	const double tick_length = plot_length(plot, 0.01);
 
+	igt_plot_draw_grid(plot, flush);
+
 	/* X-axis */
 	cairo_move_to(plot->cr, area->x1, area->y2);
 	cairo_line_to(plot->cr, area->x2, area->y2);