Go CLI that turns the markdown currency of this stack (Redmine notes, Discourse posts, briefing output) into typeset PDFs: two embedded typst templates (report with title page/TOC/headers, dense brief), front-matter (title/subtitle/author/date/classification/template), GFM tables, and bar charts rendered in pure Go (go-chart) from fenced chart data blocks. Engine is a prebuilt typst 0.15.1 container pinned by digest and run --network none; PDF bytes to stdout or -o. Exit codes 0/1/2. All dev in docker (dev.sh/Makefile); unit tests + golden typst fixtures plus a host-side smoke against the real engine container. Generated with Crush Assisted-by: Crush:glm-5.2
62 lines
1.8 KiB
Go
62 lines
1.8 KiB
Go
// 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
|
|
}
|