add plot Y axis label type (float, integer)

Signed-off-by: Navid Yaghoobi <navidys@fedoraproject.org>
This commit is contained in:
Navid Yaghoobi 2024-09-28 12:33:20 +10:00
parent ef7a0912d9
commit 1dfc3feec1

76
plot.go
View File

@ -19,6 +19,14 @@ const (
PlotMarkerDot 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). // PlotType represents plot type (line chart or scatter).
type PlotType uint type PlotType uint
@ -42,33 +50,35 @@ type brailleCell struct {
// Plot represents a plot primitive used for different charts. // Plot represents a plot primitive used for different charts.
type Plot struct { type Plot struct {
*tview.Box *tview.Box
data [][]float64 data [][]float64
maxVal float64 maxVal float64
marker Marker marker Marker
ptype PlotType ptype PlotType
dotMarkerRune rune dotMarkerRune rune
lineColors []tcell.Color lineColors []tcell.Color
axesColor tcell.Color axesColor tcell.Color
axesLabelColor tcell.Color axesLabelColor tcell.Color
drawAxes bool drawAxes bool
drawXAxisLabel bool drawXAxisLabel bool
drawYAxisLabel bool drawYAxisLabel bool
brailleCellMap map[image.Point]brailleCell yAxisLabelDataType PlotYAxisLabelDataType
mu sync.Mutex brailleCellMap map[image.Point]brailleCell
mu sync.Mutex
} }
// NewPlot returns a plot widget. // NewPlot returns a plot widget.
func NewPlot() *Plot { func NewPlot() *Plot {
return &Plot{ return &Plot{
Box: tview.NewBox(), Box: tview.NewBox(),
marker: PlotMarkerDot, marker: PlotMarkerDot,
ptype: PlotTypeLineChart, ptype: PlotTypeLineChart,
dotMarkerRune: dotRune, dotMarkerRune: dotRune,
axesColor: tcell.ColorDimGray, axesColor: tcell.ColorDimGray,
axesLabelColor: tcell.ColorDimGray, axesLabelColor: tcell.ColorDimGray,
drawAxes: true, drawAxes: true,
drawXAxisLabel: true, drawXAxisLabel: true,
drawYAxisLabel: true, drawYAxisLabel: true,
yAxisLabelDataType: PlotYAxisLabelDataFloat,
lineColors: []tcell.Color{ lineColors: []tcell.Color{
tcell.ColorSteelBlue, tcell.ColorSteelBlue,
}, },
@ -99,6 +109,11 @@ func (plot *Plot) SetLineColor(color []tcell.Color) {
plot.lineColors = 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. // SetAxesColor sets axes x and y lines color.
func (plot *Plot) SetAxesColor(color tcell.Color) { func (plot *Plot) SetAxesColor(color tcell.Color) {
plot.axesColor = color plot.axesColor = color
@ -238,9 +253,24 @@ func (plot *Plot) drawXAxisLabelToScreen(
func (plot *Plot) drawYAxisLabelToScreen(screen tcell.Screen, plotYAxisLabelsWidth int, x int, y int, height int) { func (plot *Plot) drawYAxisLabelToScreen(screen tcell.Screen, plotYAxisLabelsWidth int, x int, y int, height int) {
verticalScale := plot.maxVal / float64(height-plotXAxisLabelsHeight-1) verticalScale := plot.maxVal / float64(height-plotXAxisLabelsHeight-1)
previousLabel := ""
for i := 0; i*(plotYAxisLabelsGap+1) < height-1; i++ { 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, tview.Print(screen,
label, label,
x, x,