Skip to content

Commit 0ca4e3e

Browse files
committed
Add examples of rendering each chart type using the Painter API
In addition two bugs were discovered using this API: * Bar chart series type and filtering was not handled correctly (used line chart and default of line chart is confusing). Fixed in this commit. * Using the Painter API means defaults are not filled out. I will fill this as an issue to fix. Additional TODO notes are added for future investigation before v0.4.0 is tagged
1 parent 845ba06 commit 0ca4e3e

File tree

19 files changed

+556
-281
lines changed

19 files changed

+556
-281
lines changed

bar_chart.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,6 @@ func (b *barChart) Render() (Box, error) {
211211
if err != nil {
212212
return BoxZero, err
213213
}
214-
seriesList := opt.SeriesList.Filter(ChartTypeLine)
214+
seriesList := opt.SeriesList.Filter(ChartTypeBar)
215215
return b.render(renderResult, seriesList)
216216
}

chart_option.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ type ChartOption struct {
5050
// Children are child charts to render together.
5151
Children []ChartOption
5252
parent *Painter
53+
// TODO - review how this is set on other Option structs
5354
// ValueFormatter to format numeric values into labels.
5455
ValueFormatter ValueFormatter
5556
}

examples/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,23 @@ Examples are our primary method for demonstrating the starting point our use and
55
## Example List
66

77
* [bar_chart-1](./bar_chart-1) - Bar chart with included mark points and mark lines.
8+
* [bar_chart-2](./bar_chart-2) - The above example bar chart re-demonstrated using the Painter API.
89
* [chinese](./chinese) - Line chart with chinese characters that uses a custom font (must be downloaded by user, see comment in code).
910
* [funnel_chart-1](./funnel_chart-1) - Basic funnel chart.
11+
* [funnel_chart-2](./funnel_chart-2) - The above example funnel chart re-demonstrated using the Painter API.
1012
* [horizontal_bar_chart-1](./horizontal_bar_chart-1) - Basic horizontal bar chart.
13+
* [horizontal_bar_chart-2](./horizontal_bar_chart-2) - The above example bar chart re-demonstrated using the Painter API.
1114
* [line_chart-1](./line_chart-1) - Basic line chart with some simple styling changes and a demonstration of `null` values.
12-
* [line_chart-2](./line_chart-2) - Line chart with dense data and more custom styling configured.
15+
* [line_chart-2](./line_chart-2) - The above example line chart re-demonstrated using the Painter API.
16+
* [line_chart-3](./line_chart-3) - Line chart with dense data and more custom styling configured.
1317
* [line_chart-area](./line_chart-area) - Example line chart with the area below the line shaded.
1418
* [multiple_charts-1](./multiple_charts-1) - Example of manually building a painter so that you can render 4 charts on the same image.
1519
* [multiple_charts-2](./multiple_charts-2) - Combining two charts together by writting one chart over the other.
1620
* [multiple_charts-3](./multiple_charts-3) - An alternative API for overlaying two charts together in the same image.
1721
* [pie_chart-1](./pie_chart-1) - Pie chart with a variety of customization demonstrated including positioning the legend in the bottom right corner.
22+
* [pie_chart-2](./pie_chart-2) - The above example pie chart re-demonstrated using the Painter API.
1823
* [radar_chart-1](./radar_chart-1) - Basic radar chart.
24+
* [radar_chart-2](./radar_chart-2) - The above example radar chart re-demonstrated using the Painter API.
1925
* [table-1](./table-1) - Table with a variety of table specific configuration and styling demonstrated.
2026
* [web-1](./web-1) - Hosts an example http server which will render the charts to the web page.
2127

examples/bar_chart-1/main.go

Lines changed: 5 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -17,61 +17,23 @@ func writeFile(buf []byte) error {
1717
return err
1818
}
1919

20-
file := filepath.Join(tmpPath, "bar-chart.png")
20+
file := filepath.Join(tmpPath, "bar-chart-1.png")
2121
return os.WriteFile(file, buf, 0600)
2222
}
2323

