You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Aug 23, 2024. It is now read-only.
I am working with go-chart to create a linear chart and want to display values directly over the dots representing data points on the line. However, I'm unsure how to properly implement this, as my current attempts have not displayed the labels.
What I've Tried
Here's a snippet of my code that attempts to render text labels over the dots:
typeTimeSeriesstruct {
chart.TimeSeries
}
// Render renders the series to the chart.func (ats*TimeSeries) Render(renderer chart.Renderer, canvasBox chart.Box, xRange, yRange chart.Range, defaultStyle chart.Style) {
style:=ats.Style.InheritFrom(defaultStyle)
// Render the line and dots as usualats.TimeSeries.Render(renderer, canvasBox, xRange, yRange, style)
// Set font and color for text renderingrenderer.SetFont(style.GetFont())
renderer.SetFontColor(chart.ColorAlternateGreen) // Set a bright color for visibility// X is filled with time.Time valuesforindex, date:=rangeats.XValues {
// Convert time.Time to float64 for the x-axisxValue:=xRange.Translate(chart.TimeToFloat64(date))
yValue:=yRange.Translate(ats.YValues[index])
// Format the label textlabel:=fmt.Sprintf("%.2f", ats.YValues[index])
// Measure text sizetextDimensions:=renderer.MeasureText(label)
// Calculate text position to ensure visibilitytextX:=canvasBox.Left+xValue-textDimensions.Width()/2textY:=canvasBox.Top+yValue-textDimensions.Height() -10// Adjusted for visibility// Draw the textrenderer.Text(label, textX, textY)
}
}
Current result
It draws the lines and the dots but not the labels; see the example:
Question
I am working with
go-chart
to create a linear chart and want to display values directly over the dots representing data points on the line. However, I'm unsure how to properly implement this, as my current attempts have not displayed the labels.What I've Tried
Here's a snippet of my code that attempts to render text labels over the dots:
Current result
It draws the lines and the dots but not the labels; see the example:
If you want to see all the code, see here.
The text was updated successfully, but these errors were encountered: