2026-02-12 09:00:14 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"image/color"
|
|
|
|
|
"math"
|
|
|
|
|
|
|
|
|
|
"fyne.io/fyne/v2"
|
|
|
|
|
"fyne.io/fyne/v2/canvas"
|
2026-03-08 20:37:57 -05:00
|
|
|
"fyne.io/fyne/v2/container"
|
2026-02-12 09:00:14 -06:00
|
|
|
"fyne.io/fyne/v2/widget"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// barGraphCanvas is a custom widget that renders a bar graph
|
|
|
|
|
type barGraphCanvas struct {
|
|
|
|
|
widget.BaseWidget
|
|
|
|
|
stats *DiceStatistics
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newBarGraphCanvas(stats *DiceStatistics) *barGraphCanvas {
|
|
|
|
|
graph := &barGraphCanvas{
|
|
|
|
|
stats: stats,
|
|
|
|
|
}
|
|
|
|
|
graph.ExtendBaseWidget(graph)
|
|
|
|
|
return graph
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (b *barGraphCanvas) CreateRenderer() fyne.WidgetRenderer {
|
|
|
|
|
b.ExtendBaseWidget(b)
|
|
|
|
|
return &barGraphCanvasRenderer{
|
|
|
|
|
graph: b,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type barGraphCanvasRenderer struct {
|
|
|
|
|
graph *barGraphCanvas
|
|
|
|
|
objects []fyne.CanvasObject
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *barGraphCanvasRenderer) Layout(size fyne.Size) {
|
|
|
|
|
r.Refresh()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *barGraphCanvasRenderer) MinSize() fyne.Size {
|
2026-03-24 10:18:31 -05:00
|
|
|
return fyne.NewSize(320, 240)
|
2026-02-12 09:00:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *barGraphCanvasRenderer) Refresh() {
|
|
|
|
|
r.objects = []fyne.CanvasObject{}
|
|
|
|
|
|
|
|
|
|
if r.graph.stats == nil || len(r.graph.stats.Results) == 0 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stats := r.graph.stats
|
|
|
|
|
outcomes := stats.GetSortedOutcomes()
|
|
|
|
|
maxPercentage := stats.GetMaxPercentage()
|
|
|
|
|
|
|
|
|
|
// Round up maxPercentage to nearest 5%
|
|
|
|
|
roundedMaxPercent := math.Ceil(maxPercentage/5) * 5
|
|
|
|
|
if roundedMaxPercent < 5 {
|
|
|
|
|
roundedMaxPercent = 5
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-08 20:37:57 -05:00
|
|
|
size := r.graph.Size()
|
|
|
|
|
if size.Width == 0 || size.Height == 0 {
|
|
|
|
|
size = fyne.NewSize(900, 550)
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-24 10:18:31 -05:00
|
|
|
// Padding and typography scale with the available area so the graph fits the window.
|
|
|
|
|
topPadding := clamp(size.Height*0.14, 48, 75)
|
|
|
|
|
bottomPadding := clamp(size.Height*0.15, 56, 80)
|
|
|
|
|
leftPadding := clamp(size.Width*0.12, 60, 100)
|
|
|
|
|
rightPadding := clamp(size.Width*0.03, 16, 24)
|
|
|
|
|
|
2026-03-08 20:37:57 -05:00
|
|
|
graphWidth := size.Width - leftPadding - rightPadding
|
|
|
|
|
graphHeight := size.Height - topPadding - bottomPadding
|
2026-03-24 10:18:31 -05:00
|
|
|
if graphWidth <= 0 || graphHeight <= 0 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
titleSize := clamp(size.Height*0.03, 12, 16)
|
|
|
|
|
bodyTextSize := clamp(size.Height*0.02, 9, 11)
|
|
|
|
|
labelTextSize := clamp(size.Height*0.022, 10, 12)
|
|
|
|
|
tickTextSize := clamp(size.Height*0.018, 8, 10)
|
2026-02-12 09:00:14 -06:00
|
|
|
|
|
|
|
|
// Background
|
|
|
|
|
background := canvas.NewRectangle(color.NRGBA{R: 20, G: 20, B: 20, A: 255})
|
|
|
|
|
background.Move(fyne.NewPos(0, 0))
|
2026-03-08 20:37:57 -05:00
|
|
|
background.Resize(size)
|
2026-02-12 09:00:14 -06:00
|
|
|
r.objects = append(r.objects, background)
|
|
|
|
|
|
|
|
|
|
// Y-axis
|
|
|
|
|
yAxisLine := canvas.NewLine(color.White)
|
|
|
|
|
yAxisLine.StrokeWidth = 2
|
|
|
|
|
yAxisLine.Move(fyne.NewPos(leftPadding, topPadding))
|
|
|
|
|
yAxisLine.Resize(fyne.NewSize(0, graphHeight))
|
|
|
|
|
r.objects = append(r.objects, yAxisLine)
|
|
|
|
|
|
|
|
|
|
// X-axis
|
|
|
|
|
xAxisLine := canvas.NewLine(color.White)
|
|
|
|
|
xAxisLine.StrokeWidth = 2
|
|
|
|
|
xAxisLine.Move(fyne.NewPos(leftPadding, topPadding+graphHeight))
|
|
|
|
|
xAxisLine.Resize(fyne.NewSize(graphWidth, 0))
|
|
|
|
|
r.objects = append(r.objects, xAxisLine)
|
|
|
|
|
|
|
|
|
|
// Title
|
2026-02-12 09:10:42 -06:00
|
|
|
title := canvas.NewText("Probability Distribution", color.White)
|
2026-03-24 10:18:31 -05:00
|
|
|
title.TextSize = titleSize
|
2026-02-12 09:10:42 -06:00
|
|
|
title.Move(fyne.NewPos(leftPadding, 5))
|
2026-02-12 09:00:14 -06:00
|
|
|
r.objects = append(r.objects, title)
|
|
|
|
|
|
2026-02-12 09:10:42 -06:00
|
|
|
// Statistics info line 1
|
|
|
|
|
statsLine1 := canvas.NewText(fmt.Sprintf("Range: %d to %d | Total Outcomes: %d", stats.MinValue, stats.MaxValue, stats.Total), color.White)
|
2026-03-24 10:18:31 -05:00
|
|
|
statsLine1.TextSize = bodyTextSize
|
|
|
|
|
statsLine1.Move(fyne.NewPos(leftPadding, 5+title.MinSize().Height))
|
2026-02-12 09:10:42 -06:00
|
|
|
r.objects = append(r.objects, statsLine1)
|
|
|
|
|
|
|
|
|
|
// Statistics info line 2
|
|
|
|
|
statsLine2 := canvas.NewText(fmt.Sprintf("Average: %.2f | Most Common: %d", stats.Average, stats.MostCommon), color.White)
|
2026-03-24 10:18:31 -05:00
|
|
|
statsLine2.TextSize = bodyTextSize
|
|
|
|
|
statsLine2.Move(fyne.NewPos(leftPadding, 5+title.MinSize().Height+statsLine1.MinSize().Height))
|
2026-02-12 09:10:42 -06:00
|
|
|
r.objects = append(r.objects, statsLine2)
|
|
|
|
|
|
2026-02-12 09:00:14 -06:00
|
|
|
// Y-axis label
|
|
|
|
|
yLabel := canvas.NewText("Probability (%)", color.White)
|
2026-03-24 10:18:31 -05:00
|
|
|
yLabel.TextSize = labelTextSize
|
|
|
|
|
yLabel.Move(fyne.NewPos(clamp(leftPadding*0.15, 8, 15), topPadding+graphHeight/2-yLabel.MinSize().Height/2))
|
2026-02-12 09:00:14 -06:00
|
|
|
r.objects = append(r.objects, yLabel)
|
|
|
|
|
|
|
|
|
|
// X-axis label
|
|
|
|
|
xLabel := canvas.NewText("Result Value", color.White)
|
2026-03-24 10:18:31 -05:00
|
|
|
xLabel.TextSize = labelTextSize
|
|
|
|
|
xLabel.Move(fyne.NewPos(leftPadding+graphWidth/2-xLabel.MinSize().Width/2, topPadding+graphHeight+clamp(bottomPadding*0.35, 18, 28)))
|
2026-02-12 09:00:14 -06:00
|
|
|
r.objects = append(r.objects, xLabel)
|
|
|
|
|
|
|
|
|
|
// Y-axis tick marks and labels
|
|
|
|
|
numYTicks := int(roundedMaxPercent/5) + 1
|
|
|
|
|
for i := 0; i <= numYTicks; i++ {
|
|
|
|
|
percent := float64(i) * 5
|
|
|
|
|
|
|
|
|
|
yPos := topPadding + graphHeight - (float32(percent/roundedMaxPercent) * graphHeight)
|
|
|
|
|
|
|
|
|
|
// Tick mark
|
|
|
|
|
tick := canvas.NewLine(color.White)
|
|
|
|
|
tick.StrokeWidth = 1
|
|
|
|
|
tick.Move(fyne.NewPos(leftPadding-5, yPos))
|
|
|
|
|
tick.Resize(fyne.NewSize(5, 0))
|
|
|
|
|
r.objects = append(r.objects, tick)
|
|
|
|
|
|
|
|
|
|
// Label
|
|
|
|
|
label := canvas.NewText(fmt.Sprintf("%.0f%%", percent), color.White)
|
2026-03-24 10:18:31 -05:00
|
|
|
label.TextSize = tickTextSize
|
2026-02-12 09:00:14 -06:00
|
|
|
label.Move(fyne.NewPos(leftPadding-50, yPos-7))
|
|
|
|
|
r.objects = append(r.objects, label)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Draw bars
|
|
|
|
|
numBars := len(outcomes)
|
|
|
|
|
barSpacing := float32(2)
|
2026-03-24 10:18:31 -05:00
|
|
|
totalSpacing := float32(numBars+1) * barSpacing
|
|
|
|
|
barWidth := (graphWidth - totalSpacing) / float32(numBars)
|
|
|
|
|
if barWidth < 1 {
|
|
|
|
|
barWidth = 1
|
|
|
|
|
}
|
2026-02-12 09:00:14 -06:00
|
|
|
|
2026-02-20 14:40:29 -06:00
|
|
|
// Calculate label step to prevent overlapping
|
|
|
|
|
labelStep := calculateLabelStep(graphWidth, numBars)
|
|
|
|
|
|
2026-02-12 09:00:14 -06:00
|
|
|
for i, value := range outcomes {
|
|
|
|
|
percentage := stats.Percentages[value]
|
|
|
|
|
|
|
|
|
|
// Bar height proportional to percentage
|
|
|
|
|
barHeight := (float32(percentage) / float32(roundedMaxPercent)) * graphHeight
|
|
|
|
|
|
|
|
|
|
// X position
|
2026-03-24 10:18:31 -05:00
|
|
|
xPos := leftPadding + barSpacing + float32(i)*(barWidth+barSpacing)
|
2026-02-12 09:00:14 -06:00
|
|
|
|
|
|
|
|
// Draw bar
|
|
|
|
|
bar := canvas.NewRectangle(color.NRGBA{R: 100, G: 180, B: 255, A: 255})
|
|
|
|
|
bar.Move(fyne.NewPos(xPos, topPadding+graphHeight-barHeight))
|
|
|
|
|
bar.Resize(fyne.NewSize(barWidth, barHeight))
|
|
|
|
|
r.objects = append(r.objects, bar)
|
|
|
|
|
|
|
|
|
|
// X-axis label
|
2026-02-20 14:40:29 -06:00
|
|
|
// Always show first and last label
|
|
|
|
|
isFirst := i == 0
|
|
|
|
|
isLast := i == numBars-1
|
|
|
|
|
|
|
|
|
|
// Determine if we should show this intermediate label
|
|
|
|
|
// We show it if it matches the step, BUT we also need to make sure it doesn't clash with the last label
|
|
|
|
|
// So if we are very close to the end, don't show it (unless it IS the end)
|
|
|
|
|
showIntermediate := i%labelStep == 0 && i < numBars-labelStep
|
|
|
|
|
|
|
|
|
|
if isFirst || isLast || showIntermediate {
|
|
|
|
|
label := canvas.NewText(fmt.Sprintf("%d", value), color.White)
|
2026-03-24 10:18:31 -05:00
|
|
|
label.TextSize = tickTextSize
|
2026-02-20 14:40:29 -06:00
|
|
|
|
|
|
|
|
// Center label under bar
|
|
|
|
|
label.Alignment = fyne.TextAlignCenter
|
|
|
|
|
label.Move(fyne.NewPos(xPos+barWidth/2-label.MinSize().Width/2, topPadding+graphHeight+10))
|
|
|
|
|
|
|
|
|
|
r.objects = append(r.objects, label)
|
|
|
|
|
}
|
2026-02-12 09:00:14 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-24 10:18:31 -05:00
|
|
|
func clamp(value, minValue, maxValue float32) float32 {
|
|
|
|
|
if value < minValue {
|
|
|
|
|
return minValue
|
|
|
|
|
}
|
|
|
|
|
if value > maxValue {
|
|
|
|
|
return maxValue
|
|
|
|
|
}
|
|
|
|
|
return value
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-20 14:40:29 -06:00
|
|
|
func calculateLabelStep(graphWidth float32, numBars int) int {
|
|
|
|
|
labelWidthEstimate := float32(35) // Estimate width of a label
|
|
|
|
|
maxLabels := int(graphWidth / labelWidthEstimate)
|
|
|
|
|
if maxLabels < 1 {
|
|
|
|
|
maxLabels = 1
|
|
|
|
|
}
|
|
|
|
|
labelStep := int(math.Ceil(float64(numBars) / float64(maxLabels)))
|
|
|
|
|
if labelStep < 1 {
|
|
|
|
|
labelStep = 1
|
|
|
|
|
}
|
|
|
|
|
return labelStep
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-12 09:00:14 -06:00
|
|
|
func (r *barGraphCanvasRenderer) Objects() []fyne.CanvasObject {
|
|
|
|
|
return r.objects
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *barGraphCanvasRenderer) Destroy() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ShowStatisticsWindow creates and shows a statistics window for the given expression
|
|
|
|
|
func ShowStatisticsWindow(expression string) {
|
|
|
|
|
stats, err := CalculateDiceStatistics(expression)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Printf("Error calculating statistics: %v\n", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create the bar graph
|
|
|
|
|
graph := newBarGraphCanvas(stats)
|
|
|
|
|
|
|
|
|
|
// Create and show the window
|
|
|
|
|
window := fyne.CurrentApp().NewWindow("Statistics: " + expression)
|
2026-03-24 10:18:31 -05:00
|
|
|
window.SetContent(container.NewMax(graph))
|
2026-02-12 09:10:42 -06:00
|
|
|
window.Resize(fyne.NewSize(900, 550))
|
2026-02-12 09:00:14 -06:00
|
|
|
window.Show()
|
|
|
|
|
}
|