Sparkline in unicode: Difference between revisions
m
→{{header|Wren}}: Minor tidy
m (→{{header|Wren}}: Minor tidy) |
|||
(18 intermediate revisions by 15 users not shown) | |||
Line 36:
=={{header|APL}}==
Note → this is in a 0-indexed version of APL
'''Solution''':<
'''Example''':<
▂▃▄▅▅▆▇█▇▆▅▅▄▃▂
sparkln 1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5
▂▁▄▃▆▅█▇
</syntaxhighlight>
Note → APL accepts the input with commas and spaces naturally. If one wanted to read input as a string they could use ⍎⍞ to do so.
=={{header|AppleScript}}==
<
use framework "Foundation"
use scripting additions
Line 360:
set my text item delimiters to dlm
return s
end unwords</
{{Out}}
<pre>▁▁██
Line 389:
Median 4.0
Max 7.5</pre>
=={{header|Arturo}}==
<syntaxhighlight lang="rebol">bar: "▁▂▃▄▅▆▇█"
barcount: to :floating dec size bar
while ø [
line: input "Numbers separated by spaces: "
numbers: to [:floating] split.words line
mn: min numbers
mx: max numbers
extent: mx-mn
sparkLine: new ""
loop numbers 'n [
i: to :integer barcount*(n-mn)//extent
'sparkLine ++ bar\[i]
]
print ["min:" round.to:1 mn "max:" round.to:1 mx]
print sparkLine
print ""
]</syntaxhighlight>
{{out}}
<pre>Numbers separated by spaces: 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
min: 1.0 max: 8.0
▁▂▃▄▅▆▇█▇▆▅▄▃▂▁
Numbers separated by spaces: 1.5 0.5 3.5 2.5 5.5 4.5 7.5 6.5
min: 0.5 max: 7.5
▂▁▄▃▆▅█▇
</pre>
=={{header|AutoHotkey}}==
{{works with|AutoHotkey_L}}
<
strings := ["1 2 3 4 5 6 7 8 7 6 5 4 3 2 1"
, "1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5"]
Line 414 ⟶ 446:
Chars .= Chr(0x2581 + Round(7 * (A_LoopField - Min) / Rng))
return, {"Min": Min, "Max": Max, "Rng": Rng, "Chars": Chars}
}</
{{out}}
<pre>Min: 1, Max: 8, Range: 7
Line 421 ⟶ 453:
Min: 0.5, Max: 7.5, Range: 7.0
▂▁▄▃▆▅█▇</pre>
=={{header|BASIC}}==
==={{header|VBScript}}===
<syntaxhighlight lang="vb">
'Sparkline
sub ensure_cscript()
if instrrev(ucase(WScript.FullName),"WSCRIPT.EXE")then
createobject("wscript.shell").run "CSCRIPT //nologo """ &_
WScript.ScriptFullName &"""" ,,0
wscript.quit
end if
end sub
class bargraph
private bar,mn,mx,nn,cnt
Private sub class_initialize()
bar=chrw(&h2581)&chrw(&h2582)&chrw(&h2583)&chrw(&h2584)&chrw(&h2585)&_
chrw(&h2586)&chrw(&h2587)&chrw(&h2588)
nn=8
end sub
public function bg (s)
a=split(replace(replace(s,","," ")," "," ")," ")
mn=999999:mx=-999999:cnt=ubound(a)+1
for i=0 to ubound(a)
a(i)=cdbl(trim(a(i)))
if a(i)>mx then mx=a(i)
if a(i)<mn then mn=a(i)
next
ss="Data: "
for i=0 to ubound(a) :ss=ss & right (" "& a(i),6) :next
ss=ss+vbcrlf + "sparkline: "
for i=0 to ubound(a)
x=scale(a(i))
'wscript.echo mn,mx, a(i),x
ss=ss & string(6,mid(bar,x,1))
next
bg=ss &vbcrlf & "min: "&mn & " max: "& mx & _
" cnt: "& ubound(a)+1 &vbcrlf
end function
private function scale(x)
if x=<mn then
scale=1
elseif x>=mx then
scale=nn
else
scale=int(nn* (x-mn)/(mx-mn)+1)
end if
end function
end class
ensure_cscript
set b=new bargraph
wscript.stdout.writeblanklines 2
wscript.echo b.bg("1, 2, 3, 4, 5, 6, 7, 8, 7, 6, 5, 4, 3, 2, 1")
wscript.echo b.bg("1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5")
wscript.echo b.bg("0, 1, 19, 20")
wscript.echo b.bg("0, 999, 4000, 4999, 7000, 7999")
set b=nothing
wscript.echo "If bars don't display correctly please set the the console " & _
"font to DejaVu Sans Mono or any other that has the bargrph characters" & _
vbcrlf
wscript.stdout.write "Press any key.." : wscript.stdin.read 1
</syntaxhighlight>
{{out}}
<pre>
Data: 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
sparkline: ▁▁▁▁▁▁▂▂▂▂▂▂▃▃▃▃▃▃▄▄▄▄▄▄▅▅▅▅▅▅▆▆▆▆▆▆▇▇▇▇▇▇██████▇▇▇▇▇▇▆▆▆▆▆▆▅▅▅▅▅▅▄▄▄▄▄▄▃▃▃▃▃▃▂▂▂▂▂▂▁▁▁▁▁▁
min: 1 max: 8 cnt: 15
Data: 15 5 35 25 55 45 75 65
sparkline: ▂▂▂▂▂▂▁▁▁▁▁▁▄▄▄▄▄▄▃▃▃▃▃▃▆▆▆▆▆▆▅▅▅▅▅▅██████▇▇▇▇▇▇
min: 5 max: 75 cnt: 8
Data: 0 1 19 20
sparkline: ▁▁▁▁▁▁▁▁▁▁▁▁████████████
min: 0 max: 20 cnt: 4
Data: 0 999 4000 4999 7000 7999
sparkline: ▁▁▁▁▁▁▁▁▁▁▁▁▅▅▅▅▅▅▅▅▅▅▅▅████████████
min: 0 max: 7999 cnt: 6
If bars don't display correctly please set the the console
font to DejaVu Sans Mono or any other that has the bargrph characters
Press any key..
</pre>
=={{header|C}}==
Line 426 ⟶ 559:
===Linux version===
Accepts data via command line, prints out usage on incorrect invocation.
<syntaxhighlight lang="c">
#include<string.h>
#include<stdlib.h>
Line 474 ⟶ 607:
return 0;
}
</syntaxhighlight>
Invocation and output :
<pre>
Line 487 ⟶ 620:
=={{header|C++}}==
<
#include <iostream>
#include <sstream>
Line 555 ⟶ 688:
return 0;
}</
{{out}}
<pre>Min: 1; Max: 8; Range: 7
Line 563 ⟶ 696:
=={{header|Clojure}}==
<
(let [sparks "▁▂▃▄▅▆▇█"
high (apply max nums)
Line 577 ⟶ 710:
(recur (read-line)))))
(spark (read-line))</
{{Out}}
Line 586 ⟶ 719:
=={{header|Common Lisp}}==
<
(loop with min = (apply #'min numbers)
with max = (apply #'max numbers)
Line 628 ⟶ 761:
(string->sparkline "1 2 3 4 5 6 7 8 7 6 5 4 3 2 1")
(string->sparkline "1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5")</
{{out}}
<pre>Min: 1, Max: 8, Range: 7
Line 637 ⟶ 770:
=={{header|D}}==
{{trans|Python}}
<
import std.stdio, std.range, std.algorithm, std.conv,
std.string, std.regex;
Line 652 ⟶ 785:
immutable div = (mm[1] - mm[0]) / (bars.length - 1);
numbers.map!(n => bars[cast(int)((n - mm[0]) / div)]).writeln;
}</
The output is the same as the Python entry
(but it only accepts one series of values at a time).
=={{header|Delphi}}==
<syntaxhighlight lang="delphi">
program Sparkline_in_unicode;
Line 707 ⟶ 840:
Readln;
end.
</syntaxhighlight>
=={{header|Elixir}}==
<
def sparkline(str) do
values = str |> String.split(~r/(,| )+/)
Line 716 ⟶ 849:
IO.puts Enum.map(values, &(round((&1 - min) / (max - min) * 7 + 0x2581)))
end
end</
Usage:
<syntaxhighlight lang="text">str1 = "1 2 3 4 5 6 7 8 7 6 5 4 3 2 1"
str2 = "1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5"
RC.sparkline(str1)
IO.puts "" # newline
RC.sparkline(str2)</
{{out}}
<pre>
Line 732 ⟶ 865:
=={{header|F_Sharp|F#}}==
<
open System.Globalization
open System.Text.RegularExpressions
Line 753 ⟶ 886:
|> List.map (fun x -> bars.[int ((x - min)/(max - min) * barsCount)])
|> String.Concat
|> printfn "%s"</
{{out}}
<pre>Numbers separated by anything: 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
Line 765 ⟶ 898:
=={{header|Factor}}==
<
math.statistics sequences splitting ;
Line 782 ⟶ 915:
"0, 1, 19, 20"
"0, 999, 4000, 4999, 7000, 7999"
} [ dup sparkline "%u -> %s\n" printf ] each</
{{out}}
<pre>
Line 792 ⟶ 925:
=={{header|FALSE}}==
<
variables:
s: sign (1 or -1)
Line 859 ⟶ 992:
q;1-q: {move pointer}
]#
]?</
This implementation can only accept one series of numbers at a time.
{{out}}
Line 868 ⟶ 1,001:
=={{header|Go}}==
<
import (
Line 941 ⟶ 1,074:
}
return
}</
{{out}}
<pre>
Line 979 ⟶ 1,112:
=={{header|Groovy}}==
<
def (min, max) = [list.min(), list.max()]
def div = (max - min) / 7
list.collect { (char)(0x2581 + (it-min) * div) }.join()
}
def sparkline(String text) { sparkline(text.split(/[ ,]+/).collect { it as Double }) }</
Test Code
<
println " Dataset: $dataset"
println "Sparkline: ${sparkline(dataset)}"
}</
{{out}}
<pre>
Line 999 ⟶ 1,132:
=={{header|Haskell}}==
<
import Data.Char (chr)
Line 1,055 ⟶ 1,188:
, "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"
]</
{{Out}}
<pre>▁▁██
Line 1,073 ⟶ 1,206:
Or, stripping back a little:
{{Trans|Python}}
<syntaxhighlight lang
import Data.List (findIndex)
import Data.List.Split (splitOneOf)
import Data.Maybe (maybe)
------------------------ SPARK LINE ----------------------
sparkLine :: [Float] -> String
sparkLine
sparkLine xxs@(x : xs) =
fmap
( maybe '█' ("▁▂▃▄▅▆▇" !!)
. flip findIndex lbounds
. (<)
)
xxs
where
(mn, mx) = foldr (
w = (mx - mn) / 8
lbounds = (mn +) . (w *) <$> [1 .. 7]
--------------------------- TEST -------------------------
main :: IO ()
main =
mapM_ putStrLn $
sparkLine . parseFloats
<$> [ "0, 1, 19, 20",
]
parseFloats :: String -> [Float]
parseFloats =
fmap read
. filter (not . null)
. splitOneOf " ,"</syntaxhighlight>
{{Out}}
<pre>▁▁██
Line 1,104 ⟶ 1,250:
=={{header|J}}==
'''Solution''' (''explicit''):<
y spkln~ 4 u:16b2581+i.8 NB. ▁▂▃▄▅▆▇█
:
Line 1,110 ⟶ 1,256:
N =. # x
x {~ <. (N-1) * (y-MIN) % MAX-MIN
)</
'''Solution''' (''tacit''):<
'''Solution''' (''look Ma, no hands!''):<
'''Examples''':<pre> spkln 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
▁▂▃▄▅▆▇█▇▆▅▄▃▂▁
Line 1,122 ⟶ 1,268:
=={{header|Java}}==
<
public class Sparkline
{
Line 1,164 ⟶ 1,310:
}
}
</syntaxhighlight>
{{out}}
<pre>
Line 1,178 ⟶ 1,324:
===ES6===
<
'use strict';
Line 1,335 ⟶ 1,481:
// MAIN ---
return main();
})();</
{{Out}}
<pre>▁▁██
Line 1,354 ⟶ 1,500:
=={{header|jq}}==
<
min as $min
| ( (max - $min) / 7 ) as $div
Line 1,362 ⟶ 1,508:
def string2array:
def tidy: select( length > 0 );
[split(" ") | .[] | split(",") | .[] | tidy | tonumber];</
'''Task'''
( "1 2 3 4 5 6 7 8 7 6 5 4 3 2 1",
Line 1,373 ⟶ 1,519:
=={{header|Julia}}==
<syntaxhighlight lang="julia">function sparklineit(arr)
sparkchars = '\u2581':'\u2588'
dyn = length(sparkchars)
Line 1,386 ⟶ 1,530:
println("$v → ", sparklineit(v))
v = 10rand(10)
println("$(round.(v, 2)) → ", sparklineit(v))
lines = strip.(split("""
1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5""", "\n"))
arrays = map(lin -> split(lin, r"\s+|\s*,\s*") .|> s -> parse(Float64, s), lines)
foreach(v -> println("$v → ", sparklineit(v)), arrays)
</syntaxhighlight>
{{out}}
<pre>[6, 3, 9, 5, 1, 10, 0, 1, 3, 6] → ▅▃█▄▁█▁▁▃▅
[0.57, 0.14, 4.73, 6.61, 6.9, 0.8, 9.71, 7.39, 2.75, 5.7] → ▁▁▄▆▆▁█▇▃▅
[1, 2, 3, 4, 5, 6, 7, 8, 7, 6, 5, 4, 3, 2, 1] → ▁▂▃▄▅▆▇█▇▆▅▄▃▂▁
[1.5, 0.5, 3.5, 2.5, 5.5, 4.5, 7.5, 6.5] → ▂▁▄▃▆▅█▇</pre>
=={{header|Kotlin}}==
{{trans|Java}}
<
internal const val n = bars.length - 1
Line 1,415 ⟶ 1,567:
println(s2)
println(s2.toSparkline())
}</
{{Out}}
<pre>1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
Line 1,423 ⟶ 1,575:
=={{header|LiveCode}}==
<
local utfbase=0x2581
local tStats, utfp, tmin,tmax,trange
Line 1,444 ⟶ 1,596:
end repeat
put plot
end sparklines</
Test
Line 1,463 ⟶ 1,615:
<syntaxhighlight lang="m2000 interpreter">
Module CheckIt {
Function Row$(a) {
Line 1,498 ⟶ 1,650:
}
Checkit
</syntaxhighlight>
Function Param() get a string an put it as inline code in expressions
<syntaxhighlight lang="m2000 interpreter">
Module CheckIt {
Font "Dejavu Sans Mono"
Line 1,530 ⟶ 1,682:
}
Checkit
</syntaxhighlight>
Third program use strings for stack input, which skip coma as white space. We have to pass to current stack, here calling a function (which always have a new stack for values). First we flush the stack (make it empty), then we use Stack Dat$ to parse the dat$ (also empty the string). Last we rtuern an array using array() using as argument a stack object. Read only function [] do two things replace the current stack object with an empty one, and return the old stack.
<syntaxhighlight lang="m2000 interpreter">
Module CheckIt {
Function ExtractDat(dat$) {
Line 1,568 ⟶ 1,720:
}
Checkit
</syntaxhighlight>
{{out}}
<pre>
Line 1,577 ⟶ 1,729:
</pre >
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
<pre>
toSparkline["1 2 3 4 5 6 7 8 7 6 5,4 3 2 1 "]
Line 1,588 ⟶ 1,740:
=={{header|NetRexx}}==
<
options replace format comments java crossref symbols nobinary
Line 1,642 ⟶ 1,794:
return
</syntaxhighlight>
{{out}}
<pre>
Line 1,654 ⟶ 1,806:
=={{header|Nim}}==
{{trans|Python}}
<
const bar = [
const barcount = float(bar.high)
while
let
line = readLineFromStdin "Numbers please separated by space/commas: "
numbers = line.split({' ', ','}).filterIt(it.len != 0).map(parseFloat)
mn = min(numbers)
mx = max(numbers)
Line 1,668 ⟶ 1,820:
var sparkline = ""
for n in numbers:
let i = int((n - mn) / extent * barcount)
sparkline.add
echo "min: ", mn.formatFloat(precision = 0), "; max: ", mx.formatFloat(precision =
echo sparkline</
{{out}}
<pre>Numbers please separated by space/commas: 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
Line 1,679 ⟶ 1,831:
min: 0.5; max: 7.5
▂▁▄▃▆▅█▇</pre>
=={{header|OCaml}}==
<syntaxhighlight lang="ocaml">let before_first_bar = 0x2580
let num_bars = 8
let sparkline numbers =
let max_num = List.fold_left max 0. numbers in
let scale = float_of_int num_bars /. max_num in
let bars = Buffer.create num_bars in
let add_bar number =
let scaled = scale *. number |> Float.round |> int_of_float in
if scaled <= 0 then
(* Using underscore character to differentiate between zero and one *)
Buffer.add_char bars '_'
else
scaled + before_first_bar |> Uchar.of_int |> Buffer.add_utf_8_uchar bars
in
List.iter add_bar numbers;
Buffer.contents bars
let print_sparkline line =
Printf.printf "Numbers: %s\n" line;
line
|> String.trim
|> String.split_on_char ' '
|> List.map (String.split_on_char ',')
|> List.flatten
|> List.filter_map (function
| "" -> None
| number -> Some (float_of_string number))
|> sparkline
|> print_endline
let () =
"0 0 1 1; 0 1 19 20; 0 999 4000 4999 7000 7999;
1 2 3 4 5 6 7 8 7 6 5 4 3 2 1;
1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5 "
|> String.trim
|> String.split_on_char ';'
|> List.iter print_sparkline
</syntaxhighlight>
{{out}}
<pre>
Numbers: 0 0 1 1
__██
Numbers: 0 1 19 20
__██
Numbers: 0 999 4000 4999 7000 7999
_▁▄▅▇█
Numbers:
1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
▁▂▃▄▅▆▇█▇▆▅▄▃▂▁
Numbers:
1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5
▂▁▄▃▆▅█▇
</pre>
=={{header|Perl}}==
<
our @sparks=map {chr} 0x2581 .. 0x2588;
sub sparkline :prototype(@) {
my @n=map {0+$_} grep {length} @_ or return "";
my($min,$max)=($n[0])x2;
Line 1,710 ⟶ 1,918:
# one sparkline per line
print sparkline( split /[\s,]+/ ) while <>;
</syntaxhighlight>
{{out}}
<pre>
Line 1,752 ⟶ 1,960:
=={{header|Phix}}==
Works fine on Linux, with or without unicode_console.e as that does little apart from check environment settings for clues, but on my
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1.0.2"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (a fix for JS)</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">builtins</span><span style="color: #0000FF;">\</span><span style="color: #000000;">unicode_console</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">tests</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"0 1 19 20"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"0 0 1 1"</span><span style="color: #0000FF;">,</span>
<span style="color: #008000;">"0 999 4000 4999 7000 7999"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"1 1 2 2 3 3"</span><span style="color: #0000FF;">,</span>
<span style="color: #008000;">"1 2 3 4 5 6 7 8 7 6 5 4 3 2 1"</span><span style="color: #0000FF;">,</span>
<span style="color: #008000;">"1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5"</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">ti</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">split_any</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #008000;">" ,"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #0000FF;">{{</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]}}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">scanf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">],</span><span style="color: #008000;">"%f"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">mn</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">min</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">mx</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">max</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">range</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">mx</span><span style="color: #0000FF;">-</span><span style="color: #000000;">mn</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Min :%g, Max :%g, Range :%g\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">mn</span><span style="color: #0000FF;">,</span><span style="color: #000000;">mx</span><span style="color: #0000FF;">,</span><span style="color: #000000;">range</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">unicode_console</span><span style="color: #0000FF;">()</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">ti</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">#2581</span> <span style="color: #0000FF;">+</span> <span style="color: #7060A8;">min</span><span style="color: #0000FF;">(</span><span style="color: #000000;">7</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">((</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]-</span><span style="color: #000000;">mn</span><span style="color: #0000FF;">)/</span><span style="color: #000000;">range</span><span style="color: #0000FF;">*</span><span style="color: #000000;">8</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">utf32_to_utf8</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">)})</span>
<span style="color: #008080;">else</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"unicode is not supported\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,796 ⟶ 2,008:
=={{header|PicoLisp}}==
<
(let (Min (apply min Lst) Max (apply max Lst) Rng (- Max Min))
(for N Lst
(prin
(char (+ 9601 (*/ (- N Min) 7 Rng)) ) ) )
(prinl) ) )</
Test:
<
(sparkLine (scl 1 (str "1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5")))</
Output:
<pre>▁▂▃▄▅▆▇█▇▆▅▄▃▂▁
Line 1,810 ⟶ 2,022:
=={{header|Python}}==
<
# Unicode: 9601, 9602, 9603, 9604, 9605, 9606, 9607, 9608
Line 1,834 ⟶ 2,046:
mn, mx, sp = sparkline(numbers)
print(' min: %5f; max: %5f' % (mn, mx))
print(" " + sp)</
{{out}}
Line 1,868 ⟶ 2,080:
If the search result is 'not found' (Nothing), then we use the highest (nominally 8th, or index 7) block.
<
from functools import reduce
Line 2,037 ⟶ 2,249:
# MAIN ---
if __name__ == '__main__':
main()</
{{Out}}
<pre>▁▁██
Line 2,054 ⟶ 2,266:
1.5 0.5 3.5 2.5 5.5 4.5 7.5 6.5
Min 0.5 Mean 4.0 Median 4.0 Max 7.5</pre>
=={{header|R}}==
<syntaxhighlight lang="rsplus">
bars <- intToUtf8(seq(0x2581, 0x2588), multiple = T) # ▁ ▂ ▃ ▄ ▅ ▆ ▇ █
n_chars <- length(bars)
sparkline <- function(numbers) {
mn <- min(numbers)
mx <- max(numbers)
interval <- mx - mn
bins <- sapply(
numbers,
function(i)
bars[[1 + min(n_chars - 1, floor((i - mn) / interval * n_chars))]]
)
sparkline <- paste0(bins, collapse = "")
return(sparkline)
}
sparkline(c(1, 2, 3, 4, 5, 6, 7, 8, 7, 6, 5, 4, 3, 2, 1))
sparkline(c(1.5, 0.5, 3.5, 2.5, 5.5, 4.5, 7.5, 6.5))
sparkline(c(0, 999, 4000, 4999, 7000, 7999))
sparkline(c(0, 0, 1, 1))
sparkline(c(0, 1, 19, 20))
</syntaxhighlight>
{{out}}
<syntaxhighlight lang="r">
[1] "▂▁▄▃▆▅█▇"
[1] "▁▂▃▄▅▆▇█▇▆▅▄▃▂▁"
[1] "▂▁▄▃▆▅█▇"
[1] "▁▁▅▅██"
[1] "▁▁██"
[1] "▁▁██"
</syntaxhighlight>
=={{header|Racket}}==
<
#lang racket (require syntax/parse)
Line 2,070 ⟶ 2,318:
(sparks "1 2 3 4 5 6 7 8 7 6 5 4 3 2 1")
(sparks "1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5")
</syntaxhighlight>
{{out}}
<
"▁▂▃▄▅▆▇█▇▆▅▄▃▂▁"
"▂▁▄▃▆▅█▇"
</syntaxhighlight>
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku"
while prompt 'Numbers separated by anything: ' -> $_ {
my @numbers = map +*, .comb(/ '-'? [[\d+ ['.' \d*]?] | ['.' \d+]] /);
Line 2,085 ⟶ 2,333:
say "min: $mn.fmt('%5f'); max: $mx.fmt('%5f')";
say @bars[ @numbers.map: { @bars * ($_ - $mn) / ($mx - $mn) min @bars - 1 } ].join;
}</
{{out}}
<pre>Numbers separated by anything: 9 18 27 36 45 54 63 72 63 54 45 36 27 18 9
Line 2,105 ⟶ 2,353:
{{Works with|ooRexx}}
{{Works with|Regina|3.4}}
<
parse arg aaa
Line 2,172 ⟶ 2,420:
return
</syntaxhighlight>
{{out}}
<pre>
Line 2,197 ⟶ 2,445:
It should also be noted that the CMS and TSO versions of REXX (and practically all others) don't support ''single line comments''.
<
if arg()==0 then do /*Optional arguments? Then use defaults*/
call sparkGraph 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
Line 2,221 ⟶ 2,469:
end /*j*/
say 'output: ' z; say; return /*show the output, + a blank line*/</
{{out|output|text= when using the default inputs:}}
<pre>
Line 2,233 ⟶ 2,481:
=={{header|Ruby}}==
{{trans|Raku}} with added protection for input like "0 0 0 0".
<
loop {print 'Numbers please separated by space/commas: '
numbers = gets.split(/[\s,]+/).map(&:to_f)
Line 2,240 ⟶ 2,488:
div = (max - min) / (bar.size - 1)
puts min == max ? bar.last*numbers.size : numbers.map{|num| bar[((num - min) / div).to_i]}.join
}</
{{out}} Used Go testcases
Line 2,267 ⟶ 2,515:
=={{header|Rust}}==
<
const BARS: &'static str = "▁▂▃▄▅▆▇█";
Line 2,285 ⟶ 2,533:
print_sparkline(s2);
}
</syntaxhighlight>
{{out}}
"▁▂▃▄▅▆▇█▇▆▅▄▃▂▁"
Line 2,292 ⟶ 2,540:
=={{header|S-lang}}==
<
% Just to demonstrate alternate ways of defining unicode:
private variable spchrs = "\u{2581}\u{2582}\u{2583}\u{2584}\u{2585}\u{2586}\u{2587}\u{2588}";
Line 2,318 ⟶ 2,566:
sparkline("1 2 3 4 5 6 7 8 7 6 5 4 3 2 1");
sparkline("1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5 ");
</syntaxhighlight>
{{out}}
"▁▂▃▄▅▆▇█▇▆▅▄▃▂▁"
Line 2,325 ⟶ 2,573:
=={{header|Scala}}==
<
numStr.split( "[\\s,]+" ).map(_.toFloat) match {
case v if v.isEmpty => ""
Line 2,340 ⟶ 2,588:
// A random test...
println( mkSparks( Stream.continually( math.abs(util.Random.nextInt % 8)).take(64).mkString(" ") ))</
{{out}}
<pre> ▁▂▃▄▅▆▇█▇▆▅▄▃▂▁
Line 2,347 ⟶ 2,595:
=={{header|Seed7}}==
<
include "scanfile.s7i";
include "float.s7i";
Line 2,399 ⟶ 2,647:
data := readDataLine;
end while;
end func;</
{{out}}
Line 2,408 ⟶ 2,656:
▂▁▄▃▆▅█▇
Numbers separated by anything:
</pre>
=={{header|SenseTalk}}==
This solution makes use of SenseTalk's "word" chunks, in this case delimited by any number of spaces and/or commas, to simplify parsing the input string. Also note the use of the <code>but no more than</code> operator to handle the edge case where a number equal to the top value would otherwise refer to the 9th symbol, which doesn't exist.
<syntaxhighlight lang="sensetalk">put sparklineGraph of "1 2 3 4 5 6 7 8 7 6 5 4 3 2 1"
put sparklineGraph of "1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5"
put sparklineGraph of "0, 1, 19, 20"
put sparklineGraph of "0, 999, 4000, 4999, 7000, 7999"
to handle sparklineGraph of input
put each word delimited by " ," of input into numbers
put the lowestValue of numbers into bottom
put the highestValue of numbers into top
put top - bottom into range -- total range of values
put range/8 into step -- the size of each incremental step in the graph
repeat with each number in numbers
put (trunc((number - bottom) / step) + 1) but no more than 8 into symbolNum
put character symbolNum of "▁▂▃▄▅▆▇█" after graph
end repeat
put !" (range: [[bottom]] to [[top]])" after graph
return graph
end sparklineGraph
</syntaxhighlight>
{{out}}
<pre>
▁▂▃▄▅▆▇█▇▆▅▄▃▂▁ (range: 1 to 8)
▂▁▄▃▆▅█▇ (range: 0.5 to 7.5)
▁▁██ (range: 0 to 20)
▁▁▅▅██ (range: 0 to 7999)
</pre>
=={{header|Sidef}}==
{{trans|Ruby}}
<
loop {
print 'Numbers, please, separated by space/commas: ';
Line 2,420 ⟶ 2,698:
var div = ((max - min) / bar.end);
say (min == max ? bar.last*numbers.len : numbers.map{|num| bar[(num - min) / div]}.join);
}</
{{out}}
<pre>Numbers, please, separated by space/commas: 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
Line 2,431 ⟶ 2,709:
=={{header|Tcl}}==
{{works with|Tcl|8.6}}
<
proc extractValues {series} {
Line 2,445 ⟶ 2,723:
set max [tcl::mathfunc::max {*}$values]
return [join [lmap v $values {renderValue $min $max $v}] ""]
}</
Demonstrating:
<
"1 2 3 4 5 6 7 8 7 6 5 4 3 2 1"
"1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5"
Line 2,454 ⟶ 2,732:
puts "Series: $series"
puts "Sparkline: [sparkline $series]"
}</
{{out}}
<pre>
Line 2,461 ⟶ 2,739:
Series: 1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5
Sparkline: ▂▁▄▃▆▅█▇
</pre>
=={{header|Wren}}==
{{trans|Go}}
{{libheader|Wren-pattern}}
<syntaxhighlight lang="wren">import "io" for Stdin, Stdout
import "./pattern" for Pattern
var p = Pattern.new("[,+1/s|+1/s,|+1/s|,]")
var spark = Fn.new { |s0|
var ss = p.splitAll(s0)
var n = ss.count
var vs = List.filled(n, 0)
var min = 1/0
var max = -min
var i = 0
for (s in ss) {
var v = Num.fromString(s)
if (v.isNan || v.isInfinity) Fiber.abort("Infinities and NaN not supported.")
if (v < min) min = v
if (v > max) max = v
vs[i] = v
i = i + 1
}
var sp
if (min == max) {
sp = "▄" * n
} else {
var rs = List.filled(n, null)
var f = 8 / (max - min)
var j = 0
for (v in vs) {
var i = (f * (v - min)).floor
if (i > 7) i = 7
rs[j] = String.fromCodePoint(0x2581 + i)
j = j + 1
}
sp = rs.join()
}
return [sp, n, min, max]
}
while (true) {
System.print("Numbers please separated by spaces/commas or just press return to quit:")
Stdout.flush()
var numbers = Stdin.readLine().trim()
if (numbers == "") break
var res = spark.call(numbers)
var s = res[0]
var n = res[1]
var min = res[2]
var max = res[3]
if (n == 1) {
System.print("1 value = %(min)")
} else {
System.print("%(n) values. Min: %(min) Max: %(max)")
}
System.print(s)
System.print()
}</syntaxhighlight>
{{out}}
<pre>
Numbers please separated by spaces/commas or just press return to quit:
1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
15 values. Min: 1 Max: 8
▁▂▃▄▅▆▇█▇▆▅▄▃▂▁
Numbers please separated by spaces/commas or just press return to quit:
1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5
8 values. Min: 0.5 Max: 7.5
▂▁▄▃▆▅█▇
Numbers please separated by spaces/commas or just press return to quit:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
24 values. Min: 1 Max: 24
▁▁▁▂▂▂▃▃▃▄▄▄▅▅▅▆▆▆▇▇▇███
Numbers please separated by spaces/commas or just press return to quit:
0 -.09 -.11 -.69 -.71 -.8
6 values. Min: -0.8 Max: 0
██▇▂▁▁
Numbers please separated by spaces/commas or just press return to quit:
3 3 3
3 values. Min: 3 Max: 3
▄▄▄
Numbers please separated by spaces/commas or just press return to quit:
1e99
1 value = 1e+99
▄
Numbers please separated by spaces/commas or just press return to quit:
</pre>
=={{header|zkl}}==
<
var sl=(sparks.len()-1);
Line 2,472 ⟶ 2,845:
println("Range [",min,"-",max,"]", xs);
xs.pump(String,'wrap(x){ sparks[(x - min)*sl/range] }).println();
}</
<
two:=("1.5, 0.5 3.5, 2.5 5.5 4.5 7.5, 6.5" - ",").split(" ").apply("toFloat");
sparkLine(one); sparkLine(two);</
{{out}}
<pre>
Line 2,485 ⟶ 2,858:
=={{header|Zoea}}==
<syntaxhighlight lang="zoea">
program: sparkline
input: '1 2, 3 4.5 5 6,7 8'
Line 2,491 ⟶ 2,864:
derive: [1,2,3,4,5,6,7,8]
output: '▁▂▃▄▅▆▇█'
</syntaxhighlight>
=={{header|Zoea Visual}}==
[http://zoea.co.uk/examples/zv-rc/Sparkline.png Sparkline]
|