MCPcopy Create free account
hub / github.com/codechenx/FastTableViewer / getPlot

Method getPlot

stats.go:287–324  ·  view source on GitHub ↗

getPlot generates a bar chart visualization for discrete data

()

Source from the content-addressed store, hash-verified

285
286// getPlot generates a bar chart visualization for discrete data
287func (s *DiscreteStats) getPlot() string {
288 if len(s.counter) == 0 {
289 return "No data to plot"
290 }
291
292 // Sort by frequency
293 type kv struct {
294 Key string
295 Value int
296 }
297 var ss []kv
298 for k, v := range s.counter {
299 ss = append(ss, kv{k, v})
300 }
301 sort.Slice(ss, func(i, j int) bool {
302 return ss[i].Value > ss[j].Value
303 })
304
305 // Take top 15 values for the plot
306 maxDisplay := 15
307 if len(ss) < maxDisplay {
308 maxDisplay = len(ss)
309 }
310
311 // Prepare data for plotting
312 frequencies := make([]float64, maxDisplay)
313 for i := 0; i < maxDisplay; i++ {
314 frequencies[i] = float64(ss[i].Value)
315 }
316
317 // Generate the plot
318 plot := asciigraph.Plot(frequencies,
319 asciigraph.Height(12),
320 asciigraph.Width(60),
321 asciigraph.Caption("Top Values Frequency"))
322
323 return plot
324}

Callers 2

Calls

no outgoing calls

Tested by 2