-
Notifications
You must be signed in to change notification settings - Fork 35
/
main.go
143 lines (124 loc) · 2.69 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright 2017 The go-hep Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"image"
"image/color"
"log"
"math/rand"
"go-hep.org/x/hep/hbook"
"go-hep.org/x/hep/hplot"
"go-hep.org/x/hep/hplot/vgshiny"
"golang.org/x/exp/shiny/driver"
"golang.org/x/exp/shiny/screen"
"golang.org/x/mobile/event/key"
"golang.org/x/mobile/event/paint"
"gonum.org/v1/plot/plotter"
"gonum.org/v1/plot/vg"
vgdraw "gonum.org/v1/plot/vg/draw"
)
const (
NPOINTS = 100000
xmax = 400
ymax = 400
)
var (
bkgCol = color.Black
)
func newPlot() *hplot.Plot {
// Draw some random values from the standard
// normal distribution.
hist1 := hbook.NewH1D(100, -5, +5)
hist2 := hbook.NewH1D(100, -5, +5)
for i := 0; i < NPOINTS; i++ {
v1 := rand.NormFloat64() - 1
v2 := rand.NormFloat64() + 1
hist1.Fill(v1, 1)
hist2.Fill(v2, 1)
}
// Make a plot and set its title.
p := hplot.New()
p.Title.Text = "Histogram"
p.X.Label.Text = "X"
p.Y.Label.Text = "Y"
// Create a histogram of our values drawn
// from the standard normal.
h1 := hplot.NewH1D(hist1)
h1.Infos.Style = hplot.HInfoSummary
h1.Color = color.Black
h1.FillColor = nil
h2 := hplot.NewH1D(hist2)
h2.Infos.Style = hplot.HInfoNone
h2.Color = color.RGBA{255, 0, 0, 255}
h2.FillColor = nil
p.Add(h1, h2)
p.Add(plotter.NewGrid())
return p
}
func main() {
driver.Main(func(scr screen.Screen) {
{
p := newPlot()
c, err := p.Show(-1, -1, scr)
if err != nil {
log.Fatal(err)
}
go func() {
c.Run(nil)
c.Release()
}()
}
w, err := newWidget(scr, image.Point{xmax, ymax})
if err != nil {
log.Fatal(err)
}
defer w.Release()
w.canvas.Run(func(e interface{}) bool {
switch e := e.(type) {
case key.Event:
repaint := false
switch e.Code {
case key.CodeEscape, key.CodeQ:
if e.Direction == key.DirPress {
return false
}
case key.CodeR:
if e.Direction == key.DirPress {
repaint = true
}
case key.CodeN, key.CodeSpacebar:
if e.Direction == key.DirPress {
p := newPlot()
p.Draw(vgdraw.New(w.canvas))
repaint = true
}
}
if repaint {
w.canvas.Send(paint.Event{})
}
case paint.Event:
w.canvas.Paint()
}
return true
})
})
}
type widget struct {
s screen.Screen
canvas *vgshiny.Canvas
}
func newWidget(s screen.Screen, size image.Point) (*widget, error) {
c, err := vgshiny.New(s, vg.Length(size.X), vg.Length(size.Y))
if err != nil {
return nil, err
}
return &widget{s: s, canvas: c}, err
}
func (w *widget) Release() {
if w.canvas != nil {
w.canvas.Release()
w.canvas = nil
}
w.s = nil
}