Skip to content

Commit

Permalink
multiple lines clear colors
Browse files Browse the repository at this point in the history
  • Loading branch information
jharshman committed Dec 13, 2023
1 parent 5639bb1 commit a49123c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 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, "classification")
plot, err := tsplot.NewPlotFromTimeSeriesIterator(tsi, "classification", tsplot.WithXTimeTicks(time.Kitchen), tsplot.WithTitle("decodes"), tsplot.WithXAxisName("UTC"))
if err != nil {
log.Fatal(err)
}
Expand Down
15 changes: 14 additions & 1 deletion tsplot/color_palettes.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,21 @@ import (

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

// simple colors (subset of golang.org/x/image/colornames)
var availableColors = map[string]color.RGBA{
"blue": color.RGBA{0x00, 0x00, 0xff, 0xff}, // rgb(0, 0, 255)
"brown": color.RGBA{0xa5, 0x2a, 0x2a, 0xff}, // rgb(165, 42, 42)
"orange": color.RGBA{0xff, 0xa5, 0x00, 0xff}, // rgb(255, 165, 0)
"hotpink": color.RGBA{0xff, 0x69, 0xb4, 0xff}, // rgb(255, 105, 180)
"red": color.RGBA{0xff, 0x00, 0x00, 0xff}, // rgb(255, 0, 0)
"purple": color.RGBA{0x80, 0x00, 0x80, 0xff}, // rgb(128, 0, 128)
"yellow": color.RGBA{0xff, 0xff, 0x00, 0xff}, // rgb(255, 255, 0)
"green": color.RGBA{0x00, 0x80, 0x00, 0xff}, // rgb(0, 128, 0)

}

func getUnusedColor() color.RGBA {
for k, v := range colornames.Map {
for k, v := range availableColors {
if _, ok := usedColors[k]; !ok {
usedColors[k] = v
return v
Expand Down
9 changes: 5 additions & 4 deletions tsplot/plot.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package tsplot

import (
monitoring "cloud.google.com/go/monitoring/apiv3/v2"
"fmt"

monitoring "cloud.google.com/go/monitoring/apiv3/v2"
"gonum.org/v1/plot"
"gonum.org/v1/plot/plotter"
"google.golang.org/api/iterator"
Expand Down Expand Up @@ -72,7 +73,7 @@ func NewPlotFromTimeSeries(ts *monitoringpb.TimeSeries, opts ...PlotOption) (*pl
return p, nil
}

func NewPlotFromTimeSeriesIterator(tsi *monitoring.TimeSeriesIterator, groupByLabel string, opts ...PlotOption) (*plot.Plot, error) {
func NewPlotFromTimeSeriesIterator(tsi *monitoring.TimeSeriesIterator, legendKey string, opts ...PlotOption) (*plot.Plot, error) {
p := plot.New()
for {
timeSeries, err := tsi.Next()
Expand All @@ -91,7 +92,7 @@ func NewPlotFromTimeSeriesIterator(tsi *monitoring.TimeSeriesIterator, groupByLa
// add to legend
legendEntry, _ := plotter.NewPolygon()
legendEntry.Color = lineColor
p.Legend.Add(timeSeries.GetMetric().GetLabels()[groupByLabel], legendEntry)
p.Legend.Add(timeSeries.GetMetric().GetLabels()[legendKey], legendEntry)

p.Y.Max = 400 // todo: make dynamic according to data
}
Expand All @@ -108,7 +109,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() // todo: This breaks if the valuetype is an int64
y := point.GetValue().GetDoubleValue() // todo: This breaks if the value type is an int64
XYs = append(XYs, plotter.XY{
X: float64(x),
Y: y,
Expand Down

0 comments on commit a49123c

Please sign in to comment.