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
where
top = maximum xs
top = maximum xs
bot = minimum xs
bot = minimum xs
range = top - bot
range = top - bot
cl x = chr $ 0x2581 + round ((x - bot) / range * 7)
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
where parsed = map read $ filter (not . null) $ splitOneOf " ," xs
parsed = map read $ filter (not . null) $ splitOneOf " ," xs

data Stats = Stats { minValue, maxValue, rangeOfValues :: Double,
data Stats = Stats
numberOfValues :: Int }
{ minValue, maxValue, rangeOfValues :: Double
, numberOfValues :: Int
}


instance Show Stats where
instance Show Stats where
show (Stats mn mx r n) = "min: " ++ show mn ++ "; max: " ++ show mx ++
show (Stats mn mx r n) =
"min: " ++
"; range: " ++ show r ++ "; no. of values: " ++ show n
show mn ++
"; max: " ++
show mx ++ "; range: " ++ show r ++ "; no. of values: " ++ show n

stats :: [Double] -> Stats
stats :: [Double] -> Stats
stats xs = Stats { minValue = mn, maxValue = mx,
stats xs =
Stats
rangeOfValues = mx - mn, numberOfValues = length xs }
where
{ minValue = mn
mn = minimum xs
, maxValue = mx
mx = maximum xs
, rangeOfValues = mx - mn
, numberOfValues = length xs
}
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
where (sp, st) = makeSparkLine xs
(sp, st) = makeSparkLine xs


main :: IO ()
main :: IO ()
main =
main = mapM_ drawSparkLineWithStats
mapM_
["1 2 3 4 5 6 7 8 7 6 5 4 3 2 1",
drawSparkLineWithStats
"1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5",
"3 2 1 0 -1 -2 -3 -4 -3 -2 -1 0 1 2 3",
[ "0, 1, 19, 20"
, "0, 999, 4000, 4999, 7000, 7999"
"-1000 100 1000 500 200 -400 -700 621 -189 3"]
, "1 2 3 4 5 6 7 8 7 6 5 4 3 2 1"
</lang>
, "1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5"
, "3 2 1 0 -1 -2 -3 -4 -3 -2 -1 0 1 2 3"
, "-1000 100 1000 500 200 -400 -700 621 -189 3"
]</lang>
{{Out}}
{{Out}}
<pre>▁▁██
<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>