Skip to content

Commit

Permalink
plot: Sort data points by time elapsed
Browse files Browse the repository at this point in the history
This commit makes the `Plot.data` method return `dataPoints` sorted by
their X axis value (i.e. elapsed seconds)

Previously, each series would be added discontinuously to each other,
essentially not being merge-sorted correctly.

Fixes #391
  • Loading branch information
tsenart committed Apr 28, 2019
1 parent 579df76 commit 0494333
Show file tree
Hide file tree
Showing 2 changed files with 812 additions and 799 deletions.
13 changes: 13 additions & 0 deletions lib/plot/plot.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,8 @@ func (p *Plot) data() (dataPoints, []string, error) {
labels[i+1] = s.attack + ": " + s.label
}

sort.Sort(data)

return data, labels, nil
}

Expand All @@ -314,6 +316,17 @@ func (cw *countingWriter) Write(p []byte) (int, error) {

type dataPoints [][]float64

func (ps dataPoints) Len() int { return len(ps) }

func (ps dataPoints) Less(i, j int) bool {
// Sort by X axis (seconds elapsed)
return ps[i][0] < ps[j][0]
}

func (ps dataPoints) Swap(i, j int) {
ps[i], ps[j] = ps[j], ps[i]
}

func (ps dataPoints) Append(buf []byte) []byte {
buf = append(buf, "[\n "...)

Expand Down
Loading

0 comments on commit 0494333

Please sign in to comment.