red-data-tools / unicode_plot.rb

Plot your data by Unicode characters
MIT License
246 stars 12 forks source link

How to get the available canvas types? #42

Closed kojix2 closed 3 years ago

kojix2 commented 3 years ago

Currently it appears to be provided by the case statement. I would like to have a way to externally access the list of symbols that represent the Canvas.

https://github.com/red-data-tools/unicode_plot.rb/blob/a22f1293a63225003fabafa3acb81f45e301fed8/lib/unicode_plot/canvas.rb#L5-L18

nanobowers commented 3 years ago

Maybe there we could define a Hash constant (CANVAS_MAP?) with mapping of Symbol to Class name and dispatch that way instead of a case? I think it would work as long as the method call is consistent for all canvases e.g.: klass.new(width, height, **kw)

And also provide a public method to (self.canvases or self.canvas_list) get the list of available canvasses (keys of CANVAS_MAP), to give the Symbol list you desire.

Or did you have something else in mind?

kojix2 commented 3 years ago

Hi @nanobowers. Yes, you are right. That's what I was thinking about.

mrkn commented 3 years ago

I'll fix it.

kojix2 commented 3 years ago

This issue has been resolved. Thank you for your work.

Nakilon commented 3 years ago

I'm not sure why to have the map at all if there is

https://stackoverflow.com/a/6084914/322020 https://docs.ruby-lang.org/en/master/ObjectSpace.html#method-c-each_object

irb> ObjectSpace.each_object(Numeric.singleton_class).to_a
=> [Complex, Rational, Bignum, Float, Fixnum, Integer, Numeric]

So:

$ irb -runicode_plot
irb> ObjectSpace.each_object(UnicodePlot::Canvas.singleton_class).to_a
=> [UnicodePlot::Canvas, UnicodePlot::DotCanvas, UnicodePlot::DensityCanvas, UnicodePlot::BrailleCanvas, UnicodePlot::BlockCanvas, UnicodePlot::AsciiCanvas, UnicodePlot::LookupCanvas]
mrkn commented 3 years ago

@Nakilon Why do you thought ObjectSpace.each_object is appropriate here? This method scans all the object slots in the heap. I don’t accept that computational complexity.

ObjectSpace.each_object should not be used easily in the library.

Nakilon commented 3 years ago

@mrkn I'm not saying it should be used in a library. @kojix2 wanted to know which canvas types are available and that's how he can do that.

mrkn commented 3 years ago

@Nakilon, I'm sorry for misreading. I guess @kojix2 didn't want to know there were canvas classes, but to know what canvas names were available.

kojix2 commented 3 years ago

@Nakilon Thanks. ObjectSpace.each_object is very interesting one. I did not know about this method. I appreciate your help.

use case

However, as @mrkn said, what I really wanted to do was to get symbols to represent the canvases and show the list of available canvases in the YouPlot command line options help.

uplot line --help
Usage: YouPlot line [options] <in.tsv>

Options for line:
        --xlim FLOAT,FLOAT  plotting range for the x coordinate
        --ylim FLOAT,FLOAT  plotting range for the y coordinate
        --fmt STR           xy : header is like x, y...
                            yx : header is like y, x...
        --[no-]grid         draws grid-lines at the origin
        --canvas STR        type of canvas
                            (ascii, block, braille, density, dot)  <<<<<< ◆ here

Common options:
...

This is what it looks like when you actually specify the --canvas option. This is the number of new positive cases of coronavirus in Japan, and the lineplot is displayed very nicely.

image