feat option to draw y or x axis label independently (#25)

Signed-off-by: Hidayat Hamir <hidayat.03@erajaya.com>
This commit is contained in:
Hidayat 2023-12-29 14:51:02 +07:00 committed by GitHub
parent 82465e0608
commit d8a9aeb20e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 2 deletions

View File

@ -35,6 +35,7 @@ func main() {
})
bmLineChart.SetMarker(tvxwidgets.PlotMarkerBraille)
bmLineChart.SetData(sinData)
bmLineChart.SetDrawXAxisLabel(false)
dmLineChart := tvxwidgets.NewPlot()
dmLineChart.SetBorder(true)
@ -72,6 +73,7 @@ func main() {
dmScatterPlot.SetPlotType(tvxwidgets.PlotTypeScatter)
dmScatterPlot.SetMarker(tvxwidgets.PlotMarkerDot)
dmScatterPlot.SetData(scatterPlotData)
dmScatterPlot.SetDrawYAxisLabel(false)
bmScatterPlot := tvxwidgets.NewPlot()
bmScatterPlot.SetBorder(true)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 35 KiB

After

Width:  |  Height:  |  Size: 46 KiB

30
plot.go
View File

@ -50,6 +50,8 @@ type Plot struct {
axesColor tcell.Color
axesLabelColor tcell.Color
drawAxes bool
drawXAxisLabel bool
drawYAxisLabel bool
brailleCellMap map[image.Point]brailleCell
mu sync.Mutex
}
@ -64,6 +66,8 @@ func NewPlot() *Plot {
axesColor: tcell.ColorDimGray,
axesLabelColor: tcell.ColorDimGray,
drawAxes: true,
drawXAxisLabel: true,
drawYAxisLabel: true,
lineColors: []tcell.Color{
tcell.ColorSteelBlue,
},
@ -109,6 +113,16 @@ func (plot *Plot) SetDrawAxes(draw bool) {
plot.drawAxes = draw
}
// SetDrawXAxisLabel set true in order to draw x axis label to screen.
func (plot *Plot) SetDrawXAxisLabel(draw bool) {
plot.drawXAxisLabel = draw
}
// SetDrawYAxisLabel set true in order to draw y axis label to screen.
func (plot *Plot) SetDrawYAxisLabel(draw bool) {
plot.drawYAxisLabel = draw
}
// SetMarker sets marker type braille or dot mode.
func (plot *Plot) SetMarker(marker Marker) {
plot.marker = marker
@ -192,7 +206,18 @@ func (plot *Plot) drawAxesToScreen(screen tcell.Screen) {
y+height-plotXAxisLabelsHeight-1,
tview.BoxDrawingsLightUpAndRight, axesStyle)
// draw x axis labels
if plot.drawXAxisLabel {
plot.drawXAxisLabelToScreen(screen, plotYAxisLabelsWidth, x, y, width, height)
}
if plot.drawYAxisLabel {
plot.drawYAxisLabelToScreen(screen, plotYAxisLabelsWidth, x, y, height)
}
}
func (plot *Plot) drawXAxisLabelToScreen(
screen tcell.Screen, plotYAxisLabelsWidth int, x int, y int, width int, height int,
) {
tview.Print(screen, "0",
x+plotYAxisLabelsWidth,
y+height-plotXAxisLabelsHeight,
@ -210,8 +235,9 @@ func (plot *Plot) drawAxesToScreen(screen tcell.Screen) {
labelX += (len(label) + plotXAxisLabelsGap) * plotHorizontalScale
}
}
// draw Y axis labels
func (plot *Plot) drawYAxisLabelToScreen(screen tcell.Screen, plotYAxisLabelsWidth int, x int, y int, height int) {
verticalScale := plot.maxVal / float64(height-plotXAxisLabelsHeight-1)
for i := 0; i*(plotYAxisLabelsGap+1) < height-1; i++ {