go-chart/style.go

443 lines
11 KiB
Go
Raw Permalink Normal View History

2016-07-06 21:54:00 -04:00
package chart
2016-07-08 01:18:53 -04:00
import (
"fmt"
"strings"
2016-07-09 13:27:47 -04:00
2016-07-10 04:11:47 -04:00
"github.com/golang/freetype/truetype"
2016-07-09 13:27:47 -04:00
"github.com/wcharczuk/go-chart/drawing"
2018-04-05 01:06:34 -04:00
"github.com/wcharczuk/go-chart/util"
2016-07-08 01:18:53 -04:00
)
2016-07-06 21:54:00 -04:00
const (
// Disabled indicates if the value should be interpreted as set intentionally to zero.
// this is because golang optionals aren't here yet.
Disabled = -1
)
2016-07-31 19:54:09 -04:00
// StyleShow is a prebuilt style with the `Show` property set to true.
func StyleShow() Style {
return Style{
Show: true,
}
}
2017-03-26 19:19:25 -04:00
// StyleTextDefaults returns a style for drawing outside a
// chart context.
func StyleTextDefaults() Style {
font, _ := GetDefaultFont()
return Style{
Show: true,
Font: font,
2017-03-26 19:21:52 -04:00
FontColor: DefaultTextColor,
FontSize: DefaultTitleFontSize,
2017-03-26 19:19:25 -04:00
}
}
2016-07-06 21:54:00 -04:00
// Style is a simple style set.
type Style struct {
2016-07-10 04:11:47 -04:00
Show bool
Padding Box
2016-07-11 21:48:51 -04:00
StrokeWidth float64
StrokeColor drawing.Color
StrokeDashArray []float64
DotColor drawing.Color
DotWidth float64
2017-04-14 20:43:52 -04:00
DotWidthProvider SizeProvider
DotColorProvider DotColorProvider
2017-04-14 20:43:52 -04:00
2016-07-11 21:48:51 -04:00
FillColor drawing.Color
2016-07-11 21:48:51 -04:00
FontSize float64
FontColor drawing.Color
Font *truetype.Font
2016-07-29 19:36:29 -04:00
2016-07-31 19:54:09 -04:00
TextHorizontalAlign TextHorizontalAlign
TextVerticalAlign TextVerticalAlign
TextWrap TextWrap
2016-07-29 21:24:25 -04:00
TextLineSpacing int
2016-09-05 16:26:12 -04:00
TextRotationDegrees float64 //0 is unset or normal
2016-07-06 21:54:00 -04:00
}
// IsZero returns if the object is set or not.
func (s Style) IsZero() bool {
return s.StrokeColor.IsZero() &&
s.StrokeWidth == 0 &&
s.DotColor.IsZero() &&
s.DotWidth == 0 &&
s.FillColor.IsZero() &&
s.FontColor.IsZero() &&
s.FontSize == 0 &&
s.Font == nil
2016-07-06 21:54:00 -04:00
}
// String returns a text representation of the style.
2016-07-18 14:43:41 -04:00
func (s Style) String() string {
if s.IsZero() {
return "{}"
}
var output []string
if s.Show {
output = []string{"\"show\": true"}
} else {
output = []string{"\"show\": false"}
}
if !s.Padding.IsZero() {
output = append(output, fmt.Sprintf("\"padding\": %s", s.Padding.String()))
} else {
output = append(output, "\"padding\": null")
}
if s.StrokeWidth >= 0 {
output = append(output, fmt.Sprintf("\"stroke_width\": %0.2f", s.StrokeWidth))
} else {
output = append(output, "\"stroke_width\": null")
}
if !s.StrokeColor.IsZero() {
output = append(output, fmt.Sprintf("\"stroke_color\": %s", s.StrokeColor.String()))
} else {
output = append(output, "\"stroke_color\": null")
}
if len(s.StrokeDashArray) > 0 {
var elements []string
for _, v := range s.StrokeDashArray {
elements = append(elements, fmt.Sprintf("%.2f", v))
}
dashArray := strings.Join(elements, ", ")
output = append(output, fmt.Sprintf("\"stroke_dash_array\": [%s]", dashArray))
} else {
output = append(output, "\"stroke_dash_array\": null")
}
if s.DotWidth >= 0 {
output = append(output, fmt.Sprintf("\"dot_width\": %0.2f", s.DotWidth))
} else {
output = append(output, "\"dot_width\": null")
}
if !s.DotColor.IsZero() {
output = append(output, fmt.Sprintf("\"dot_color\": %s", s.DotColor.String()))
} else {
output = append(output, "\"dot_color\": null")
}
2016-07-18 14:43:41 -04:00
if !s.FillColor.IsZero() {
output = append(output, fmt.Sprintf("\"fill_color\": %s", s.FillColor.String()))
} else {
output = append(output, "\"fill_color\": null")
}
if s.FontSize != 0 {
output = append(output, fmt.Sprintf("\"font_size\": \"%0.2fpt\"", s.FontSize))
} else {
2016-07-28 05:34:44 -04:00
output = append(output, "\"font_size\": null")
2016-07-18 14:43:41 -04:00
}
2016-07-28 05:34:44 -04:00
if !s.FontColor.IsZero() {
output = append(output, fmt.Sprintf("\"font_color\": %s", s.FontColor.String()))
2016-07-18 14:43:41 -04:00
} else {
output = append(output, "\"font_color\": null")
}
if s.Font != nil {
output = append(output, fmt.Sprintf("\"font\": \"%s\"", s.Font.Name(truetype.NameIDFontFamily)))
} else {
output = append(output, "\"font_color\": null")
}
return "{" + strings.Join(output, ", ") + "}"
}
2016-07-06 21:54:00 -04:00
// GetStrokeColor returns the stroke color.
2016-07-09 14:23:35 -04:00
func (s Style) GetStrokeColor(defaults ...drawing.Color) drawing.Color {
if s.StrokeColor.IsZero() {
2016-07-07 17:44:03 -04:00
if len(defaults) > 0 {
return defaults[0]
}
2016-07-09 14:23:35 -04:00
return drawing.ColorTransparent
2016-07-06 21:54:00 -04:00
}
return s.StrokeColor
}
// GetFillColor returns the fill color.
2016-07-09 14:23:35 -04:00
func (s Style) GetFillColor(defaults ...drawing.Color) drawing.Color {
if s.FillColor.IsZero() {
2016-07-07 17:44:03 -04:00
if len(defaults) > 0 {
return defaults[0]
}
2016-07-09 14:23:35 -04:00
return drawing.ColorTransparent
2016-07-06 21:54:00 -04:00
}
return s.FillColor
}
// GetDotColor returns the stroke color.
func (s Style) GetDotColor(defaults ...drawing.Color) drawing.Color {
if s.DotColor.IsZero() {
if len(defaults) > 0 {
return defaults[0]
}
return drawing.ColorTransparent
}
return s.DotColor
}
2016-07-06 21:54:00 -04:00
// GetStrokeWidth returns the stroke width.
2016-07-07 17:44:03 -04:00
func (s Style) GetStrokeWidth(defaults ...float64) float64 {
2016-07-06 21:54:00 -04:00
if s.StrokeWidth == 0 {
2016-07-07 17:44:03 -04:00
if len(defaults) > 0 {
return defaults[0]
}
2016-07-07 20:50:16 -04:00
return DefaultStrokeWidth
2016-07-06 21:54:00 -04:00
}
return s.StrokeWidth
}
2016-07-07 17:44:03 -04:00
// GetDotWidth returns the dot width for scatter plots.
func (s Style) GetDotWidth(defaults ...float64) float64 {
if s.DotWidth == 0 {
if len(defaults) > 0 {
return defaults[0]
}
return DefaultDotWidth
}
return s.DotWidth
}
2016-07-11 21:48:51 -04:00
// GetStrokeDashArray returns the stroke dash array.
func (s Style) GetStrokeDashArray(defaults ...[]float64) []float64 {
if len(s.StrokeDashArray) == 0 {
if len(defaults) > 0 {
return defaults[0]
}
return nil
}
return s.StrokeDashArray
}
2016-07-07 17:44:03 -04: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 14:23:35 -04:00
func (s Style) GetFontColor(defaults ...drawing.Color) drawing.Color {
if s.FontColor.IsZero() {
2016-07-07 17:44:03 -04:00
if len(defaults) > 0 {
return defaults[0]
}
2016-07-09 14:23:35 -04:00
return drawing.ColorTransparent
2016-07-07 17:44:03 -04:00
}
return s.FontColor
}
2016-07-08 01:18:53 -04:00
2016-07-10 04:11:47 -04:00
// GetFont returns the font face.
func (s Style) GetFont(defaults ...*truetype.Font) *truetype.Font {
if s.Font == nil {
if len(defaults) > 0 {
return defaults[0]
}
return nil
}
return s.Font
}
// GetPadding returns the padding.
func (s Style) GetPadding(defaults ...Box) Box {
if s.Padding.IsZero() {
if len(defaults) > 0 {
return defaults[0]
}
return Box{}
}
return s.Padding
}
2016-07-29 19:36:29 -04:00
// GetTextHorizontalAlign returns the horizontal alignment.
2016-07-31 19:54:09 -04:00
func (s Style) GetTextHorizontalAlign(defaults ...TextHorizontalAlign) TextHorizontalAlign {
2016-07-29 19:36:29 -04:00
if s.TextHorizontalAlign == TextHorizontalAlignUnset {
if len(defaults) > 0 {
return defaults[0]
}
2016-07-29 21:40:43 -04:00
return TextHorizontalAlignUnset
2016-07-29 19:36:29 -04:00
}
return s.TextHorizontalAlign
}
// GetTextVerticalAlign returns the vertical alignment.
2016-07-31 19:54:09 -04:00
func (s Style) GetTextVerticalAlign(defaults ...TextVerticalAlign) TextVerticalAlign {
2016-07-29 19:36:29 -04:00
if s.TextVerticalAlign == TextVerticalAlignUnset {
if len(defaults) > 0 {
return defaults[0]
}
2016-07-29 21:40:43 -04:00
return TextVerticalAlignUnset
2016-07-29 19:36:29 -04:00
}
return s.TextVerticalAlign
}
// GetTextWrap returns the word wrap.
2016-07-31 19:54:09 -04:00
func (s Style) GetTextWrap(defaults ...TextWrap) TextWrap {
2016-07-29 19:36:29 -04:00
if s.TextWrap == TextWrapUnset {
if len(defaults) > 0 {
return defaults[0]
}
2016-07-29 21:40:43 -04:00
return TextWrapUnset
2016-07-29 19:36:29 -04:00
}
return s.TextWrap
}
2016-07-29 21:24:25 -04:00
// GetTextLineSpacing returns the spacing in pixels between lines of text (vertically).
func (s Style) GetTextLineSpacing(defaults ...int) int {
if s.TextLineSpacing == 0 {
if len(defaults) > 0 {
return defaults[0]
}
return DefaultLineSpacing
}
return s.TextLineSpacing
}
2016-09-01 01:11:52 -04:00
// GetTextRotationDegrees returns the text rotation in degrees.
func (s Style) GetTextRotationDegrees(defaults ...float64) float64 {
if s.TextRotationDegrees == 0 {
if len(defaults) > 0 {
return defaults[0]
}
}
return s.TextRotationDegrees
}
2016-07-29 19:36:29 -04:00
// WriteToRenderer passes the style's options to a renderer.
func (s Style) WriteToRenderer(r Renderer) {
r.SetStrokeColor(s.GetStrokeColor())
r.SetStrokeWidth(s.GetStrokeWidth())
r.SetStrokeDashArray(s.GetStrokeDashArray())
r.SetFillColor(s.GetFillColor())
r.SetFont(s.GetFont())
r.SetFontColor(s.GetFontColor())
r.SetFontSize(s.GetFontSize())
2016-09-01 01:11:52 -04:00
2016-09-05 16:26:12 -04:00
r.ClearTextRotation()
if s.GetTextRotationDegrees() != 0 {
r.SetTextRotation(util.Math.DegreesToRadians(s.GetTextRotationDegrees()))
2016-09-01 01:11:52 -04:00
}
2016-07-29 19:36:29 -04:00
}
// WriteDrawingOptionsToRenderer passes just the drawing style options to a renderer.
func (s Style) WriteDrawingOptionsToRenderer(r Renderer) {
r.SetStrokeColor(s.GetStrokeColor())
r.SetStrokeWidth(s.GetStrokeWidth())
r.SetStrokeDashArray(s.GetStrokeDashArray())
2016-07-28 05:34:44 -04:00
r.SetFillColor(s.GetFillColor())
2016-07-29 19:36:29 -04:00
}
// WriteTextOptionsToRenderer passes just the text style options to a renderer.
func (s Style) WriteTextOptionsToRenderer(r Renderer) {
r.SetFont(s.GetFont())
r.SetFontColor(s.GetFontColor())
r.SetFontSize(s.GetFontSize())
}
// InheritFrom coalesces two styles into a new style.
func (s Style) InheritFrom(defaults Style) (final Style) {
2016-07-11 02:06:14 -04:00
final.StrokeColor = s.GetStrokeColor(defaults.StrokeColor)
final.StrokeWidth = s.GetStrokeWidth(defaults.StrokeWidth)
2016-07-14 12:27:23 -04:00
final.StrokeDashArray = s.GetStrokeDashArray(defaults.StrokeDashArray)
final.DotColor = s.GetDotColor(defaults.DotColor)
final.DotWidth = s.GetDotWidth(defaults.DotWidth)
2017-04-14 20:43:52 -04:00
final.DotWidthProvider = s.DotWidthProvider
final.DotColorProvider = s.DotColorProvider
2016-07-10 04:11:47 -04:00
final.FillColor = s.GetFillColor(defaults.FillColor)
final.FontColor = s.GetFontColor(defaults.FontColor)
2016-07-14 22:45:23 -04:00
final.FontSize = s.GetFontSize(defaults.FontSize)
2016-07-10 04:11:47 -04:00
final.Font = s.GetFont(defaults.Font)
final.Padding = s.GetPadding(defaults.Padding)
2016-07-29 19:36:29 -04:00
final.TextHorizontalAlign = s.GetTextHorizontalAlign(defaults.TextHorizontalAlign)
final.TextVerticalAlign = s.GetTextVerticalAlign(defaults.TextVerticalAlign)
final.TextWrap = s.GetTextWrap(defaults.TextWrap)
2016-07-29 21:24:25 -04:00
final.TextLineSpacing = s.GetTextLineSpacing(defaults.TextLineSpacing)
2016-09-01 01:11:52 -04:00
final.TextRotationDegrees = s.GetTextRotationDegrees(defaults.TextRotationDegrees)
2016-07-10 04:11:47 -04:00
return
}
2016-07-29 19:36:29 -04:00
// GetStrokeOptions returns the stroke components.
func (s Style) GetStrokeOptions() Style {
2016-07-10 21:09:41 -04:00
return Style{
StrokeDashArray: s.StrokeDashArray,
StrokeColor: s.StrokeColor,
StrokeWidth: s.StrokeWidth,
2016-07-10 21:09:41 -04:00
}
}
2016-07-29 19:36:29 -04:00
// GetFillOptions returns the fill components.
func (s Style) GetFillOptions() Style {
2016-07-10 21:09:41 -04:00
return Style{
FillColor: s.FillColor,
}
}
// GetDotOptions returns the dot components.
func (s Style) GetDotOptions() Style {
return Style{
StrokeDashArray: nil,
FillColor: s.DotColor,
StrokeColor: s.DotColor,
StrokeWidth: 1.0,
}
}
2016-07-29 19:36:29 -04:00
// GetFillAndStrokeOptions returns the fill and stroke components.
func (s Style) GetFillAndStrokeOptions() Style {
2016-07-10 21:09:41 -04:00
return Style{
StrokeDashArray: s.StrokeDashArray,
FillColor: s.FillColor,
StrokeColor: s.StrokeColor,
StrokeWidth: s.StrokeWidth,
2016-07-10 21:09:41 -04:00
}
}
2016-07-29 19:36:29 -04:00
// GetTextOptions returns just the text components of the style.
func (s Style) GetTextOptions() Style {
2016-07-10 21:09:41 -04:00
return Style{
2016-07-29 19:36:29 -04:00
FontColor: s.FontColor,
FontSize: s.FontSize,
Font: s.Font,
TextHorizontalAlign: s.TextHorizontalAlign,
TextVerticalAlign: s.TextVerticalAlign,
TextWrap: s.TextWrap,
2016-07-29 21:24:25 -04:00
TextLineSpacing: s.TextLineSpacing,
2016-09-01 01:11:52 -04:00
TextRotationDegrees: s.TextRotationDegrees,
}
}
// ShouldDrawStroke tells drawing functions if they should draw the stroke.
func (s Style) ShouldDrawStroke() bool {
return !s.StrokeColor.IsZero() && s.StrokeWidth > 0
}
// ShouldDrawDot tells drawing functions if they should draw the dot.
func (s Style) ShouldDrawDot() bool {
2017-04-14 20:43:52 -04:00
return (!s.DotColor.IsZero() && s.DotWidth > 0) || s.DotColorProvider != nil || s.DotWidthProvider != nil
}
// ShouldDrawFill tells drawing functions if they should draw the stroke.
func (s Style) ShouldDrawFill() bool {
return !s.FillColor.IsZero()
}