Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change Series Styling and Themes #15

Merged
merged 3 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
)

type Box = chartdraw.Box
type Style = chartdraw.Style
type Color = drawing.Color
type FontStyle = chartdraw.FontStyle

var BoxZero = chartdraw.BoxZero

Expand Down
35 changes: 14 additions & 21 deletions axis.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"math"
"strings"

"github.com/golang/freetype/truetype"
"github.com/go-analyze/charts/chartdraw"
)

type axisPainter struct {
Expand Down Expand Up @@ -41,12 +41,8 @@ type AxisOption struct {
TickLength int
// LabelMargin specifies the margin value of each label.
LabelMargin int
// FontSize specifies the font size of each label.
FontSize float64
// Font is the font used to render each label.
Font *truetype.Font
// FontColor is the color used for text rendered.
FontColor Color
// FontStyle specifies the font configuration for each label.
FontStyle FontStyle
// SplitLineShow, set this to true will show axis split line.
SplitLineShow bool
// TextRotation are the radians for rotating the label.
Expand Down Expand Up @@ -81,14 +77,13 @@ func (a *axisPainter) Render() (Box, error) {
strokeWidth = 1
}

font := getPreferredFont(opt.Font, a.p.font)
fontColor := opt.FontColor
if fontColor.IsZero() {
fontColor = theme.GetTextColor()
fontStyle := opt.FontStyle
fontStyle.Font = getPreferredFont(fontStyle.Font, a.p.font)
if fontStyle.FontColor.IsZero() {
fontStyle.FontColor = theme.GetTextColor()
}
fontSize := opt.FontSize
if fontSize == 0 {
fontSize = defaultFontSize
if fontStyle.FontSize == 0 {
fontStyle.FontSize = defaultFontSize
}

formatter := opt.Formatter
Expand All @@ -106,14 +101,12 @@ func (a *axisPainter) Render() (Box, error) {
tickLength := getDefaultInt(opt.TickLength, 5)
labelMargin := getDefaultInt(opt.LabelMargin, 5)

style := Style{
style := chartdraw.Style{
StrokeColor: theme.GetAxisStrokeColor(),
StrokeWidth: strokeWidth,
Font: font,
FontColor: fontColor,
FontSize: fontSize,
FontStyle: fontStyle,
}
top.SetDrawingStyle(style).OverrideTextStyle(style)
top.SetDrawingStyle(style).OverrideFontStyle(style.FontStyle)

isTextRotation := opt.TextRotation != 0

Expand Down Expand Up @@ -164,8 +157,8 @@ func (a *axisPainter) Render() (Box, error) {
case PositionTop:
labelPaddingTop = 0
x1 = p.Width()
y0 = labelMargin + int(opt.FontSize)
ticksPaddingTop = int(opt.FontSize)
y0 = labelMargin + int(opt.FontStyle.FontSize)
ticksPaddingTop = int(opt.FontStyle.FontSize)
y1 = y0
orient = OrientHorizontal
case PositionLeft:
Expand Down
28 changes: 12 additions & 16 deletions bar_chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,9 @@ func (b *barChart) render(result *defaultRenderResult, seriesList SeriesList) (B

h := yRange.getHeight(item.Value)
fillColor := seriesColor
if !item.Style.FillColor.IsZero() {
fillColor = item.Style.FillColor
}
top := barMaxHeight - h

seriesPainter.OverrideDrawingStyle(Style{
seriesPainter.OverrideDrawingStyle(chartdraw.Style{
FillColor: fillColor,
})
if flagIs(true, opt.RoundedBarCaps) {
Expand Down Expand Up @@ -146,28 +143,27 @@ func (b *barChart) render(result *defaultRenderResult, seriesList SeriesList) (B
}
y := barMaxHeight - h
radians := float64(0)
fontColor := series.Label.Color
fontStyle := series.Label.FontStyle
if series.Label.Position == PositionBottom {
y = barMaxHeight
radians = -math.Pi / 2
if fontColor.IsZero() {
if fontStyle.FontColor.IsZero() {
if isLightColor(fillColor) {
fontColor = defaultLightFontColor
fontStyle.FontColor = defaultLightFontColor
} else {
fontColor = defaultDarkFontColor
fontStyle.FontColor = defaultDarkFontColor
}
}
}
labelPainter.Add(LabelValue{
Index: index,
Value: item.Value,
X: x + barWidth>>1,
Y: y,
Index: index,
Value: item.Value,
FontStyle: fontStyle,
X: x + barWidth>>1,
Y: y,
// rotate
Radians: radians,
FontColor: fontColor,
Offset: series.Label.Offset,
FontSize: series.Label.FontSize,
Radians: radians,
Offset: series.Label.Offset,
})
}

Expand Down
8 changes: 4 additions & 4 deletions chart_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ func TableOptionRender(opt TableChartOption) (*Painter, error) {
OutputFormat: opt.OutputFormat,
Width: opt.Width,
Height: 100, // is only used to calculate the height of the table
Font: opt.Font,
Font: opt.FontStyle.Font,
})
if err != nil {
return nil, err
Expand All @@ -399,9 +399,9 @@ func TableOptionRender(opt TableChartOption) (*Painter, error) {

p, err = NewPainter(PainterOptions{
OutputFormat: opt.OutputFormat,
Width: info.Width,
Height: info.Height,
Font: opt.Font,
Width: info.width,
Height: info.height,
Font: opt.FontStyle.Font,
})
if err != nil {
return nil, err
Expand Down
8 changes: 5 additions & 3 deletions chartdraw/annotation_series.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ func (as AnnotationSeries) GetYAxis() YAxisType {

func (as AnnotationSeries) annotationStyleDefaults(defaults Style) Style {
return Style{
FontColor: DefaultTextColor,
Font: defaults.Font,
FontStyle: FontStyle{
FontColor: DefaultTextColor,
Font: defaults.Font,
FontSize: DefaultAnnotationFontSize,
},
FillColor: DefaultAnnotationFillColor,
FontSize: DefaultAnnotationFontSize,
StrokeColor: defaults.StrokeColor,
StrokeWidth: defaults.StrokeWidth,
Padding: DefaultAnnotationPadding,
Expand Down
12 changes: 8 additions & 4 deletions chartdraw/annotation_series_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ func TestAnnotationSeriesMeasure(t *testing.T) {
Bottom: 105,
}
sd := Style{
FontSize: 10.0,
Font: f,
FontStyle: FontStyle{
FontSize: 10.0,
Font: f,
},
}

box := as.Measure(r, cb, xrange, yrange, sd)
Expand Down Expand Up @@ -94,8 +96,10 @@ func TestAnnotationSeriesRender(t *testing.T) {
Bottom: 105,
}
sd := Style{
FontSize: 10.0,
Font: f,
FontStyle: FontStyle{
FontSize: 10.0,
Font: f,
},
}

as.Render(r, cb, xrange, yrange, sd)
Expand Down
12 changes: 8 additions & 4 deletions chartdraw/bar_chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,10 +455,12 @@ func (bc BarChart) getTitleFontSize() float64 {

func (bc BarChart) styleDefaultsAxes() Style {
return Style{
FontStyle: FontStyle{
Font: bc.GetFont(),
FontSize: DefaultAxisFontSize,
FontColor: bc.GetColorPalette().TextColor(),
},
StrokeColor: bc.GetColorPalette().AxisStrokeColor(),
Font: bc.GetFont(),
FontSize: DefaultAxisFontSize,
FontColor: bc.GetColorPalette().TextColor(),
TextHorizontalAlign: TextHorizontalAlignCenter,
TextVerticalAlign: TextVerticalAlignTop,
TextWrap: TextWrapWord,
Expand All @@ -467,7 +469,9 @@ func (bc BarChart) styleDefaultsAxes() Style {

func (bc BarChart) styleDefaultsElements() Style {
return Style{
Font: bc.GetFont(),
FontStyle: FontStyle{
Font: bc.GetFont(),
},
}
}

Expand Down
18 changes: 12 additions & 6 deletions chartdraw/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,27 +517,33 @@ func (c Chart) styleDefaultsCanvas() Style {

func (c Chart) styleDefaultsSeries(seriesIndex int) Style {
return Style{
FontStyle: FontStyle{
Font: c.GetFont(),
FontSize: DefaultFontSize,
},
DotColor: c.GetColorPalette().GetSeriesColor(seriesIndex),
StrokeColor: c.GetColorPalette().GetSeriesColor(seriesIndex),
StrokeWidth: DefaultSeriesLineWidth,
Font: c.GetFont(),
FontSize: DefaultFontSize,
}
}

func (c Chart) styleDefaultsAxes() Style {
return Style{
Font: c.GetFont(),
FontColor: c.GetColorPalette().TextColor(),
FontSize: DefaultAxisFontSize,
FontStyle: FontStyle{
Font: c.GetFont(),
FontColor: c.GetColorPalette().TextColor(),
FontSize: DefaultAxisFontSize,
},
StrokeColor: c.GetColorPalette().AxisStrokeColor(),
StrokeWidth: DefaultAxisLineWidth,
}
}

func (c Chart) styleDefaultsElements() Style {
return Style{
Font: c.GetFont(),
FontStyle: FontStyle{
Font: c.GetFont(),
},
}
}

Expand Down
25 changes: 17 additions & 8 deletions chartdraw/donut_chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,10 @@ func (pc DonutChart) drawSlices(r Renderer, canvasBox Box, values []Value) {
//making the donut hole
v := Value{Value: 100, Label: "center"}
styletemp := pc.SliceStyle.InheritFrom(Style{
StrokeColor: ColorWhite, StrokeWidth: 4.0, FillColor: ColorWhite, FontColor: ColorWhite, //Font: pc.GetFont(),//FontSize: pc.getScaledFontSize(),
StrokeColor: ColorWhite, StrokeWidth: 4.0, FillColor: ColorWhite,
FontStyle: FontStyle{
FontColor: ColorWhite, //Font: pc.GetFont(),//FontSize: pc.getScaledFontSize(),
},
})
v.Style.InheritFrom(styletemp).WriteToRenderer(r)
r.MoveTo(cx, cy)
Expand Down Expand Up @@ -223,12 +226,14 @@ func (pc DonutChart) styleDefaultsCanvas() Style {

func (pc DonutChart) styleDonutChartValue(index int) Style {
return pc.SliceStyle.InheritFrom(Style{
FontStyle: FontStyle{
FontSize: pc.getScaledFontSize(),
FontColor: pc.GetColorPalette().TextColor(),
Font: pc.GetFont(),
},
StrokeColor: ColorWhite,
StrokeWidth: 4.0,
FillColor: pc.GetColorPalette().GetSeriesColor(index),
FontSize: pc.getScaledFontSize(),
FontColor: pc.GetColorPalette().TextColor(),
Font: pc.GetFont(),
})
}

Expand Down Expand Up @@ -256,15 +261,19 @@ func (pc DonutChart) styleDefaultsBackground() Style {

func (pc DonutChart) styleDefaultsElements() Style {
return Style{
Font: pc.GetFont(),
FontStyle: FontStyle{
Font: pc.GetFont(),
},
}
}

func (pc DonutChart) styleDefaultsTitle() Style {
return pc.TitleStyle.InheritFrom(Style{
FontColor: pc.GetColorPalette().TextColor(),
Font: pc.GetFont(),
FontSize: pc.getTitleFontSize(),
FontStyle: FontStyle{
FontColor: pc.GetColorPalette().TextColor(),
Font: pc.GetFont(),
FontSize: pc.getTitleFontSize(),
},
TextHorizontalAlign: TextHorizontalAlignCenter,
TextVerticalAlign: TextVerticalAlignTop,
TextWrap: TextWrapWord,
Expand Down
Loading