Sparkline in unicode: Difference between revisions
Content added Content deleted
(→{{header|Python}}: Redraft of second Python version.) |
(→{{header|Haskell}}: Edit to older Haskell draft – added two test cases, adjusted indexing to normalize their sparkline representation) |
||
Line 768: | Line 768: | ||
toSparkLine :: [Double] -> [Char] |
toSparkLine :: [Double] -> [Char] |
||
toSparkLine xs = map cl xs |
toSparkLine xs = map cl xs |
||
where |
|||
top = maximum xs |
|||
bot = minimum xs |
|||
range = top - bot |
|||
cl x = chr $ 0x2581 + floor (min 7((x - bot) / range * 8)) |
|||
makeSparkLine :: String -> (String, Stats) |
makeSparkLine :: String -> (String, Stats) |
||
makeSparkLine xs = (toSparkLine parsed, stats parsed) |
makeSparkLine xs = (toSparkLine parsed, stats parsed) |
||
where |
|||
parsed = map read $ filter (not . null) $ splitOneOf " ," xs |
|||
⚫ | |||
⚫ | |||
data Stats = Stats |
|||
⚫ | |||
⚫ | |||
⚫ | |||
} |
|||
instance Show Stats where |
instance Show Stats where |
||
show (Stats mn mx r n) = |
|||
"min: " ++ |
|||
⚫ | |||
show mn ++ |
|||
⚫ | |||
⚫ | |||
stats :: [Double] -> Stats |
stats :: [Double] -> Stats |
||
stats xs = |
stats xs = |
||
Stats |
|||
⚫ | |||
{ minValue = mn |
|||
, maxValue = mx |
|||
, rangeOfValues = mx - mn |
|||
⚫ | |||
} |
|||
where |
|||
mn = minimum xs |
|||
mx = maximum xs |
|||
drawSparkLineWithStats :: String -> IO () |
drawSparkLineWithStats :: String -> IO () |
||
drawSparkLineWithStats xs = putStrLn sp >> print st |
drawSparkLineWithStats xs = putStrLn sp >> print st |
||
where |
|||
(sp, st) = makeSparkLine xs |
|||
main :: IO () |
main :: IO () |
||
main = |
|||
⚫ | |||
mapM_ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
" |
[ "0, 1, 19, 20" |
||
, "0, 999, 4000, 4999, 7000, 7999" |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
, "3 2 1 0 -1 -2 -3 -4 -3 -2 -1 0 1 2 3" |
|||
⚫ | |||
⚫ | |||
{{Out}} |
{{Out}} |
||
<pre>▁▁██ |
|||
⚫ | |||
min: 0.0; max: 20.0; range: 20.0; no. of values: 4 |
|||
▁▁▅▅██ |
|||
min: 0.0; max: 7999.0; range: 7999.0; no. of values: 6 |
|||
⚫ | |||
min: 1.0; max: 8.0; range: 7.0; no. of values: 15 |
min: 1.0; max: 8.0; range: 7.0; no. of values: 15 |
||
▂▁▄▃▆▅█▇ |
▂▁▄▃▆▅█▇ |
||
Line 810: | Line 830: | ||
█▇▆▅▄▃▂▁▂▃▄▅▆▇█ |
█▇▆▅▄▃▂▁▂▃▄▅▆▇█ |
||
min: -4.0; max: 3.0; range: 7.0; no. of values: 15 |
min: -4.0; max: 3.0; range: 7.0; no. of values: 15 |
||
▁▅█▇▅▃▂▇▄▅ |
|||
▁▅█▆▅▃▂▇▄▅ |
|||
min: -1000.0; max: 1000.0; range: 2000.0; no. of values: 10</pre> |
min: -1000.0; max: 1000.0; range: 2000.0; no. of values: 10</pre> |
||