@@ -4,6 +4,12 @@ import strformat
44import tempfile
55import os
66
7+ func numberToStr [T](data: seq [T]): seq [string ] = data.map (proc (k:T):string = $ k)
8+
9+ func makeList [T](data: seq [T]): string =
10+ let str = numberToStr (data).join (" ," )
11+ return fmt" [{ str} ] "
12+
713type Plot = ref object of RootObj
814method render (this: Plot ): string {.base .} = " "
915
@@ -43,14 +49,46 @@ proc newFigure*(): Figure =
4349proc add * (figure: Figure , plot: Plot ) =
4450 figure.plots.add plot
4551
52+ # Line plots
4653type LinePlot [A, B] = ref object of Plot
4754 linestyle* : string
55+ colour* : string
4856 x* : seq [A]
4957 y* : seq [B]
5058method render [A,B](this: LinePlot [A,B]): string =
51- let xs = this.x.map (proc (k:A):string = $ k).join (" ," )
52- let ys = this.y.map (proc (k:B):string = $ k).join (" ," )
53- return fmt" plt.plot([{ xs} ],[{ ys} ]) "
59+ let xs = makeList (this.x)
60+ let ys = makeList (this.y)
61+ var options: seq [string ] = @ []
62+ if this.linestyle!= " " :
63+ options.add fmt" linestyle='{ this.linestyle} ' "
64+ if this.colour!= " " :
65+ options.add fmt" color='{ this.colour} ' "
66+ if len (options)> 0 :
67+ let optstr = options.join (" ," )
68+ return fmt" plt.plot({ xs} ,{ ys} ,{ optstr} ) "
69+ else :
70+ return fmt" plt.plot({ xs} ,{ ys} ) "
5471
5572proc newLinePlot * [A,B](x: seq [A], y: seq [B]): LinePlot [A, B] =
56- LinePlot [A, B](linestyle: " -" , x: x, y: y)
73+ LinePlot [A, B](linestyle: " " , colour: " " , x: x, y: y)
74+
75+ # Scatter plots
76+ type ScatterPlot [A,B] = ref object of Plot
77+ colour* : string
78+ x* : seq [A]
79+ y* : seq [B]
80+ method render [A,B](this: ScatterPlot [A,B]): string =
81+ let xs = makeList (this.x)
82+ let ys = makeList (this.y)
83+ var options: seq [string ] = @ []
84+ if this.colour!= " " :
85+ options.add fmt" color='{ this.colour} ' "
86+ if len (options)> 0 :
87+ let optstr = options.join (" ," )
88+ return fmt" plt.scatter({ xs} ,{ ys} ,{ optstr} ) "
89+ else :
90+ return fmt" plt.scatter({ xs} ,{ ys} ) "
91+
92+ proc newScatterPlot * [A,B](x: seq [A], y: seq [B]): ScatterPlot [A, B] =
93+ ScatterPlot [A, B](colour: " " , x: x, y: y)
94+
0 commit comments