-
Notifications
You must be signed in to change notification settings - Fork 556
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add render interface details (#430)
- Loading branch information
Showing
3 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# Render | ||
|
||
Render is an interface for render charts, alias the `Renderer`. | ||
|
||
```go | ||
// Renderer | ||
// Any kinds of charts have their render implementation, and | ||
// you can define your own render logic easily. | ||
type Renderer interface { | ||
Render(w io.Writer) error | ||
RenderContent() []byte | ||
RenderSnippet() ChartSnippet | ||
} | ||
``` | ||
|
||
## `Render(w io.Writer) error` | ||
|
||
The `Render(w io.Writer) error` function aims to generate chart to target `io.Writer`. | ||
Underlying, it depends on the `RenderContent() []byte` to generate the chart bytes streaming. | ||
|
||
## `RenderContent() []byte` | ||
|
||
The `RenderContent() []byte` uses templates to generate the chart bytes streaming. | ||
i.e. | ||
> The `BaseRender` is a base Render with default implementations of Renderer. | ||
> It helps you can only implement your own necessary functions of `Renderer`. | ||
```go | ||
type chartRender struct { | ||
BaseRender | ||
c interface{} | ||
// before the pre-process functions for chart render, it only calls once to support multi renders | ||
before []func () | ||
} | ||
|
||
``` | ||
|
||
!> You need run the `before` functions to make sure all the charts options set in place. | ||
|
||
## `RenderSnippet() ChartSnippet` | ||
|
||
The `RenderSnippet() ChartSnippet` function can extract the element(chart container), the script | ||
and the options from a chart. | ||
|
||
!> It only works for Chart, Page is unsupported. | ||
|
||
e.g. | ||
> Full Code: [examples/renderer.go](https://github.com/go-echarts/examples/blob/master/examples/renderer.go) | ||
```go | ||
bar := charts.NewBar() | ||
bar.SetGlobalOptions(charts.WithTitleOpts(opts.Title{ | ||
Title: "Bar chart for Snippets", | ||
})) | ||
|
||
bar.SetXAxis([]string{"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}). | ||
AddSeries("Category A", generateBarItems()). | ||
AddSeries("Category B", generateBarItems()) | ||
|
||
// pure extracted snippets | ||
chartSnippet := bar.RenderSnippet() | ||
|
||
tmpl := "{{.Element}} {{.Script}} {{.Option}}" | ||
t := template.New("snippet") | ||
t, err := t.Parse(tmpl) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
data := struct { | ||
Element template.HTML | ||
Script template.HTML | ||
Option template.HTML | ||
}{ | ||
Element: template.HTML(chartSnippet.Element), | ||
Script: template.HTML(chartSnippet.Script), | ||
Option: template.HTML(chartSnippet.Option), | ||
} | ||
|
||
// rerender the snippets out | ||
err = t.Execute(os.Stdout, data) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters