ascii_chart icon indicating copy to clipboard operation
ascii_chart copied to clipboard

The array `[147, 147, -15_334, -15_334]` breaks the graph.

Open KarlHeitmann opened this issue 2 years ago • 0 comments

I used this array: [147, 147, -15_334, -15_334] as input data for the class Line through AsciiChart#plot and the graph breaks:

image

If I pass a greater offset param, the problem persists:

image

Below is a failing test with the expected value to add to test/ascii_chart_test.rb

  def test_writes_array_with_positive_and_negative_numbers
    plot_array = [147, 147, -15_334, -15_334]
    expected_graph = <<~EXPECTED_OUTPUT
       147.00  ┤    
     -1260.36  ┼-╮  
     -2667.73  ┤ |  
     -4075.09  ┤ |  
     -5482.45  ┤ |  
     -6889.82  ┤ |  
     -8297.18  ┤ |  
     -9704.55  ┤ |  
    -11111.91  ┤ |  
    -12519.27  ┤ |  
    -13926.64  ┼ |  
    -15334.00  ┤ ╰- 
    EXPECTED_OUTPUT
    assert_equal(AsciiChart.plot(plot_array, { height: 10 }), expected_graph[...-1])
  end

I don't know how to solve this issue, I believe it can be solved by tweaking lib/ascii_chart/line.rb:34, but I don't know how this result matrix works. Maybe by using something like: label.rjust(x) you can justify the label at the y axis to add padding to the left side of the string, so the elements will get justified at the right side.

KarlHeitmann avatar Jul 08 '23 03:07 KarlHeitmann