go-chart icon indicating copy to clipboard operation
go-chart copied to clipboard

How to have a TimeSeries with Annotations

Open wayneforrest opened this issue 4 years ago • 3 comments

Hi can you please show how to have a TimeSeries with Annotations.

wayneforrest avatar Apr 24 '20 22:04 wayneforrest

Pretty much like in the example: https://github.com/wcharczuk/go-chart/blob/master/examples/annotations/main.go But use output of time.UnixNano() as XValue in chart.Value2.

Jeiwan avatar Jun 08 '20 12:06 Jeiwan

Pretty much like in the example: https://github.com/wcharczuk/go-chart/blob/master/examples/annotations/main.go But use output of time.UnixNano() as XValue in chart.Value2.

need to cast to float64 though, as time.UnixNano() returns int64

j0rdan0 avatar Nov 26 '20 17:11 j0rdan0

like that.

graph := chart.Chart{
	Series: []chart.Series{
		chart.TimeSeries{
			XValues: []time.Time{x1, x2, x3},
			YValues: []float64{1.0, 2.0, 3.0},
		},
		chart.AnnotationSeries{
			Annotations: []chart.Value2{
				{XValue: chart.TimeToFloat64(x1), YValue: 1.0, Label: "first"},
				{XValue: chart.TimeToFloat64(x2), YValue: 2.0, Label: "second"},
				{XValue: chart.TimeToFloat64(x3), YValue: 3.0, Label: "last"},
			},
		},
	},
}

jimmykuu avatar Mar 20 '21 03:03 jimmykuu