File: scatterplot.rb

package info (click to toggle)
ruby-unicode-plot 0.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,492 kB
  • sloc: ruby: 2,871; makefile: 4
file content (49 lines) | stat: -rw-r--r-- 1,255 bytes parent folder | download | duplicates (2)
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
module UnicodePlot
  class Scatterplot < GridPlot
  end

  module_function def scatterplot(*args,
                                  canvas: :braille,
                                  color: :auto,
                                  name: "",
                                  **kw)
    case args.length
    when 1
      # y only
      y = Array(args[0])
      x = Array(1 .. y.length)
    when 2
      # x and y
      x = Array(args[0])
      y = Array(args[1])
    else
      raise ArgumentError, "worng number of arguments"
    end

    plot = Scatterplot.new(x, y, canvas, **kw)
    scatterplot!(plot, x, y, color: color, name: name)
  end

  module_function def scatterplot!(plot,
                                   *args,
                                   color: :auto,
                                   name: "")
    case args.length
    when 1
      # y only
      y = Array(args[0])
      x = Array(1 .. y.length)
    when 2
      # x and y
      x = Array(args[0])
      y = Array(args[1])
    else
      raise ArgumentError, "worng number of arguments"
    end

    color = color == :auto ? plot.next_color : color
    plot.annotate!(:r, name.to_s, color: color) unless name.nil? || name == ""
    plot.points!(x, y, color)
    plot
  end
end