From 717d8126c38319106583cc4f525f42c3448e23f8 Mon Sep 17 00:00:00 2001 From: Josh Harshman Date: Wed, 13 Dec 2023 12:50:36 -0700 Subject: [PATCH] calculate Y Axis scale for multi-timeseries plot --- tsplot/plot.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/tsplot/plot.go b/tsplot/plot.go index eae5cec..7d200bf 100644 --- a/tsplot/plot.go +++ b/tsplot/plot.go @@ -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) @@ -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() @@ -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) + if curMax > yMax { + yMax = curMax + } + // add colored line to plot lineColor := getUnusedColor() applyLine := WithColoredLineFromPoints(timeSeries.GetPoints(), lineColor) @@ -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 {