Skip to content

Commit

Permalink
multiple lines multiple colors
Browse files Browse the repository at this point in the history
  • Loading branch information
jharshman committed Dec 12, 2023
1 parent 318279f commit 5639bb1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
2 changes: 1 addition & 1 deletion tscli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func executeQuery(cmd *cobra.Command, args []string) error {

tsi := GoogleCloudMonitoringClient.ListTimeSeries(context.Background(), request)
// todo: handle multiple time series
plot, err := tsplot.NewPlotFromTimeSeriesIterator(tsi)
plot, err := tsplot.NewPlotFromTimeSeriesIterator(tsi, "classification")
if err != nil {
log.Fatal(err)
}
Expand Down
12 changes: 12 additions & 0 deletions tsplot/color_palettes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ import (
"golang.org/x/image/colornames"
)

var usedColors = make(map[string]color.RGBA)

func getUnusedColor() color.RGBA {
for k, v := range colornames.Map {
if _, ok := usedColors[k]; !ok {
usedColors[k] = v
return v
}
}
return colornames.Black
}

type ColorPalette struct {
Foreground color.Color
Background color.Color
Expand Down
21 changes: 17 additions & 4 deletions tsplot/plot.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func NewPlotFromTimeSeries(ts *monitoringpb.TimeSeries, opts ...PlotOption) (*pl
return p, nil
}

func NewPlotFromTimeSeriesIterator(tsi *monitoring.TimeSeriesIterator) (*plot.Plot, error) {
func NewPlotFromTimeSeriesIterator(tsi *monitoring.TimeSeriesIterator, groupByLabel string, opts ...PlotOption) (*plot.Plot, error) {
p := plot.New()
for {
timeSeries, err := tsi.Next()
Expand All @@ -82,9 +82,22 @@ func NewPlotFromTimeSeriesIterator(tsi *monitoring.TimeSeriesIterator) (*plot.Pl
}
return nil, err
}
line, _ := createLine(timeSeries.GetPoints())

// add colored line to plot
lineColor := getUnusedColor()
applyLine := WithColoredLineFromPoints(timeSeries.GetPoints(), lineColor)
applyLine(p)

// add to legend
legendEntry, _ := plotter.NewPolygon()
legendEntry.Color = lineColor
p.Legend.Add(timeSeries.GetMetric().GetLabels()[groupByLabel], legendEntry)

p.Y.Max = 400 // todo: make dynamic according to data
p.Add(line)
}

for _, opt := range opts {
opt(p)
}

return p, nil
Expand All @@ -95,7 +108,7 @@ func createLine(dataPoints []*monitoringpb.Point) (*plotter.Line, error) {
var XYs plotter.XYs
for _, point := range dataPoints {
x := point.GetInterval().GetEndTime().GetSeconds()
y := point.GetValue().GetDoubleValue()
y := point.GetValue().GetDoubleValue() // todo: This breaks if the valuetype is an int64
XYs = append(XYs, plotter.XY{
X: float64(x),
Y: y,
Expand Down

0 comments on commit 5639bb1

Please sign in to comment.