2019-01-28 23:09:52 +00:00
|
|
|
package widgets
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-02-02 14:45:53 +00:00
|
|
|
"github.com/sqshq/sampler/console"
|
|
|
|
"github.com/sqshq/sampler/data"
|
2019-01-28 23:09:52 +00:00
|
|
|
"image"
|
2019-01-31 00:02:38 +00:00
|
|
|
"log"
|
2019-01-31 04:15:15 +00:00
|
|
|
"math"
|
2019-01-31 00:02:38 +00:00
|
|
|
"strconv"
|
2019-01-28 23:09:52 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
. "github.com/sqshq/termui"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-02-04 04:03:59 +00:00
|
|
|
xAxisLegendWidth = 20
|
|
|
|
xAxisLabelsHeight = 1
|
|
|
|
xAxisLabelsWidth = 8
|
|
|
|
xAxisLabelsGap = 2
|
|
|
|
xAxisGridWidth = xAxisLabelsGap + xAxisLabelsWidth
|
|
|
|
|
|
|
|
yAxisLabelsHeight = 1
|
|
|
|
yAxisLabelsGap = 1
|
|
|
|
|
|
|
|
chartHistoryReserve = 5
|
2019-01-28 23:09:52 +00:00
|
|
|
)
|
|
|
|
|
2019-01-31 01:41:51 +00:00
|
|
|
type RunChart struct {
|
|
|
|
Block
|
2019-02-02 04:39:34 +00:00
|
|
|
lines []TimeLine
|
|
|
|
grid ChartGrid
|
|
|
|
precision int
|
2019-02-04 04:03:59 +00:00
|
|
|
timescale time.Duration
|
2019-02-03 03:30:45 +00:00
|
|
|
selection *time.Time
|
2019-02-02 04:39:34 +00:00
|
|
|
mutex *sync.Mutex
|
2019-01-31 01:41:51 +00:00
|
|
|
}
|
|
|
|
|
2019-01-28 23:09:52 +00:00
|
|
|
type TimePoint struct {
|
2019-02-03 03:30:45 +00:00
|
|
|
time time.Time
|
|
|
|
value float64
|
|
|
|
line *TimeLine
|
2019-01-28 23:09:52 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 23:40:05 +00:00
|
|
|
type TimeLine struct {
|
|
|
|
points []TimePoint
|
2019-02-03 03:30:45 +00:00
|
|
|
color Color
|
|
|
|
label string
|
2019-01-28 23:09:52 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 01:41:51 +00:00
|
|
|
type ChartGrid struct {
|
2019-02-04 04:03:59 +00:00
|
|
|
linesCount int
|
|
|
|
paddingWidth int
|
|
|
|
maxTimeWidth int
|
|
|
|
minTimeWidth int
|
|
|
|
valueExtremum ValueExtremum
|
|
|
|
timeExtremum TimeExtremum
|
2019-01-31 01:41:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type TimeExtremum struct {
|
|
|
|
max time.Time
|
|
|
|
min time.Time
|
2019-01-28 23:09:52 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 01:41:51 +00:00
|
|
|
type ValueExtremum struct {
|
|
|
|
max float64
|
|
|
|
min float64
|
|
|
|
}
|
2019-01-28 23:09:52 +00:00
|
|
|
|
2019-02-04 04:03:59 +00:00
|
|
|
func NewRunChart(title string, precision int, refreshRateMs int) *RunChart {
|
2019-01-31 04:15:15 +00:00
|
|
|
block := *NewBlock()
|
|
|
|
block.Title = title
|
|
|
|
return &RunChart{
|
2019-02-02 04:39:34 +00:00
|
|
|
Block: block,
|
|
|
|
lines: []TimeLine{},
|
|
|
|
mutex: &sync.Mutex{},
|
2019-02-04 04:03:59 +00:00
|
|
|
precision: precision,
|
|
|
|
timescale: calculateTimescale(refreshRateMs),
|
2019-01-31 04:15:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-31 01:41:51 +00:00
|
|
|
func (self *RunChart) newChartGrid() ChartGrid {
|
|
|
|
|
2019-02-04 04:03:59 +00:00
|
|
|
linesCount := (self.Inner.Max.X - self.Inner.Min.X - self.grid.minTimeWidth) / xAxisGridWidth
|
2019-01-31 01:41:51 +00:00
|
|
|
|
|
|
|
return ChartGrid{
|
2019-02-04 04:03:59 +00:00
|
|
|
linesCount: linesCount,
|
|
|
|
paddingWidth: xAxisGridWidth,
|
|
|
|
maxTimeWidth: self.Inner.Max.X,
|
|
|
|
minTimeWidth: self.getMaxValueLength(),
|
|
|
|
timeExtremum: getTimeExtremum(linesCount, self.timescale),
|
|
|
|
valueExtremum: getChartValueExtremum(self.lines),
|
2019-01-28 23:09:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-03 03:30:45 +00:00
|
|
|
func (self *RunChart) Draw(buffer *Buffer) {
|
2019-01-28 23:09:52 +00:00
|
|
|
|
2019-01-31 04:15:15 +00:00
|
|
|
self.mutex.Lock()
|
2019-02-03 03:30:45 +00:00
|
|
|
self.Block.Draw(buffer)
|
2019-01-31 01:41:51 +00:00
|
|
|
self.grid = self.newChartGrid()
|
2019-01-28 23:09:52 +00:00
|
|
|
|
2019-01-29 14:34:15 +00:00
|
|
|
drawArea := image.Rect(
|
2019-02-02 04:39:34 +00:00
|
|
|
self.Inner.Min.X+self.grid.minTimeWidth+1, self.Inner.Min.Y,
|
2019-01-29 14:34:15 +00:00
|
|
|
self.Inner.Max.X, self.Inner.Max.Y-xAxisLabelsHeight-1,
|
|
|
|
)
|
2019-01-28 23:09:52 +00:00
|
|
|
|
2019-02-03 03:30:45 +00:00
|
|
|
selectedPoints := self.getSelectedTimePoints()
|
|
|
|
|
|
|
|
self.renderAxes(buffer)
|
|
|
|
self.renderItems(buffer, drawArea)
|
|
|
|
self.renderSelection(buffer, drawArea, selectedPoints)
|
|
|
|
self.renderLegend(buffer, drawArea, selectedPoints)
|
2019-01-31 04:15:15 +00:00
|
|
|
self.mutex.Unlock()
|
2019-01-28 23:09:52 +00:00
|
|
|
}
|
|
|
|
|
2019-02-03 03:30:45 +00:00
|
|
|
func (self *RunChart) ConsumeSample(sample data.Sample) {
|
|
|
|
|
|
|
|
float, err := strconv.ParseFloat(sample.Value, 64)
|
2019-01-31 00:02:38 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2019-02-03 03:30:45 +00:00
|
|
|
log.Printf("Expected float number, but got %v", sample.Value) // TODO visual notification + check sample.Error
|
2019-01-31 00:02:38 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 04:15:15 +00:00
|
|
|
self.mutex.Lock()
|
2019-02-03 03:30:45 +00:00
|
|
|
|
|
|
|
lineIndex := -1
|
2019-01-31 04:15:15 +00:00
|
|
|
|
2019-01-31 23:40:05 +00:00
|
|
|
for i, line := range self.lines {
|
2019-02-03 03:30:45 +00:00
|
|
|
if line.label == sample.Label {
|
|
|
|
lineIndex = i
|
2019-01-31 04:15:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-03 03:30:45 +00:00
|
|
|
if lineIndex == -1 {
|
|
|
|
line := &TimeLine{
|
|
|
|
points: []TimePoint{},
|
|
|
|
color: sample.Color,
|
|
|
|
label: sample.Label,
|
2019-01-31 04:15:15 +00:00
|
|
|
}
|
2019-02-03 03:30:45 +00:00
|
|
|
self.lines = append(self.lines, *line)
|
|
|
|
lineIndex = len(self.lines) - 1
|
2019-01-31 04:15:15 +00:00
|
|
|
}
|
|
|
|
|
2019-02-03 03:30:45 +00:00
|
|
|
line := self.lines[lineIndex]
|
|
|
|
|
|
|
|
timePoint := TimePoint{value: float, time: time.Now(), line: &line}
|
|
|
|
line.points = append(line.points, timePoint)
|
|
|
|
self.lines[lineIndex] = line
|
|
|
|
|
2019-01-28 23:09:52 +00:00
|
|
|
self.trimOutOfRangeValues()
|
2019-01-31 04:15:15 +00:00
|
|
|
self.mutex.Unlock()
|
2019-01-28 23:09:52 +00:00
|
|
|
}
|
|
|
|
|
2019-02-03 03:30:45 +00:00
|
|
|
func (self *RunChart) SelectPoint(x int, y int) {
|
|
|
|
|
|
|
|
point := image.Point{X: x, Y: y}
|
|
|
|
|
|
|
|
if !point.In(self.Rectangle) {
|
|
|
|
self.selection = nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-04 04:03:59 +00:00
|
|
|
timeDeltaToPaddingRelation := (self.grid.maxTimeWidth - x) / xAxisGridWidth
|
|
|
|
timeDeltaWithGridMaxTime := timeDeltaToPaddingRelation * int(self.timescale.Nanoseconds())
|
2019-02-03 03:30:45 +00:00
|
|
|
selection := self.grid.timeExtremum.max.Add(-time.Duration(timeDeltaWithGridMaxTime) * time.Nanosecond)
|
|
|
|
|
|
|
|
self.selection = &selection
|
2019-01-31 00:02:38 +00:00
|
|
|
}
|
|
|
|
|
2019-02-03 03:30:45 +00:00
|
|
|
func (self *RunChart) getSelectedTimePoints() []TimePoint {
|
|
|
|
|
|
|
|
selected := []TimePoint{}
|
|
|
|
|
|
|
|
if self.selection == nil {
|
|
|
|
return selected
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, line := range self.lines {
|
|
|
|
|
|
|
|
if len(line.points) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
closest := line.points[0]
|
|
|
|
|
|
|
|
for _, point := range line.points {
|
|
|
|
|
|
|
|
diffWithClosest := math.Abs(float64(self.selection.UnixNano() - closest.time.UnixNano()))
|
|
|
|
diffWithCurrent := math.Abs(float64(self.selection.UnixNano() - point.time.UnixNano()))
|
|
|
|
|
|
|
|
if diffWithClosest > diffWithCurrent {
|
|
|
|
closest = point
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
selected = append(selected, closest)
|
|
|
|
}
|
|
|
|
|
|
|
|
return selected
|
2019-02-02 04:39:34 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 01:41:51 +00:00
|
|
|
func (self *RunChart) trimOutOfRangeValues() {
|
2019-02-02 04:39:34 +00:00
|
|
|
|
2019-02-04 04:03:59 +00:00
|
|
|
historyReserve := self.timescale * time.Duration(self.grid.linesCount) * chartHistoryReserve
|
2019-02-03 03:30:45 +00:00
|
|
|
minRangeTime := self.grid.timeExtremum.min.Add(-historyReserve)
|
2019-02-02 04:39:34 +00:00
|
|
|
|
2019-01-31 23:40:05 +00:00
|
|
|
for i, item := range self.lines {
|
2019-01-31 04:15:15 +00:00
|
|
|
lastOutOfRangeValueIndex := -1
|
2019-01-28 23:09:52 +00:00
|
|
|
|
2019-02-02 04:39:34 +00:00
|
|
|
for j, point := range item.points {
|
2019-02-03 03:30:45 +00:00
|
|
|
if point.time.Before(minRangeTime) {
|
2019-01-31 04:15:15 +00:00
|
|
|
lastOutOfRangeValueIndex = j
|
|
|
|
}
|
2019-01-28 23:09:52 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 04:15:15 +00:00
|
|
|
if lastOutOfRangeValueIndex > 0 {
|
2019-01-31 23:40:05 +00:00
|
|
|
item.points = append(item.points[:0], item.points[lastOutOfRangeValueIndex+1:]...)
|
|
|
|
self.lines[i] = item
|
2019-01-31 04:15:15 +00:00
|
|
|
}
|
2019-01-28 23:09:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-01 05:07:25 +00:00
|
|
|
func (self *RunChart) renderItems(buffer *Buffer, drawArea image.Rectangle) {
|
2019-01-28 23:09:52 +00:00
|
|
|
|
|
|
|
canvas := NewCanvas()
|
|
|
|
canvas.Rectangle = drawArea
|
|
|
|
|
2019-01-31 23:40:05 +00:00
|
|
|
for _, line := range self.lines {
|
2019-01-28 23:09:52 +00:00
|
|
|
|
2019-01-31 04:15:15 +00:00
|
|
|
xToPoint := make(map[int]image.Point)
|
|
|
|
pointsOrder := make([]int, 0)
|
2019-01-28 23:09:52 +00:00
|
|
|
|
2019-02-03 03:30:45 +00:00
|
|
|
for _, timePoint := range line.points {
|
2019-01-28 23:09:52 +00:00
|
|
|
|
2019-02-03 03:30:45 +00:00
|
|
|
timeDeltaWithGridMaxTime := self.grid.timeExtremum.max.Sub(timePoint.time).Nanoseconds()
|
2019-02-04 04:03:59 +00:00
|
|
|
timeDeltaToPaddingRelation := float64(timeDeltaWithGridMaxTime) / float64(self.timescale.Nanoseconds())
|
|
|
|
x := self.grid.maxTimeWidth - (int(float64(xAxisGridWidth) * timeDeltaToPaddingRelation))
|
2019-01-28 23:09:52 +00:00
|
|
|
|
2019-02-02 04:39:34 +00:00
|
|
|
var y int
|
|
|
|
if self.grid.valueExtremum.max-self.grid.valueExtremum.min == 0 {
|
|
|
|
y = (drawArea.Dy() - 2) / 2
|
|
|
|
} else {
|
|
|
|
valuePerY := (self.grid.valueExtremum.max - self.grid.valueExtremum.min) / float64(drawArea.Dy()-2)
|
2019-02-03 03:30:45 +00:00
|
|
|
y = int(float64(timePoint.value-self.grid.valueExtremum.min) / valuePerY)
|
2019-02-02 04:39:34 +00:00
|
|
|
}
|
2019-01-28 23:09:52 +00:00
|
|
|
|
2019-02-02 04:39:34 +00:00
|
|
|
point := image.Pt(x, drawArea.Max.Y-y-1)
|
2019-01-28 23:09:52 +00:00
|
|
|
|
2019-01-31 04:15:15 +00:00
|
|
|
if _, exists := xToPoint[x]; exists {
|
|
|
|
continue
|
|
|
|
}
|
2019-01-28 23:09:52 +00:00
|
|
|
|
2019-02-02 04:39:34 +00:00
|
|
|
if !point.In(drawArea) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
xToPoint[x] = point
|
2019-01-31 04:15:15 +00:00
|
|
|
pointsOrder = append(pointsOrder, x)
|
|
|
|
}
|
2019-01-28 23:09:52 +00:00
|
|
|
|
2019-01-31 04:15:15 +00:00
|
|
|
for i, x := range pointsOrder {
|
2019-01-28 23:09:52 +00:00
|
|
|
|
2019-01-31 04:15:15 +00:00
|
|
|
currentPoint := xToPoint[x]
|
|
|
|
var previousPoint image.Point
|
2019-01-28 23:09:52 +00:00
|
|
|
|
2019-01-31 04:15:15 +00:00
|
|
|
if i == 0 {
|
|
|
|
previousPoint = currentPoint
|
|
|
|
} else {
|
|
|
|
previousPoint = xToPoint[pointsOrder[i-1]]
|
|
|
|
}
|
2019-01-28 23:09:52 +00:00
|
|
|
|
2019-01-31 04:15:15 +00:00
|
|
|
canvas.Line(
|
|
|
|
braillePoint(previousPoint),
|
|
|
|
braillePoint(currentPoint),
|
2019-02-03 03:30:45 +00:00
|
|
|
line.color,
|
2019-01-31 04:15:15 +00:00
|
|
|
)
|
|
|
|
}
|
2019-01-28 23:09:52 +00:00
|
|
|
}
|
|
|
|
|
2019-02-01 05:07:25 +00:00
|
|
|
canvas.Draw(buffer)
|
2019-01-28 23:09:52 +00:00
|
|
|
}
|
|
|
|
|
2019-02-02 04:39:34 +00:00
|
|
|
func (self *RunChart) renderAxes(buffer *Buffer) {
|
|
|
|
// draw origin cell
|
|
|
|
buffer.SetCell(
|
|
|
|
NewCell(BOTTOM_LEFT, NewStyle(ColorWhite)),
|
|
|
|
image.Pt(self.Inner.Min.X+self.grid.minTimeWidth, self.Inner.Max.Y-xAxisLabelsHeight-1),
|
|
|
|
)
|
|
|
|
|
|
|
|
// draw x axis line
|
|
|
|
for i := self.grid.minTimeWidth + 1; i < self.Inner.Dx(); i++ {
|
|
|
|
buffer.SetCell(
|
|
|
|
NewCell(HORIZONTAL_DASH, NewStyle(ColorWhite)),
|
|
|
|
image.Pt(i+self.Inner.Min.X, self.Inner.Max.Y-xAxisLabelsHeight-1),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// draw grid lines
|
|
|
|
for y := 0; y < self.Inner.Dy()-xAxisLabelsHeight-2; y = y + 2 {
|
|
|
|
for x := 1; x <= self.grid.linesCount; x++ {
|
|
|
|
buffer.SetCell(
|
|
|
|
NewCell(VERTICAL_DASH, NewStyle(console.ColorDarkGrey)),
|
2019-02-04 04:03:59 +00:00
|
|
|
image.Pt(self.grid.maxTimeWidth-x*xAxisGridWidth, y+self.Inner.Min.Y+1),
|
2019-02-02 04:39:34 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// draw y axis line
|
|
|
|
for i := 0; i < self.Inner.Dy()-xAxisLabelsHeight-1; i++ {
|
|
|
|
buffer.SetCell(
|
|
|
|
NewCell(VERTICAL_DASH, NewStyle(ColorWhite)),
|
|
|
|
image.Pt(self.Inner.Min.X+self.grid.minTimeWidth, i+self.Inner.Min.Y),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// draw x axis time labels
|
|
|
|
for i := 1; i <= self.grid.linesCount; i++ {
|
2019-02-04 04:03:59 +00:00
|
|
|
labelTime := self.grid.timeExtremum.max.Add(time.Duration(-i) * self.timescale)
|
2019-02-02 04:39:34 +00:00
|
|
|
buffer.SetString(
|
|
|
|
labelTime.Format("15:04:05"),
|
|
|
|
NewStyle(ColorWhite),
|
2019-02-04 04:03:59 +00:00
|
|
|
image.Pt(self.grid.maxTimeWidth-xAxisLabelsWidth/2-i*(xAxisGridWidth), self.Inner.Max.Y-1),
|
2019-02-02 04:39:34 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// draw y axis labels
|
|
|
|
if self.grid.valueExtremum.max != self.grid.valueExtremum.min {
|
|
|
|
labelsCount := (self.Inner.Dy() - xAxisLabelsHeight - 1) / (yAxisLabelsGap + yAxisLabelsHeight)
|
|
|
|
valuePerY := (self.grid.valueExtremum.max - self.grid.valueExtremum.min) / float64(self.Inner.Dy()-xAxisLabelsHeight-3)
|
|
|
|
for i := 0; i < int(labelsCount); i++ {
|
|
|
|
value := self.grid.valueExtremum.max - (valuePerY * float64(i) * (yAxisLabelsGap + yAxisLabelsHeight))
|
|
|
|
buffer.SetString(
|
|
|
|
formatValue(value, self.precision),
|
|
|
|
NewStyle(ColorWhite),
|
|
|
|
image.Pt(self.Inner.Min.X, 1+self.Inner.Min.Y+i*(yAxisLabelsGap+yAxisLabelsHeight)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
buffer.SetString(
|
|
|
|
formatValue(self.grid.valueExtremum.max, self.precision),
|
|
|
|
NewStyle(ColorWhite),
|
|
|
|
image.Pt(self.Inner.Min.X, self.Inner.Dy()/2))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-03 03:30:45 +00:00
|
|
|
func (self *RunChart) renderLegend(buffer *Buffer, rectangle image.Rectangle, selectedPoints []TimePoint) {
|
2019-01-28 23:09:52 +00:00
|
|
|
|
2019-02-03 03:30:45 +00:00
|
|
|
for i, line := range self.lines {
|
2019-01-28 23:09:52 +00:00
|
|
|
|
2019-02-01 05:07:25 +00:00
|
|
|
buffer.SetString(
|
2019-02-02 04:39:34 +00:00
|
|
|
string(DOT),
|
2019-02-03 03:30:45 +00:00
|
|
|
NewStyle(line.color),
|
2019-02-01 05:07:25 +00:00
|
|
|
image.Pt(self.Inner.Max.X-xAxisLegendWidth-2, self.Inner.Min.Y+1+i*5),
|
2019-01-28 23:09:52 +00:00
|
|
|
)
|
2019-02-01 05:07:25 +00:00
|
|
|
buffer.SetString(
|
2019-02-03 03:30:45 +00:00
|
|
|
fmt.Sprintf("%s", line.label),
|
|
|
|
NewStyle(line.color),
|
2019-02-01 05:07:25 +00:00
|
|
|
image.Pt(self.Inner.Max.X-xAxisLegendWidth, self.Inner.Min.Y+1+i*5),
|
2019-01-28 23:09:52 +00:00
|
|
|
)
|
2019-02-03 03:30:45 +00:00
|
|
|
|
|
|
|
if len(selectedPoints) > 0 {
|
|
|
|
|
|
|
|
index := -1
|
|
|
|
|
|
|
|
for i, p := range selectedPoints {
|
|
|
|
if p.line.label == line.label {
|
|
|
|
index = i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if index != -1 {
|
|
|
|
buffer.SetString(
|
|
|
|
fmt.Sprintf("time: %v", selectedPoints[index].time.Format("15:04:05.000")),
|
|
|
|
NewStyle(ColorWhite),
|
|
|
|
image.Pt(self.Inner.Max.X-xAxisLegendWidth, self.Inner.Min.Y+2+i*5),
|
|
|
|
)
|
|
|
|
buffer.SetString(
|
|
|
|
fmt.Sprintf("value: %s", formatValue(selectedPoints[index].value, self.precision)),
|
|
|
|
NewStyle(ColorWhite),
|
|
|
|
image.Pt(self.Inner.Max.X-xAxisLegendWidth, self.Inner.Min.Y+3+i*5),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
} else {
|
2019-02-04 04:03:59 +00:00
|
|
|
extremum := getLineValueExtremum(line.points)
|
2019-02-03 03:30:45 +00:00
|
|
|
|
|
|
|
buffer.SetString(
|
|
|
|
fmt.Sprintf("cur %s", formatValue(line.points[len(line.points)-1].value, self.precision)),
|
|
|
|
NewStyle(ColorWhite),
|
|
|
|
image.Pt(self.Inner.Max.X-xAxisLegendWidth, self.Inner.Min.Y+2+i*5),
|
|
|
|
)
|
|
|
|
buffer.SetString(
|
|
|
|
fmt.Sprintf("max %s", formatValue(extremum.max, self.precision)),
|
|
|
|
NewStyle(ColorWhite),
|
|
|
|
image.Pt(self.Inner.Max.X-xAxisLegendWidth, self.Inner.Min.Y+3+i*5),
|
|
|
|
)
|
|
|
|
buffer.SetString(
|
|
|
|
fmt.Sprintf("min %s", formatValue(extremum.min, self.precision)),
|
|
|
|
NewStyle(ColorWhite),
|
|
|
|
image.Pt(self.Inner.Max.X-xAxisLegendWidth, self.Inner.Min.Y+4+i*5),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *RunChart) renderSelection(buffer *Buffer, drawArea image.Rectangle, selectedPoints []TimePoint) {
|
|
|
|
|
|
|
|
for _, timePoint := range selectedPoints {
|
|
|
|
|
|
|
|
timeDeltaWithGridMaxTime := self.grid.timeExtremum.max.Sub(timePoint.time).Nanoseconds()
|
2019-02-04 04:03:59 +00:00
|
|
|
timeDeltaToPaddingRelation := float64(timeDeltaWithGridMaxTime) / float64(self.timescale.Nanoseconds())
|
|
|
|
x := self.grid.maxTimeWidth - (int(float64(xAxisGridWidth) * timeDeltaToPaddingRelation))
|
2019-02-03 03:30:45 +00:00
|
|
|
|
|
|
|
var y int
|
|
|
|
if self.grid.valueExtremum.max-self.grid.valueExtremum.min == 0 {
|
|
|
|
y = (drawArea.Dy() - 2) / 2
|
|
|
|
} else {
|
|
|
|
valuePerY := (self.grid.valueExtremum.max - self.grid.valueExtremum.min) / float64(drawArea.Dy()-2)
|
|
|
|
y = int(float64(timePoint.value-self.grid.valueExtremum.min) / valuePerY)
|
|
|
|
}
|
|
|
|
|
|
|
|
point := image.Pt(x, drawArea.Max.Y-y-1)
|
|
|
|
|
|
|
|
if point.In(drawArea) {
|
|
|
|
buffer.SetCell(NewCell('▲', NewStyle(timePoint.line.color)), point)
|
|
|
|
}
|
2019-01-28 23:09:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-02 04:39:34 +00:00
|
|
|
func (self *RunChart) getMaxValueLength() int {
|
|
|
|
|
|
|
|
maxValueLength := 0
|
|
|
|
|
|
|
|
for _, line := range self.lines {
|
|
|
|
for _, point := range line.points {
|
2019-02-03 03:30:45 +00:00
|
|
|
l := len(formatValue(point.value, self.precision))
|
2019-02-02 04:39:34 +00:00
|
|
|
if l > maxValueLength {
|
|
|
|
maxValueLength = l
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return maxValueLength
|
|
|
|
}
|
|
|
|
|
|
|
|
func formatValue(value float64, precision int) string {
|
2019-02-03 03:30:45 +00:00
|
|
|
format := "%." + strconv.Itoa(precision) + "f"
|
2019-02-02 04:39:34 +00:00
|
|
|
return fmt.Sprintf(format, value)
|
2019-02-01 05:07:25 +00:00
|
|
|
}
|
|
|
|
|
2019-02-04 04:03:59 +00:00
|
|
|
func getChartValueExtremum(items []TimeLine) ValueExtremum {
|
2019-01-28 23:09:52 +00:00
|
|
|
|
2019-01-31 04:15:15 +00:00
|
|
|
if len(items) == 0 {
|
2019-01-31 01:41:51 +00:00
|
|
|
return ValueExtremum{0, 0}
|
2019-01-28 23:09:52 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 04:15:15 +00:00
|
|
|
var max, min = -math.MaxFloat64, math.MaxFloat64
|
2019-01-28 23:09:52 +00:00
|
|
|
|
2019-01-31 04:15:15 +00:00
|
|
|
for _, item := range items {
|
2019-01-31 23:40:05 +00:00
|
|
|
for _, point := range item.points {
|
2019-02-03 03:30:45 +00:00
|
|
|
if point.value > max {
|
|
|
|
max = point.value
|
2019-01-31 04:15:15 +00:00
|
|
|
}
|
2019-02-03 03:30:45 +00:00
|
|
|
if point.value < min {
|
|
|
|
min = point.value
|
2019-01-31 04:15:15 +00:00
|
|
|
}
|
2019-01-28 23:09:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-31 04:15:15 +00:00
|
|
|
return ValueExtremum{max: max, min: min}
|
2019-01-31 01:41:51 +00:00
|
|
|
}
|
|
|
|
|
2019-02-04 04:03:59 +00:00
|
|
|
func getLineValueExtremum(points []TimePoint) ValueExtremum {
|
2019-02-01 05:07:25 +00:00
|
|
|
|
|
|
|
if len(points) == 0 {
|
|
|
|
return ValueExtremum{0, 0}
|
|
|
|
}
|
|
|
|
|
|
|
|
var max, min = -math.MaxFloat64, math.MaxFloat64
|
|
|
|
|
|
|
|
for _, point := range points {
|
2019-02-03 03:30:45 +00:00
|
|
|
if point.value > max {
|
|
|
|
max = point.value
|
2019-02-01 05:07:25 +00:00
|
|
|
}
|
2019-02-03 03:30:45 +00:00
|
|
|
if point.value < min {
|
|
|
|
min = point.value
|
2019-02-01 05:07:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ValueExtremum{max: max, min: min}
|
|
|
|
}
|
|
|
|
|
2019-02-04 04:03:59 +00:00
|
|
|
func getTimeExtremum(linesCount int, scale time.Duration) TimeExtremum {
|
2019-01-31 01:41:51 +00:00
|
|
|
maxTime := time.Now()
|
|
|
|
return TimeExtremum{
|
|
|
|
max: maxTime,
|
2019-02-04 04:03:59 +00:00
|
|
|
min: maxTime.Add(-time.Duration(scale.Nanoseconds() * int64(linesCount))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func calculateTimescale(refreshRateMs int) time.Duration {
|
|
|
|
|
|
|
|
multiplier := refreshRateMs * xAxisGridWidth / 2
|
|
|
|
timescale := time.Duration(time.Millisecond * time.Duration(multiplier)).Round(time.Second)
|
|
|
|
|
|
|
|
if timescale.Seconds() == 0 {
|
|
|
|
return time.Second
|
|
|
|
} else {
|
|
|
|
return timescale
|
2019-01-31 01:41:51 +00:00
|
|
|
}
|
2019-01-28 23:09:52 +00:00
|
|
|
}
|