mirror of
https://github.com/navidys/tvxwidgets.git
synced 2025-04-28 13:48:52 +08:00
add plot Y axis label type (float, integer)
Signed-off-by: Navid Yaghoobi <navidys@fedoraproject.org>
This commit is contained in:
parent
ef7a0912d9
commit
1dfc3feec1
32
plot.go
32
plot.go
@ -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
|
||||||
|
|
||||||
@ -53,6 +61,7 @@ type Plot struct {
|
|||||||
drawAxes bool
|
drawAxes bool
|
||||||
drawXAxisLabel bool
|
drawXAxisLabel bool
|
||||||
drawYAxisLabel bool
|
drawYAxisLabel bool
|
||||||
|
yAxisLabelDataType PlotYAxisLabelDataType
|
||||||
brailleCellMap map[image.Point]brailleCell
|
brailleCellMap map[image.Point]brailleCell
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
}
|
}
|
||||||
@ -69,6 +78,7 @@ func NewPlot() *Plot {
|
|||||||
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,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user