|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "math" |
| 6 | + "os" |
| 7 | + "strings" |
| 8 | + |
| 9 | + svg "github.com/ajstarks/svgo" |
| 10 | +) |
| 11 | + |
| 12 | +func main() { |
| 13 | + if err := doMain(); err != nil { |
| 14 | + panic(err) |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +func doMain() error { |
| 19 | + f, err := os.OpenFile("release.svg", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666) |
| 20 | + if err != nil { |
| 21 | + return err |
| 22 | + } |
| 23 | + defer f.Close() |
| 24 | + canvas := svg.New(f) |
| 25 | + canvas.Start(600, 400) |
| 26 | + canvas.Translate(300, 200) |
| 27 | + for i, month := range strings.Split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec", " ") { |
| 28 | + angle := func(midx int) float64 { |
| 29 | + return (float64(midx) - 3) * 2 * math.Pi / 12 |
| 30 | + } |
| 31 | + begin, end := angle(i), angle(i+1) |
| 32 | + |
| 33 | + // Draw a single black wedge of the calendar. |
| 34 | + path := fmt.Sprintf("M 0,0 L %v,%v A 100,100 0 0 0 %v,%v L 0 0", |
| 35 | + 100*math.Sin(begin), 100*math.Cos(begin), 100*math.Sin(end), 100*math.Cos(end)) |
| 36 | + canvas.Path(path, "stroke: black; fill:none") |
| 37 | + |
| 38 | + // Draw the text. Spin it around for readability in the second half. |
| 39 | + canvas.RotateTranslate(50, 0, angle(i)*360/(2*math.Pi)+20) |
| 40 | + if i < 6 { |
| 41 | + canvas.Text(0, 0, month) |
| 42 | + } else { |
| 43 | + canvas.Group(`transform="rotate(180)"`, `transform-origin="13 -3"`) |
| 44 | + canvas.Text(0, 0, month) |
| 45 | + canvas.Gend() |
| 46 | + } |
| 47 | + canvas.Gend() |
| 48 | + } |
| 49 | + |
| 50 | + type milestone struct { |
| 51 | + month, week int |
| 52 | + name string |
| 53 | + } |
| 54 | + milestones := []milestone{ |
| 55 | + {1, 1, "Planning"}, |
| 56 | + {1, 3, "General development"}, |
| 57 | + {5, 4, "Freeze"}, |
| 58 | + {6, 2, "First RC"}, |
| 59 | + {8, 2, "Release"}, |
| 60 | + } |
| 61 | + for relIdx, relName := range []string{"Summer", "Winter"} { |
| 62 | + angle := func(m milestone) float64 { |
| 63 | + return (float64(m.month-1) - 3 + (float64(m.week)-0.5)/4) * 2 * math.Pi / 12 |
| 64 | + } |
| 65 | + |
| 66 | + // Shift the milestones 6 months for the winter release. |
| 67 | + milestones := milestones |
| 68 | + for i := range milestones { |
| 69 | + milestones[i].month = (milestones[i].month + 6*relIdx) % 12 |
| 70 | + } |
| 71 | + |
| 72 | + frozen := false |
| 73 | + for i, m := range milestones { |
| 74 | + x, y := math.Cos(angle(m)), math.Sin(angle(m)) |
| 75 | + // Align the text away from the center of the circle. |
| 76 | + textAnchor := "start" |
| 77 | + if x < 0 { |
| 78 | + textAnchor = "end" |
| 79 | + } |
| 80 | + // Color the arc depending on the freeze state. |
| 81 | + if m.name == "Freeze" { |
| 82 | + frozen = true |
| 83 | + } |
| 84 | + color := "green" |
| 85 | + if frozen { |
| 86 | + color = "blue" |
| 87 | + } |
| 88 | + |
| 89 | + // Center radius of the release arc. |
| 90 | + arcRadius := float64(120 + 20*relIdx) |
| 91 | + // Length of the line to the label. Vary a bit to avoid text overlap. |
| 92 | + lineLength := float64(30 + 5*((i+1)%2)) |
| 93 | + // Distance from the end of the line to the text. |
| 94 | + textoff := float64(10) |
| 95 | + |
| 96 | + // Draw the arc to the next milestone. |
| 97 | + if i+1 < len(milestones) { |
| 98 | + nx, ny := math.Cos(angle(milestones[i+1])), math.Sin(angle(milestones[i+1])) |
| 99 | + canvas.Arc(int(x*arcRadius), int(y*arcRadius), int(arcRadius), int(arcRadius), 0, false, true, int(nx*arcRadius), int(ny*arcRadius), "stroke-width:10; fill:none; stroke: "+color) |
| 100 | + } |
| 101 | + // Draw the line from the inner edge of the arc. |
| 102 | + canvas.Line(int(x*(arcRadius-5)), int(y*(arcRadius-5)), int(x*(arcRadius+lineLength)), int(y*(arcRadius+lineLength)), "stroke:black") |
| 103 | + canvas.Text(int(x*(arcRadius+lineLength+textoff)), int(y*(arcRadius+lineLength+textoff)), relName+": "+m.name, "text-anchor: "+textAnchor) |
| 104 | + } |
| 105 | + } |
| 106 | + canvas.Gend() |
| 107 | + canvas.End() |
| 108 | + return f.Close() |
| 109 | +} |
0 commit comments