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

add plot Y axis label type (float, integer) #66

Merged
merged 1 commit into from
Sep 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 53 additions & 23 deletions plot.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ const (
PlotMarkerDot
)

// PlotYAxisLabelDataType represents plot y axis type (integer or float).
type PlotYAxisLabelDataType uint

const (
PlotYAxisLabelDataInt PlotYAxisLabelDataType = iota
PlotYAxisLabelDataFloat
)

// PlotType represents plot type (line chart or scatter).
type PlotType uint

Expand All @@ -42,33 +50,35 @@ type brailleCell struct {
// Plot represents a plot primitive used for different charts.
type Plot struct {
*tview.Box
data [][]float64
maxVal float64
marker Marker
ptype PlotType
dotMarkerRune rune
lineColors []tcell.Color
axesColor tcell.Color
axesLabelColor tcell.Color
drawAxes bool
drawXAxisLabel bool
drawYAxisLabel bool
brailleCellMap map[image.Point]brailleCell
mu sync.Mutex
data [][]float64
maxVal float64
marker Marker
ptype PlotType
dotMarkerRune rune
lineColors []tcell.Color
axesColor tcell.Color
axesLabelColor tcell.Color
drawAxes bool
drawXAxisLabel bool
drawYAxisLabel bool
yAxisLabelDataType PlotYAxisLabelDataType
brailleCellMap map[image.Point]brailleCell
mu sync.Mutex
}

// NewPlot returns a plot widget.
func NewPlot() *Plot {
return &Plot{
Box: tview.NewBox(),
marker: PlotMarkerDot,
ptype: PlotTypeLineChart,
dotMarkerRune: dotRune,
axesColor: tcell.ColorDimGray,
axesLabelColor: tcell.ColorDimGray,
drawAxes: true,
drawXAxisLabel: true,
drawYAxisLabel: true,
Box: tview.NewBox(),
marker: PlotMarkerDot,
ptype: PlotTypeLineChart,
dotMarkerRune: dotRune,
axesColor: tcell.ColorDimGray,
axesLabelColor: tcell.ColorDimGray,
drawAxes: true,
drawXAxisLabel: true,
drawYAxisLabel: true,
yAxisLabelDataType: PlotYAxisLabelDataFloat,
lineColors: []tcell.Color{
tcell.ColorSteelBlue,
},
Expand Down Expand Up @@ -99,6 +109,11 @@ func (plot *Plot) SetLineColor(color []tcell.Color) {
plot.lineColors = color
}

// SetYAxisLabelDataType sets Y axis label data type (integer or float).
func (plot *Plot) SetYAxisLabelDataType(dataType PlotYAxisLabelDataType) {
plot.yAxisLabelDataType = dataType
}

// SetAxesColor sets axes x and y lines color.
func (plot *Plot) SetAxesColor(color tcell.Color) {
plot.axesColor = color
Expand Down Expand Up @@ -238,9 +253,24 @@ func (plot *Plot) drawXAxisLabelToScreen(

func (plot *Plot) drawYAxisLabelToScreen(screen tcell.Screen, plotYAxisLabelsWidth int, x int, y int, height int) {
verticalScale := plot.maxVal / float64(height-plotXAxisLabelsHeight-1)
previousLabel := ""

for i := 0; i*(plotYAxisLabelsGap+1) < height-1; i++ {
label := fmt.Sprintf("%.2f", float64(i)*verticalScale*(plotYAxisLabelsGap+1))
var label string
if plot.yAxisLabelDataType == PlotYAxisLabelDataFloat {
label = fmt.Sprintf("%.2f", float64(i)*verticalScale*(plotYAxisLabelsGap+1))
} else {
label = strconv.Itoa(int(float64(i) * verticalScale * (plotYAxisLabelsGap + 1)))
}

// Prevent same label being shown twice.
// Mainly relevant for integer labels with small data sets (in value)
if label == previousLabel {
continue
}

previousLabel = label

tview.Print(screen,
label,
x,
Expand Down
Loading