Documentation ¶
Overview ¶
Package gantt is an object oriented approach to define mermaid gantt diagrams as defined at https://mermaidjs.github.io/gantt.html and render them to mermaid code.
Start exploring the Gantt type and the example "Gantt (Basics)", then proceed with the other examples.
Index ¶
- Constants
- Variables
- type Gantt
- func (g *Gantt) AddSection(id string) (newSection *Section, err error)
- func (g *Gantt) AddTask(id string, init ...interface{}) (newTask *Task, err error)
- func (g *Gantt) GetSection(id string) (existingSection *Section)
- func (g *Gantt) GetTask(id string) (existingTask *Task)
- func (g *Gantt) ListLocalTasks() (localTasks []*Task)
- func (g *Gantt) ListSections() (allSections []*Section)
- func (g *Gantt) ListTasks() (allTasks []*Task)
- func (g *Gantt) LiveURL() (url string)
- func (g *Gantt) String() (renderedElement string)
- func (g *Gantt) ViewInBrowser() (err error)
- type Section
- type Task
- func (t *Task) CopyFields(task *Task)
- func (t *Task) Gantt() (topLevel *Gantt)
- func (t *Task) ID() (id string)
- func (t *Task) Section() (containingSection *Section)
- func (t *Task) SetDuration(duration interface{}) (err error)
- func (t *Task) SetStart(start interface{}) (err error)
- func (t *Task) String() (renderedElement string)
Examples ¶
Constants ¶
const ( FormatDateTime24WithSeconds axisFormat = `%Y-%m-%d %H:%M:%S` FormatDateTime24 axisFormat = `%Y-%m-%d %H:%M` FormatDateTime24Short axisFormat = `%y%m%d %H:%M` FormatDate axisFormat = `%Y-%m-%d` FormatDateShort axisFormat = `%y%m%d` FormatWeekdayTime24 axisFormat = `%a %H:%M` FormatWeekdayTime24WithSeconds axisFormat = `%a %H:%M:%S` FormatTime24 axisFormat = `%H:%M` FormatTime24WithSeconds axisFormat = `%H:%M:%S` )
Format definitions for x axis scale as described at https://mermaidjs.github.io/gantt.html#scale. The default is no axisFormat statement which results in FormatDate.
Variables ¶
var IsValidID = regexp.MustCompile(`^[a-zA-Z0-9_-]+$`).MatchString
IsValidID is used to check if Task IDs are valid: IsValidID(string) bool. These limitations don't apply to Section IDs which are Section's titles at the same time.
Functions ¶
This section is empty.
Types ¶
type Gantt ¶
type Gantt struct { Title string // Title of the Gantt diagram AxisFormat axisFormat // Optional time format for x axis // contains filtered or unexported fields }
Gantt objects are the entrypoints to this package, the whole diagram is constructed around a Gantt object. Create an instance of Gantt via Gantt's constructor NewGantt, do not create instances directly.
Example (Basics) ¶
Defining and rendering a gantt diagram
package main import ( "fmt" "time" "github.com/Heiko-san/mermaidgen/gantt" ) func main() { // create a gantt diagram g, _ := gantt.NewGantt() // set properties for the different elements g.Title = "My diagram" // add some tasks to it directly t1, _ := g.AddTask("t1") t1.SetStart(time.Date(2019, 6, 20, 9, 15, 30, 0, time.UTC)) t1.Critical = true // or add sections with tasks s1, _ := g.AddSection("s1") s1.AddTask("t2", "section1 task", "10h30m", t1) s2, _ := g.AddSection("s2") s2.AddTask("t3", "section2 task", "20h", t1) // stringify renders the mermaid code fmt.Print(g) // you can also look up already defined Sections and Tasks s1x := g.GetSection("s1") t1x := g.GetTask("t1") fmt.Println("they are the same:", s1 == s1x, t1 == t1x) }
Output: gantt dateFormat YYYY-MM-DDTHH:mm:ssZ title My diagram t1 : crit, t1, 2019-06-20T09:15:30Z, 1d section s1 section1 task : t2, after t1, 37800s section s2 section2 task : t3, after t1, 72000s they are the same: true true
Example (ErrorHandling) ¶
The creation of Gantts, Sections and Tasks may yield errors
package main import ( "fmt" "github.com/Heiko-san/mermaidgen/gantt" ) func main() { // parameter errors will be returned and no Gantt will be created g1, err := gantt.NewGantt(5, gantt.FormatDate) fmt.Println(g1, err) g2, err := gantt.NewGantt("title", 5) fmt.Println(g2, err) g, _ := gantt.NewGantt() // same applies to Task creation t1, err := g.AddTask("id1", "my title", "1h50xyz") fmt.Println(t1, err) t1, err = g.AddTask("id1", "my title", "1h50m", "foobar") fmt.Println(t1, err) // Sections have no initializers that may fail, however errors will be // returned if you try to create a duplicate ID s1, _ := g.AddSection("s1") s2, err := g.AddSection("s1") fmt.Println(s2, err) // same applies to Task creation t1, err = s1.AddTask("id1") t2, err := g.AddTask("id1") fmt.Println(t2, err) // or if an invalid ID is provided for Task creation t2, err = g.AddTask("foo bar baz") fmt.Println(t2, err) }
Output: <nil> value for Title was no string <nil> value for AxisFormat was no axisFormat <nil> SetDuration: "1h50xyz" is neither a valid duration nor Task ID <nil> SetStart: "foobar" is neither RFC3339 nor a valid Task ID <nil> id already exists <nil> id already exists <nil> invalid id
Example (IterateSectionsAndTasks) ¶
Iterate over the Tasks and Sections of a Gantt diagram
package main import ( "fmt" "github.com/Heiko-san/mermaidgen/gantt" ) func main() { g, _ := gantt.NewGantt() // add some local tasks to Gantt g.AddTask("t1") g.AddTask("t2") // add sections s1, _ := g.AddSection("This is my section") s2, _ := g.AddSection("Another section") // add some tasks to them s1.AddTask("a3") s2.AddTask("b4") // iterate over Sections fmt.Println("all sections") for _, s := range g.ListSections() { fmt.Println(s.ID()) } // iterate over Tasks fmt.Println("this Gantt's local tasks") for _, t := range g.ListLocalTasks() { fmt.Println(t.ID()) } fmt.Println("all tasks") for _, t := range g.ListTasks() { fmt.Println(t.ID()) } }
Output: all sections This is my section Another section this Gantt's local tasks t1 t2 all tasks a3 b4 t1 t2
Example (LiveEditorLinks) ¶
Generating URLs for the mermaid live editor
package main import ( "fmt" "time" "github.com/Heiko-san/mermaidgen/gantt" ) func main() { g, _ := gantt.NewGantt() timestamp := time.Date(2019, 6, 20, 9, 15, 30, 0, time.UTC) g.AddTask("t1", "a task", "1h", timestamp) g.AddTask("t2", "another task", "2h") // you can also use g.ViewInBrowser() to open the URL in browser directly fmt.Println(g.LiveURL()) }
Output: https://mermaid.live/view/#pako:eNo0zbEKg0AMgOFXOTJ7EJVazCzFxc3lxCX00lranOClk_jupYWuPz98O1zXKEBw52Q2p8gml3VTNhdCCH4YfNeNfU-qlPM0J3bG-enIWVm4CsvWY-MrHLGl8kQ1ToWrG8Q8J06rLbL9_3P1q1CAyqb8iEA72CL61aPc-P0yOI5PAAAA___xki0z
Example (Settings) ¶
Properties of a gantt diagram
package main import ( "fmt" "github.com/Heiko-san/mermaidgen/gantt" ) func main() { // setting Title and AxisFormat on object creation g, _ := gantt.NewGantt("This is my title", "%H:%M") // setting the Title afterwards g.Title = "This is my title" // setting AxisFormat afterwards using constants g.AxisFormat = gantt.FormatTime24 fmt.Print(g) }
Output: gantt dateFormat YYYY-MM-DDTHH:mm:ssZ axisFormat %H:%M title This is my title
func NewGantt ¶
NewGantt is the constructor used to create a new Gantt object. This object is the entrypoint for any further interactions with your diagram. Always use the constructor, don't create Gantt objects directly. Optional initializer parameters can be given in the order Title, AxisFormat.
func (*Gantt) AddSection ¶
AddSection is used to add a new Section to this Gantt diagram. If the provided ID already exists, no new Section is created and an error is returned. The ID can later be used to look up the created Section using Gantt's GetSection method.
func (*Gantt) AddTask ¶
AddTask is used to add a new Task to this Gantt diagram. If the provided ID already exists or is invalid, no new Task is created and an error is returned. The ID can later be used to look up the created Task using Gantt's GetTask method. Optional initializer parameters can be given in the order Title, Duration, Start, Critical, Active, Done. Duration and Start are set via Task's SetDuration and SetStart respectively.
func (*Gantt) GetSection ¶
GetSection looks up a previously defined Section by its ID. If this ID doesn't exist, nil is returned. Use Gantt's AddSection to create new Sections.
func (*Gantt) GetTask ¶
GetTask looks up a previously defined Task by its ID. If this ID doesn't exist, nil is returned. Use Gantt's or Section's AddTask to create new Tasks.
func (*Gantt) ListLocalTasks ¶
ListLocalTasks returns a slice of all Tasks previously added to this Gantt diagram directly (not to Sections) in the order they were defined.
func (*Gantt) ListSections ¶
ListSections returns a slice of all Sections previously added to this Gantt diagram in the order they were defined.
func (*Gantt) ListTasks ¶
ListTasks returns a slice of all Tasks previously added to this Gantt diagram and all of its Sections in alphabetic order by ID.
func (*Gantt) LiveURL ¶
LiveURL renders the Gantt and generates a view URL for https://mermaidjs.github.io/mermaid-live-editor from it.
func (*Gantt) ViewInBrowser ¶
ViewInBrowser uses the URL generated by Gantt's LiveURL method and opens that URL in the OS's default browser. It starts the browser command non-blocking and eventually returns any error occured.
type Section ¶
type Section struct {
// contains filtered or unexported fields
}
Section represents gantt sections that can be added to the Gantt diagram. Create an instance of Section via Gantt's AddSection method, do not create instances directly. Already defined IDs can be looked up via Gantt's GetSection method or iterated over via its ListSections method.
Example (IterateSectionTasks) ¶
Iterate over the Tasks of a Section
package main import ( "fmt" "github.com/Heiko-san/mermaidgen/gantt" ) func main() { g, _ := gantt.NewGantt() g.AddTask("t1") // add sections s1, _ := g.AddSection("This is my section") s2, _ := g.AddSection("Another section") // add some tasks to them s1.AddTask("t2") s1.AddTask("t3") s2.AddTask("t4") // iterate over s1 Tasks for _, t := range s1.ListLocalTasks() { fmt.Println(t.ID()) } }
Output: t2 t3
Example (PrivateFields) ¶
Accessing the readonly fields of a Section
package main import ( "fmt" "github.com/Heiko-san/mermaidgen/gantt" ) func main() { g, _ := gantt.NewGantt() s, _ := g.AddSection("this_is_my_id") // get a copy of the id field id := s.ID() // access the top level Gantt gx := s.Gantt() fmt.Println(id, gx == g) }
Output: this_is_my_id true
func (*Section) AddTask ¶
AddTask is used to add a new Task to this Section. If the provided ID already exists or is invalid, no new Task is created and an error is returned. The ID can later be used to look up the created Task using Gantt's GetTask method. Optional initializer parameters can be given in the order Title, Duration, Start, Critical, Active, Done. Duration and Start are set via Task's SetDuration and SetStart respectively.
func (*Section) Gantt ¶
Gantt provides access to the top level Gantt diagram to be able to access Adder, Getter and Lister methods.
func (*Section) ID ¶
ID provides access to the Sections readonly field id. This is used as the Section's title since the title needs to be unique. However the strict rules for Task's IDs don't apply here, feel free to use spaces and special characters.
func (*Section) ListLocalTasks ¶
ListLocalTasks returns a slice of all Tasks previously added to this Section in the order they were defined.
type Task ¶
type Task struct { Title string // Title of the Task, if not set, ID is used Start *time.Time // Time when the Task starts (Start wins over After) After *Task // Task after which this Task starts Duration *time.Duration // Duration of the Task (the absolute value is used) Critical bool // The crit flag Active bool // The active flag Done bool // The done flag // contains filtered or unexported fields }
Task represents gantt tasks that can be added to Sections or the Gantt diagram itself. Create an instance of Task via Gantt's or Section's AddTask method, do not create instances directly. Already defined IDs can be looked up via Gantt's GetTask method or iterated over via its ListTasks method.
Example (PrivateFields) ¶
Accessing the readonly fields of a Task
package main import ( "fmt" "github.com/Heiko-san/mermaidgen/gantt" ) func main() { g, _ := gantt.NewGantt() s, _ := g.AddSection("sect1") t, _ := s.AddTask("this_is_my_id") // get a copy of the id field id := t.ID() // access the top level Gantt gx := t.Gantt() // access this Task's Section sx := t.Section() fmt.Println(id, gx == g, sx == s) }
Output: this_is_my_id true true
Example (SettingValues) ¶
Different ways to set the fields of a Task
package main import ( "fmt" "time" "github.com/Heiko-san/mermaidgen/gantt" ) func main() { g, _ := gantt.NewGantt() // Get a Task with only ID and default values, there is little chance for // an error here if you don't mess up the ID. t1, _ := g.AddTask("id1") // you can set the fields directly timestamp := time.Date(2019, 6, 20, 9, 15, 30, 0, time.UTC) t1.Start = ×tamp t1.Critical = true // to set Start (or After) and Duration there are helper functions that take // different values, see method description for details t2, _ := g.AddTask("id2") t3, _ := g.AddTask("id3") t4, _ := g.AddTask("id4") t5, _ := g.AddTask("id5") t6, _ := g.AddTask("id6") // start at time.Time t2.SetStart(×tamp) t3.SetStart(timestamp) // start after Task t4.SetStart(t3) t5.SetStart("id4") // start at RFC3339 string t6.SetStart("2019-06-20T11:15:30+02:00") duration := time.Hour * 10 end := timestamp.Add(time.Hour * 5) // duration by time.Time - Start (Start needs to be defined, not After) t2.SetDuration(&end) t2.SetDuration(end) // duration by time.Duration t3.SetDuration(&duration) t3.SetDuration(duration) // copy duration from other Task t4.SetDuration(t1) t5.SetDuration("id3") // duration string t6.SetDuration("12h30m30s") // you can provide values directly to AddTask method // (id, Title, SetDuration(), SetStart(), Critical, Active, Done) t7, _ := g.AddTask("id7", "A Task", time.Hour*20, timestamp, true, true, true) // finally you can copy the settings from other Tasks t8, _ := g.AddTask("id8") t8.CopyFields(t7) fmt.Print(g) }
Output: gantt dateFormat YYYY-MM-DDTHH:mm:ssZ id1 : crit, id1, 2019-06-20T09:15:30Z, 1d id2 : id2, 2019-06-20T09:15:30Z, 18000s id3 : id3, 2019-06-20T09:15:30Z, 36000s id4 : id4, after id3, 1d id5 : id5, after id4, 36000s id6 : id6, 2019-06-20T11:15:30+02:00, 45030s A Task : crit, active, done, id7, 2019-06-20T09:15:30Z, 72000s A Task : crit, active, done, id8, 2019-06-20T09:15:30Z, 72000s
func (*Task) CopyFields ¶
CopyFields sets all (non-readonly) fields according to the given Task.
func (*Task) Gantt ¶
Gantt provides access to the top level Gantt diagram to be able to access Adder, Getter and Lister methods.
func (*Task) Section ¶
Section provides access to the Section that contains this Task to be able to access Adder methods. If this Task is contained in the top level Gantt itself, nil is returned.
func (*Task) SetDuration ¶
SetDuration takes a time.Duration or a pointer to it, a time.Time or a pointer to it, a Task pointer or a string that represents an existing Task ID or a parsable duration definition and sets this Task's Duration field from that information. If this information represents a time.Time, the difference to Start is calculated, if it represents a Task, that Task's Duration is copied. An error is returned if the given type is not supported, Start is undefined for a Time definition or the string can't be parsed.
func (*Task) SetStart ¶
SetStart takes a time.Time or a pointer to it, a Task pointer or a string that represents an existing Task ID or a RFC3339 time definition and sets this Task's Start or After field from that information. An error is returned if the given type is not supported or the string can't be parsed.