Skip to content

Commit

Permalink
pie charts!
Browse files Browse the repository at this point in the history
  • Loading branch information
wcharczuk committed Jul 28, 2016
1 parent ec4d92f commit c17c9a4
Show file tree
Hide file tree
Showing 10 changed files with 467 additions and 27 deletions.
86 changes: 68 additions & 18 deletions defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,43 +65,86 @@ const (
DefaultPercentValueFormat = "%0.2f%%"
)

var (
// ColorWhite is white.
ColorWhite = drawing.Color{R: 255, G: 255, B: 255, A: 255}
// ColorBlue is the basic theme blue color.
ColorBlue = drawing.Color{R: 0, G: 116, B: 217, A: 255}
// ColorCyan is the basic theme cyan color.
ColorCyan = drawing.Color{R: 0, G: 217, B: 210, A: 255}
// ColorGreen is the basic theme green color.
ColorGreen = drawing.Color{R: 0, G: 217, B: 101, A: 255}
// ColorRed is the basic theme red color.
ColorRed = drawing.Color{R: 217, G: 0, B: 116, A: 255}
// ColorOrange is the basic theme orange color.
ColorOrange = drawing.Color{R: 217, G: 101, B: 0, A: 255}
// ColorYellow is the basic theme yellow color.
ColorYellow = drawing.Color{R: 217, G: 210, B: 0, A: 255}
// ColorBlack is the basic theme black color.
ColorBlack = drawing.Color{R: 51, G: 51, B: 51, A: 255}
// ColorLightGray is the basic theme light gray color.
ColorLightGray = drawing.Color{R: 239, G: 239, B: 239, A: 255}

// ColorAlternateBlue is a alternate theme color.
ColorAlternateBlue = drawing.Color{R: 106, G: 195, B: 203, A: 255}
// ColorAlternateGreen is a alternate theme color.
ColorAlternateGreen = drawing.Color{R: 42, G: 190, B: 137, A: 255}
// ColorAlternateGray is a alternate theme color.
ColorAlternateGray = drawing.Color{R: 110, G: 128, B: 139, A: 255}
// ColorAlternateYellow is a alternate theme color.
ColorAlternateYellow = drawing.Color{R: 240, G: 174, B: 90, A: 255}
// ColorAlternateLightGray is a alternate theme color.
ColorAlternateLightGray = drawing.Color{R: 187, G: 190, B: 191, A: 255}
)

var (
// DefaultBackgroundColor is the default chart background color.
// It is equivalent to css color:white.
DefaultBackgroundColor = drawing.Color{R: 255, G: 255, B: 255, A: 255}
DefaultBackgroundColor = ColorWhite
// DefaultBackgroundStrokeColor is the default chart border color.
// It is equivalent to color:white.
DefaultBackgroundStrokeColor = drawing.Color{R: 255, G: 255, B: 255, A: 255}
DefaultBackgroundStrokeColor = ColorWhite
// DefaultCanvasColor is the default chart canvas color.
// It is equivalent to css color:white.
DefaultCanvasColor = drawing.Color{R: 255, G: 255, B: 255, A: 255}
DefaultCanvasColor = ColorWhite
// DefaultCanvasStrokeColor is the default chart canvas stroke color.
// It is equivalent to css color:white.
DefaultCanvasStrokeColor = drawing.Color{R: 255, G: 255, B: 255, A: 255}
DefaultCanvasStrokeColor = ColorWhite
// DefaultTextColor is the default chart text color.
// It is equivalent to #333333.
DefaultTextColor = drawing.Color{R: 51, G: 51, B: 51, A: 255}
DefaultTextColor = ColorBlack
// DefaultAxisColor is the default chart axis line color.
// It is equivalent to #333333.
DefaultAxisColor = drawing.Color{R: 51, G: 51, B: 51, A: 255}
DefaultAxisColor = ColorBlack
// DefaultStrokeColor is the default chart border color.
// It is equivalent to #efefef.
DefaultStrokeColor = drawing.Color{R: 239, G: 239, B: 239, A: 255}
DefaultStrokeColor = ColorLightGray
// DefaultFillColor is the default fill color.
// It is equivalent to #0074d9.
DefaultFillColor = drawing.Color{R: 0, G: 217, B: 116, A: 255}
DefaultFillColor = ColorBlue
// DefaultAnnotationFillColor is the default annotation background color.
DefaultAnnotationFillColor = drawing.Color{R: 255, G: 255, B: 255, A: 255}
DefaultAnnotationFillColor = ColorWhite
// DefaultGridLineColor is the default grid line color.
DefaultGridLineColor = drawing.Color{R: 239, G: 239, B: 239, A: 255}
DefaultGridLineColor = ColorLightGray
)

