+static void
+_compute_axis_ranges (acre_t *acre)
+{
+ unsigned int d, i;
+ acre_data_t *data;
+ double x_adjust, y_adjust;
+ cairo_t *cr = acre->cr;
+
+ acre->x_axis.min = acre->data[0]->points[0].x;
+ acre->x_axis.max = acre->data[0]->points[0].x;
+ acre->y_axis.min = acre->data[0]->points[0].y;
+ acre->y_axis.min = acre->data[0]->points[0].y;
+
+ /* First, simply find the extrema of the data. */
+ for (d = 0; d < acre->num_data; d++) {
+ data = acre->data[d];
+ for (i = 0; i < data->num_points; i++) {
+ if (data->points[i].x < acre->x_axis.min)
+ acre->x_axis.min = data->points[i].x;
+ if (data->points[i].x > acre->x_axis.max)
+ acre->x_axis.max = data->points[i].x;
+
+ if (data->points[i].y < acre->y_axis.min)
+ acre->y_axis.min = data->points[i].y;
+ if (data->points[i].y > acre->y_axis.max)
+ acre->y_axis.max = data->points[i].y;
+ }
+ }
+
+ /* Next, we want to ensure that the data never collides with the
+ * ticks. So we expand each axis on its minimum side as needed. */
+ cairo_save (cr);
+ {
+ double x, y;
+
+ _set_transform_to_data_space (acre);
+
+ x = ACRE_TICK_MAJOR_SIZE + 2.0;
+ y = ACRE_TICK_MAJOR_SIZE + 2.0;
+ cairo_device_to_user_distance (cr, &x, &y);
+
+ acre->x_axis.min -= x;
+ acre->y_axis.min += y;
+ }
+ cairo_restore (cr);
+
+ /* Then, increase the axis ranges just enough so that the step
+ * sizes for the ticks will be integers.
+ */
+ _expand_range_for_width (&acre->x_axis.min,
+ &acre->x_axis.max,
+ acre->chart.width);
+
+ _expand_range_for_width (&acre->y_axis.min,
+ &acre->y_axis.max,
+ acre->chart.height);
+
+ /* Finally, we also translate the axis ranges slightly so that the
+ * ticks land on half-integer device-pixel positions.
+ */
+ cairo_save (cr);
+ {
+ _set_transform_to_data_space (acre);
+
+ x_adjust = 0.0;
+ y_adjust = 0.0;
+ cairo_user_to_device (cr, &x_adjust, &y_adjust);
+ x_adjust = (round (x_adjust + 0.5) - 0.5) - x_adjust;
+ y_adjust = (round (y_adjust + 0.5) - 0.5) - y_adjust;
+ cairo_device_to_user_distance (cr, &x_adjust, &y_adjust);
+
+ acre->x_axis.min -= x_adjust;
+ acre->x_axis.max -= x_adjust;
+
+ acre->y_axis.min -= y_adjust;
+ acre->y_axis.max -= y_adjust;
+ }
+ cairo_restore (cr);
+}
+