org-table: Fix ranges in `orgtbl-ascii-draw'

* lisp/org-table.el (orgtbl-ascii-draw): Fix float to integer
  conversion.
* testing/lisp/test-org-table.el (test-org-table/orgtbl-ascii-draw):
  Adapt test.
This commit is contained in:
Michael Brand 2016-01-30 16:30:52 +01:00
parent 6b03fd9a86
commit b4105b430c
2 changed files with 40 additions and 41 deletions

View File

@ -5256,26 +5256,26 @@ supported. It is also possible to use the following ones:
(defun orgtbl-ascii-draw (value min max &optional width characters)
"Draw an ascii bar in a table.
VALUE is a the value to plot, the width of the bar to draw. A
value equal to MIN will be displayed as empty (zero width bar).
A value equal to MAX will draw a bar filling all the WIDTH.
WIDTH is the expected width in characters of the column.
CHARACTERS is a string that will compose the bar, with shades of
grey from pure white to pure black. It defaults to a 10
characters string of regular ascii characters."
(let* ((characters (or characters " .:;c!lhVHW"))
(width (or width 12))
(value (if (numberp value) value (string-to-number value)))
(value (* (/ (- (+ value 0.0) min) (- max min)) width)))
(cond
((< value 0) "too small")
((> value width) "too large")
(t
(let ((len (1- (length characters))))
(concat
(make-string (floor value) (elt characters len))
(string (elt characters
(floor (* (- value (floor value)) len))))))))))
VALUE is the value to plot, it determines the width of the bar to draw.
MIN is the value that will be displayed as empty (zero width bar).
MAX is the value that will draw a bar filling all the WIDTH.
WIDTH is the span in characters from MIN to MAX.
CHARACTERS is a string that will compose the bar, with shades of grey
from pure white to pure black. It defaults to a 10 characters string
of regular ascii characters."
(let* ((width (ceiling (or width 12)))
(characters (or characters " .:;c!lhVHW"))
(len (1- (length characters)))
(value (float (if (numberp value)
value (string-to-number value))))
(relative (/ (- value min) (- max min)))
(steps (round (* relative width len))))
(cond ((< steps 0) "too small")
((> steps (* width len)) "too large")
(t (let* ((int-division (/ steps len))
(remainder (- steps (* int-division len))))
(concat (make-string int-division (elt characters len))
(string (elt characters remainder))))))))
;;;###autoload
(defun orgtbl-ascii-plot (&optional ask)

View File

@ -1852,7 +1852,6 @@ is t, then new columns should be added as needed"
;; First value: Make sure that an integer input value is converted to a
;; float before division. Further values: Show some float input value
;; ranges corresponding to the same bar width.
;; TODO Fix ranges in `orgtbl-ascii-draw'.
(should
(equal
(org-test-with-temp-text
@ -1861,16 +1860,16 @@ is t, then new columns should be added as needed"
|----------+---------|
| 19 | replace |
|----------+---------|
| -0.00001 | replace |
| 0.00000 | replace |
| 0.99999 | replace |
| 1.00000 | replace |
| 1.99999 | replace |
| 23.00000 | replace |
| 23.99999 | replace |
| 24.00000 | replace |
| 24.00000 | replace |
| 24.00001 | replace |
| -0.50001 | replace |
| -0.49999 | replace |
| 0.49999 | replace |
| 0.50001 | replace |
| 1.49999 | replace |
| 22.50001 | replace |
| 23.49999 | replace |
| 23.50001 | replace |
| 24.49999 | replace |
| 24.50001 | replace |
<point>#+TBLFM: $2 = '(orgtbl-ascii-draw $1 0 24 3 \" 12345678\")"
(org-table-calc-current-TBLFM)
(buffer-string))
@ -1879,16 +1878,16 @@ is t, then new columns should be added as needed"
|----------+-----------|
| 19 | 883 |
|----------+-----------|
| -0.00001 | too small |
| 0.00000 | |
| 0.99999 | |
| 1.00000 | 1 |
| 1.99999 | 1 |
| 23.00000 | 887 |
| 23.99999 | 887 |
| 24.00000 | 888 |
| 24.00000 | 888 |
| 24.00001 | too large |
| -0.50001 | too small |
| -0.49999 | |
| 0.49999 | |
| 0.50001 | 1 |
| 1.49999 | 1 |
| 22.50001 | 887 |
| 23.49999 | 887 |
| 23.50001 | 888 |
| 24.49999 | 888 |
| 24.50001 | too large |
#+TBLFM: $2 = '(orgtbl-ascii-draw $1 0 24 3 \" 12345678\")"))
;; Draw bars with a bullet. The bullet does not count in the parameter
;; WIDTH of `orgtbl-ascii-draw'.