gizak / termui

Golang terminal dashboard
MIT License
13.14k stars 787 forks source link

Allow to plot datapoints with both coordinates defined. Closes #68 #78

Closed missingdays closed 5 years ago

missingdays commented 8 years ago

Allows users to define both x and y coordinates for linechart. E.g. this will plot line with three "main" points at (4, 1), (6,4) and (10,2) with linear interpolation in between.

lc.Data = [1, 4, 2]
lc.IsXS = true
lc.XS = [4, 6, 10]

This closes #68.

mranney commented 8 years ago

@missingdays what do you think about an API like this:

Data map[string][][]float64

// example:
lc.Data["name"] = [[1, 1.2], [2, 1.3], [7,1.5], [8,1.7], [30, 1.5], [35,1.6], [40,1.8], [41, 2.0]]

That way we can support multiple named series and each series can specify its X values individually.

mranney commented 8 years ago

Of course, I forgot that Go doesn't allow multi-dimensional slice literals like that, but that'd be the layout of the data once you wrangled creating the slices. Presumably users are getting this data from some other format anyway and then transforming it into whatever format termui wants, so I imagine the lack of literal syntax only makes the code in the readme more complicated.

missingdays commented 8 years ago

@mranney yes, if multiple series is a thing then the API you are suggesting is better, though my API doesn't brake the existing one. That was the whole point when I was writing it. Changing from one API to another doesn't seem to be that hard in this case. The more important is to decide which one is better.

mranney commented 8 years ago

Did you see my PR for multiple series? #81

I've been using it in my application, and it's pretty useful. That's why I suggested an API that merges the two features.

mranney commented 8 years ago

@missingdays I'm moving forward with building my app on top of my fork that's here:

https://github.com/uber-common/termui

If you are interested in adapting your work to my multi-series work, we could work on it there. I have a few other things that I'm planning on fixing there as well. Hopefully this will be in line with what @gizak wants with the project so we can merge it all back together at some point.

cjbassi commented 5 years ago

Hi, thanks for this PR! So I just finished up rewriting most of termui, and part of that included rewriting LineChart to enable scatter plotting. So now LineChart has been renamed to Plot, and you can specify either LineChart or ScatterPlot as the Type. It also supports either Dot or Braille Markers, and it multiple data lines. You can check out the plot.go example for a full demonstration of the different ways of configuring things. Hopefully it works well but let me know if there's any issues with it.

mosiman commented 4 years ago

Hi, I'm looking at the plot.go example and I really am not seeing how this allows you to plot coordinates. Did the intent of this PR change between the original PR and the v3 rewrite?

For example, this snippet plots multiple lines:

p3 := widgets.NewPlot()               
p3.Title = "braille-mode Scatter Plot"
p3.Data = make([][]float64, 2)        
p3.Data[0] = []float64{1, 2, 3, 4, 5} 
p3.Data[1] = sinData[1][4:]           
p3.SetRect(45, 15, 80, 30)            
p3.AxesColor = ui.ColorWhite          
p3.LineColors[0] = ui.ColorCyan       
p3.Marker = widgets.MarkerBraille     
p3.PlotType = widgets.ScatterPlot     

From what I can tell here, the "x coordinates" are simply the indices of the slices of the data points.

If the api were to allow plotting coordinates, I would expect something like

p2 := widgets.NewPlot()                 
p2.Title = "dot-mode Scatter Plot"      
p2.Marker = widgets.MarkerDot           
p2.Data = make([][]float64, 2)          
p2.Data[0] = []float64{5, 4, 3, 2, 1}   
p2.Data[1] = []float64{5, 4, 3, 2, 1}   
p2.SetRect(0, 15, 50, 30)               
p2.AxesColor = ui.ColorWhite            
p2.LineColors[0] = ui.ColorCyan         
p2.PlotType = widgets.ScatterPlot       

to plot what looks like the line f(x) = x, since if we are indeed talking about plotting coordinates, the coordinates would be (5,5), (4,4), (3,3), (2,2), (1,1). However, this plots what appears to be one line, f(x) = -x.