Skip to content

Commit

Permalink
fix: fix only one data of pie chart, #12
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Jul 1, 2022
1 parent f483e2a commit c862467
Showing 1 changed file with 69 additions and 78 deletions.
147 changes: 69 additions & 78 deletions pie_chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,88 +99,79 @@ func (p *pieChart) render(result *defaultRenderResult, seriesList SeriesList) (B
seriesNames = seriesList.Names()
}
theme := opt.Theme
if len(values) == 1 {

currentValue := float64(0)
prevEndX := 0
prevEndY := 0
for index, v := range values {
seriesPainter.OverrideDrawingStyle(Style{
StrokeWidth: 1,
StrokeColor: theme.GetSeriesColor(0),
FillColor: theme.GetSeriesColor(0),
StrokeColor: theme.GetSeriesColor(index),
FillColor: theme.GetSeriesColor(index),
})
seriesPainter.MoveTo(cx, cy).
Circle(radius, cx, cy)
} else {
currentValue := float64(0)
prevEndX := 0
prevEndY := 0
for index, v := range values {
seriesPainter.OverrideDrawingStyle(Style{
StrokeWidth: 1,
StrokeColor: theme.GetSeriesColor(index),
FillColor: theme.GetSeriesColor(index),
})
seriesPainter.MoveTo(cx, cy)
start := chart.PercentToRadians(currentValue/total) - math.Pi/2
currentValue += v
percent := (v / total)
delta := chart.PercentToRadians(percent)
seriesPainter.ArcTo(cx, cy, radius, radius, start, delta).
LineTo(cx, cy).
Close().
FillStroke()

series := seriesList[index]
// 是否显示label
showLabel := series.Label.Show
if !showLabel {
continue
}

// label的角度为饼块中间
angle := start + delta/2
startx := cx + int(radius*math.Cos(angle))
starty := cy + int(radius*math.Sin(angle))

endx := cx + int(labelRadius*math.Cos(angle))
endy := cy + int(labelRadius*math.Sin(angle))
// 计算是否有重叠,如果有则调整y坐标位置
if index != 0 &&
math.Abs(float64(endx-prevEndX)) < labelFontSize &&
math.Abs(float64(endy-prevEndY)) < labelFontSize {
endy -= (labelFontSize << 1)
}
prevEndX = endx
prevEndY = endy

seriesPainter.MoveTo(startx, starty)
seriesPainter.LineTo(endx, endy)
offset := labelLineWidth
if endx < cx {
offset *= -1
}
seriesPainter.MoveTo(endx, endy)
endx += offset
seriesPainter.LineTo(endx, endy)
seriesPainter.Stroke()

textStyle := Style{
FontColor: theme.GetTextColor(),
FontSize: labelFontSize,
Font: opt.Font,
}
if !series.Label.Color.IsZero() {
textStyle.FontColor = series.Label.Color
}
seriesPainter.OverrideTextStyle(textStyle)
text := NewPieLabelFormatter(seriesNames, series.Label.Formatter)(index, v, percent)
textBox := seriesPainter.MeasureText(text)
textMargin := 3
x := endx + textMargin
y := endy + textBox.Height()>>1 - 1
if offset < 0 {
textWidth := textBox.Width()
x = endx - textWidth - textMargin
}
seriesPainter.Text(text, x, y)
seriesPainter.MoveTo(cx, cy)
start := chart.PercentToRadians(currentValue/total) - math.Pi/2
currentValue += v
percent := (v / total)
delta := chart.PercentToRadians(percent)
seriesPainter.ArcTo(cx, cy, radius, radius, start, delta).
LineTo(cx, cy).
Close().
FillStroke()

series := seriesList[index]
// 是否显示label
showLabel := series.Label.Show
if !showLabel {
continue
}

// label的角度为饼块中间
angle := start + delta/2
startx := cx + int(radius*math.Cos(angle))
starty := cy + int(radius*math.Sin(angle))

endx := cx + int(labelRadius*math.Cos(angle))
endy := cy + int(labelRadius*math.Sin(angle))
// 计算是否有重叠,如果有则调整y坐标位置
if index != 0 &&
math.Abs(float64(endx-prevEndX)) < labelFontSize &&
math.Abs(float64(endy-prevEndY)) < labelFontSize {
endy -= (labelFontSize << 1)
}
prevEndX = endx
prevEndY = endy

seriesPainter.MoveTo(startx, starty)
seriesPainter.LineTo(endx, endy)
offset := labelLineWidth
if endx < cx {
offset *= -1
}
seriesPainter.MoveTo(endx, endy)
endx += offset
seriesPainter.LineTo(endx, endy)
seriesPainter.Stroke()

textStyle := Style{
FontColor: theme.GetTextColor(),
FontSize: labelFontSize,
Font: opt.Font,
}
if !series.Label.Color.IsZero() {
textStyle.FontColor = series.Label.Color
}
seriesPainter.OverrideTextStyle(textStyle)
text := NewPieLabelFormatter(seriesNames, series.Label.Formatter)(index, v, percent)
textBox := seriesPainter.MeasureText(text)
textMargin := 3
x := endx + textMargin
y := endy + textBox.Height()>>1 - 1
if offset < 0 {
textWidth := textBox.Width()
x = endx - textWidth - textMargin
}
seriesPainter.Text(text, x, y)
}

return p.p.box, nil
Expand Down

0 comments on commit c862467

Please sign in to comment.