draw

package module
v0.33.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 21, 2023 License: MIT Imports: 6 Imported by: 1

README

Build Status codecov Maintainability

draw - package for writing software design diagrams

Buy Me A Coffee

Documentation

Overview

Package draw provides SVG writing features.

Example (Inline)
package main

import (
	"fmt"

	"github.com/gregoryv/draw/design"
)

func main() {
	d := design.NewSequenceDiagram()
	fmt.Println(d.Inline())
}
Output:

<svg
  xmlns="http://www.w3.org/2000/svg"
  xmlns:xlink="http://www.w3.org/1999/xlink"
  font-family="Arial,Helvetica,sans-serif" width="1" height="1"></svg>
Example (Overview)

The overview is used as social preview in github. Transform to png with e.g. inkscape -z -w 890 -h 356 overview.svg -e overview.png

package main

import (
	"database/sql"

	"github.com/gregoryv/draw/design"
	"github.com/gregoryv/draw/internal/app"
	"github.com/gregoryv/draw/shape"
)

func main() {
	var (
		d   = design.NewSequenceDiagram()
		cli = d.AddStruct(app.Client{})
		srv = d.AddStruct(app.Server{})
		db  = d.AddStruct(sql.DB{})
	)
	d.Link(cli, srv, "connect()")
	d.Link(srv, db, "...")

	// Flow part
	var (
		start = shape.NewDot()
		run   = shape.NewState("Run")
		end   = shape.NewExitDot()
	)
	d.Place(start).At(100, 140)
	d.Place(run, end).Below(start)
	d.VAlignCenter(start, run, end)
	d.LinkAll(start, run, end)

	// Class diagram (manual)
	var (
		circle = design.NewVRecord(shape.Circle{})
		shp    = design.NewVRecord((*shape.Shape)(nil))
	)
	circle.HideMethods()
	d.Place(shp).RightOf(start, 100)
	d.Place(circle).RightOf(shp, 100)
	d.HAlignBottom(shp, circle)
	lnk := shape.NewArrowBetween(circle, shp)
	lnk.SetClass("implements-arrow")
	d.Place(lnk)

	// Actors and notes
	var (
		note  = shape.NewNote("Anything is possible!\nGo draw your next design")
		actor = shape.NewActor()
	)
	d.Place(note).Above(circle, 60)
	shape.Move(note, 50, 0)
	d.Place(actor).Above(note)
	d.VAlignLeft(note, actor)
	d.Place(shape.NewArrowBetween(actor, note))

	// components
	var (
		dbcomp  = shape.NewDatabase("database")
		inet    = shape.NewInternet()
		service = shape.NewComponent("Service")
		browser = shape.NewComponent("Browser")
	)
	browser.SetClass("external")
	d.Place(service).RightOf(note, 70)
	shape.Move(service, 0, -70)
	d.Place(dbcomp).RightOf(service)
	d.Place(inet).Below(service)
	d.Place(browser).Below(inet)
	d.VAlignCenter(service, inet, browser)

	d.SetCaption("gregoryv/draw provided shapes and diagrams")
	d.SaveAs("overview.svg")
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	DefaultFont    = Font{Height: 12, LineHeight: 16, /* contains filtered or unexported fields */}
	DefaultTextPad = Padding{Left: 6, Top: 4, Bottom: 6, Right: 10}
	DefaultPad     = Padding{Left: 10, Top: 2, Bottom: 7, Right: 10}
	DefaultSpacing = 30 // between elements

	DefaultFontFamily = `font-family="Arial,Helvetica,sans-serif"`

	DefaultClassAttributes = ClassAttributes{
		"area-red-label":   `font-style="italic" ` + DefaultFontFamily,
		"area-green-label": `font-style="italic" ` + DefaultFontFamily,
		"area-blue-label":  `font-style="italic" ` + DefaultFontFamily,
		"area-red":         `stroke="black" stroke-width="0" fill="#ff9999" fill-opacity="0.1"`,
		"area-green":       `stroke="black" stroke-width="0" fill="#ccff99" fill-opacity="0.1"`,
		"area-blue":        `stroke="black" stroke-width="0" fill="#99e6ff" fill-opacity="0.1"`,

		"actor":                 `stroke="black" stroke-width="2" fill="#ffffff"`,
		"circle":                `stroke="#d3d3d3" stroke-width="2" fill="#ffffff"`,
		"container":             `stroke="#737373" stroke-dasharray="5,5,5" fill="transparent"`,
		"cylinder":              `stroke="#d3d3d3" stroke-width="1" fill="#ffffff"`,
		"card":                  `stroke="#d3d3d3" stroke-width="1" fill="#ffffff"`,
		"card-external":         `stroke="#d3d3d3" stroke-width="1" fill="#f8f9fa"`,
		"card-title":            DefaultFontFamily + ` font-weight="bold"`,
		"database":              `stroke="#d3d3d3" stroke-width="1" fill="#ffffff"`,
		"dot":                   `stroke="black"`,
		"exit":                  `stroke="black" stroke-width="2" fill="#ffffff"`,
		"exit-dot":              `stroke="black"`,
		"note":                  DefaultFontFamily,
		"note-box":              `stroke="#d3d3d3" fill="#ffffcc"`,
		"highlight":             `stroke="red"`,
		"highlight-head":        `stroke="red" fill="#ffffff"`,
		"implements-arrow":      `stroke="black" stroke-dasharray="5,5,5"`,
		"implements-arrow-head": `stroke="black" fill="#ffffff"`,
		"arrow":                 `stroke="black"`,
		"arrow-head":            `stroke="black" fill="#ffffff"`,
		"arrow-tail":            `stroke="black" fill="#777777"`,
		"dashed-arrow":          `stroke="black"`,
		"dashed-arrow-head":     `stroke="black" fill="#ffffff"`,
		"dashed-arrow-tail":     `stroke="black" fill="#777777"`,
		"activity-arrow":        `stroke="black"`,
		"activity-arrow-head":   `stroke="black" fill="#ffffff"`,
		"activity-arrow-tail":   `stroke="black" fill="#777777"`,
		"compose-arrow":         `stroke="black"`,
		"compose-arrow-head":    `stroke="black" fill="#ffffff"`,
		"compose-arrow-tail":    `stroke="black" fill="#777777"`,
		"aggregate-arrow":       `stroke="black"`,
		"aggregate-arrow-head":  `stroke="black" fill="#ffffff"`,
		"aggregate-arrow-tail":  `stroke="black" fill="#ffffff"`,
		"external":              `stroke="#d3d3d3" fill="#e2e2e2"`,
		"dim":                   `stroke="#d3d3d3" fill="#e2e2e2"`,
		"hexagon":               `stroke="#d3d3d3" fill="#ffffff"`,
		"hexagon-title":         DefaultFontFamily,
		"internet":              `stroke="#d3d3d3" fill="#e2e2e2"`,
		"internet-title":        DefaultFontFamily,
		"line":                  `stroke="black"`,
		"triangle":              `stroke="black"`,
		"column-line":           `stroke="#d3d3d3"`,
		"process":               `stroke="#d3d3d3" fill="#ffffff"`,
		"process-title":         DefaultFontFamily,
		"record":                `stroke="#d3d3d3" fill="#ffffff"`,
		"record-line":           `stroke="#d3d3d3"`,
		"record-title":          DefaultFontFamily,
		"rect":                  `stroke="#d3d3d3" fill="#ffffff"`,
		"rect-title":            DefaultFontFamily,
		"root":                  DefaultFontFamily,
		"skip":                  `stroke="#ffffff" stroke-dasharray="2,2,2"`,
		"span-green":            `stroke="#d3d3d3" fill="#ccff99" rx="5" ry="5"`,
		"span-green-title":      DefaultFontFamily,
		"span-blue":             `stroke="#d3d3d3" fill="#99e6ff" rx="5" ry="5"`,
		"span-blue-title":       DefaultFontFamily,
		"span-red":              `stroke="#d3d3d3" fill="#ff9999" rx="5" ry="5"`,
		"span-red-title":        DefaultFontFamily,
		"state-title":           DefaultFontFamily,
		"state":                 `stroke="#d3d3d3" fill="#ffffff" rx="10" ry="10"`,
		"store":                 `stroke="#d3d3d3" fill="#ffffff"`,
		"store-title":           DefaultFontFamily,
		"component":             `stroke="#d3d3d3" fill="#ffffff"`,
		"component-title":       DefaultFontFamily,
		"field":                 DefaultFontFamily,
		"method":                DefaultFontFamily,
		"record-label":          DefaultFontFamily,
		"label":                 DefaultFontFamily,
		"weekend":               DefaultFontFamily + ` fill="#f3f3f3"`,
		"weekend-title":         DefaultFontFamily,
		"caption":               DefaultFontFamily,
		"diamond":               `stroke="#d3d3d3" fill="#333333"`,
		"decision":              `stroke="#d3d3d3" fill="#ffffff"`,
	}
)