var (
// DefaultSeriesStrokeColors are a couple default series colors.
DefaultSeriesStrokeColors = []drawing.Color{
{R: 0, G: 116, B: 217, A: 255},
{R: 0, G: 217, B: 116, A: 255},
{R: 217, G: 0, B: 116, A: 255},
// DefaultColors are a couple default series colors.
DefaultColors = []drawing.Color{
ColorBlue,
ColorGreen,
ColorRed,
ColorCyan,
ColorOrange,
}

// DefaultAlternateColors are a couple alternate colors.
DefaultAlternateColors = []drawing.Color{
ColorAlternateBlue,
ColorAlternateGreen,
ColorAlternateGray,
ColorAlternateYellow,
ColorAlternateLightGray,
}
)

Expand All @@ -117,10 +160,17 @@ var (
)

// GetDefaultSeriesStrokeColor returns a color from the default list by index.
// NOTE: the index will wrap around (using a modulo).g
// NOTE: the index will wrap around (using a modulo).
func GetDefaultSeriesStrokeColor(index int) drawing.Color {
finalIndex := index % len(DefaultSeriesStrokeColors)
return DefaultSeriesStrokeColors[finalIndex]
finalIndex := index % len(DefaultColors)
return DefaultColors[finalIndex]
}

// GetDefaultPieChartValueColor returns a color from the default list by index.
// NOTE: the index will wrap around (using a modulo).
func GetDefaultPieChartValueColor(index int) drawing.Color {
finalIndex := index % len(DefaultAlternateColors)
return DefaultAlternateColors[finalIndex]
}

var (
Expand Down
8 changes: 4 additions & 4 deletions drawing/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ func (p *Path) CubicCurveTo(cx1, cy1, cx2, cy2, x, y float64) {
}

// ArcTo adds an arc to the path
func (p *Path) ArcTo(cx, cy, rx, ry, startAngle, angle float64) {
endAngle := startAngle + angle
func (p *Path) ArcTo(cx, cy, rx, ry, startAngle, delta float64) {
endAngle := startAngle + delta
clockWise := true
if angle < 0 {
if delta < 0 {
clockWise = false
}
// normalize
Expand All @@ -124,7 +124,7 @@ func (p *Path) ArcTo(cx, cy, rx, ry, startAngle, angle float64) {
} else {
p.MoveTo(startX, startY)
}
p.appendToPath(ArcToComponent, cx, cy, rx, ry, startAngle, angle)
p.appendToPath(ArcToComponent, cx, cy, rx, ry, startAngle, delta)
p.x = cx + math.Cos(endAngle)*rx
p.y = cy + math.Sin(endAngle)*ry
}
Expand Down
4 changes: 2 additions & 2 deletions drawing/stack_graphic_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ func (gc *StackGraphicContext) CubicCurveTo(cx1, cy1, cx2, cy2, x, y float64) {
}

// ArcTo draws an arc.
func (gc *StackGraphicContext) ArcTo(cx, cy, rx, ry, startAngle, angle float64) {
gc.current.Path.ArcTo(cx, cy, rx, ry, startAngle, angle)
func (gc *StackGraphicContext) ArcTo(cx, cy, rx, ry, startAngle, delta float64) {
gc.current.Path.ArcTo(cx, cy, rx, ry, startAngle, delta)
}

// Close closes a path.
Expand Down
35 changes: 35 additions & 0 deletions examples/pie_chart/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"fmt"
"log"
"net/http"

"github.com/wcharczuk/go-chart"
)

func drawChart(res http.ResponseWriter, req *http.Request) {
pie := chart.PieChart{
Canvas: chart.Style{
FillColor: chart.ColorLightGray,
},
Values: []chart.PieChartValue{
{Value: 0.3, Label: "Blue"},
{Value: 0.2, Label: "Green"},
{Value: 0.2, Label: "Gray"},
{Value: 0.1, Label: "Orange"},
{Value: 0.1, Label: "??"},
},
}

res.Header().Set("Content-Type", "image/png")
err := pie.Render(chart.PNG, res)
if err != nil {
fmt.Printf("Error rendering pie chart: %v\n", err)
}
}

func main() {
http.HandleFunc("/", drawChart)
log.Fatal(http.ListenAndServe(":8080", nil))
}
Loading

0 comments on commit c17c9a4

Please sign in to comment.