d2/docs/examples/lib/3-lowlevel/lowlevel.go

31 lines
934 B
Go
Raw Normal View History

package main
import (
"context"
"io/ioutil"
"path/filepath"
"strings"
"oss.terrastruct.com/d2/d2compiler"
"oss.terrastruct.com/d2/d2exporter"
"oss.terrastruct.com/d2/d2layouts/d2dagrelayout"
"oss.terrastruct.com/d2/d2renderers/d2svg"
"oss.terrastruct.com/d2/d2themes/d2themescatalog"
"oss.terrastruct.com/d2/lib/textmeasure"
)
// Remember to add if err != nil checks in production.
func main() {
2023-07-29 18:31:03 +00:00
graph, config, _ := d2compiler.Compile("", strings.NewReader("x -> y"), nil)
2023-03-14 17:40:52 +00:00
graph.ApplyTheme(d2themescatalog.NeutralDefault.ID)
ruler, _ := textmeasure.NewRuler()
2022-12-21 07:43:45 +00:00
_ = graph.SetDimensions(nil, ruler, nil)
2022-12-30 21:36:49 +00:00
_ = d2dagrelayout.Layout(context.Background(), graph, nil)
2023-01-27 21:41:59 +00:00
diagram, _ := d2exporter.Export(context.Background(), graph, nil)
2023-07-29 18:31:03 +00:00
diagram.Config = config
2022-12-21 07:43:45 +00:00
out, _ := d2svg.Render(diagram, &d2svg.RenderOpts{
2023-07-14 20:08:26 +00:00
ThemeID: &d2themescatalog.NeutralDefault.ID,
2022-12-21 07:43:45 +00:00
})
_ = ioutil.WriteFile(filepath.Join("out.svg"), out, 0600)
}