Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

242
Visualizações
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

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda