From b4105b430cbd37f99c641e53eee515888ab35459 Mon Sep 17 00:00:00 2001 From: Michael Brand Date: Sat, 30 Jan 2016 16:30:52 +0100 Subject: [PATCH] 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. --- lisp/org-table.el | 40 ++++++++++++++++----------------- testing/lisp/test-org-table.el | 41 +++++++++++++++++----------------- 2 files changed, 40 insertions(+), 41 deletions(-) diff --git a/lisp/org-table.el b/lisp/org-table.el index 3100f8062..488c91210 100644 --- a/lisp/org-table.el +++ b/lisp/org-table.el @@ -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) diff --git a/testing/lisp/test-org-table.el b/testing/lisp/test-org-table.el index dc8dc6883..aded523e2 100644 --- a/testing/lisp/test-org-table.el +++ b/testing/lisp/test-org-table.el @@ -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 | #+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'.