Sparkline in unicode: Difference between revisions

Content deleted Content added
Thundergnat (talk | contribs)
m syntax highlighting fixup automation
Line 36: Line 36:
=={{header|APL}}==
=={{header|APL}}==
Note → this is in a 0-indexed version of APL
Note → this is in a 0-indexed version of APL
'''Solution''':<lang APL> sparkln←{'▁▂▃▄▅▆▇█'[⌊0.5+7×⍵÷⌈/⍵]}</lang>
'''Solution''':<syntaxhighlight lang="apl"> sparkln←{'▁▂▃▄▅▆▇█'[⌊0.5+7×⍵÷⌈/⍵]}</syntaxhighlight>
'''Example''':<lang APL> sparkln 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
'''Example''':<syntaxhighlight lang="apl"> sparkln 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
▂▃▄▅▅▆▇█▇▆▅▅▄▃▂
▂▃▄▅▅▆▇█▇▆▅▅▄▃▂
sparkln 1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5
sparkln 1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5
▂▁▄▃▆▅█▇
▂▁▄▃▆▅█▇
</syntaxhighlight>
</lang>
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.
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}}==
=={{header|AppleScript}}==
<lang AppleScript>use AppleScript version "2.4"
<syntaxhighlight lang="applescript">use AppleScript version "2.4"
use framework "Foundation"
use framework "Foundation"
use scripting additions
use scripting additions
Line 360: Line 360:
set my text item delimiters to dlm
set my text item delimiters to dlm
return s
return s
end unwords</lang>
end unwords</syntaxhighlight>
{{Out}}
{{Out}}
<pre>▁▁██
<pre>▁▁██
Line 392: Line 392:
=={{header|Arturo}}==
=={{header|Arturo}}==


<lang rebol>bar: "▁▂▃▄▅▆▇█"
<syntaxhighlight lang="rebol">bar: "▁▂▃▄▅▆▇█"
barcount: to :floating dec size bar
barcount: to :floating dec size bar


Line 409: Line 409:
print sparkLine
print sparkLine
print ""
print ""
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}
Line 424: Line 424:
=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
{{works with|AutoHotkey_L}}
{{works with|AutoHotkey_L}}
<lang AutoHotkey>SetFormat, FloatFast, 0.1
<syntaxhighlight lang="autohotkey">SetFormat, FloatFast, 0.1
strings := ["1 2 3 4 5 6 7 8 7 6 5 4 3 2 1"
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"]
, "1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5"]
Line 446: Line 446:
Chars .= Chr(0x2581 + Round(7 * (A_LoopField - Min) / Rng))
Chars .= Chr(0x2581 + Round(7 * (A_LoopField - Min) / Rng))
return, {"Min": Min, "Max": Max, "Rng": Rng, "Chars": Chars}
return, {"Min": Min, "Max": Max, "Rng": Rng, "Chars": Chars}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Min: 1, Max: 8, Range: 7
<pre>Min: 1, Max: 8, Range: 7
Line 458: Line 458:
===Linux version===
===Linux version===
Accepts data via command line, prints out usage on incorrect invocation.
Accepts data via command line, prints out usage on incorrect invocation.
<syntaxhighlight lang="c">
<lang C>
#include<string.h>
#include<string.h>
#include<stdlib.h>
#include<stdlib.h>
Line 506: Line 506:
return 0;
return 0;
}
}
</syntaxhighlight>
</lang>
Invocation and output :
Invocation and output :
<pre>
<pre>
Line 519: Line 519:
=={{header|C++}}==
=={{header|C++}}==


<lang cpp>
<syntaxhighlight lang="cpp">
#include <iostream>
#include <iostream>
#include <sstream>
#include <sstream>
Line 587: Line 587:


return 0;
return 0;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Min: 1; Max: 8; Range: 7
<pre>Min: 1; Max: 8; Range: 7
Line 595: Line 595:


=={{header|Clojure}}==
=={{header|Clojure}}==
<lang Clojure>(defn sparkline [nums]
<syntaxhighlight lang="clojure">(defn sparkline [nums]
(let [sparks "▁▂▃▄▅▆▇█"
(let [sparks "▁▂▃▄▅▆▇█"
high (apply max nums)
high (apply max nums)
Line 609: Line 609:
(recur (read-line)))))
(recur (read-line)))))


(spark (read-line))</lang>
(spark (read-line))</syntaxhighlight>


