// Package chart renders ```chart data blocks into PNG assets using the // permissive go-chart library (pure Go). v0 proves one chart type end to // end: bar. package chart import ( "bytes" "fmt" "github.com/wcharczuk/go-chart/v2" "github.com/wcharczuk/go-chart/v2/drawing" "ukrrs.com/mopac/pdf/internal/markdown" ) // accent matches the shipped templates (#1f4e79). var accent = drawing.Color{R: 31, G: 78, B: 121, A: 255} // Bar renders a bar chart to PNG bytes. Unknown chart types are an error: // v0 is deliberately one type, proven end to end. func Bar(c *markdown.Chart) ([]byte, error) { if c == nil || len(c.Labels) == 0 { return nil, fmt.Errorf("chart: no data points") } if c.Type != "bar" { return nil, fmt.Errorf("chart: unsupported type %q (v0: bar only)", c.Type) } values := make([]chart.Value, len(c.Labels)) for i, label := range c.Labels { values[i] = chart.Value{ Value: c.Values[i], Label: label, Style: chart.Style{FillColor: accent, StrokeColor: accent, StrokeWidth: 0.5}, } } yfmt := chart.ValueFormatter(chart.FloatValueFormatter) if c.Unit != "" { unit := c.Unit yfmt = func(v interface{}) string { return chart.FloatValueFormatter(v) + " " + unit } } graph := chart.BarChart{ Title: c.Title, Background: chart.Style{ Padding: chart.Box{Top: 24, Left: 12, Right: 12, Bottom: 8}, }, XAxis: chart.Style{FontSize: 9, FontColor: drawing.Color{R: 60, G: 60, B: 60, A: 255}}, YAxis: chart.YAxis{ValueFormatter: yfmt, Style: chart.Style{FontSize: 8}}, BarWidth: 48, BarSpacing: 26, Height: 320, Width: 260 + len(values)*74, Bars: values, } var buf bytes.Buffer if err := graph.Render(chart.PNG, &buf); err != nil { return nil, fmt.Errorf("chart: render: %w", err) } return buf.Bytes(), nil }