Merge branch 'pr/123'
This commit is contained in:
commit
0f525b821c
2 changed files with 73 additions and 0 deletions
58
_examples/bar_chart/main.go
Normal file
58
_examples/bar_chart/main.go
Normal file
|
|
@ -0,0 +1,58 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/wcharczuk/go-chart"
|
||||||
|
)
|
||||||
|
|
||||||
|
func drawChart(res http.ResponseWriter, req *http.Request) {
|
||||||
|
sbc := chart.BarChart{
|
||||||
|
Title: "Test Bar Chart",
|
||||||
|
TitleStyle: chart.StyleShow(),
|
||||||
|
Background: chart.Style{
|
||||||
|
Padding: chart.Box{
|
||||||
|
Top: 40,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Height: 512,
|
||||||
|
Width: -1,
|
||||||
|
BarWidth: 60,
|
||||||
|
XAxis: chart.StyleShow(),
|
||||||
|
YAxis: chart.YAxis{
|
||||||
|
Style: chart.StyleShow(),
|
||||||
|
},
|
||||||
|
Bars: []chart.Value{
|
||||||
|
{Value: 5.25, Label: "Blue"},
|
||||||
|
{Value: 4.88, Label: "Green"},
|
||||||
|
{Value: 4.74, Label: "Gray"},
|
||||||
|
{Value: 3.22, Label: "Orange"},
|
||||||
|
{Value: 3, Label: "Test"},
|
||||||
|
{Value: 2.27, Label: "??"},
|
||||||
|
{Value: 1, Label: "!!"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
res.Header().Set("Content-Type", "image/png")
|
||||||
|
err := sbc.Render(chart.PNG, res)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error rendering chart: %v\n", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func port() string {
|
||||||
|
if len(os.Getenv("PORT")) > 0 {
|
||||||
|
return os.Getenv("PORT")
|
||||||
|
}
|
||||||
|
return "8080"
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
listenPort := fmt.Sprintf(":%s", port())
|
||||||
|
fmt.Printf("Listening on %s\n", listenPort)
|
||||||
|
http.HandleFunc("/", drawChart)
|
||||||
|
log.Fatal(http.ListenAndServe(listenPort, nil))
|
||||||
|
}
|
||||||
15
bar_chart.go
15
bar_chart.go
|
|
@ -60,6 +60,8 @@ func (bc BarChart) GetFont() *truetype.Font {
|
||||||
func (bc BarChart) GetWidth() int {
|
func (bc BarChart) GetWidth() int {
|
||||||
if bc.Width == 0 {
|
if bc.Width == 0 {
|
||||||
return DefaultChartWidth
|
return DefaultChartWidth
|
||||||
|
} else if bc.Width == -1 {
|
||||||
|
return bc.autoWidth()
|
||||||
}
|
}
|
||||||
return bc.Width
|
return bc.Width
|
||||||
}
|
}
|
||||||
|
|
@ -88,6 +90,19 @@ func (bc BarChart) GetBarWidth() int {
|
||||||
return bc.BarWidth
|
return bc.BarWidth
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (bc BarChart) autoWidth() (totalWidth int) {
|
||||||
|
|
||||||
|
totalWidth = 0
|
||||||
|
barWidth := bc.GetBarWidth()
|
||||||
|
barSpacing := bc.GetBarSpacing()
|
||||||
|
|
||||||
|
for _, _ = range bc.Bars {
|
||||||
|
totalWidth += barWidth + barSpacing
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Render renders the chart with the given renderer to the given io.Writer.
|
// Render renders the chart with the given renderer to the given io.Writer.
|
||||||
func (bc BarChart) Render(rp RendererProvider, w io.Writer) error {
|
func (bc BarChart) Render(rp RendererProvider, w io.Writer) error {
|
||||||
if len(bc.Bars) == 0 {
|
if len(bc.Bars) == 0 {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue