go-chart/style.go

114 lines
2.4 KiB
Go
Raw Normal View History

2016-07-07 03:54:00 +02:00
package chart
2016-07-08 07:18:53 +02:00
import (
"fmt"
"strings"
2016-07-09 19:27:47 +02:00
"github.com/wcharczuk/go-chart/drawing"
2016-07-08 07:18:53 +02:00
)
2016-07-07 03:54:00 +02:00
// Style is a simple style set.
type Style struct {
2016-07-07 23:44:03 +02:00
Show bool
2016-07-09 20:23:35 +02:00
StrokeColor drawing.Color
FillColor drawing.Color
2016-07-07 23:44:03 +02:00
StrokeWidth float64
FontSize float64
2016-07-09 20:23:35 +02:00
FontColor drawing.Color
Padding Box
2016-07-07 03:54:00 +02:00
}
// IsZero returns if the object is set or not.
func (s Style) IsZero() bool {
2016-07-09 20:23:35 +02:00
return s.StrokeColor.IsZero() && s.FillColor.IsZero() && s.StrokeWidth == 0 && s.FontSize == 0
2016-07-07 03:54:00 +02:00
}
// GetStrokeColor returns the stroke color.
2016-07-09 20:23:35 +02:00
func (s Style) GetStrokeColor(defaults ...drawing.Color) drawing.Color {
if s.StrokeColor.IsZero() {
2016-07-07 23:44:03 +02:00
if len(defaults) > 0 {
return defaults[0]
}
2016-07-09 20:23:35 +02:00
return drawing.ColorTransparent
2016-07-07 03:54:00 +02:00
}
return s.StrokeColor
}
// GetFillColor returns the fill color.
2016-07-09 20:23:35 +02:00
func (s Style) GetFillColor(defaults ...drawing.Color) drawing.Color {
if s.FillColor.IsZero() {
2016-07-07 23:44:03 +02:00
if len(defaults) > 0 {
return defaults[0]
}
2016-07-09 20:23:35 +02:00
return drawing.ColorTransparent
2016-07-07 03:54:00 +02:00
}
return s.FillColor
}
// GetStrokeWidth returns the stroke width.
2016-07-07 23:44:03 +02:00
func (s Style) GetStrokeWidth(defaults ...float64) float64 {
2016-07-07 03:54:00 +02:00
if s.StrokeWidth == 0 {
2016-07-07 23:44:03 +02:00
if len(defaults) > 0 {
return defaults[0]
}
2016-07-08 02:50:16 +02:00
return DefaultStrokeWidth
2016-07-07 03:54:00 +02:00
}
return s.StrokeWidth
}
2016-07-07 23:44:03 +02:00
// GetFontSize gets the font size.
func (s Style) GetFontSize(defaults ...float64) float64 {
if s.FontSize == 0 {
if len(defaults) > 0 {
return defaults[0]
}
return DefaultFontSize
}
return s.FontSize
}
// GetFontColor gets the font size.
2016-07-09 20:23:35 +02:00
func (s Style) GetFontColor(defaults ...drawing.Color) drawing.Color {
if s.FontColor.IsZero() {
2016-07-07 23:44:03 +02:00
if len(defaults) > 0 {
return defaults[0]
}
2016-07-09 20:23:35 +02:00
return drawing.ColorTransparent
2016-07-07 23:44:03 +02:00
}
return s.FontColor
}
2016-07-08 07:18:53 +02:00
// SVG returns the style as a svg style string.
2016-07-09 19:27:47 +02:00
func (s Style) SVG(dpi float64) string {
2016-07-08 07:18:53 +02:00
sw := s.StrokeWidth
sc := s.StrokeColor
fc := s.FillColor
fs := s.FontSize
fnc := s.FontColor
strokeWidthText := "stroke-width:0"
if sw != 0 {
strokeWidthText = "stroke-width:" + fmt.Sprintf("%d", int(sw))
}
strokeText := "stroke:none"
2016-07-09 20:23:35 +02:00
if !sc.IsZero() {
strokeText = "stroke:" + sc.String()
2016-07-08 07:18:53 +02:00
}
fillText := "fill:none"
2016-07-09 20:23:35 +02:00
if !fc.IsZero() {
fillText = "fill:" + fc.String()
2016-07-08 07:18:53 +02:00
}
fontSizeText := ""
if fs != 0 {
2016-07-09 19:27:47 +02:00
fontSizeText = "font-size:" + fmt.Sprintf("%.1fpx", drawing.PointsToPixels(dpi, fs))
2016-07-08 07:18:53 +02:00
}
2016-07-09 20:23:35 +02:00
if !fnc.IsZero() {
fillText = "fill:" + fnc.String()
2016-07-08 07:18:53 +02:00
}
return strings.Join([]string{strokeWidthText, strokeText, fillText, fontSizeText}, ";")
}