Skip to content

Commit

Permalink
calculate Y Axis scale for multi-timeseries plot
Browse files Browse the repository at this point in the history
  • Loading branch information
jharshman committed Dec 13, 2023
1 parent a49123c commit 717d812
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion tsplot/plot.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func findMaxFromInt64Data(points []*monitoringpb.Point) float64 {
return float64(maximum)
}

// NewPlotFromTimeSeries creates a plot from a single time series.
func NewPlotFromTimeSeries(ts *monitoringpb.TimeSeries, opts ...PlotOption) (*plot.Plot, error) {
points := ts.GetPoints()
YMax, err := findMaxFromPoints(ts.GetValueType(), points)
Expand All @@ -73,7 +74,10 @@ func NewPlotFromTimeSeries(ts *monitoringpb.TimeSeries, opts ...PlotOption) (*pl
return p, nil
}

// NewPlotFromTimeSeriesIterator creates a plot from multiple time series.
func NewPlotFromTimeSeriesIterator(tsi *monitoring.TimeSeriesIterator, legendKey string, opts ...PlotOption) (*plot.Plot, error) {

var yMax float64
p := plot.New()
for {
timeSeries, err := tsi.Next()
Expand All @@ -84,6 +88,15 @@ func NewPlotFromTimeSeriesIterator(tsi *monitoring.TimeSeriesIterator, legendKey
return nil, err
}

points := timeSeries.GetPoints()

// Find the maximum datapoint between the different time series data.
// Use this to scale the Y Axis.
curMax, _ := findMaxFromPoints(points)

Check failure on line 95 in tsplot/plot.go

View workflow job for this annotation

GitHub Actions / tsplot

not enough arguments in call to findMaxFromPoints
if curMax > yMax {
yMax = curMax
}

// add colored line to plot
lineColor := getUnusedColor()
applyLine := WithColoredLineFromPoints(timeSeries.GetPoints(), lineColor)
Expand All @@ -94,7 +107,7 @@ func NewPlotFromTimeSeriesIterator(tsi *monitoring.TimeSeriesIterator, legendKey
legendEntry.Color = lineColor
p.Legend.Add(timeSeries.GetMetric().GetLabels()[legendKey], legendEntry)

p.Y.Max = 400 // todo: make dynamic according to data
p.Y.Max = yMax + 200
}

for _, opt := range opts {
Expand Down

0 comments on commit 717d812

Please sign in to comment.