From 77f57e1f8fbef40120cde8b78359c1e5600ba0cf Mon Sep 17 00:00:00 2001 From: Manuel Rubio Date: Thu, 25 Jan 2024 19:26:45 +0100 Subject: [PATCH 1/3] fix zero and negative value for grouped barcharts --- lib/chart/barchart.ex | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/chart/barchart.ex b/lib/chart/barchart.ex index a9f2209..6d8ef6b 100644 --- a/lib/chart/barchart.ex +++ b/lib/chart/barchart.ex @@ -504,12 +504,18 @@ defmodule Contex.BarChart do end defp prepare_bar_values(series_values, scale, :grouped) do - {scale_min, _} = Scale.get_range(scale) + scale_zero = Scale.domain_to_range(scale, 0.0) results = Enum.reduce(series_values, [], fn data_val, points -> range_val = Scale.domain_to_range(scale, data_val) - [{scale_min, range_val} | points] + cond do + data_val != 0 -> + [{scale_zero, range_val} | points] + + :else -> + [{scale_zero, scale_zero} | points] + end end) Enum.reverse(results) From 7a35a3de423b23fd8fee57cea2f18c8660f54b90 Mon Sep 17 00:00:00 2001 From: Manuel Rubio Date: Sun, 4 Feb 2024 22:01:04 +0100 Subject: [PATCH 2/3] remove unclear code and keep it simpler for setting zero and negative values for grouped barcharts --- lib/chart/barchart.ex | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/lib/chart/barchart.ex b/lib/chart/barchart.ex index 6d8ef6b..fe64b77 100644 --- a/lib/chart/barchart.ex +++ b/lib/chart/barchart.ex @@ -509,13 +509,7 @@ defmodule Contex.BarChart do results = Enum.reduce(series_values, [], fn data_val, points -> range_val = Scale.domain_to_range(scale, data_val) - cond do - data_val != 0 -> - [{scale_zero, range_val} | points] - - :else -> - [{scale_zero, scale_zero} | points] - end + [{scale_zero, range_val} | points] end) Enum.reverse(results) From 3563ba5224b2c1d1d238468ab0ac017a60b6c6a0 Mon Sep 17 00:00:00 2001 From: Manuel Rubio Date: Tue, 6 Feb 2024 09:02:31 +0100 Subject: [PATCH 3/3] zero axis (as @mindok suggested) --- lib/chart/axis.ex | 11 +++++++++++ lib/chart/barchart.ex | 3 +++ 2 files changed, 14 insertions(+) diff --git a/lib/chart/axis.ex b/lib/chart/axis.ex index 615fff7..0883e53 100644 --- a/lib/chart/axis.ex +++ b/lib/chart/axis.ex @@ -131,6 +131,17 @@ defmodule Contex.Axis do ] end + @doc """ + Draw the zero line for ensuring it's drawn even when zero + isn't aligned to the base of the graph. + """ + def get_zero_line_svg(%Axis{scale: scale} = axis) do + # Check that scale intersects zero by looking at domain + # If so... + domain_to_range_fn = Scale.domain_to_range_fn(scale) + get_svg_gridline(axis, domain_to_range_fn.(0.0)) + end + defp get_svg_gridlines(%Axis{scale: scale} = axis) do domain_ticks = Scale.ticks_domain(scale) domain_to_range_fn = Scale.domain_to_range_fn(scale) diff --git a/lib/chart/barchart.ex b/lib/chart/barchart.ex index fe64b77..16668c5 100644 --- a/lib/chart/barchart.ex +++ b/lib/chart/barchart.ex @@ -357,9 +357,12 @@ defmodule Contex.BarChart do val_axis_svg = if plot_options.show_val_axis, do: Axis.to_svg(value_axis), else: "" + zero_line = Axis.get_zero_line_svg(category_axis) + [ cat_axis_svg, val_axis_svg, + zero_line, "", get_svg_bars(plot), ""