diff --git a/src/Axis.vala b/src/Axis.vala index d8fcb04..1501d34 100644 --- a/src/Axis.vala +++ b/src/Axis.vala @@ -1,22 +1,9 @@ namespace CairoChart { - // If one of axis:title or axis:min/max are different + // If one of axis:title or axis:range.min/range.max are different // then draw separate axis for each/all series // or specify series name near the axis public class Axis { - Float128 _min = 0; - Float128 _max = 0; - public Float128 min { - get { return _min; } - set { _min = zoom_min = value; } - default = 0; - } - public Float128 max { - get { return _max; } - set { _max = zoom_max = value; } - default = 1; - } - public Float128 zoom_min = 0; - public Float128 zoom_max = 1; + public Range range = new Range(); public Text title = new Text (""); public enum Type { NUMBERS = 0, @@ -87,8 +74,8 @@ namespace CairoChart { axis.font_spacing = this.font_spacing; axis.font_style = this.font_style; axis.line_style = this.line_style; - axis.max = this.max; - axis.min = this.min; + axis.range.max = this.range.max; + axis.range.min = this.range.min; axis.position = this.position; axis.scale_type = this.scale_type; axis.title = this.title.copy(); @@ -113,7 +100,7 @@ namespace CairoChart { public virtual void calc_rec_sizes (Chart chart, out double max_rec_width, out double max_rec_height, bool horizontal = true) { max_rec_width = max_rec_height = 0; for (var i = 0; i < nrecords; ++i) { - Float128 x = (int64)(zoom_min + (zoom_max - zoom_min) / nrecords * i) + 1.0/3.0; + Float128 x = (int64)(range.zmin + (range.zmax - range.zmin) / nrecords * i) + 1.0/3.0; switch (type) { case Axis.Type.NUMBERS: var text = new Text (format.printf((LongDouble)x) + (horizontal ? "_" : ""), font_style); @@ -145,8 +132,7 @@ namespace CairoChart { } public virtual void unzoom () { - zoom_min = min; - zoom_max = max; + range.unzoom(); } } } diff --git a/src/Chart.vala b/src/Chart.vala index bcb8cde..a892d89 100644 --- a/src/Chart.vala +++ b/src/Chart.vala @@ -196,34 +196,34 @@ namespace CairoChart { var real_y1 = s.get_real_y (area.y1); var real_height = real_y0 - real_y1; // if selected square does not intersect with the series's square - if ( real_x1 <= s.axis_x.zoom_min || real_x0 >= s.axis_x.zoom_max - || real_y0 <= s.axis_y.zoom_min || real_y1 >= s.axis_y.zoom_max) { + if ( real_x1 <= s.axis_x.range.zmin || real_x0 >= s.axis_x.range.zmax + || real_y0 <= s.axis_y.range.zmin || real_y1 >= s.axis_y.range.zmax) { s.zoom_show = false; continue; } - if (real_x0 >= s.axis_x.zoom_min) { - s.axis_x.zoom_min = real_x0; + if (real_x0 >= s.axis_x.range.zmin) { + s.axis_x.range.zmin = real_x0; s.place.zx0 = 0.0; } else { - s.place.zx0 = (s.axis_x.zoom_min - real_x0) / real_width; + s.place.zx0 = (s.axis_x.range.zmin - real_x0) / real_width; } - if (real_x1 <= s.axis_x.zoom_max) { - s.axis_x.zoom_max = real_x1; + if (real_x1 <= s.axis_x.range.zmax) { + s.axis_x.range.zmax = real_x1; s.place.zx1 = 1.0; } else { - s.place.zx1 = (s.axis_x.zoom_max - real_x0) / real_width; + s.place.zx1 = (s.axis_x.range.zmax - real_x0) / real_width; } - if (real_y1 >= s.axis_y.zoom_min) { - s.axis_y.zoom_min = real_y1; + if (real_y1 >= s.axis_y.range.zmin) { + s.axis_y.range.zmin = real_y1; s.place.zy0 = 0.0; } else { - s.place.zy0 = (s.axis_y.zoom_min - real_y1) / real_height; + s.place.zy0 = (s.axis_y.range.zmin - real_y1) / real_height; } - if (real_y0 <= s.axis_y.zoom_max) { - s.axis_y.zoom_max = real_y0; + if (real_y0 <= s.axis_y.range.zmax) { + s.axis_y.range.zmax = real_y0; s.place.zy1 = 1.0; } else { - s.place.zy1 = (s.axis_y.zoom_max - real_y1) / real_height; + s.place.zy1 = (s.axis_y.range.zmax - real_y1) / real_height; } } diff --git a/src/Range.vala b/src/Range.vala index 77e38a4..ad923f1 100644 --- a/src/Range.vala +++ b/src/Range.vala @@ -5,64 +5,64 @@ namespace CairoChart { */ public class Range { - double _low = 0; - double _high = 1; + Float128 _min = 0; + Float128 _max = 1; /** - * Zoomed low bound. + * Zoomed min bound. */ - double zlow = 0; + public Float128 zmin = 0; /** - * Zoomed high bound. + * Zoomed max bound. */ - double zhigh = 1; + public Float128 zmax = 1; /** * Low bound. */ - public double low { + public Float128 min { get { - return _low; + return _min; } set { - zlow = _low = value; + zmin = _min = value; } } /** * High bound. */ - public double high { + public Float128 max { get { - return _high; + return _max; } set { - zhigh = _high = value; + zmax = _max = value; } } /** * ``Range`` value. */ - public double range { + public Float128 range { get { - return _high - _low; + return _max - _min; } set { - zhigh = _high = _low + value; + zmax = _max = _min + value; } } /** * ``Range`` zoomed value. */ - public double zrange { + public Float128 zrange { get { - return zhigh - zlow; + return zmax - zmin; } set { - zhigh = zlow + value; + zmax = zmin + value; } } @@ -75,23 +75,23 @@ namespace CairoChart { * Constructs a new ``Range`` with a ``Range`` instance. */ public Range.with_range (Range range) { - this.low = range.low; - this.high = range.high; + this.min = range.min; + this.max = range.max; } /** * Constructs a new ``Range`` with absolute coordinates. */ - public Range.with_abs (double low, double high) { - this.low = low; - this.high = high; + public Range.with_abs (Float128 min, Float128 max) { + this.min = min; + this.max = max; } /** * Constructs a new ``Range`` with relative coordinates. */ - public Range.with_rel (double low, double range) { - this.low = low; + public Range.with_rel (Float128 min, Float128 range) { + this.min = min; this.range = range; } @@ -106,8 +106,8 @@ namespace CairoChart { * Unzooms ``Range``. */ public void unzoom () { - zlow = low; - zhigh = high; + zmin = min; + zmax = max; } } } diff --git a/src/Series.vala b/src/Series.vala index 7e8921f..255166f 100644 --- a/src/Series.vala +++ b/src/Series.vala @@ -92,8 +92,8 @@ namespace CairoChart { public virtual bool equal_x_axis (Series s) { if ( axis_x.position != s.axis_x.position - || axis_x.zoom_min != s.axis_x.zoom_min - || axis_x.zoom_max != s.axis_x.zoom_max + || axis_x.range.zmin != s.axis_x.range.zmin + || axis_x.range.zmax != s.axis_x.range.zmax || place.zx0 != s.place.zx0 || place.zx1 != s.place.zx1 || axis_x.type != s.axis_x.type @@ -104,8 +104,8 @@ namespace CairoChart { public virtual bool equal_y_axis (Series s) { if ( axis_y.position != s.axis_y.position - || axis_y.zoom_min != s.axis_y.zoom_min - || axis_y.zoom_max != s.axis_y.zoom_max + || axis_y.range.zmin != s.axis_y.range.zmin + || axis_y.range.zmax != s.axis_y.range.zmax || place.zy0 != s.place.zy0 || place.zy1 != s.place.zy1 || axis_y.type != s.axis_y.type @@ -249,7 +249,7 @@ namespace CairoChart { var ctx = chart.ctx; var joint_x = chart.joint_x; - for (Float128 x = x_min, x_max = axis_x.zoom_max; Math.point_belong (x, x_min, x_max); x += step) { + for (Float128 x = x_min, x_max = axis_x.range.zmax; Math.point_belong (x, x_min, x_max); x += step) { if (joint_x) chart.color = chart.joint_color; else chart.color = axis_x.color; string text = "", time_text = ""; @@ -334,21 +334,21 @@ namespace CairoChart { long max_nrecs = (long) (chart.plarea.width * (s.place.zx1 - s.place.zx0) / max_rec_width); // 3. Calculate grid step. - Float128 step = Math.calc_round_step ((s.axis_x.zoom_max - s.axis_x.zoom_min) / max_nrecs, s.axis_x.type == Axis.Type.DATE_TIME); - if (step > s.axis_x.zoom_max - s.axis_x.zoom_min) - step = s.axis_x.zoom_max - s.axis_x.zoom_min; + Float128 step = Math.calc_round_step ((s.axis_x.range.zmax - s.axis_x.range.zmin) / max_nrecs, s.axis_x.type == Axis.Type.DATE_TIME); + if (step > s.axis_x.range.zmax - s.axis_x.range.zmin) + step = s.axis_x.range.zmax - s.axis_x.range.zmin; - // 4. Calculate x_min (s.axis_x.zoom_min / step, round, multiply on step, add step if < s.axis_x.zoom_min). + // 4. Calculate x_min (s.axis_x.range.zmin / step, round, multiply on step, add step if < s.axis_x.range.zmin). Float128 x_min = 0.0; if (step >= 1) { - int64 x_min_nsteps = (int64) (s.axis_x.zoom_min / step); + int64 x_min_nsteps = (int64) (s.axis_x.range.zmin / step); x_min = x_min_nsteps * step; } else { - int64 round_axis_x_min = (int64)s.axis_x.zoom_min; - int64 x_min_nsteps = (int64) ((s.axis_x.zoom_min - round_axis_x_min) / step); + int64 round_axis_x_min = (int64)s.axis_x.range.zmin; + int64 x_min_nsteps = (int64) ((s.axis_x.range.zmin - round_axis_x_min) / step); x_min = round_axis_x_min + x_min_nsteps * step; } - if (x_min < s.axis_x.zoom_min) x_min += step; + if (x_min < s.axis_x.range.zmin) x_min += step; // 4.2. Cursor values for joint X axis if (chart.joint_x && chart.cursors.cursor_style.orientation == Cursors.Orientation.VERTICAL && chart.cursors.cursors_crossings.length != 0) { @@ -396,7 +396,7 @@ namespace CairoChart { var ctx = chart.ctx; var joint_y = chart.joint_y; - for (Float128 y = y_min, y_max = axis_y.zoom_max; Math.point_belong (y, y_min, y_max); y += step) { + for (Float128 y = y_min, y_max = axis_y.range.zmax; Math.point_belong (y, y_min, y_max); y += step) { if (joint_y) chart.color = chart.joint_color; else chart.color = axis_y.color; var text = axis_y.format.printf((LongDouble)y); @@ -454,21 +454,21 @@ namespace CairoChart { long max_nrecs = (long) (chart.plarea.height * (s.place.zy1 - s.place.zy0) / max_rec_height); // 3. Calculate grid step. - Float128 step = Math.calc_round_step ((s.axis_y.zoom_max - s.axis_y.zoom_min) / max_nrecs); - if (step > s.axis_y.zoom_max - s.axis_y.zoom_min) - step = s.axis_y.zoom_max - s.axis_y.zoom_min; + Float128 step = Math.calc_round_step ((s.axis_y.range.zmax - s.axis_y.range.zmin) / max_nrecs); + if (step > s.axis_y.range.zmax - s.axis_y.range.zmin) + step = s.axis_y.range.zmax - s.axis_y.range.zmin; - // 4. Calculate y_min (s.axis_y.zoom_min / step, round, multiply on step, add step if < s.axis_y.zoom_min). + // 4. Calculate y_min (s.axis_y.range.zmin / step, round, multiply on step, add step if < s.axis_y.range.zmin). Float128 y_min = 0.0; if (step >= 1) { - int64 y_min_nsteps = (int64) (s.axis_y.zoom_min / step); + int64 y_min_nsteps = (int64) (s.axis_y.range.zmin / step); y_min = y_min_nsteps * step; } else { - int64 round_axis_y_min = (int64)s.axis_y.zoom_min; - int64 y_min_nsteps = (int64) ((s.axis_y.zoom_min - round_axis_y_min) / step); + int64 round_axis_y_min = (int64)s.axis_y.range.zmin; + int64 y_min_nsteps = (int64) ((s.axis_y.range.zmin - round_axis_y_min) / step); y_min = round_axis_y_min + y_min_nsteps * step; } - if (y_min < s.axis_y.zoom_min) y_min += step; + if (y_min < s.axis_y.range.zmin) y_min += step; // 4.2. Cursor values for joint Y axis if (chart.joint_y && chart.cursors.cursor_style.orientation == Cursors.Orientation.HORIZONTAL && chart.cursors.cursors_crossings.length != 0) { @@ -518,21 +518,21 @@ namespace CairoChart { public virtual double compact_rec_x_pos (Float128 x, Text text) { var sz = text.get_size(chart.ctx); return get_scr_x(x) - sz.width / 2.0 - - sz.width * (x - (axis_x.zoom_min + axis_x.zoom_max) / 2.0) / (axis_x.zoom_max - axis_x.zoom_min); + - sz.width * (x - (axis_x.range.zmin + axis_x.range.zmax) / 2.0) / (axis_x.range.zmax - axis_x.range.zmin); } public virtual double compact_rec_y_pos (Float128 y, Text text) { var sz = text.get_size(chart.ctx); return get_scr_y(y) + sz.height / 2.0 - + sz.height * (y - (axis_y.zoom_min + axis_y.zoom_max) / 2.0) / (axis_y.zoom_max - axis_y.zoom_min); + + sz.height * (y - (axis_y.range.zmin + axis_y.range.zmax) / 2.0) / (axis_y.range.zmax - axis_y.range.zmin); } public virtual double get_scr_x (Float128 x) { - return chart.plarea.x0 + chart.plarea.width * (place.zx0 + (x - axis_x.zoom_min) / (axis_x.zoom_max - axis_x.zoom_min) * (place.zx1 - place.zx0)); + return chart.plarea.x0 + chart.plarea.width * (place.zx0 + (x - axis_x.range.zmin) / (axis_x.range.zmax - axis_x.range.zmin) * (place.zx1 - place.zx0)); } public virtual double get_scr_y (Float128 y) { - return chart.plarea.y0 + chart.plarea.height * (1.0 - (place.zy0 + (y - axis_y.zoom_min) / (axis_y.zoom_max - axis_y.zoom_min) * (place.zy1 - place.zy0))); + return chart.plarea.y0 + chart.plarea.height * (1.0 - (place.zy0 + (y - axis_y.range.zmin) / (axis_y.range.zmax - axis_y.range.zmin) * (place.zy1 - place.zy0))); } public virtual Point get_scr_point (Point128 p) { @@ -540,13 +540,13 @@ namespace CairoChart { } public virtual Float128 get_real_x (double scr_x) { - return axis_x.zoom_min + ((scr_x - chart.plarea.x0) / chart.plarea.width - place.zx0) - * (axis_x.zoom_max - axis_x.zoom_min) / (place.zx1 - place.zx0); + return axis_x.range.zmin + ((scr_x - chart.plarea.x0) / chart.plarea.width - place.zx0) + * (axis_x.range.zmax - axis_x.range.zmin) / (place.zx1 - place.zx0); } public virtual Float128 get_real_y (double scr_y) { - return axis_y.zoom_min + ((chart.plarea.y1 - scr_y) / chart.plarea.height - place.zy0) - * (axis_y.zoom_max - axis_y.zoom_min) / (place.zy1 - place.zy0); + return axis_y.range.zmin + ((chart.plarea.y1 - scr_y) / chart.plarea.height - place.zy0) + * (axis_y.range.zmax - axis_y.range.zmin) / (place.zy1 - place.zy0); } public virtual Point128 get_real_point (Point p) { diff --git a/test/ChartTest.vala b/test/ChartTest.vala index 9c4eef8..25438c3 100644 --- a/test/ChartTest.vala +++ b/test/ChartTest.vala @@ -15,18 +15,18 @@ void plot_chart1 (Chart chart) { s3.points = {Point128(9, 17), Point128(2, 10), Point128(122, 31)}; s3.axis_y.position = Axis.Position.HIGH; - s1.axis_x.min = 0; s1.axis_x.max = 2; - s1.axis_y.min = 0; s1.axis_y.max = 3; + s1.axis_x.range.min = 0; s1.axis_x.range.max = 2; + s1.axis_y.range.min = 0; s1.axis_y.range.max = 3; s1.place.x0 = 0.25; s1.place.x1 = 0.75; s1.place.y0 = 0.3; s1.place.y1 = 0.9; - s2.axis_x.min = -15; s2.axis_x.max = 30; - s2.axis_y.min = -20; s2.axis_y.max = 200; + s2.axis_x.range.min = -15; s2.axis_x.range.max = 30; + s2.axis_y.range.min = -20; s2.axis_y.range.max = 200; s2.place.x0 = 0.5; s2.place.x1 = 1; s2.place.y0 = 0.0; s2.place.y1 = 0.5; - s3.axis_x.min = 0; s3.axis_x.max = 130; - s3.axis_y.min = 15; s3.axis_y.max = 35; + s3.axis_x.range.min = 0; s3.axis_x.range.max = 130; + s3.axis_y.range.min = 15; s3.axis_y.range.max = 35; s3.place.x0 = 0; s3.place.x1 = 0.5; s3.place.y0 = 0.5; s3.place.y1 = 1.0; @@ -59,18 +59,18 @@ void plot_chart2 (Chart chart) { s3.points = {Point128(9, 17), Point128(2, 10), Point128(-15, 31)}; s3.axis_y.position = Axis.Position.HIGH; - s1.axis_x.min = -15; s1.axis_x.max = 30; - s1.axis_y.min = 0; s1.axis_y.max = 3; + s1.axis_x.range.min = -15; s1.axis_x.range.max = 30; + s1.axis_y.range.min = 0; s1.axis_y.range.max = 3; s1.place.x0 = 0.0; s1.place.x1 = 1.0; s1.place.y0 = 0.3; s1.place.y1 = 0.9; - s2.axis_x.min = -15; s2.axis_x.max = 30; - s2.axis_y.min = -20; s2.axis_y.max = 200; + s2.axis_x.range.min = -15; s2.axis_x.range.max = 30; + s2.axis_y.range.min = -20; s2.axis_y.range.max = 200; s2.place.x0 = 0.0; s2.place.x1 = 1.0; s2.place.y0 = 0.0; s2.place.y1 = 0.5; - s3.axis_x.min = -15; s3.axis_x.max = 30; - s3.axis_y.min = 15; s3.axis_y.max = 35; + s3.axis_x.range.min = -15; s3.axis_x.range.max = 30; + s3.axis_y.range.min = 15; s3.axis_y.range.max = 35; s3.place.x0 = 0.0; s3.place.x1 = 1.0; s3.place.y0 = 0.5; s3.place.y1 = 1.0; @@ -87,7 +87,7 @@ void plot_chart2 (Chart chart) { //s1.axis_x.position = s2.axis_x.position = s3.axis_x.position = Axis.Position.HIGH; //s1.axis_x.type = s2.axis_x.type = s3.axis_x.type = Axis.Type.DATE_TIME; - //s1.axis_x.max = s2.axis_x.max = s3.axis_x.max = 5*24*3600; + //s1.axis_x.range.max = s2.axis_x.range.max = s3.axis_x.range.max = 5*24*3600; chart.series = { s1, s2, s3 }; } @@ -109,18 +109,18 @@ void plot_chart3 (Chart chart) { s3.points = {Point128(9, -17), Point128(2, 10), Point128(122, 31)}; s3.axis_y.position = Axis.Position.HIGH; - s1.axis_x.min = 0; s1.axis_x.max = 2; - s1.axis_y.min = -20; s1.axis_y.max = 200; + s1.axis_x.range.min = 0; s1.axis_x.range.max = 2; + s1.axis_y.range.min = -20; s1.axis_y.range.max = 200; s1.place.x0 = 0.25; s1.place.x1 = 0.75; s1.place.y0 = 0.0; s1.place.y1 = 1.0; - s2.axis_x.min = -15; s2.axis_x.max = 30; - s2.axis_y.min = -20; s2.axis_y.max = 200; + s2.axis_x.range.min = -15; s2.axis_x.range.max = 30; + s2.axis_y.range.min = -20; s2.axis_y.range.max = 200; s2.place.x0 = 0.5; s2.place.x1 = 1; s2.place.y0 = 0.0; s2.place.y1 = 1.0; - s3.axis_x.min = 0; s3.axis_x.max = 130; - s3.axis_y.min = -20; s3.axis_y.max = 200; + s3.axis_x.range.min = 0; s3.axis_x.range.max = 130; + s3.axis_y.range.min = -20; s3.axis_y.range.max = 200; s3.place.x0 = 0; s3.place.x1 = 0.5; s3.place.y0 = 0.0; s3.place.y1 = 1.0; @@ -168,23 +168,23 @@ void plot_chart4 (Chart chart) { s4.points = {Point128(high + 0.005, -19.05), Point128(high + 0.0051, 28), Point128(high + 0.0052, 55), Point128(high + 0.0053, 44)}; s4.axis_y.position = Axis.Position.HIGH; - s1.axis_x.min = now - 100000; s1.axis_x.max = now + 100000; - s1.axis_y.min = -20; s1.axis_y.max = 200; + s1.axis_x.range.min = now - 100000; s1.axis_x.range.max = now + 100000; + s1.axis_y.range.min = -20; s1.axis_y.range.max = 200; s1.place.x0 = 0.25; s1.place.x1 = 0.75; s1.place.y0 = 0.0; s1.place.y1 = 1.0; - s2.axis_x.min = -15; s2.axis_x.max = 30; - s2.axis_y.min = -20; s2.axis_y.max = 200; + s2.axis_x.range.min = -15; s2.axis_x.range.max = 30; + s2.axis_y.range.min = -20; s2.axis_y.range.max = 200; s2.place.x0 = 0.2; s2.place.x1 = 1; s2.place.y0 = 0.0; s2.place.y1 = 1.0; - s3.axis_x.min = high - 2; s3.axis_x.max = high + 1; - s3.axis_y.min = -20; s3.axis_y.max = 200; + s3.axis_x.range.min = high - 2; s3.axis_x.range.max = high + 1; + s3.axis_y.range.min = -20; s3.axis_y.range.max = 200; s3.place.x0 = 0; s3.place.x1 = 0.8; s3.place.y0 = 0.0; s3.place.y1 = 1.0; - s4.axis_x.min = high + 0.0049; s4.axis_x.max = high + 0.0054; - s4.axis_y.min = -20; s4.axis_y.max = 200; + s4.axis_x.range.min = high + 0.0049; s4.axis_x.range.max = high + 0.0054; + s4.axis_y.range.min = -20; s4.axis_y.range.max = 200; s4.place.x0 = 0.2; s4.place.x1 = 1.0; s4.place.y0 = 0.0; s4.place.y1 = 1.0;