Functions

This section is empty.

Types

type ClassAttributes added in v0.13.0

type ClassAttributes map[string]string

ClassAttributes define mapping between classes and svg attributes. Setting attributes that modify size or position is not advised.

func (ClassAttributes) CSS added in v0.15.0

func (me ClassAttributes) CSS() string

CSS returns cascading rules for embedding in html

type Font added in v0.13.0

type Font struct {
	Height int

	// It is allowed to have a smaller line height than height.
	LineHeight int
	// contains filtered or unexported fields
}

func (*Font) SetScale added in v0.32.0

func (f *Font) SetScale(v float64)

func (Font) TextWidth added in v0.13.0

func (f Font) TextWidth(txt string) int

TextWidth returns the width of the given text based on a 12px arial font.

type Padding added in v0.13.0

type Padding struct {
	Left, Top, Right, Bottom int
}

func (*Padding) SetScale added in v0.32.0

func (p *Padding) SetScale(v float64)

type SVG added in v0.12.0

type SVG struct {
	Content []SVGWriter
	// contains filtered or unexported fields
}

func NewSVG added in v0.13.0

func NewSVG() *SVG

NewSVG returns an empty SVG of size 100x100

func (*SVG) Append added in v0.12.0

func (s *SVG) Append(w ...SVGWriter)