{{Out}}
{{Out}}
Line 618: Line 618:


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
<lang lisp>(defun buckets (numbers)
<syntaxhighlight lang="lisp">(defun buckets (numbers)
(loop with min = (apply #'min numbers)
(loop with min = (apply #'min numbers)
with max = (apply #'max numbers)
with max = (apply #'max numbers)
Line 660: Line 660:


(string->sparkline "1 2 3 4 5 6 7 8 7 6 5 4 3 2 1")
(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")</lang>
(string->sparkline "1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5")</syntaxhighlight>
{{out}}
{{out}}
<pre>Min: 1, Max: 8, Range: 7
<pre>Min: 1, Max: 8, Range: 7
Line 669: Line 669:
=={{header|D}}==
=={{header|D}}==
{{trans|Python}}
{{trans|Python}}
<lang d>void main() {
<syntaxhighlight lang="d">void main() {
import std.stdio, std.range, std.algorithm, std.conv,
import std.stdio, std.range, std.algorithm, std.conv,
std.string, std.regex;
std.string, std.regex;
Line 684: Line 684:
immutable div = (mm[1] - mm[0]) / (bars.length - 1);
immutable div = (mm[1] - mm[0]) / (bars.length - 1);
numbers.map!(n => bars[cast(int)((n - mm[0]) / div)]).writeln;
numbers.map!(n => bars[cast(int)((n - mm[0]) / div)]).writeln;
}</lang>
}</syntaxhighlight>
The output is the same as the Python entry
The output is the same as the Python entry
(but it only accepts one series of values at a time).
(but it only accepts one series of values at a time).
=={{header|Delphi}}==
=={{header|Delphi}}==
<syntaxhighlight lang="delphi">
<lang Delphi>
program Sparkline_in_unicode;
program Sparkline_in_unicode;


Line 739: Line 739:
Readln;
Readln;
end.
end.
</syntaxhighlight>
</lang>
=={{header|Elixir}}==
=={{header|Elixir}}==
<lang elixir>defmodule RC do
<syntaxhighlight lang="elixir">defmodule RC do
def sparkline(str) do
def sparkline(str) do
values = str |> String.split(~r/(,| )+/)
values = str |> String.split(~r/(,| )+/)
Line 748: Line 748:
IO.puts Enum.map(values, &(round((&1 - min) / (max - min) * 7 + 0x2581)))
IO.puts Enum.map(values, &(round((&1 - min) / (max - min) * 7 + 0x2581)))
end
end
end</lang>
end</syntaxhighlight>
Usage:
Usage:
<lang>str1 = "1 2 3 4 5 6 7 8 7 6 5 4 3 2 1"
<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"
str2 = "1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5"


RC.sparkline(str1)
RC.sparkline(str1)
IO.puts "" # newline
IO.puts "" # newline
RC.sparkline(str2)</lang>
RC.sparkline(str2)</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 764: Line 764:


=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
<lang fsharp>open System
<syntaxhighlight lang="fsharp">open System
open System.Globalization
open System.Globalization
open System.Text.RegularExpressions
open System.Text.RegularExpressions
Line 785: Line 785:
|> List.map (fun x -> bars.[int ((x - min)/(max - min) * barsCount)])
|> List.map (fun x -> bars.[int ((x - min)/(max - min) * barsCount)])
|> String.Concat
|> String.Concat
|> printfn "%s"</lang>
|> printfn "%s"</syntaxhighlight>
{{out}}
{{out}}
<pre>Numbers separated by anything: 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
<pre>Numbers separated by anything: 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
Line 797: Line 797:


=={{header|Factor}}==
=={{header|Factor}}==
<lang factor>USING: formatting kernel math math.order math.parser
<syntaxhighlight lang="factor">USING: formatting kernel math math.order math.parser
math.statistics sequences splitting ;
math.statistics sequences splitting ;


Line 814: Line 814:
"0, 1, 19, 20"
"0, 1, 19, 20"
"0, 999, 4000, 4999, 7000, 7999"
"0, 999, 4000, 4999, 7000, 7999"
} [ dup sparkline "%u -> %s\n" printf ] each</lang>
} [ dup sparkline "%u -> %s\n" printf ] each</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 824: Line 824:


=={{header|FALSE}}==
=={{header|FALSE}}==
<lang false>{
<syntaxhighlight lang="false">{
variables:
variables:
s: sign (1 or -1)
s: sign (1 or -1)
Line 891: Line 891:
q;1-q: {move pointer}
q;1-q: {move pointer}
]#
]#
]?</lang>
]?</syntaxhighlight>
This implementation can only accept one series of numbers at a time.
This implementation can only accept one series of numbers at a time.
{{out}}
{{out}}
Line 900: Line 900:


=={{header|Go}}==
=={{header|Go}}==
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 973: Line 973:
}
}
return
return
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,011: Line 1,011:


=={{header|Groovy}}==
=={{header|Groovy}}==
<lang groovy>def sparkline(List<Number> list) {
<syntaxhighlight lang="groovy">def sparkline(List<Number> list) {
def (min, max) = [list.min(), list.max()]
def (min, max) = [list.min(), list.max()]
def div = (max - min) / 7
def div = (max - min) / 7
list.collect { (char)(0x2581 + (it-min) * div) }.join()
list.collect { (char)(0x2581 + (it-min) * div) }.join()
}
}
def sparkline(String text) { sparkline(text.split(/[ ,]+/).collect { it as Double }) }</lang>
def sparkline(String text) { sparkline(text.split(/[ ,]+/).collect { it as Double }) }</syntaxhighlight>
Test Code
Test Code
<lang groovy>["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"].each { dataset ->
<syntaxhighlight lang="groovy">["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"].each { dataset ->
println " Dataset: $dataset"
println " Dataset: $dataset"
println "Sparkline: ${sparkline(dataset)}"
println "Sparkline: ${sparkline(dataset)}"
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,031: Line 1,031:


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>import Data.List.Split (splitOneOf)
<syntaxhighlight lang="haskell">import Data.List.Split (splitOneOf)
import Data.Char (chr)
import Data.Char (chr)


Line 1,087: Line 1,087:
, "3 2 1 0 -1 -2 -3 -4 -3 -2 -1 0 1 2 3"
, "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"
, "-1000 100 1000 500 200 -400 -700 621 -189 3"
]</lang>
]</syntaxhighlight>
{{Out}}
{{Out}}
<pre>▁▁██
<pre>▁▁██
Line 1,105: Line 1,105:
Or, stripping back a little:
Or, stripping back a little:
{{Trans|Python}}
{{Trans|Python}}
<lang haskell>import Data.Bifunctor (bimap)
<syntaxhighlight lang="haskell">import Data.Bifunctor (bimap)
import Data.List (findIndex)
import Data.List (findIndex)
import Data.List.Split (splitOneOf)
import Data.List.Split (splitOneOf)
Line 1,141: Line 1,141:
fmap read
fmap read
. filter (not . null)
. filter (not . null)
. splitOneOf " ,"</lang>
. splitOneOf " ,"</syntaxhighlight>
{{Out}}
{{Out}}
<pre>▁▁██
<pre>▁▁██
Line 1,149: Line 1,149:


=={{header|J}}==
=={{header|J}}==
'''Solution''' (''explicit''):<lang j> spkln =: verb define
'''Solution''' (''explicit''):<syntaxhighlight lang="j"> spkln =: verb define
y spkln~ 4 u:16b2581+i.8 NB. ▁▂▃▄▅▆▇█
y spkln~ 4 u:16b2581+i.8 NB. ▁▂▃▄▅▆▇█
:
:
Line 1,155: Line 1,155:
N =. # x
N =. # x
x {~ <. (N-1) * (y-MIN) % MAX-MIN
x {~ <. (N-1) * (y-MIN) % MAX-MIN
)</lang>
)</syntaxhighlight>
'''Solution''' (''tacit''):<lang j> spkln =: (4 u:16b2581+i.8)&$: : ([ {~ <:@#@[ <.@* ] (- % >./@[ - ]) <./@])</lang>
'''Solution''' (''tacit''):<syntaxhighlight lang="j"> spkln =: (4 u:16b2581+i.8)&$: : ([ {~ <:@#@[ <.@* ] (- % >./@[ - ]) <./@])</syntaxhighlight>
'''Solution''' (''look Ma, no hands!''):<lang j> spkln =: (u:9601+i.8)&$: : ([ {~ ((<.@* <:@#)~ ((- % (- >./))~ <./)))</lang>
'''Solution''' (''look Ma, no hands!''):<syntaxhighlight lang="j"> spkln =: (u:9601+i.8)&$: : ([ {~ ((<.@* <:@#)~ ((- % (- >./))~ <./)))</syntaxhighlight>
'''Examples''':<pre> spkln 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
'''Examples''':<pre> spkln 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
▁▂▃▄▅▆▇█▇▆▅▄▃▂▁
▁▂▃▄▅▆▇█▇▆▅▄▃▂▁
Line 1,167: Line 1,167:


=={{header|Java}}==
=={{header|Java}}==
<lang java>
<syntaxhighlight lang="java">
public class Sparkline
public class Sparkline
{
{
Line 1,209: Line 1,209:
}
}
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,223: Line 1,223:
===ES6===
===ES6===


<lang JavaScript>(() => {
<syntaxhighlight lang="javascript">(() => {
'use strict';
'use strict';


Line 1,380: Line 1,380:
// MAIN ---
// MAIN ---
return main();
return main();
})();</lang>
})();</syntaxhighlight>
{{Out}}
{{Out}}
<pre>▁▁██
<pre>▁▁██
Line 1,399: Line 1,399:


=={{header|jq}}==
=={{header|jq}}==
<lang jq>def sparkline:
<syntaxhighlight lang="jq">def sparkline:
min as $min
min as $min
| ( (max - $min) / 7 ) as $div
| ( (max - $min) / 7 ) as $div
Line 1,407: Line 1,407:
def string2array:
def string2array:
def tidy: select( length > 0 );
def tidy: select( length > 0 );
[split(" ") | .[] | split(",") | .[] | tidy | tonumber];</lang>
[split(" ") | .[] | split(",") | .[] | tidy | tonumber];</syntaxhighlight>
'''Task'''
'''Task'''
( "1 2 3 4 5 6 7 8 7 6 5 4 3 2 1",
( "1 2 3 4 5 6 7 8 7 6 5 4 3 2 1",
Line 1,418: Line 1,418:


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>function sparklineit(arr)
<syntaxhighlight lang="julia">function sparklineit(arr)
sparkchars = '\u2581':'\u2588'
sparkchars = '\u2581':'\u2588'
dyn = length(sparkchars)
dyn = length(sparkchars)
Line 1,436: Line 1,436:
arrays = map(lin -> split(lin, r"\s+|\s*,\s*") .|> s -> parse(Float64, s), lines)
arrays = map(lin -> split(lin, r"\s+|\s*,\s*") .|> s -> parse(Float64, s), lines)
foreach(v -> println("$v → ", sparklineit(v)), arrays)
foreach(v -> println("$v → ", sparklineit(v)), arrays)
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>[6, 3, 9, 5, 1, 10, 0, 1, 3, 6] → ▅▃█▄▁█▁▁▃▅
<pre>[6, 3, 9, 5, 1, 10, 0, 1, 3, 6] → ▅▃█▄▁█▁▁▃▅
Line 1,445: Line 1,445:
=={{header|Kotlin}}==
=={{header|Kotlin}}==
{{trans|Java}}
{{trans|Java}}
<lang scala>internal const val bars = "▁▂▃▄▅▆▇█"
<syntaxhighlight lang="scala">internal const val bars = "▁▂▃▄▅▆▇█"
internal const val n = bars.length - 1
internal const val n = bars.length - 1


Line 1,466: Line 1,466:
println(s2)
println(s2)
println(s2.toSparkline())
println(s2.toSparkline())
}</lang>
}</syntaxhighlight>
{{Out}}
{{Out}}
<pre>1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
<pre>1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
Line 1,474: Line 1,474:


=={{header|LiveCode}}==
=={{header|LiveCode}}==
<lang LiveCode>command sparklines listOfNums
<syntaxhighlight lang="livecode">command sparklines listOfNums
local utfbase=0x2581
local utfbase=0x2581
local tStats, utfp, tmin,tmax,trange
local tStats, utfp, tmin,tmax,trange
Line 1,495: Line 1,495:
end repeat
end repeat
put plot
put plot
end sparklines</lang>
end sparklines</syntaxhighlight>


Test
Test
Line 1,514: Line 1,514:




<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module CheckIt {
Module CheckIt {
Function Row$(a) {
Function Row$(a) {
Line 1,549: Line 1,549:
}
}
Checkit
Checkit
</syntaxhighlight>
</lang>


Function Param() get a string an put it as inline code in expressions
Function Param() get a string an put it as inline code in expressions


<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module CheckIt {
Module CheckIt {
Font "Dejavu Sans Mono"
Font "Dejavu Sans Mono"
Line 1,581: Line 1,581:
}
}
Checkit
Checkit
</syntaxhighlight>
</lang>


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.
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">
<lang M2000 Interpreter>
Module CheckIt {
Module CheckIt {
Function ExtractDat(dat$) {
Function ExtractDat(dat$) {
Line 1,619: Line 1,619:
}
}
Checkit
Checkit
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,629: Line 1,629:


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang Mathematica>toSparkline[data_String] := FromCharacterCode[Round[7 Rescale@Flatten@ImportString[data, "Table", "FieldSeparators" -> {" ", ","}]] + 16^^2581];</lang>
<syntaxhighlight lang="mathematica">toSparkline[data_String] := FromCharacterCode[Round[7 Rescale@Flatten@ImportString[data, "Table", "FieldSeparators" -> {" ", ","}]] + 16^^2581];</syntaxhighlight>
<pre>
<pre>
toSparkline["1 2 3 4 5 6 7 8 7 6 5,4 3 2 1 "]
toSparkline["1 2 3 4 5 6 7 8 7 6 5,4 3 2 1 "]
Line 1,639: Line 1,639:


=={{header|NetRexx}}==
=={{header|NetRexx}}==
<lang NetRexx>/* NetRexx */
<syntaxhighlight lang="netrexx">/* NetRexx */
options replace format comments java crossref symbols nobinary
options replace format comments java crossref symbols nobinary


Line 1,693: Line 1,693:
return
return
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,705: Line 1,705:
=={{header|Nim}}==
=={{header|Nim}}==
{{trans|Python}}
{{trans|Python}}
<lang nim>import rdstdin, sequtils, strutils
<syntaxhighlight lang="nim">import rdstdin, sequtils, strutils


const bar = ["\u2581", "\u2582", "\u2583", "\u2584", "\u2585", "\u2586", "\u2587", "\u2588"]
const bar = ["\u2581", "\u2582", "\u2583", "\u2584", "\u2585", "\u2586", "\u2587", "\u2588"]
Line 1,722: Line 1,722:
sparkline.add bar[i]
sparkline.add bar[i]
echo "min: ", mn.formatFloat(precision = 0), "; max: ", mx.formatFloat(precision = -1)
echo "min: ", mn.formatFloat(precision = 0), "; max: ", mx.formatFloat(precision = -1)
echo sparkline</lang>
echo sparkline</syntaxhighlight>
{{out}}
{{out}}
<pre>Numbers please separated by space/commas: 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
<pre>Numbers please separated by space/commas: 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
Line 1,732: Line 1,732:


=={{header|OCaml}}==
=={{header|OCaml}}==
<lang ocaml>let before_first_bar = 0x2580
<syntaxhighlight lang="ocaml">let before_first_bar = 0x2580
let num_bars = 8
let num_bars = 8


Line 1,770: Line 1,770:
|> String.split_on_char ';'
|> String.split_on_char ';'
|> List.iter print_sparkline
|> List.iter print_sparkline
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,788: Line 1,788:


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>binmode(STDOUT, ":utf8");
<syntaxhighlight lang="perl">binmode(STDOUT, ":utf8");
our @sparks=map {chr} 0x2581 .. 0x2588;
our @sparks=map {chr} 0x2581 .. 0x2588;
sub sparkline(@) {
sub sparkline(@) {
Line 1,817: Line 1,817:
# one sparkline per line
# one sparkline per line
print sparkline( split /[\s,]+/ ) while <>;
print sparkline( split /[\s,]+/ ) while <>;
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,860: Line 1,860:
=={{header|Phix}}==
=={{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 Windows box, on which it invokes kernel32/SetConsoleOutputCP(CP_UTF8), unicode_console.e improves it only slightly, getting just two of the eight characters right. Changing the font on the windows(10) console to "NSimSum" or "SimSun-ExtB" (manually, by right clicking on the title bar and selecting properties/fonts) improves things considerably - suggestions welcome.
Works fine on Linux, with or without unicode_console.e as that does little apart from check environment settings for clues, but on my Windows box, on which it invokes kernel32/SetConsoleOutputCP(CP_UTF8), unicode_console.e improves it only slightly, getting just two of the eight characters right. Changing the font on the windows(10) console to "NSimSum" or "SimSun-ExtB" (manually, by right clicking on the title bar and selecting properties/fonts) improves things considerably - suggestions welcome.
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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: #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>
Line 1,889: Line 1,889:
<span style="color: #008080;">end</span> <span style="color: #008080;">if</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>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 1,907: Line 1,907:


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
<lang PicoLisp>(de sparkLine (Lst)
<syntaxhighlight lang="picolisp">(de sparkLine (Lst)
(let (Min (apply min Lst) Max (apply max Lst) Rng (- Max Min))
(let (Min (apply min Lst) Max (apply max Lst) Rng (- Max Min))
(for N Lst
(for N Lst
(prin
(prin
(char (+ 9601 (*/ (- N Min) 7 Rng)) ) ) )
(char (+ 9601 (*/ (- N Min) 7 Rng)) ) ) )
(prinl) ) )</lang>
(prinl) ) )</syntaxhighlight>
Test:
Test:
<lang PicoLisp>(sparkLine (str "1 2 3 4 5 6 7 8 7 6 5 4 3 2 1"))
<syntaxhighlight lang="picolisp">(sparkLine (str "1 2 3 4 5 6 7 8 7 6 5 4 3 2 1"))
(sparkLine (scl 1 (str "1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5")))</lang>
(sparkLine (scl 1 (str "1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5")))</syntaxhighlight>
Output:
Output:
<pre>▁▂▃▄▅▆▇█▇▆▅▄▃▂▁
<pre>▁▂▃▄▅▆▇█▇▆▅▄▃▂▁
Line 1,921: Line 1,921:


=={{header|Python}}==
=={{header|Python}}==
<lang python># -*- coding: utf-8 -*-
<syntaxhighlight lang="python"># -*- coding: utf-8 -*-


# Unicode: 9601, 9602, 9603, 9604, 9605, 9606, 9607, 9608
# Unicode: 9601, 9602, 9603, 9604, 9605, 9606, 9607, 9608
Line 1,945: Line 1,945:
mn, mx, sp = sparkline(numbers)
mn, mx, sp = sparkline(numbers)
print(' min: %5f; max: %5f' % (mn, mx))
print(' min: %5f; max: %5f' % (mn, mx))
print(" " + sp)</lang>
print(" " + sp)</syntaxhighlight>


{{out}}
{{out}}
Line 1,979: Line 1,979:
If the search result is 'not found' (Nothing), then we use the highest (nominally 8th, or index 7) block.
If the search result is 'not found' (Nothing), then we use the highest (nominally 8th, or index 7) block.


<lang python>'''Sparkline in Unicode'''
<syntaxhighlight lang="python">'''Sparkline in Unicode'''


from functools import reduce
from functools import reduce
Line 2,148: Line 2,148:
# MAIN ---
# MAIN ---
if __name__ == '__main__':
if __name__ == '__main__':
main()</lang>
main()</syntaxhighlight>
{{Out}}
{{Out}}
<pre>▁▁██
<pre>▁▁██
Line 2,167: Line 2,167:


=={{header|R}}==
=={{header|R}}==
<lang rsplus>
<syntaxhighlight lang="rsplus">
bars <- intToUtf8(seq(0x2581, 0x2588), multiple = T) # ▁ ▂ ▃ ▄ ▅ ▆ ▇ █
bars <- intToUtf8(seq(0x2581, 0x2588), multiple = T) # ▁ ▂ ▃ ▄ ▅ ▆ ▇ █
n_chars <- length(bars)
n_chars <- length(bars)
Line 2,191: Line 2,191:
sparkline(c(0, 0, 1, 1))
sparkline(c(0, 0, 1, 1))
sparkline(c(0, 1, 19, 20))
sparkline(c(0, 1, 19, 20))
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<syntaxhighlight lang="r">
<lang r>
[1] "▂▁▄▃▆▅█▇"
[1] "▂▁▄▃▆▅█▇"
[1] "▁▂▃▄▅▆▇█▇▆▅▄▃▂▁"
[1] "▁▂▃▄▅▆▇█▇▆▅▄▃▂▁"
Line 2,200: Line 2,200:
[1] "▁▁██"
[1] "▁▁██"
[1] "▁▁██"
[1] "▁▁██"
</syntaxhighlight>
</lang>


=={{header|Racket}}==
=={{header|Racket}}==
<lang racket>
<syntaxhighlight lang="racket">
#lang racket (require syntax/parse)
#lang racket (require syntax/parse)


Line 2,217: Line 2,217:
(sparks "1 2 3 4 5 6 7 8 7 6 5 4 3 2 1")
(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")
(sparks "1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5")
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<lang racket>
<syntaxhighlight lang="racket">
"▁▂▃▄▅▆▇█▇▆▅▄▃▂▁"
"▁▂▃▄▅▆▇█▇▆▅▄▃▂▁"
"▂▁▄▃▆▅█▇"
"▂▁▄▃▆▅█▇"
</syntaxhighlight>
</lang>


=={{header|Raku}}==
=={{header|Raku}}==
(formerly Perl 6)
(formerly Perl 6)
<lang perl6>constant @bars = '▁' ... '█';
<syntaxhighlight lang="raku" line>constant @bars = '▁' ... '█';
while prompt 'Numbers separated by anything: ' -> $_ {
while prompt 'Numbers separated by anything: ' -> $_ {
my @numbers = map +*, .comb(/ '-'? [[\d+ ['.' \d*]?] | ['.' \d+]] /);
my @numbers = map +*, .comb(/ '-'? [[\d+ ['.' \d*]?] | ['.' \d+]] /);
Line 2,232: Line 2,232:
say "min: $mn.fmt('%5f'); max: $mx.fmt('%5f')";
say "min: $mn.fmt('%5f'); max: $mx.fmt('%5f')";
say @bars[ @numbers.map: { @bars * ($_ - $mn) / ($mx - $mn) min @bars - 1 } ].join;
say @bars[ @numbers.map: { @bars * ($_ - $mn) / ($mx - $mn) min @bars - 1 } ].join;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Numbers separated by anything: 9 18 27 36 45 54 63 72 63 54 45 36 27 18 9
<pre>Numbers separated by anything: 9 18 27 36 45 54 63 72 63 54 45 36 27 18 9
Line 2,252: Line 2,252:
{{Works with|ooRexx}}
{{Works with|ooRexx}}
{{Works with|Regina|3.4}}
{{Works with|Regina|3.4}}
<lang REXX>/* Rexx */
<syntaxhighlight lang="rexx">/* Rexx */


parse arg aaa
parse arg aaa
Line 2,319: Line 2,319:
return
return
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 2,344: Line 2,344:


It should also be noted that the CMS and TSO versions of REXX (and practically all others) don't support ''single line comments''.
It should also be noted that the CMS and TSO versions of REXX (and practically all others) don't support ''single line comments''.
<lang rexx>/*REXX program displays a sparkline (spark graph) for a group of values. */
<syntaxhighlight lang="rexx">/*REXX program displays a sparkline (spark graph) for a group of values. */
if arg()==0 then do /*Optional arguments? Then use defaults*/
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
call sparkGraph 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
Line 2,368: Line 2,368:
end /*j*/
end /*j*/


say 'output: ' z; say; return /*show the output, + a blank line*/</lang>
say 'output: ' z; say; return /*show the output, + a blank line*/</syntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
<pre>
Line 2,380: Line 2,380:
=={{header|Ruby}}==
=={{header|Ruby}}==
{{trans|Raku}} with added protection for input like "0 0 0 0".
{{trans|Raku}} with added protection for input like "0 0 0 0".
<lang ruby>bar = ('▁'..'█').to_a
<syntaxhighlight lang="ruby">bar = ('▁'..'█').to_a
loop {print 'Numbers please separated by space/commas: '
loop {print 'Numbers please separated by space/commas: '
numbers = gets.split(/[\s,]+/).map(&:to_f)
numbers = gets.split(/[\s,]+/).map(&:to_f)
Line 2,387: Line 2,387:
div = (max - min) / (bar.size - 1)
div = (max - min) / (bar.size - 1)
puts min == max ? bar.last*numbers.size : numbers.map{|num| bar[((num - min) / div).to_i]}.join
puts min == max ? bar.last*numbers.size : numbers.map{|num| bar[((num - min) / div).to_i]}.join
}</lang>
}</syntaxhighlight>


{{out}} Used Go testcases
{{out}} Used Go testcases
Line 2,414: Line 2,414:


=={{header|Rust}}==
=={{header|Rust}}==
<lang rust>
<syntaxhighlight lang="rust">
const BARS: &'static str = "▁▂▃▄▅▆▇█";
const BARS: &'static str = "▁▂▃▄▅▆▇█";


Line 2,432: Line 2,432:
print_sparkline(s2);
print_sparkline(s2);
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
"▁▂▃▄▅▆▇█▇▆▅▄▃▂▁"
"▁▂▃▄▅▆▇█▇▆▅▄▃▂▁"
Line 2,439: Line 2,439:


=={{header|S-lang}}==
=={{header|S-lang}}==
<lang S-lang>
<syntaxhighlight lang="s-lang">
% Just to demonstrate alternate ways of defining unicode:
% 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}";
private variable spchrs = "\u{2581}\u{2582}\u{2583}\u{2584}\u{2585}\u{2586}\u{2587}\u{2588}";
Line 2,465: Line 2,465:
sparkline("1 2 3 4 5 6 7 8 7 6 5 4 3 2 1");
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 ");
sparkline("1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5 ");
</syntaxhighlight>
</lang>
{{out}}
{{out}}
"▁▂▃▄▅▆▇█▇▆▅▄▃▂▁"
"▁▂▃▄▅▆▇█▇▆▅▄▃▂▁"
Line 2,472: Line 2,472:


=={{header|Scala}}==
=={{header|Scala}}==
<lang scala>def mkSparks( numStr:String ) : String =
<syntaxhighlight lang="scala">def mkSparks( numStr:String ) : String =
numStr.split( "[\\s,]+" ).map(_.toFloat) match {
numStr.split( "[\\s,]+" ).map(_.toFloat) match {
case v if v.isEmpty => ""
case v if v.isEmpty => ""
Line 2,487: Line 2,487:


// A random test...
// A random test...
println( mkSparks( Stream.continually( math.abs(util.Random.nextInt % 8)).take(64).mkString(" ") ))</lang>
println( mkSparks( Stream.continually( math.abs(util.Random.nextInt % 8)).take(64).mkString(" ") ))</syntaxhighlight>
{{out}}
{{out}}
<pre> ▁▂▃▄▅▆▇█▇▆▅▄▃▂▁
<pre> ▁▂▃▄▅▆▇█▇▆▅▄▃▂▁
Line 2,494: Line 2,494:


=={{header|Seed7}}==
=={{header|Seed7}}==
<lang seed7>$ include "seed7_05.s7i";
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "scanfile.s7i";
include "scanfile.s7i";
include "float.s7i";
include "float.s7i";
Line 2,546: Line 2,546:
data := readDataLine;
data := readDataLine;
end while;
end while;
end func;</lang>
end func;</syntaxhighlight>


{{out}}
{{out}}
Line 2,559: Line 2,559:
=={{header|SenseTalk}}==
=={{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.
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.
<lang sensetalk>put sparklineGraph of "1 2 3 4 5 6 7 8 7 6 5 4 3 2 1"
<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 "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, 1, 19, 20"
Line 2,578: Line 2,578:
return graph
return graph
end sparklineGraph
end sparklineGraph
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 2,589: Line 2,589:
=={{header|Sidef}}==
=={{header|Sidef}}==
{{trans|Ruby}}
{{trans|Ruby}}
<lang ruby>var bar = @('▁'..'█');
<syntaxhighlight lang="ruby">var bar = @('▁'..'█');
loop {
loop {
print 'Numbers, please, separated by space/commas: ';
print 'Numbers, please, separated by space/commas: ';
Line 2,597: Line 2,597:
var div = ((max - min) / bar.end);
var div = ((max - min) / bar.end);
say (min == max ? bar.last*numbers.len : numbers.map{|num| bar[(num - min) / div]}.join);
say (min == max ? bar.last*numbers.len : numbers.map{|num| bar[(num - min) / div]}.join);
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Numbers, please, separated by space/commas: 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
<pre>Numbers, please, separated by space/commas: 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
Line 2,608: Line 2,608:
=={{header|Tcl}}==
=={{header|Tcl}}==
{{works with|Tcl|8.6}}
{{works with|Tcl|8.6}}
<lang tcl>package require Tcl 8.6
<syntaxhighlight lang="tcl">package require Tcl 8.6


proc extractValues {series} {
proc extractValues {series} {
Line 2,622: Line 2,622:
set max [tcl::mathfunc::max {*}$values]
set max [tcl::mathfunc::max {*}$values]
return [join [lmap v $values {renderValue $min $max $v}] ""]
return [join [lmap v $values {renderValue $min $max $v}] ""]
}</lang>
}</syntaxhighlight>
Demonstrating:
Demonstrating:
<lang tcl>set data {
<syntaxhighlight lang="tcl">set data {
"1 2 3 4 5 6 7 8 7 6 5 4 3 2 1"
"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"
"1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5"
Line 2,631: Line 2,631:
puts "Series: $series"
puts "Series: $series"
puts "Sparkline: [sparkline $series]"
puts "Sparkline: [sparkline $series]"
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,641: Line 2,641:


=={{header|VBScript}}==
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
<lang vb>
'Sparkline
'Sparkline


Line 2,713: Line 2,713:


wscript.stdout.write "Press any key.." : wscript.stdin.read 1
wscript.stdout.write "Press any key.." : wscript.stdin.read 1
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 2,744: Line 2,744:
{{trans|Go}}
{{trans|Go}}
{{libheader|Wren-pattern}}
{{libheader|Wren-pattern}}
<lang ecmascript>import "io" for Stdin, Stdout
<syntaxhighlight lang="ecmascript">import "io" for Stdin, Stdout
import "/pattern" for Pattern
import "/pattern" for Pattern


Line 2,799: Line 2,799:
System.print(s)
System.print(s)
System.print()
System.print()
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 2,837: Line 2,837:


=={{header|zkl}}==
=={{header|zkl}}==
<lang zkl>var sparks=[0x2581..0x2588].apply("toString",-8); // int.toString(-8)-->UTF-8
<syntaxhighlight lang="zkl">var sparks=[0x2581..0x2588].apply("toString",-8); // int.toString(-8)-->UTF-8
var sl=(sparks.len()-1);
var sl=(sparks.len()-1);
Line 2,845: Line 2,845:
println("Range [",min,"-",max,"]", xs);
println("Range [",min,"-",max,"]", xs);
xs.pump(String,'wrap(x){ sparks[(x - min)*sl/range] }).println();
xs.pump(String,'wrap(x){ sparks[(x - min)*sl/range] }).println();
}</lang>
}</syntaxhighlight>
<lang zkl>one:="1 2 3 4 5 6 7 8 7 6 5 4 3 2 1".split(" ").apply("toInt");
<syntaxhighlight lang="zkl">one:="1 2 3 4 5 6 7 8 7 6 5 4 3 2 1".split(" ").apply("toInt");
two:=("1.5, 0.5 3.5, 2.5 5.5 4.5 7.5, 6.5" - ",").split(" ").apply("toFloat");
two:=("1.5, 0.5 3.5, 2.5 5.5 4.5 7.5, 6.5" - ",").split(" ").apply("toFloat");
sparkLine(one); sparkLine(two);</lang>
sparkLine(one); sparkLine(two);</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,858: Line 2,858:


=={{header|Zoea}}==
=={{header|Zoea}}==
<syntaxhighlight lang="zoea">
<lang Zoea>
program: sparkline
program: sparkline
input: '1 2, 3 4.5 5 6,7 8'
input: '1 2, 3 4.5 5 6,7 8'
Line 2,864: Line 2,864:
derive: [1,2,3,4,5,6,7,8]
derive: [1,2,3,4,5,6,7,8]
output: '▁▂▃▄▅▆▇█'
output: '▁▂▃▄▅▆▇█'
</syntaxhighlight>
</lang>


=={{header|Zoea Visual}}==
=={{header|Zoea Visual}}==