mindok / contex

Charting and graphing library for Elixir
https://contex-charts.org/
MIT License
699 stars 54 forks source link

BarChart 0 is not 0 #91

Open Hermanverschooten opened 8 months ago

Hermanverschooten commented 8 months ago

I just made my first grouped BarChart and I immediately noticed an oddity. I have 2 series and I had 1 value in a serie that was below 0. This causes all the bars that are 0 to display a bar from the bottom up to 0.

297056045-3633c82f-8993-4183-aab1-1843a5061e78

In a regular barchart, 0 would always be the baseline and the negative would be shown below it.

mindok commented 7 months ago

Thanks @Hermanverschooten - I'll take a look. I think the way the x-axis is drawn may require a bit of code shuffling to solve this.

manuel-rubio commented 7 months ago

I experience the same problem, while I was expecting the graph to do this: image It was doing this: image

manuel-rubio commented 7 months ago

As far as I can see... in addition to 0 being 0, the negative numbers are linked to a minimum as you can see in my previous message, I think it's starting here:

https://github.com/mindok/contex/blob/master/lib/chart/barchart.ex#L507-L513

It could be changed for this code:

    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)
        cond do
          data_val != 0 ->
            [{scale_zero, range_val} | points]

          :else ->
            [{scale_zero, scale_zero} | points]
        end
      end)

And it worked for me: image