Skip to content

Commit 3d78c32

Browse files
authored
[US-25] Update tick calculation to show nicer rounded value (#4)
* add license loading in examples * update examples dependecy * add rounded tick label calculation for float type label only * update example results * update max tick rendering * handle descending ticks order * update example resuls * fix typo * add unit test * add pdf to image generator in examples folder * fix shifted x ticks * rename preview image file name * update readme file * separate example module * remove dependency to unipdf * auto crop preview image and use fixed time in code * fix y axis and tick position * replace float tick label check * revert go mod changes * go mod tidy * render y axis line after rendering chart series
1 parent 570b9b5 commit 3d78c32

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

106 files changed

+866
-44
lines changed

README.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,9 @@ For usage and output samples, see the [examples](examples) directory.
3838

3939
![Sample donut chart output](examples/donut_chart/preview.png)
4040

41-
### Linear Progress Bar
41+
### Progress Bar
4242

43-
![Sample linear progress bar](examples/progress_bar/preview_linear.png)
44-
45-
### Circular Progress Bar
46-
47-
![Sample circular progress bar](examples/progress_bar/preview_circular.png)
43+
![Sample progress bar](examples/progress_bar/preview.png)
4844

4945
# License
5046

bar_chart.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,17 @@ func (bc *BarChart) Render(rp render.RendererProvider, w io.Writer) error {
128128

129129
if bc.hasAxes() {
130130
yt = bc.getAxesTicks(r, yr, yf)
131+
132+
// Adjust domain range before adjusting the canvas box
133+
// if the generated max tick value is exceeding the original max range.
134+
if yr.IsDescending() {
135+
yr.SetMax(yt[0].Value)
136+
} else {
137+
yr.SetMax(yt[len(yt)-1].Value)
138+
}
139+
140+
yr = bc.setRangeDomains(canvasBox, yr)
141+
131142
canvasBox = bc.getAdjustedCanvasBox(r, canvasBox, yr, yt)
132143
yr = bc.setRangeDomains(canvasBox, yr)
133144
}

chart.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,32 @@ func (c *Chart) Render(rp render.RendererProvider, w io.Writer) error {
114114

115115
if c.hasAxes() {
116116
xt, yt, yta = c.getAxesTicks(r, xr, yr, yra, xf, yf, yfa)
117+
118+
// Adjust domain range before adjusting the canvas box
119+
// if the generated max tick value is exceeding the original max range.
120+
firstXTick := xt[0].Value
121+
lastXTick := xt[len(xt)-1].Value
122+
firstYTick := yt[0].Value
123+
lastYTick := yt[len(yt)-1].Value
124+
125+
if xr.IsDescending() {
126+
xr.SetMin(lastXTick)
127+
xr.SetMax(firstXTick)
128+
} else {
129+
xr.SetMin(firstXTick)
130+
xr.SetMax(lastXTick)
131+
}
132+
133+
if yr.IsDescending() {
134+
yr.SetMin(lastYTick)
135+
yr.SetMax(firstYTick)
136+
} else {
137+
yr.SetMin(firstYTick)
138+
yr.SetMax(lastYTick)
139+
}
140+
141+
xr, yr, yra = c.setRangeDomains(canvasBox, xr, yr, yra)
142+
117143
canvasBox = c.getAxesAdjustedCanvasBox(r, canvasBox, xr, yr, yra, xt, yt, yta)
118144
xr, yr, yra = c.setRangeDomains(canvasBox, xr, yr, yra)
119145

@@ -135,6 +161,7 @@ func (c *Chart) Render(rp render.RendererProvider, w io.Writer) error {
135161
c.drawSeries(r, canvasBox, xr, yr, yra, series, index)
136162
}
137163

164+
c.drawYAxisLine(r, canvasBox, xr, yr, yra, xt, yt, yta)
138165
c.drawTitle(r)
139166

140167
for _, a := range c.Elements {
@@ -473,6 +500,15 @@ func (c *Chart) drawAxes(r render.Renderer, canvasBox render.Box, xrange, yrange
473500
}
474501
}
475502

503+
func (c *Chart) drawYAxisLine(r render.Renderer, canvasBox render.Box, xrange, yrange, yrangeAlt sequence.Range, xticks, yticks, yticksAlt []Tick) {
504+
if !c.YAxis.Style.Hidden {
505+
c.YAxis.RenderAxisLine(r, canvasBox, yrange, c.styleDefaultsAxes(), yticks)
506+
}
507+
if !c.YAxisSecondary.Style.Hidden {
508+
c.YAxisSecondary.RenderAxisLine(r, canvasBox, yrangeAlt, c.styleDefaultsAxes(), yticksAlt)
509+
}
510+
}
511+
476512
func (c *Chart) drawSeries(r render.Renderer, canvasBox render.Box, xrange, yrange, yrangeAlt sequence.Range, s dataset.Series, seriesIndex int) {
477513
if !s.GetStyle().Hidden {
478514
if s.GetYAxis() == dataset.YAxisPrimary {

examples/annotations/main.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,24 @@ package main
22

33
import (
44
"log"
5+
"os"
56

67
"github.com/unidoc/unichart"
78
"github.com/unidoc/unichart/dataset"
9+
"github.com/unidoc/unichart/examples"
10+
"github.com/unidoc/unipdf/v3/common/license"
811
"github.com/unidoc/unipdf/v3/creator"
912
)
1013

14+
func init() {
15+
// Make sure to load your metered License API key prior to using the library.
16+
// If you need a key, you can sign up and create a free one at https://cloud.unidoc.io
17+
err := license.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`))
18+
if err != nil {
19+
panic(err)
20+
}
21+
}
22+
1123
func main() {
1224
// Create chart component.
1325
chart := &unichart.Chart{
@@ -42,4 +54,6 @@ func main() {
4254
if err := c.WriteToFile("output.pdf"); err != nil {
4355
log.Fatalf("failed to write output file: %v", err)
4456
}
57+
58+
examples.RenderPDFToImage("output.pdf")
4559
}

examples/annotations/output.pdf

-2.06 KB
Binary file not shown.

examples/annotations/preview.png

69 KB
Loading

examples/axes/main.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,25 @@ package main
22

33
import (
44
"log"
5+
"os"
56

67
"github.com/unidoc/unichart"
78
"github.com/unidoc/unichart/dataset"
9+
"github.com/unidoc/unichart/examples"
810
"github.com/unidoc/unichart/render"
11+
"github.com/unidoc/unipdf/v3/common/license"
912
"github.com/unidoc/unipdf/v3/creator"
1013
)
1114

15+
func init() {
16+
// Make sure to load your metered License API key prior to using the library.
17+
// If you need a key, you can sign up and create a free one at https://cloud.unidoc.io
18+
err := license.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`))
19+
if err != nil {
20+
panic(err)
21+
}
22+
}
23+
1224
func main() {
1325
// Create chart component.
1426
chart := &unichart.Chart{
@@ -35,4 +47,6 @@ func main() {
3547
if err := c.WriteToFile("output.pdf"); err != nil {
3648
log.Fatalf("failed to write output file: %v", err)
3749
}
50+
51+
examples.RenderPDFToImage("output.pdf")
3852
}

examples/axes/output.pdf

-2.38 KB
Binary file not shown.

examples/axes/preview.png

61 KB
Loading

examples/axes_labels/main.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,25 @@ package main
33
import (
44
"image/color"
55
"log"
6+
"os"
67

78
"github.com/unidoc/unichart"
89
"github.com/unidoc/unichart/dataset"
10+
"github.com/unidoc/unichart/examples"
911
"github.com/unidoc/unichart/render"
12+
"github.com/unidoc/unipdf/v3/common/license"
1013
"github.com/unidoc/unipdf/v3/creator"
1114
)
1215

16+
func init() {
17+
// Make sure to load your metered License API key prior to using the library.
18+
// If you need a key, you can sign up and create a free one at https://cloud.unidoc.io
19+
err := license.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`))
20+
if err != nil {
21+
panic(err)
22+
}
23+
}
24+
1325
func main() {
1426
// Create chart component.
1527
chart := &unichart.Chart{
@@ -45,4 +57,6 @@ func main() {
4557
if err := c.WriteToFile("output.pdf"); err != nil {
4658
log.Fatalf("failed to write output file: %v", err)
4759
}
60+
61+
examples.RenderPDFToImage("output.pdf")
4862
}

0 commit comments

Comments
 (0)