2424
func main() {
2525
values := [][]float64{
26-
{
27-
2.0,
28-
4.9,
29-
7.0,
30-
23.2,
31-
25.6,
32-
76.7,
33-
135.6,
34-
162.2,
35-
32.6,
36-
20.0,
37-
6.4,
38-
3.3,
39-
},
40-
{
41-
2.6,
42-
5.9,
43-
9.0,
44-
26.4,
45-
28.7,
46-
70.7,
47-
175.6,
48-
182.2,
49-
48.7,
50-
18.8,
51-
6.0,
52-
2.3,
53-
},
26+
{2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3},
27+
{2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3},
5428
}
5529
p, err := charts.BarRender(
5630
values,
5731
charts.XAxisDataOptionFunc([]string{
58-
"Jan",
59-
"Feb",
60-
"Mar",
61-
"Apr",
62-
"May",
63-
"Jun",
64-
"Jul",
65-
"Aug",
66-
"Sep",
67-
"Oct",
68-
"Nov",
69-
"Dec",
32+
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
7033
}),
7134
charts.LegendOptionFunc(charts.LegendOption{
7235
Data: []string{
73-
"Rainfall",
74-
"Evaporation",
36+
"Rainfall", "Evaporation",
7537
},
7638
Offset: charts.OffsetRight,
7739
OverlayChart: charts.True(),

examples/bar_chart-2/main.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package main
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
7+
"github.com/go-analyze/charts"
8+
)
9+
10+
/*
11+
Example bar chart with a variety of basic configuration options shown using the Painter API.
12+
*/
13+
14+
func writeFile(buf []byte) error {
15+
tmpPath := "./tmp"
16+
if err := os.MkdirAll(tmpPath, 0700); err != nil {
17+
return err
18+
}
19+
20+
file := filepath.Join(tmpPath, "bar-chart-2.png")
21+
return os.WriteFile(file, buf, 0600)
22+
}
23+
24+
func main() {
25+
values := [][]float64{
26+
{2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3},
27+
{2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3},
28+
}
29+
30+
opt := charts.BarChartOption{}
31+
opt.SeriesList = charts.NewSeriesListDataFromValues(values, charts.ChartTypeBar)
32+
opt.XAxis.Data = []string{
33+
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
34+
}
35+
opt.XAxis.LabelCount = 12 // force label count due to the labels being very close
36+
opt.Legend = charts.LegendOption{
37+
Data: []string{"Rainfall", "Evaporation"},
38+
Offset: charts.OffsetRight,
39+
OverlayChart: charts.True(),
40+
}
41+
opt.SeriesList[0].MarkLine = charts.NewMarkLine(charts.SeriesMarkDataTypeAverage)
42+
opt.SeriesList[0].MarkPoint = charts.NewMarkPoint(
43+
charts.SeriesMarkDataTypeMax,
44+
charts.SeriesMarkDataTypeMin,
45+
)
46+
opt.SeriesList[1].MarkLine = charts.NewMarkLine(charts.SeriesMarkDataTypeAverage)
47+
opt.SeriesList[1].MarkPoint = charts.NewMarkPoint(
48+
charts.SeriesMarkDataTypeMax,
49+
charts.SeriesMarkDataTypeMin,
50+
)
51+
52+
p, err := charts.NewPainter(charts.PainterOptions{
53+
OutputFormat: charts.ChartOutputPNG,
54+
Width: 600,
55+
Height: 400,
56+
})
57+
if err != nil {
58+
panic(err)
59+
}
60+
if _, err = charts.NewBarChart(p, opt).Render(); err != nil {
61+
panic(err)
62+
}
63+
64+
if buf, err := p.Bytes(); err != nil {
65+
panic(err)
66+
} else if err = writeFile(buf); err != nil {
67+
panic(err)
68+
}
69+
}

examples/funnel_chart-1/main.go

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,18 @@ func writeFile(buf []byte) error {
1717
return err
1818
}
1919

20-
file := filepath.Join(tmpPath, "funnel-chart.png")
20+
file := filepath.Join(tmpPath, "funnel-chart-1.png")
2121
return os.WriteFile(file, buf, 0600)
2222
}
2323

2424
func main() {
25-
values := []float64{
26-
100,
27-
80,
28-
60,
29-
40,
30-
20,
31-
10,
32-
2,
33-
}
25+
values := []float64{100, 80, 60, 40, 20, 10, 2}
26+
3427
p, err := charts.FunnelRender(
3528
values,
3629
charts.TitleTextOptionFunc("Funnel"),
3730
charts.LegendLabelsOptionFunc([]string{
38-
"Show",
39-
"Click",
40-
"Visit",
41-
"Inquiry",
42-
"Order",
43-
"Pay",
44-
"Cancel",
31+
"Show", "Click", "Visit", "Inquiry", "Order", "Pay", "Cancel",
4532
}),
4633
func(opt *charts.ChartOption) {
4734
opt.Legend.Padding = charts.Box{Left: 100}

examples/funnel_chart-2/main.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package main
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
7+
"github.com/go-analyze/charts"
8+
)
9+
10+
/*
11+
Example funnel chart with a variety of basic configuration options shown using the Painter API.
12+
*/
13+
14+
func writeFile(buf []byte) error {
15+
tmpPath := "./tmp"
16+
if err := os.MkdirAll(tmpPath, 0700); err != nil {
17+
return err
18+
}
19+
20+
file := filepath.Join(tmpPath, "funnel-chart-2.png")
21+
return os.WriteFile(file, buf, 0600)
22+
}
23+
24+
func main() {
25+
values := []float64{100, 80, 60, 40, 20, 10, 2}
26+
27+
opt := charts.FunnelChartOption{}
28+
opt.SeriesList = charts.NewFunnelSeriesList(values)
29+
opt.Title.Text = "Funnel"
30+
opt.Legend.Data = []string{
31+
"Show", "Click", "Visit", "Inquiry", "Order", "Pay", "Cancel",
32+
}
33+
opt.Legend.Padding = charts.Box{Left: 100}
34+
35+
p, err := charts.NewPainter(charts.PainterOptions{
36+
OutputFormat: charts.ChartOutputPNG,
37+
Width: 600,
38+
Height: 400,
39+
})
40+
if err != nil {
41+
panic(err)
42+
}
43+
if _, err = charts.NewFunnelChart(p, opt).Render(); err != nil {
44+
panic(err)
45+
}
46+
47+
if buf, err := p.Bytes(); err != nil {
48+
panic(err)
49+
} else if err = writeFile(buf); err != nil {
50+
panic(err)
51+
}
52+
}

examples/horizontal_bar_chart-1/main.go

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,14 @@ func writeFile(buf []byte) error {
1717
return err
1818
}
1919

20-
file := filepath.Join(tmpPath, "horizontal-bar-chart.png")
20+
file := filepath.Join(tmpPath, "horizontal-bar-chart-1.png")
2121
return os.WriteFile(file, buf, 0600)
2222
}
2323

2424
func main() {
2525
values := [][]float64{
26-
{
27-
10,
28-
30,
29-
50,
30-
70,
31-
90,
32-
110,
33-
130,
34-
},
35-
{
36-
20,
37-
40,
38-
60,
39-
80,
40-
100,
41-
120,
42-
140,
43-
},
26+
{10, 30, 50, 70, 90, 110, 130},
27+
{20, 40, 60, 80, 100, 120, 140},
4428
}
4529
p, err := charts.HorizontalBarRender(
4630
values,
@@ -52,17 +36,10 @@ func main() {
5236
Left: 20,
5337
}),
5438
charts.LegendLabelsOptionFunc([]string{
55-
"2011",
56-
"2012",
39+
"2011", "2012",
5740
}),
5841
charts.YAxisDataOptionFunc([]string{
59-
"UN",
60-
"Brazil",
61-
"Indonesia",
62-
"USA",
63-
"India",
64-
"China",
65-
"World",
42+
"UN", "Brazil", "Indonesia", "USA", "India", "China", "World",
6643
}),
6744
)
6845
if err != nil {
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package main
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
7+
"github.com/go-analyze/charts"
8+
)
9+
10+
/*
11+
Example horizontal bar chart with a variety of basic configuration options shown using the Painter API.
12+
*/
13+
14+
func writeFile(buf []byte) error {
15+
tmpPath := "./tmp"
16+
if err := os.MkdirAll(tmpPath, 0700); err != nil {
17+
return err
18+
}
19+
20+
file := filepath.Join(tmpPath, "horizontal-bar-chart-2.png")
21+
return os.WriteFile(file, buf, 0600)
22+
}
23+
24+
func main() {
25+
values := [][]float64{
26+
{10, 30, 50, 70, 90, 110, 130},
27+
{20, 40, 60, 80, 100, 120, 140},
28+
}
29+
30+
opt := charts.HorizontalBarChartOption{}
31+
opt.SeriesList = charts.NewSeriesListDataFromValues(values, charts.ChartTypeHorizontalBar)
32+
opt.Title.Text = "World Population"
33+
opt.Padding = charts.Box{
34+
Top: 20,
35+
Right: 40,
36+
Bottom: 20,
37+
Left: 20,
38+
}
39+
opt.Legend.Data = []string{
40+
"2011", "2012",
41+
}
42+
opt.YAxis = []charts.YAxisOption{
43+
{
44+
Data: []string{
45+
"UN", "Brazil", "Indonesia", "USA", "India", "China", "World",
46+
},
47+
},
48+
}
49+
50+
p, err := charts.NewPainter(charts.PainterOptions{
51+
OutputFormat: charts.ChartOutputPNG,
52+
Width: 600,
53+
Height: 400,
54+
})
55+
if err != nil {
56+
panic(err)
57+
}
58+
if _, err = charts.NewHorizontalBarChart(p, opt).Render(); err != nil {
59+
panic(err)
60+
}
61+
62+
if buf, err := p.Bytes(); err != nil {
63+
panic(err)
64+
} else if err = writeFile(buf); err != nil {
65+
panic(err)
66+
}
67+
}

0 commit comments

Comments
 (0)