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 {