diff --git a/src/axis.vala b/src/axis.vala index a895d60..d0668fa 100644 --- a/src/axis.vala +++ b/src/axis.vala @@ -109,16 +109,18 @@ namespace LiveChart { float upper = LiveChart.cap((float) bounds.upper); var divs = LiveChart.golden_divisors(upper); - float interval = upper / divs.get(0); - foreach(float div in divs) { - interval = upper / div; - if (div > 3f && div < 7f) { - break; + if (divs.size > 0) { + float interval = upper / divs.get(0); + foreach(float div in divs) { + interval = upper / div; + if (div > 3f && div < 7f) { + break; + } + } + var limit = bounds.upper == upper ? upper : bounds.upper + interval; + for (var value = 0f; value <= limit; value += interval) { + ticks.values.add(value); } - } - var limit = bounds.upper == upper ? upper : bounds.upper + interval; - for (var value = 0f; value <= limit; value += interval) { - ticks.values.add(value); } } diff --git a/src/utils.vala b/src/utils.vala index 02f250a..25eed01 100644 --- a/src/utils.vala +++ b/src/utils.vala @@ -31,6 +31,11 @@ namespace LiveChart { } public Gee.List golden_divisors(float value) { + //No golden divisors for 0 + if (value == 0) { + return new Gee.ArrayList(); + } + //Handle values below 1 float factor = value < 10 ? cap(100 / value) : 1f; float working_value = value * factor; diff --git a/tests/axis.vala b/tests/axis.vala index 4e42276..46bb3cd 100644 --- a/tests/axis.vala +++ b/tests/axis.vala @@ -238,4 +238,13 @@ private void register_axis() { assert(ticks.values.get(2) == 0.8f); assert(ticks.values.get(3) == 1.2f); }); + + Test.add_func("/LiveChart/YAxis#get_ticks_should_not_crash_when_max_value_is_zero", () => { + + var y_axis = new LiveChart.YAxis(); + y_axis.update_bounds(0); + + //when //then + y_axis.get_ticks(); + }); } \ No newline at end of file diff --git a/tests/chart.vala b/tests/chart.vala index cf30ead..c13f159 100644 --- a/tests/chart.vala +++ b/tests/chart.vala @@ -124,4 +124,14 @@ private void register_chart() { }); Gtk.main(); }); + + Test.add_func("/LiveChart/Chart/#should_not_crash_if_value_added_is_zero", () => { + //given + var chart = new LiveChart.Chart(); + var serie = new LiveChart.Serie("Test"); + chart.add_serie(serie); + + //when //then + chart.add_value(serie, 0); + }); } diff --git a/tests/utils.vala b/tests/utils.vala index 733b294..c9fa083 100644 --- a/tests/utils.vala +++ b/tests/utils.vala @@ -108,5 +108,13 @@ private void register_utils() { assert(divisors.get(3) == 5f); assert(divisors.get(4) == 2f); assert(divisors.get(5) == 1f); + }); + + Test.add_func("/LiveChart/Utils/golden_divisors#ShouldNotCrashWhenComputingGoldenDivisorForZero", () => { + //given + var value = 0f; + + //when //then + LiveChart.golden_divisors(value); }); } \ No newline at end of file