forked from vdobler/chart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
strip.go
116 lines (102 loc) · 3.22 KB
/
strip.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
package chart
import (
// "fmt"
"math"
"math/rand"
// "os"
// "strings"
)
// StripChart represents very simple strip charts.
type StripChart struct {
Jitter bool // Add jitter to help distinguish overlapping values
ScatterChart // The embeded ScatterChart is responsible for all drawing
}
// AddData adds data to the strip chart.
func (sc *StripChart) AddData(name string, data []float64, style Style) {
n := len(sc.ScatterChart.Data) + 1
pd := make([]EPoint, len(data))
nan := math.NaN()
for i, d := range data {
pd[i].X = d
pd[i].Y = float64(n)
pd[i].DeltaX, pd[i].DeltaY = nan, nan
}
if style.empty() {
style = AutoStyle(len(sc.Data), false)
}
style.LineStyle = 0
sc.ScatterChart.AddData(name, pd, PlotStylePoints, style)
}
func (sc *StripChart) AddDataGeneric(name string, data []Value) {
n := len(sc.ScatterChart.Data) + 1
pd := make([]EPoint, len(data))
nan := math.NaN()
for i, d := range data {
pd[i].X = d.XVal()
pd[i].Y = float64(n)
pd[i].DeltaX, pd[i].DeltaY = nan, nan
}
sc.ScatterChart.AddData(name, pd, PlotStylePoints, Style{})
}
// Reset chart to state before plotting.
func (sc *StripChart) Reset() {
sc.ScatterChart.Reset()
}
// Plot outputs the strip chart sc to g.
func (sc *StripChart) Plot(g Graphics) {
sc.ScatterChart.YRange.Label = ""
sc.ScatterChart.YRange.TicSetting.Hide = true
sc.ScatterChart.YRange.TicSetting.Delta = 1
sc.ScatterChart.YRange.MinMode.Fixed = true
sc.ScatterChart.YRange.MinMode.Value = 0.5
sc.ScatterChart.YRange.MaxMode.Fixed = true
sc.ScatterChart.YRange.MaxMode.Value = float64(len(sc.ScatterChart.Data)) + 0.5
if sc.Jitter {
// Set up ranging
layout := layout(g, sc.Title, sc.XRange.Label, sc.YRange.Label,
sc.XRange.TicSetting.Hide || sc.XRange.TicSetting.HideLabels,
sc.YRange.TicSetting.Hide || sc.YRange.TicSetting.HideLabels,
&sc.Key)
_, height := layout.Width, layout.Height
topm, _ := layout.Top, layout.Left
_, numytics := layout.NumXtics, layout.NumYtics
sc.YRange.Setup(numytics, numytics+1, height, topm, true)
// amplitude of jitter: not too smal to be visible and useful, not to
// big to be ugly or even overlapp other
null := sc.YRange.Screen2Data(0)
absmin := 1.4 * math.Abs(sc.YRange.Screen2Data(1)-null) // would be one pixel
tenpc := math.Abs(sc.YRange.Screen2Data(height)-null) / 10 // 10 percent of graph area
smplcnt := len(sc.ScatterChart.Data) + 1 // as samples are borders
noverlp := math.Abs(sc.YRange.Screen2Data(height/smplcnt) - null) // do not overlapp other sample
yj := noverlp
if tenpc < yj {
yj = tenpc
}
if yj < absmin {
yj = absmin
}
// yjs := sc.YRange.Data2Screen(yj) - sc.YRange.Data2Screen(0)
// fmt.Printf("yj = %.2f : in screen = %d\n", yj, yjs)
for _, data := range sc.ScatterChart.Data {
if data.Samples == nil {
continue // should not happen
}
for i := range data.Samples {
shift := yj * rand.NormFloat64() * yj
data.Samples[i].Y += shift
}
}
}
sc.ScatterChart.Plot(g)
if sc.Jitter {
// Revert Jitter
for s, data := range sc.ScatterChart.Data {
if data.Samples == nil {
continue // should not happen
}
for i, _ := range data.Samples {
data.Samples[i].Y = float64(s + 1)
}
}
}
}