Skip to content

Commit

Permalink
Merge pull request #66 from navidys/plot_y_axis_type
Browse files Browse the repository at this point in the history
add plot Y axis label type (float, integer)
  • Loading branch information
navidys authored Sep 28, 2024
2 parents ef7a091 + 1dfc3fe commit ddfd015
Showing 1 changed file with 53 additions and 23 deletions.
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

0 comments on commit ddfd015

Please sign in to comment.