func (*SVG) Height added in v0.12.0

func (s *SVG) Height() int

func (*SVG) Prepend added in v0.12.0

func (s *SVG) Prepend(w ...SVGWriter)

func (*SVG) SetHeight added in v0.12.0

func (s *SVG) SetHeight(h int)

SetHeight sets the SVG height in pixels.

func (*SVG) SetSize added in v0.12.0

func (s *SVG) SetSize(width, height int)

SetSize sets the SVG size in pixels

func (*SVG) SetWidth added in v0.12.0

func (s *SVG) SetWidth(w int)

SetWidth sets the SVG width in pixels.

func (*SVG) Width added in v0.12.0

func (s *SVG) Width() int

func (*SVG) WriteSVG added in v0.12.0

func (s *SVG) WriteSVG(w io.Writer) error

WriteSVG writes <svg> </svg> tags and it's content to the given writer.

type SVGWriter added in v0.12.0

type SVGWriter interface {
	WriteSVG(io.Writer) error
}

type Style added in v0.13.0

type Style struct {
	Font
	TextPad Padding // Surrounding text
	Pad     Padding // E.g. records
	Spacing int     // Between shapes in e.g. diagrams
	// contains filtered or unexported fields
}

func NewStyle added in v0.13.0

func NewStyle() Style

NewStyle returns a style based on the default values, eg. draw.DefaultFont in this package.

func (*Style) SetOutput added in v0.13.0

func (s *Style) SetOutput(w io.Writer)

SetOutput sets the destination of calls to Write.

func (*Style) SetScale added in v0.32.0

func (s *Style) SetScale(v float64)

func (*Style) Write added in v0.13.0

func (s *Style) Write(p []byte) (int, error)

Write adds a style attribute based on class. Limited to 1 class only and assumes the entire classname attribute is found.

Directories

Path Synopsis
Package design provides diagram creators
Package design provides diagram creators
Package docs provides the content of github pages at https://gregoryv.github.io/draw/
Package docs provides the content of github pages at https://gregoryv.github.io/draw/
Package goviz provides means to generate Go diagrams.
Package goviz provides means to generate Go diagrams.
internal
app
Package shape provides various SVG shapes
Package shape provides various SVG shapes
Package xy provides xy Position
Package xy provides xy Position

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL