Sparkline in unicode: Difference between revisions

From Rosetta Code
Content added Content deleted
(New draft task and Python solution.)
 
(use FULLBLOCK, not LEFT SEVEN EIGHTHS BLOCK, don't skip LOWER HALF BLOCK)
Line 4: Line 4:
Use the following series of Unicode characters to create a program that takes a series of numbers separated by one or more whitespace or comma characters and generates a sparkline-type bar graph of the values on a single line of output.
Use the following series of Unicode characters to create a program that takes a series of numbers separated by one or more whitespace or comma characters and generates a sparkline-type bar graph of the values on a single line of output.


The seven characters: '▁▂▃▅▆▇▉'<br>
The eight characters: '▁▂▃▄▅▆▇█'<br>
(Unicode values 9601, 9602, 9603, 9605, 9606, 9607, and 9609).
(Unicode values U+2581 through U+2588).


Use your program to show sparklines for the following input, here on this page:
Use your program to show sparklines for the following input, here on this page:
Line 20: Line 20:
<lang python>import re
<lang python>import re


# Unicode: 9601, 9602, 9603, 9605, 9606, 9607, 9609
# Unicode: 9601, 9602, 9603, 9604, 9605, 9606, 9607, 9608
bar = '▁ ▂ ▃ ▅ ▆ ▇ '.split()
bar = '▁ ▂ ▃ ▅ ▆ ▇ '.split()
barcount = len(bar) - 1
barcount = len(bar) - 1
while True:
while True:
Line 34: Line 34:


{{out}}
{{out}}
<pre>Numbers separated by space/commas: 1 2 3 4 5 6 7 6 5 4 3 2 1
<pre>Numbers separated by space/commas: 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
min: 1.000000; max: 7.000000
min: 1.000000; max: 7.000000
▁▂▃▄▅▆▇█▇▆▅▄▃▂▁
▁▂▃▅▆▇▉▇▆▅▃▂▁
Numbers separated by space/commas: 1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5
Numbers separated by space/commas: 1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5
min: 0.500000; max: 7.500000
min: 0.500000; max: 7.500000
▂▁▄▃▆▅█▇</pre>
▁▁▃▂▆▅▉▇</pre>

Revision as of 21:58, 17 June 2013

Sparkline in unicode is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

A sparkline is a graph of successive values laid out horizontally where the height of the line is proportional to the values in succession.

Use the following series of Unicode characters to create a program that takes a series of numbers separated by one or more whitespace or comma characters and generates a sparkline-type bar graph of the values on a single line of output.

The eight characters: '▁▂▃▄▅▆▇█'
(Unicode values U+2581 through U+2588).

Use your program to show sparklines for the following input, here on this page:

  1. 1 2 3 4 5 6 7 6 5 4 3 2 1
  2. 1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5
(note the mix of separators in this second case)!


Notes
  • A space is not part of the generated sparkline.
  • The sparkline may be accompanied by simple statistics of the data such as its range.

Python

<lang python>import re

  1. Unicode: 9601, 9602, 9603, 9604, 9605, 9606, 9607, 9608

bar = '▁ ▂ ▃ ▄ ▅ ▆ ▇ █'.split() barcount = len(bar) - 1 while True:

   line = input('Numbers please separated by space/commas: ')
   numbers = [float(n) for n in re.split(r'[\s,]+', line.strip())]
   mn, mx = min(numbers), max(numbers)
   extent = mx - mn
   sparkline = .join(bar[int( (n - mn) / extent * barcount)]
                       for n in numbers)
   print('min: %5f; max: %5f' % (mn, mx))
   print(sparkline)</lang>
Output:
Numbers separated by space/commas: 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
min: 1.000000; max: 7.000000
▁▂▃▄▅▆▇█▇▆▅▄▃▂▁
Numbers separated by space/commas: 1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5
min: 0.500000; max: 7.500000
▂▁▄▃▆▅█▇