showcase
- v0.2.4
- 37 commits
- 1 branch
- 6 tags
- updated
- CI 2/2
activity.go
5.6 KB · 170 lines
1package render23import (4 "fmt"5 "html/template"6 "math"7 "strconv"8 "strings"9 "time"10)1112// WeekStart is the Sunday (UTC) starting the week n weeks before end's week,13// the same columns the heatmap uses.14func WeekStart(end time.Time, n int) time.Time {15 end = time.Date(end.Year(), end.Month(), end.Day(), 0, 0, 0, 0, time.UTC)16 return end.AddDate(0, 0, -int(end.Weekday())-7*n)17}1819// Weeks counts times into n weekly buckets ending with end's week (last).20func Weeks(times []time.Time, end time.Time, n int) []int {21 start, out := WeekStart(end, n-1), make([]int, n)22 for _, t := range times {23 if i := int(t.Sub(start).Hours() / 24 / 7); !t.Before(start) && i < n {24 out[i]++25 }26 }27 return out28}2930// Streak counts the weeks in a row with commits, ending this week, or last31// week while this one is still empty.32func Streak(weeks []int) int {33 i := len(weeks) - 134 if i >= 0 && weeks[i] == 0 {35 i--36 }37 n := 038 for ; i >= 0 && weeks[i] > 0; i-- {39 n++40 }41 return n42}4344// The bar chart's rows, in viewBox units. Its columns are the heatmap's.45const (46 barsTop, barsBase = 18, 100 // plot area47 barsH, barsLabel = 118, 114 // "this week" under the baseline48)4950// yAxis picks a 1-2-5 step that gives 2-3 grid lines (0 included), with the51// top line at or above m.52func yAxis(m int) (step, top int) {53 for s := 1; ; s *= 10 {54 for _, k := range []int{s, 2 * s, 5 * s} {55 if m <= 2*k {56 return k, k * max(1, (m+k-1)/k)57 }58 }59 }60}6162func num(v float64) string { return strconv.FormatFloat(math.Round(v*10)/10, 'f', -1, 64) }6364// ActivityBars draws weekly commit counts (oldest first, ending with end's65// week) as a bar chart on the heatmap's columns, so each bar sits above its66// week. Only the last n weeks are drawn; the aria-label sums them all. It67// marks this week and brackets the streak's weeks, counted like Streak.68func ActivityBars(counts []int, end time.Time, streak, n int) template.HTML {69 start := WeekStart(end, len(counts)-1)70 week := func(i int) time.Time { return start.AddDate(0, 0, 7*i) }71 total, peak := 0, 072 for i, c := range counts {73 if total += c; c >= counts[peak] {74 peak = i75 }76 }77 sum := "No commits in the last year"78 if total > 0 {79 sum = fmt.Sprintf("%s in %d weeks, busiest week %s with %d", commits(total), len(counts), week(peak).Format("Jan 2"), counts[peak])80 if streak > 0 {81 sum += fmt.Sprintf(", %d-week streak", streak)82 }83 }84 last := len(counts) - 1 // the streak's last week85 if counts[last] == 0 {86 last--87 }88 off := len(counts) - n89 counts, start, last = counts[off:], week(off), last-off9091 shown, peak := 0, 092 for i, c := range counts {93 if shown += c; c >= counts[peak] {94 peak = i95 }96 }97 step, ymax := yAxis(counts[peak])98 height := func(c int) float64 { return max(4, float64(c*(barsBase-barsTop))/float64(ymax)) }99 right := float64(colX(n) - gap) // the baseline's end100101 var b strings.Builder102 text := func(class string, x float64, y int, anchor, s string) {103 fmt.Fprintf(&b, `<text class="%s" x="%s" y="%d" text-anchor="%s">%s</text>`, class, num(x), y, anchor, s)104 }105 fmt.Fprintf(&b, `<svg class="bars" viewBox="0 0 %d %d" role="img" aria-label="%s">`, colX(n), barsH, sum)106 for v := 0; v <= ymax; v += step {107 y, class := barsBase-float64(v*(barsBase-barsTop))/float64(ymax), "bars-grid"108 if v == 0 {109 class = "bars-base"110 }111 fmt.Fprintf(&b, `<line class="%s" x1="%d" x2="%s" y1="%s" y2="%[4]s"/>`, class, left-2, num(right), num(y))112 text("bars-axis", left-6, int(y)+3, "end", strconv.Itoa(v))113 }114 for _, c := range monthCols(start, n) {115 if x := num(float64(colX(c)) - gap/2.0); c > 0 {116 fmt.Fprintf(&b, `<line class="bars-month" x1="%s" x2="%s" y1="%d" y2="%d"/>`, x, x, barsTop, barsBase)117 }118 }119 for i, c := range counts {120 x, h, r, class := colX(i), 2.0, 1.0, "bars-bar bars-stub"121 if c > 0 {122 h, r, class = height(c), 2, fmt.Sprintf("bars-bar h%d", level(c, counts[peak]))123 }124 if i == n-1 && shown > 0 {125 class += " bars-now"126 }127 fmt.Fprintf(&b, `<rect class="bars-hit" x="%s" y="0" width="%d" height="%d"><title>%s · week of %s – %s</title></rect>`,128 num(float64(x)-gap/2.0), cell+gap, barsBase+2, commits(c), week(i).Format("Jan 2"), week(i).AddDate(0, 0, 6).Format("Jan 2"))129 fmt.Fprintf(&b, `<path class="%s" d="M%d %dV%sa%g %g 0 0 1 %g-%gh%ga%g %g 0 0 1 %g %gV%dz"/>`,130 class, x, barsBase, num(barsBase-h+r), r, r, r, r, cell-2*r, r, r, r, r, barsBase)131 }132 if shown == 0 {133 msg := "No commits in the last year"134 if total > 0 {135 msg = fmt.Sprintf("No commits in the last %d weeks", n)136 }137 text("bars-empty", float64(left+colX(n))/2, (barsTop+barsBase)/2+3, "middle", msg)138 return template.HTML(b.String() + `</svg>`)139 }140141 text("bars-now-text", right, barsLabel, "end", "this week")142 if first := max(0, last-streak+1); streak > 0 && last >= 0 {143 fmt.Fprintf(&b, `<path class="bars-streak" d="M%d %dV%dH%dV%d"/>`, colX(first), barsBase+4, barsBase+6, colX(last)+cell, barsBase+4)144 }145 return template.HTML(b.String() + `</svg>`)146}147148// Sparkline draws weekly counts as a small line with a soft area under it.149func Sparkline(weeks []int) template.HTML {150 const w, h = 96, 24151 top := 0152 for _, n := range weeks {153 top = max(top, n)154 }155 var pts strings.Builder156 for i, n := range weeks {157 y := float64(h - 2)158 if top > 0 {159 y = float64(h-2) - float64(n)/float64(top)*float64(h-4)160 }161 fmt.Fprintf(&pts, "%.1f,%.1f ", float64(i)*w/float64(len(weeks)-1), y)162 }163 p := strings.TrimSpace(pts.String())164 class := "spark"165 if top == 0 {166 class += " spark--flat"167 }168 return template.HTML(fmt.Sprintf(`<svg class="%s" viewBox="0 0 %d %d" preserveAspectRatio="none" aria-hidden="true"><polygon class="spark-area" points="0,%d %s %d,%d"></polygon><polyline class="spark-line" points="%s"></polyline></svg>`,169 class, w, h, h, p, w, h, p))170}