Ruby algorithm for generating EChart Waterfall data

I’m trying to get this chart working.

EChart Sandbox: Waterfall Chart

And this the ruby code to elaborate my data.

    data = [ 500, 100, -1000, 300, 500]

    positive_values = []
    negative_values = []
    
    data.each_with_index do |value, index|
  
      if value.negative?
        positive_values << "-"
        negative_values << value.abs
      else
        positive_values << value
        negative_values << "-"
      end
  
    end

    base_values = []
  
    data.each_with_index do |value, index|

      base_values << 0 && next if index == 0

      base = base_values[index-1]

      base -= value.abs.to_f if value.negative?
      base += data[index-1].to_f if data[index-1].positive?

      base_values << base.round(2)

    end
  
    return { base_values: base_values, positive_values: positive_values, negative_values: negative_values }

Now this code works perfectly fine while you have positive values.

But when you start to have, as the code above, negative numbers that are going below 0, the algorithm doesn’t work, and I’m not able to understand the way to fix it.

The expected graph that i’m trying to get with the data mentioned above is the following.

Expected Graph – EChart