Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BarChart Widget Does Not Render Graphically When Data Contains Negative Values #336

Open
transcendr opened this issue Jan 25, 2025 · 0 comments

Comments

@transcendr
Copy link

The BarChart widget in termui appears to fail to render correctly when the Data slice provided to it contains negative float values. When negative values are present in the Data, the entire termui application either displays a black screen or reverts to text-only output, and the graphical UI is not rendered.

Steps to Reproduce:

  1. Create a termui application that includes a BarChart widget.
  2. Populate the BarChart widget's Data slice with float values that include negative numbers.
  3. Render the termui UI.

Code Example to Reproduce:

package main

import (
	"log"
	"time"

	ui "github.com/gizak/termui/v3"
	"github.com/gizak/termui/v3/widgets"
)

func main() {
	if err := ui.Init(); err != nil {
		log.Fatalf("failed to initialize termui: %v", err)
	}
	defer ui.Close()

	bc := widgets.NewBarChart()
	bc.Title = "Bar Chart with Negative Data"
	bc.SetRect(0, 0, 50, 10)
	bc.BorderStyle.Fg = ui.ColorWhite
	bc.BarColors = []ui.Color{ui.ColorGreen}
	bc.Data = []float64{-5, -8, -6, -9, -7} // Negative Data Values
	bc.Labels = []string{"A", "B", "C", "D", "E"}

	ui.Render(bc)

	uiEvents := ui.PollEvents()
	ticker := time.NewTicker(1 * time.Second).C
	for {
		select {
		case e := <-uiEvents:
			switch e.ID {
			case "q", "<C-c>":
				return
			}
		case <-ticker:
			// No data update needed for this example, just keep rendering
			ui.Render(bc)
		}
	}
}

Expected Behavior:

The BarChart widget should render graphically even when the Data slice contains negative values. Ideally, the chart would visually represent both positive and negative bars relative to a zero baseline. Even if negative value visualization is not fully supported, the application should still render the UI without crashing or reverting to text-only mode.

Observed Behavior:

When the Data slice of the BarChart contains negative values (as in the example code above), the termui application fails to render the graphical UI. The terminal window either becomes completely black or only displays text output (like log messages), and the BarChart widget is not visible.

If the Data is changed to contain only positive values (e.g., bc.Data = []float64{5, 8, 6, 9, 7}), the BarChart renders correctly and the graphical UI works as expected.

Similarly, if the BarChart widget is removed from the UI, and only other widgets like Paragraph or Gauge are rendered, the application works graphically even with the original weather data (including negative temperatures in the forecast data, although they are not used for the removed BarChart).

Environment:

  • macos latest
  • go 1.23

Additional Information:

This issue was discovered while developing a weather TUI application that displays forecast temperatures using a BarChart. When fetching weather data for locations with sub-zero temperatures (resulting in negative values in the forecast data), the graphical UI failed to load. Through debugging, it was isolated to the presence of negative values in the BarChart's Data.

Suggestion:

  • Investigate the rendering logic of the BarChart widget, specifically how it handles negative data values.
  • Ideally, implement proper visualization of negative values in the BarChart.
  • As a minimum, ensure that negative values in BarChart.Data do not cause the entire termui application to fail to render graphically.

Please let me know if you need any further information or clarification to investigate this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant