// CreateImage creates graph of nyanpass func (n *Nyanpass) CreateImage(fileName string) error { if n.Counts == nil { return errors.New("Count is not defined.") } p, err := plot.New() if err != nil { return err } bar, err := plotter.NewBarChart(n.Counts, vg.Points(30)) if err != nil { return err } bar.LineStyle.Width = vg.Length(0) bar.Color = plotutil.Color(2) p.Add(bar) p.Title.Text = "Nyanpass Graph" p.X.Label.Text = "Days" p.Y.Label.Text = "Nyanpass count" p.NominalX(n.labels...) p.Y.Tick.Marker = RelabelTicks{} if err := p.Save(6*vg.Inch, 6*vg.Inch, fileName); err != nil { return err } n.imagePath = fileName return nil }
// An example of making a bar chart. func Example_barChart() *plot.Plot { groupA := plotter.Values{20, 35, 30, 35, 27} groupB := plotter.Values{25, 32, 34, 20, 25} groupC := plotter.Values{12, 28, 15, 21, 8} groupD := plotter.Values{30, 42, 6, 9, 12} p, err := plot.New() if err != nil { panic(err) } p.Title.Text = "Bar chart" p.Y.Label.Text = "Heights" w := vg.Points(8) barsA := must(plotter.NewBarChart(groupA, w)).(*plotter.BarChart) barsA.Color = color.RGBA{R: 255, A: 255} barsA.Offset = -w / 2 barsB := must(plotter.NewBarChart(groupB, w)).(*plotter.BarChart) barsB.Color = color.RGBA{R: 196, G: 196, A: 255} barsB.Offset = w / 2 barsC := must(plotter.NewBarChart(groupC, w)).(*plotter.BarChart) barsC.Color = color.RGBA{B: 255, A: 255} barsC.XMin = 6 barsC.Offset = -w / 2 barsD := must(plotter.NewBarChart(groupD, w)).(*plotter.BarChart) barsD.Color = color.RGBA{B: 255, R: 255, A: 255} barsD.XMin = 6 barsD.Offset = w / 2 p.Add(barsA, barsB, barsC, barsD) p.Legend.Add("A", barsA) p.Legend.Add("B", barsB) p.Legend.Add("C", barsC) p.Legend.Add("D", barsD) p.Legend.Top = true p.NominalX("Zero", "One", "Two", "Three", "Four", "", "Six", "Seven", "Eight", "Nine", "Ten") return p }
func drawTopReleaseDownloads(ctx *context, per *period, filename string) { var rs releases ctx.WalkReleases(func(r github.RepositoryRelease) { var cnt int if r.CreatedAt.Before(per.start) || r.CreatedAt.After(per.end) { return } for _, a := range r.Assets { cnt += *a.DownloadCount } rs = append(rs, release{name: *r.TagName, download: cnt}) }) sort.Sort(rs) var names []string var downloads []int num := 10 if num > len(rs) { num = len(rs) } for i := 0; i < num; i++ { names = append(names, rs[i].name) downloads = append(downloads, rs[i].download) } p, err := plot.New() if err != nil { panic(err) } p.Title.Text = "Release Downloads" p.Y.Label.Text = "Download Count" if len(names) > 0 { p.NominalX(names...) bars, err := plotter.NewBarChart(ints(downloads), vg.Points(20)) if err != nil { panic(err) } bars.LineStyle.Width = vg.Length(0) p.Add(bars) } // Save the plot to a PNG file. if err := p.Save(defaultWidth, defaultHeight, filename); err != nil { panic(err) } }
func plotTableSizes(sess *r.Session) { sizes := []float64{} for _, t := range tables { sizes = append(sizes, float64(averageDocumentSize(sess, t))) } p, _ := plot.New() p.Title.Text = "Average document sizes" p.Y.Label.Text = "Size in bytes" w := vg.Points(20) bars, _ := plotter.NewBarChart(plotter.Values(sizes), w) bars.Color = plotutil.Color(0) bars.LineStyle.Width = vg.Length(0) p.Add(bars) p.NominalX(tables...) p.Save(6*vg.Inch, 6*vg.Inch, "avg_doc_sizes.png") }
func TestLegendAlignment(t *testing.T) { font, err := vg.MakeFont(plot.DefaultFont, 10.822510822510822) // This font size gives an entry height of 10. if err != nil { t.Fatalf("failed to create font: %v", err) } l := plot.Legend{ ThumbnailWidth: vg.Points(20), TextStyle: draw.TextStyle{Font: font}, } for _, n := range []string{"A", "B", "C", "D"} { b, err := plotter.NewBarChart(plotter.Values{0}, 1) if err != nil { t.Fatalf("failed to create bar chart %q: %v", n, err) } l.Add(n, b) } r := recorder.New(100) c := draw.NewCanvas(r, 100, 100) l.Draw(draw.Canvas{ Canvas: c.Canvas, Rectangle: draw.Rectangle{ Min: draw.Point{0, 0}, Max: draw.Point{100, 100}, }, }) got := r.Actions // want is a snapshot of the actions for the code above when the // graphical output has been visually confirmed to be correct for // the bar charts example show in gonum/plot#25. want := []recorder.Action{ &recorder.SetColor{ Color: color.Gray16{}, }, &recorder.Fill{ Path: vg.Path{ {Type: vg.MoveComp, X: 80, Y: 30}, {Type: vg.LineComp, X: 80, Y: 40}, {Type: vg.LineComp, X: 100, Y: 40}, {Type: vg.LineComp, X: 100, Y: 30}, {Type: vg.CloseComp}, }, }, &recorder.SetColor{ Color: color.Gray16{}, }, &recorder.SetLineWidth{ Width: 1, }, &recorder.SetLineDash{}, &recorder.Stroke{ Path: vg.Path{ {Type: vg.MoveComp, X: 80, Y: 30}, {Type: vg.LineComp, X: 80, Y: 40}, {Type: vg.LineComp, X: 100, Y: 40}, {Type: vg.LineComp, X: 100, Y: 30}, {Type: vg.LineComp, X: 80, Y: 30}, }, }, &recorder.SetColor{}, &recorder.FillString{ Font: string("Times-Roman"), Size: 10.822510822510822, X: 69.48051948051948, Y: 30.82251082251082, String: "A", }, &recorder.SetColor{ Color: color.Gray16{}, }, &recorder.Fill{ Path: vg.Path{ {Type: vg.MoveComp, X: 80, Y: 20}, {Type: vg.LineComp, X: 80, Y: 30}, {Type: vg.LineComp, X: 100, Y: 30}, {Type: vg.LineComp, X: 100, Y: 20}, {Type: vg.CloseComp}, }, }, &recorder.SetColor{ Color: color.Gray16{}, }, &recorder.SetLineWidth{ Width: 1, }, &recorder.SetLineDash{}, &recorder.Stroke{ Path: vg.Path{ {Type: vg.MoveComp, X: 80, Y: 20}, {Type: vg.LineComp, X: 80, Y: 30}, {Type: vg.LineComp, X: 100, Y: 30}, {Type: vg.LineComp, X: 100, Y: 20}, {Type: vg.LineComp, X: 80, Y: 20}, }, }, &recorder.SetColor{}, &recorder.FillString{ Font: string("Times-Roman"), Size: 10.822510822510822, X: 70.07575757575758, Y: 20.82251082251082, String: "B", }, &recorder.SetColor{ Color: color.Gray16{ Y: uint16(0), }, }, &recorder.Fill{ Path: vg.Path{ {Type: vg.MoveComp, X: 80, Y: 10}, {Type: vg.LineComp, X: 80, Y: 20}, {Type: vg.LineComp, X: 100, Y: 20}, {Type: vg.LineComp, X: 100, Y: 10}, {Type: vg.CloseComp}, }, }, &recorder.SetColor{ Color: color.Gray16{}, }, &recorder.SetLineWidth{ Width: 1, }, &recorder.SetLineDash{}, &recorder.Stroke{ Path: vg.Path{ {Type: vg.MoveComp, X: 80, Y: 10}, {Type: vg.LineComp, X: 80, Y: 20}, {Type: vg.LineComp, X: 100, Y: 20}, {Type: vg.LineComp, X: 100, Y: 10}, {Type: vg.LineComp, X: 80, Y: 10}, }, }, &recorder.SetColor{}, &recorder.FillString{ Font: string("Times-Roman"), Size: 10.822510822510822, X: 70.07575757575758, Y: 10.822510822510822, String: "C", }, &recorder.SetColor{ Color: color.Gray16{}, }, &recorder.Fill{ Path: vg.Path{ {Type: vg.MoveComp, X: 80, Y: 0}, {Type: vg.LineComp, X: 80, Y: 10}, {Type: vg.LineComp, X: 100, Y: 10}, {Type: vg.LineComp, X: 100, Y: 0}, {Type: vg.CloseComp}, }, }, &recorder.SetColor{ Color: color.Gray16{}, }, &recorder.SetLineWidth{ Width: 1, }, &recorder.SetLineDash{}, &recorder.Stroke{ Path: vg.Path{ {Type: vg.MoveComp, X: 80, Y: 0}, {Type: vg.LineComp, X: 80, Y: 10}, {Type: vg.LineComp, X: 100, Y: 10}, {Type: vg.LineComp, X: 100, Y: 0}, {Type: vg.LineComp, X: 80, Y: 0}, }, }, &recorder.SetColor{}, &recorder.FillString{ Font: string("Times-Roman"), Size: 10.822510822510822, X: 69.48051948051948, Y: 0.8225108225108215, String: "D", }, } if !reflect.DeepEqual(got, want) { t.Errorf("unexpected legend actions:\ngot:\n%s\nwant:\n%s", formatActions(got), formatActions(want)) } }