chart.el 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. ;;; chart.el --- Draw charts (bar charts, etc) -*- lexical-binding: t -*-
  2. ;; Copyright (C) 1996, 1998-1999, 2001, 2004-2005, 2007-2015 Free
  3. ;; Software Foundation, Inc.
  4. ;; Author: Eric M. Ludlam <zappo@gnu.org>
  5. ;; Version: 0.2
  6. ;; Keywords: OO, chart, graph
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;;
  20. ;; This package is an experiment of mine aiding in the debugging of
  21. ;; eieio, and proved to be neat enough that others may like to use
  22. ;; it. To quickly see what you can do with chart, run the command
  23. ;; `chart-test-it-all'.
  24. ;;
  25. ;; Chart current can display bar-charts in either of two
  26. ;; directions. It also supports ranged (integer) axis, and axis
  27. ;; defined by some set of strings or names. These name can be
  28. ;; automatically derived from data sequences, which are just lists of
  29. ;; anything encapsulated in a nice eieio object.
  30. ;;
  31. ;; Current example apps for chart can be accessed via these commands:
  32. ;; `chart-file-count' - count files w/ matching extensions
  33. ;; `chart-space-usage' - display space used by files/directories
  34. ;; `chart-emacs-storage' - Emacs storage units used/free (garbage-collect)
  35. ;; `chart-emacs-lists' - length of Emacs lists
  36. ;; `chart-rmail-from' - who sends you the most mail (in -summary only)
  37. ;;
  38. ;; Customization:
  39. ;;
  40. ;; If you find the default colors and pixmaps unpleasant, or too
  41. ;; short, you can change them. The variable `chart-face-color-list'
  42. ;; contains a list of colors, and `chart-face-pixmap-list' contains
  43. ;; all the pixmaps to use. The current pixmaps are those found on
  44. ;; several systems I found. The two lists should be the same length,
  45. ;; as the long list will just be truncated.
  46. ;;
  47. ;; If you would like to draw your own stipples, simply create some
  48. ;; xbm's and put them in a directory, then you can add:
  49. ;;
  50. ;; (setq x-bitmap-file-path (cons "~/mybitmaps" x-bitmap-file-path))
  51. ;;
  52. ;; to your .emacs (or wherever) and load the `chart-face-pixmap-list'
  53. ;; with all the bitmaps you want to use.
  54. (require 'eieio)
  55. ;;; Code:
  56. (define-obsolete-variable-alias 'chart-map 'chart-mode-map "24.1")
  57. (defvar chart-mode-map (make-sparse-keymap) "Keymap used in chart mode.")
  58. (defvar chart-local-object nil
  59. "Local variable containing the locally displayed chart object.")
  60. (make-variable-buffer-local 'chart-local-object)
  61. (defvar chart-face-color-list '("red" "green" "blue"
  62. "cyan" "yellow" "purple")
  63. "Colors to use when generating `chart-face-list'.
  64. Colors will be the background color.")
  65. (defvar chart-face-pixmap-list
  66. (if (and (fboundp 'display-graphic-p)
  67. (display-graphic-p))
  68. '("dimple1" "scales" "dot" "cross_weave" "boxes" "dimple3"))
  69. "If pixmaps are allowed, display these background pixmaps.
  70. Useful if new Emacs is used on B&W display.")
  71. (defcustom chart-face-use-pixmaps nil
  72. "Non-nil to use fancy pixmaps in the background of chart face colors."
  73. :group 'eieio
  74. :type 'boolean)
  75. (declare-function x-display-color-cells "xfns.c" (&optional terminal))
  76. (defvar chart-face-list
  77. (if (display-color-p)
  78. (let ((cl chart-face-color-list)
  79. (pl chart-face-pixmap-list)
  80. (faces ())
  81. nf)
  82. (while cl
  83. (setq nf (make-face
  84. (intern (concat "chart-" (car cl) "-" (car pl)))))
  85. (set-face-background nf (if (condition-case nil
  86. (> (x-display-color-cells) 4)
  87. (error t))
  88. (car cl)
  89. "white"))
  90. (set-face-foreground nf "black")
  91. (if (and chart-face-use-pixmaps
  92. pl
  93. (fboundp 'set-face-background-pixmap))
  94. (condition-case nil
  95. (set-face-background-pixmap nf (car pl))
  96. (error (message "Cannot set background pixmap %s" (car pl)))))
  97. (push nf faces)
  98. (setq cl (cdr cl)
  99. pl (cdr pl)))
  100. faces))
  101. "Faces used to colorize charts.
  102. List is limited currently, which is ok since you really can't display
  103. too much in text characters anyways.")
  104. (define-derived-mode chart-mode fundamental-mode "CHART"
  105. "Define a mode in Emacs for displaying a chart."
  106. (buffer-disable-undo)
  107. (set (make-local-variable 'font-lock-global-modes) nil)
  108. (font-lock-mode -1) ;Isn't it off already? --Stef
  109. )
  110. (defun chart-new-buffer (obj)
  111. "Create a new buffer NAME in which the chart OBJ is displayed.
  112. Returns the newly created buffer."
  113. (with-current-buffer (get-buffer-create (format "*%s*" (oref obj title)))
  114. (chart-mode)
  115. (setq chart-local-object obj)
  116. (current-buffer)))
  117. (defclass chart ()
  118. ((title :initarg :title
  119. :initform "Emacs Chart")
  120. (title-face :initarg :title-face
  121. :initform 'bold-italic)
  122. (x-axis :initarg :x-axis
  123. :initform nil )
  124. (x-margin :initarg :x-margin
  125. :initform 5)
  126. (x-width :initarg :x-width
  127. )
  128. (y-axis :initarg :y-axis
  129. :initform nil)
  130. (y-margin :initarg :y-margin
  131. :initform 5)
  132. (y-width :initarg :y-width
  133. )
  134. (key-label :initarg :key-label
  135. :initform "Key")
  136. (sequences :initarg :sequences
  137. :initform nil)
  138. )
  139. "Superclass for all charts to be displayed in an Emacs buffer.")
  140. (defmethod initialize-instance :AFTER ((obj chart) &rest _fields)
  141. "Initialize the chart OBJ being created with FIELDS.
  142. Make sure the width/height is correct."
  143. (oset obj x-width (- (window-width) 10))
  144. (oset obj y-width (- (window-height) 12)))
  145. (defclass chart-axis ()
  146. ((name :initarg :name
  147. :initform "Generic Axis")
  148. (loweredge :initarg :loweredge
  149. :initform t)
  150. (name-face :initarg :name-face
  151. :initform 'bold)
  152. (labels-face :initarg :labels-face
  153. :initform 'italic)
  154. (chart :initarg :chart
  155. :initform nil)
  156. )
  157. "Superclass used for display of an axis.")
  158. (defclass chart-axis-range (chart-axis)
  159. ((bounds :initarg :bounds
  160. :initform '(0.0 . 50.0))
  161. )
  162. "Class used to display an axis defined by a range of values.")
  163. (defclass chart-axis-names (chart-axis)
  164. ((items :initarg :items
  165. :initform nil)
  166. )
  167. "Class used to display an axis which represents different named items.")
  168. (defclass chart-sequece ()
  169. ((data :initarg :data
  170. :initform nil)
  171. (name :initarg :name
  172. :initform "Data")
  173. )
  174. "Class used for all data in different charts.")
  175. (defclass chart-bar (chart)
  176. ((direction :initarg :direction
  177. :initform vertical))
  178. "Subclass for bar charts (vertical or horizontal).")
  179. (defmethod chart-draw ((c chart) &optional buff)
  180. "Start drawing a chart object C in optional BUFF.
  181. Erases current contents of buffer."
  182. (save-excursion
  183. (if buff (set-buffer buff))
  184. (erase-buffer)
  185. (insert (make-string 100 ?\n))
  186. ;; Start by displaying the axis
  187. (chart-draw-axis c)
  188. ;; Display title
  189. (chart-draw-title c)
  190. ;; Display data
  191. (message "Rendering chart...")
  192. (sit-for 0)
  193. (chart-draw-data c)
  194. ;; Display key
  195. ; (chart-draw-key c)
  196. (message "Rendering chart...done")
  197. ))
  198. (defmethod chart-draw-title ((c chart))
  199. "Draw a title upon the chart.
  200. Argument C is the chart object."
  201. (chart-display-label (oref c title) 'horizontal 0 0 (window-width)
  202. (oref c title-face)))
  203. (defmethod chart-size-in-dir ((c chart) dir)
  204. "Return the physical size of chart C in direction DIR."
  205. (if (eq dir 'vertical)
  206. (oref c y-width)
  207. (oref c x-width)))
  208. (defmethod chart-draw-axis ((c chart))
  209. "Draw axis into the current buffer defined by chart C."
  210. (let ((ymarg (oref c y-margin))
  211. (xmarg (oref c x-margin))
  212. (ylen (oref c y-width))
  213. (xlen (oref c x-width)))
  214. (chart-axis-draw (oref c y-axis) 'vertical ymarg
  215. (if (oref (oref c y-axis) loweredge) nil xlen)
  216. xmarg (+ xmarg ylen))
  217. (chart-axis-draw (oref c x-axis) 'horizontal xmarg
  218. (if (oref (oref c x-axis) loweredge) nil ylen)
  219. ymarg (+ ymarg xlen)))
  220. )
  221. (defmethod chart-axis-draw ((a chart-axis) &optional dir margin zone start end)
  222. "Draw some axis for A in direction DIR with MARGIN in boundary.
  223. ZONE is a zone specification.
  224. START and END represent the boundary."
  225. (chart-draw-line dir (+ margin (if zone zone 0)) start end)
  226. (chart-display-label (oref a name) dir (if zone (+ zone margin 3)
  227. (if (eq dir 'horizontal)
  228. 1 0))
  229. start end (oref a name-face)))
  230. (defmethod chart-translate-xpos ((c chart) x)
  231. "Translate in chart C the coordinate X into a screen column."
  232. (let ((range (oref (oref c x-axis) bounds)))
  233. (+ (oref c x-margin)
  234. (round (* (float (- x (car range)))
  235. (/ (float (oref c x-width))
  236. (float (- (cdr range) (car range))))))))
  237. )
  238. (defmethod chart-translate-ypos ((c chart) y)
  239. "Translate in chart C the coordinate Y into a screen row."
  240. (let ((range (oref (oref c y-axis) bounds)))
  241. (+ (oref c x-margin)
  242. (- (oref c y-width)
  243. (round (* (float (- y (car range)))
  244. (/ (float (oref c y-width))
  245. (float (- (cdr range) (car range)))))))))
  246. )
  247. (defmethod chart-axis-draw ((a chart-axis-range) &optional dir margin zone _start _end)
  248. "Draw axis information based upon a range to be spread along the edge.
  249. A is the chart to draw. DIR is the direction.
  250. MARGIN, ZONE, START, and END specify restrictions in chart space."
  251. (cl-call-next-method)
  252. ;; We prefer about 5 spaces between each value
  253. (let* ((i (car (oref a bounds)))
  254. (e (cdr (oref a bounds)))
  255. (z (if zone zone 0))
  256. (s nil)
  257. (rng (- e i))
  258. ;; want to jump by units of 5 spaces or so
  259. (j (/ rng (/ (chart-size-in-dir (oref a chart) dir) 4)))
  260. p1)
  261. (if (= j 0) (setq j 1))
  262. (while (<= i e)
  263. (setq s
  264. (cond ((> i 999999)
  265. (format "%dM" (/ i 1000000)))
  266. ((> i 999)
  267. (format "%dK" (/ i 1000)))
  268. (t
  269. (format "%d" i))))
  270. (if (eq dir 'vertical)
  271. (let ((x (+ (+ margin z) (if (oref a loweredge)
  272. (- (length s)) 1))))
  273. (if (< x 1) (setq x 1))
  274. (chart-goto-xy x (chart-translate-ypos (oref a chart) i)))
  275. (chart-goto-xy (chart-translate-xpos (oref a chart) i)
  276. (+ margin z (if (oref a loweredge) -1 1))))
  277. (setq p1 (point))
  278. (insert s)
  279. (chart-zap-chars (length s))
  280. (put-text-property p1 (point) 'face (oref a labels-face))
  281. (setq i (+ i j))))
  282. )
  283. (defmethod chart-translate-namezone ((c chart) n)
  284. "Return a dot-pair representing a positional range for a name.
  285. The name in chart C of the Nth name resides.
  286. Automatically compensates for direction."
  287. (let* ((dir (oref c direction))
  288. (w (if (eq dir 'vertical) (oref c x-width) (oref c y-width)))
  289. (m (if (eq dir 'vertical) (oref c y-margin) (oref c x-margin)))
  290. (ns (length
  291. (oref (if (eq dir 'vertical) (oref c x-axis) (oref c y-axis))
  292. items)))
  293. (lpn (/ (+ 1.0 (float w)) (float ns)))
  294. )
  295. (cons (+ m (round (* lpn (float n))))
  296. (+ m -1 (round (* lpn (+ 1.0 (float n))))))
  297. ))
  298. (defmethod chart-axis-draw ((a chart-axis-names) &optional dir margin zone _start _end)
  299. "Draw axis information based upon A range to be spread along the edge.
  300. Optional argument DIR is the direction of the chart.
  301. Optional arguments MARGIN, ZONE, START and END specify boundaries of the drawing."
  302. (cl-call-next-method)
  303. ;; We prefer about 5 spaces between each value
  304. (let* ((i 0)
  305. (s (oref a items))
  306. (z (if zone zone 0))
  307. (r nil)
  308. (p nil)
  309. (odd nil)
  310. p1)
  311. (while s
  312. (setq odd (= (% (length s) 2) 1))
  313. (setq r (chart-translate-namezone (oref a chart) i))
  314. (if (eq dir 'vertical)
  315. (setq p (/ (+ (car r) (cdr r)) 2))
  316. (setq p (- (+ (car r) (/ (- (cdr r) (car r)) 2))
  317. (/ (length (car s)) 2))))
  318. (if (eq dir 'vertical)
  319. (let ((x (+ (+ margin z) (if (oref a loweredge)
  320. (- (length (car s)))
  321. (length (car s))))))
  322. (if (< x 1) (setq x 1))
  323. (if (> (length (car s)) (1- margin))
  324. (setq x (+ x margin)))
  325. (chart-goto-xy x p))
  326. (chart-goto-xy p (+ (+ margin z) (if (oref a loweredge)
  327. (if odd -2 -1)
  328. (if odd 2 1)))))
  329. (setq p1 (point))
  330. (insert (car s))
  331. (chart-zap-chars (length (car s)))
  332. (put-text-property p1 (point) 'face (oref a labels-face))
  333. (setq i (+ i 1)
  334. s (cdr s))))
  335. )
  336. (defmethod chart-draw-data ((c chart-bar))
  337. "Display the data available in a bar chart C."
  338. (let* ((data (oref c sequences))
  339. (dir (oref c direction))
  340. (odir (if (eq dir 'vertical) 'horizontal 'vertical))
  341. )
  342. (while data
  343. (if (stringp (car (oref (car data) data)))
  344. ;; skip string lists...
  345. nil
  346. ;; display number lists...
  347. (let ((i 0)
  348. (seq (oref (car data) data)))
  349. (while seq
  350. (let* ((rng (chart-translate-namezone c i))
  351. (dp (if (eq dir 'vertical)
  352. (chart-translate-ypos c (car seq))
  353. (chart-translate-xpos c (car seq))))
  354. (zp (if (eq dir 'vertical)
  355. (chart-translate-ypos c 0)
  356. (chart-translate-xpos c 0)))
  357. (fc (if chart-face-list
  358. (nth (% i (length chart-face-list)) chart-face-list)
  359. 'default))
  360. )
  361. (if (< dp zp)
  362. (progn
  363. (chart-draw-line dir (car rng) dp zp)
  364. (chart-draw-line dir (cdr rng) dp zp))
  365. (chart-draw-line dir (car rng) zp (1+ dp))
  366. (chart-draw-line dir (cdr rng) zp (1+ dp)))
  367. (if (= (car rng) (cdr rng)) nil
  368. (chart-draw-line odir dp (1+ (car rng)) (cdr rng))
  369. (chart-draw-line odir zp (car rng) (1+ (cdr rng))))
  370. (if (< dp zp)
  371. (chart-deface-rectangle dir rng (cons dp zp) fc)
  372. (chart-deface-rectangle dir rng (cons zp dp) fc))
  373. )
  374. ;; find the bounds, and chart it!
  375. ;; for now, only do one!
  376. (setq i (1+ i)
  377. seq (cdr seq)))))
  378. (setq data (cdr data))))
  379. )
  380. (defmethod chart-add-sequence ((c chart) &optional seq axis-label)
  381. "Add to chart object C the sequence object SEQ.
  382. If AXIS-LABEL, then the axis stored in C is updated with the bounds of SEQ,
  383. or is created with the bounds of SEQ."
  384. (if axis-label
  385. (let ((axis (eieio-oref c axis-label)))
  386. (if (stringp (car (oref seq data)))
  387. (let ((labels (oref seq data)))
  388. (if (not axis)
  389. (setq axis (make-instance 'chart-axis-names
  390. :name (oref seq name)
  391. :items labels
  392. :chart c))
  393. (oset axis items labels)))
  394. (let ((range (cons 0 1))
  395. (l (oref seq data)))
  396. (if (not axis)
  397. (setq axis (make-instance 'chart-axis-range
  398. :name (oref seq name)
  399. :chart c)))
  400. (while l
  401. (if (< (car l) (car range)) (setcar range (car l)))
  402. (if (> (car l) (cdr range)) (setcdr range (car l)))
  403. (setq l (cdr l)))
  404. (oset axis bounds range)))
  405. (if (eq axis-label 'x-axis) (oset axis loweredge nil))
  406. (eieio-oset c axis-label axis)
  407. ))
  408. (oset c sequences (append (oref c sequences) (list seq))))
  409. ;;; Charting optimizers
  410. (defmethod chart-trim ((c chart) max)
  411. "Trim all sequences in chart C to be at most MAX elements long."
  412. (let ((s (oref c sequences)))
  413. (while s
  414. (let ((sl (oref (car s) data)))
  415. (if (> (length sl) max)
  416. (setcdr (nthcdr (1- max) sl) nil)))
  417. (setq s (cdr s))))
  418. )
  419. (defmethod chart-sort ((c chart) pred)
  420. "Sort the data in chart C using predicate PRED.
  421. See `chart-sort-matchlist' for more details."
  422. (let* ((sl (oref c sequences))
  423. (s1 (car sl))
  424. (s2 (car (cdr sl)))
  425. (s nil))
  426. (if (stringp (car (oref s1 data)))
  427. (progn
  428. (chart-sort-matchlist s1 s2 pred)
  429. (setq s (oref s1 data)))
  430. (if (stringp (car (oref s2 data)))
  431. (progn
  432. (chart-sort-matchlist s2 s1 pred)
  433. (setq s (oref s2 data)))
  434. (error "Sorting of chart %s not supported" (eieio-object-name c))))
  435. (if (eq (oref c direction) 'horizontal)
  436. (oset (oref c y-axis) items s)
  437. (oset (oref c x-axis) items s)
  438. ))
  439. )
  440. (defun chart-sort-matchlist (namelst numlst pred)
  441. "Sort NAMELST and NUMLST (both sequence objects) based on predicate PRED.
  442. PRED should be the equivalent of '<, except it must expect two
  443. cons cells of the form (NAME . NUM). See `sort' for more details."
  444. ;; 1 - create 1 list of cons cells
  445. (let ((newlist nil)
  446. (alst (oref namelst data))
  447. (ulst (oref numlst data)))
  448. (while alst
  449. ;; this is reversed, but were are sorting anyway
  450. (setq newlist (cons (cons (car alst) (car ulst)) newlist))
  451. (setq alst (cdr alst)
  452. ulst (cdr ulst)))
  453. ;; 2 - Run sort routine on it
  454. (setq newlist (sort newlist pred)
  455. alst nil
  456. ulst nil)
  457. ;; 3 - Separate the lists
  458. (while newlist
  459. (setq alst (cons (car (car newlist)) alst)
  460. ulst (cons (cdr (car newlist)) ulst))
  461. (setq newlist (cdr newlist)))
  462. ;; 4 - Store them back
  463. (oset namelst data (reverse alst))
  464. (oset numlst data (reverse ulst))))
  465. ;;; Utilities
  466. (defun chart-goto-xy (x y)
  467. "Move cursor to position X Y in buffer, and add spaces and CRs if needed."
  468. (let ((indent-tabs-mode nil)
  469. (num (progn (goto-char (point-min)) (forward-line y))))
  470. (if (and (= 0 num) (/= 0 (current-column))) (newline 1))
  471. (if (eobp) (newline num))
  472. (if (< x 0) (setq x 0))
  473. (if (< y 0) (setq y 0))
  474. ;; Now, a quicky column moveto/forceto method.
  475. (or (= (move-to-column x) x)
  476. (let ((p (point)))
  477. (indent-to x)
  478. (remove-text-properties p (point) '(face))))))
  479. (defun chart-zap-chars (n)
  480. "Zap up to N chars without deleting EOLs."
  481. (if (not (eobp))
  482. (if (< n (- (point-at-eol) (point)))
  483. (delete-char n)
  484. (delete-region (point) (point-at-eol)))))
  485. (defun chart-display-label (label dir zone start end &optional face)
  486. "Display LABEL in direction DIR in column/row ZONE between START and END.
  487. Optional argument FACE is the property we wish to place on this text."
  488. (if (eq dir 'horizontal)
  489. (let (p1)
  490. (chart-goto-xy (+ start (- (/ (- end start) 2) (/ (length label) 2)))
  491. zone)
  492. (setq p1 (point))
  493. (insert label)
  494. (chart-zap-chars (length label))
  495. (put-text-property p1 (point) 'face face)
  496. )
  497. (let ((i 0)
  498. (stz (+ start (- (/ (- end start) 2) (/ (length label) 2)))))
  499. (while (< i (length label))
  500. (chart-goto-xy zone (+ stz i))
  501. (insert (aref label i))
  502. (chart-zap-chars 1)
  503. (put-text-property (1- (point)) (point) 'face face)
  504. (setq i (1+ i))))))
  505. (defun chart-draw-line (dir zone start end)
  506. "Draw a line using line-drawing characters in direction DIR.
  507. Use column or row ZONE between START and END."
  508. (chart-display-label
  509. (make-string (- end start) (if (eq dir 'vertical) ?| ?\-))
  510. dir zone start end))
  511. (defun chart-deface-rectangle (dir r1 r2 face)
  512. "Colorize a rectangle in direction DIR across range R1 by range R2.
  513. R1 and R2 are dotted pairs. Colorize it with FACE."
  514. (let* ((range1 (if (eq dir 'vertical) r1 r2))
  515. (range2 (if (eq dir 'vertical) r2 r1))
  516. (y (car range2)))
  517. (while (<= y (cdr range2))
  518. (chart-goto-xy (car range1) y)
  519. (put-text-property (point) (+ (point) (1+ (- (cdr range1) (car range1))))
  520. 'face face)
  521. (setq y (1+ y)))))
  522. ;;; Helpful `I don't want to learn eieio just now' washover functions
  523. (defun chart-bar-quickie (dir title namelst nametitle numlst numtitle
  524. &optional max sort-pred)
  525. "Wash over the complex EIEIO stuff and create a nice bar chart.
  526. Create it going in direction DIR ['horizontal 'vertical] with TITLE
  527. using a name sequence NAMELST labeled NAMETITLE with values NUMLST
  528. labeled NUMTITLE.
  529. Optional arguments:
  530. Set the chart's max element display to MAX, and sort lists with
  531. SORT-PRED if desired."
  532. (let ((nc (make-instance 'chart-bar
  533. :title title
  534. :key-label "8-m" ; This is a text key pic
  535. :direction dir
  536. ))
  537. (iv (eq dir 'vertical)))
  538. (chart-add-sequence nc
  539. (make-instance 'chart-sequece
  540. :data namelst
  541. :name nametitle)
  542. (if iv 'x-axis 'y-axis))
  543. (chart-add-sequence nc
  544. (make-instance 'chart-sequece
  545. :data numlst
  546. :name numtitle)
  547. (if iv 'y-axis 'x-axis))
  548. (if sort-pred (chart-sort nc sort-pred))
  549. (if (integerp max) (chart-trim nc max))
  550. (switch-to-buffer (chart-new-buffer nc))
  551. (chart-draw nc)))
  552. ;;; Test code
  553. (defun chart-test-it-all ()
  554. "Test out various charting features."
  555. (interactive)
  556. (chart-bar-quickie 'vertical "Test Bar Chart"
  557. '( "U1" "ME2" "C3" "B4" "QT" "EZ") "Items"
  558. '( 5 -10 23 20 30 -3) "Values")
  559. )
  560. ;;; Sample utility function
  561. (defun chart-file-count (dir)
  562. "Draw a chart displaying the number of different file extensions in DIR."
  563. (interactive "DDirectory: ")
  564. (if (not (string-match "/$" dir))
  565. (setq dir (concat dir "/")))
  566. (message "Collecting statistics...")
  567. (let ((flst (directory-files dir nil nil t))
  568. (extlst (list "<dir>"))
  569. (cntlst (list 0)))
  570. (while flst
  571. (let* ((j (string-match "[^\\.]\\(\\.[a-zA-Z]+\\|~\\|#\\)$" (car flst)))
  572. (s (if (file-accessible-directory-p (concat dir (car flst)))
  573. "<dir>"
  574. (if j
  575. (substring (car flst) (match-beginning 1) (match-end 1))
  576. nil)))
  577. (m (member s extlst)))
  578. (if (not s) nil
  579. (if m
  580. (let ((cell (nthcdr (- (length extlst) (length m)) cntlst)))
  581. (setcar cell (1+ (car cell))))
  582. (setq extlst (cons s extlst)
  583. cntlst (cons 1 cntlst)))))
  584. (setq flst (cdr flst)))
  585. ;; Let's create the chart!
  586. (chart-bar-quickie 'vertical "Files Extension Distribution"
  587. extlst "File Extensions"
  588. cntlst "# of occurrences"
  589. 10
  590. (lambda (a b) (> (cdr a) (cdr b))))
  591. ))
  592. (defun chart-space-usage (d)
  593. "Display a top usage chart for directory D."
  594. (interactive "DDirectory: ")
  595. (message "Collecting statistics...")
  596. (let ((nmlst nil)
  597. (cntlst nil)
  598. (b (get-buffer-create " *du-tmp*")))
  599. (set-buffer b)
  600. (erase-buffer)
  601. (insert "cd " d ";du -sk * \n")
  602. (message "Running ‘cd %s;du -sk *’..." d)
  603. (call-process-region (point-min) (point-max) shell-file-name t
  604. (current-buffer) nil)
  605. (goto-char (point-min))
  606. (message "Scanning output ...")
  607. (while (re-search-forward "^\\([0-9]+\\)[ \t]+\\([^ \n]+\\)$" nil t)
  608. (let* ((nam (buffer-substring (match-beginning 2) (match-end 2)))
  609. (num (buffer-substring (match-beginning 1) (match-end 1))))
  610. (setq nmlst (cons nam nmlst)
  611. ;; * 1000 to put it into bytes
  612. cntlst (cons (* (string-to-number num) 1000) cntlst))))
  613. (if (not nmlst)
  614. (error "No files found!"))
  615. (chart-bar-quickie 'vertical (format "Largest files in %s" d)
  616. nmlst "File Name"
  617. cntlst "File Size"
  618. 10
  619. (lambda (a b) (> (cdr a) (cdr b))))
  620. ))
  621. (defun chart-emacs-storage ()
  622. "Chart the current storage requirements of Emacs."
  623. (interactive)
  624. (let* ((data (garbage-collect)))
  625. ;; Let's create the chart!
  626. (chart-bar-quickie 'vertical "Emacs Runtime Storage Usage"
  627. (mapcar (lambda (x) (symbol-name (car x))) data)
  628. "Storage Items"
  629. (mapcar (lambda (x) (* (nth 1 x) (nth 2 x)))
  630. data)
  631. "Bytes")))
  632. (defun chart-emacs-lists ()
  633. "Chart out the size of various important lists."
  634. (interactive)
  635. (let* ((names '("buffers" "frames" "processes" "faces"))
  636. (nums (list (length (buffer-list))
  637. (length (frame-list))
  638. (length (process-list))
  639. (length (face-list))
  640. )))
  641. (if (fboundp 'x-display-list)
  642. (setq names (append names '("x-displays"))
  643. nums (append nums (list (length (x-display-list))))))
  644. ;; Let's create the chart!
  645. (chart-bar-quickie 'vertical "Emacs List Size Chart"
  646. names "Various Lists"
  647. nums "Objects")))
  648. (defun chart-rmail-from ()
  649. "If we are in an rmail summary buffer, then chart out the froms."
  650. (interactive)
  651. (if (not (eq major-mode 'rmail-summary-mode))
  652. (error "You must invoke chart-rmail-from in an rmail summary buffer"))
  653. (let ((nmlst nil)
  654. (cntlst nil))
  655. (save-excursion
  656. (goto-char (point-min))
  657. (while (re-search-forward "\\-[A-Z][a-z][a-z] +\\(\\w+\\)@\\w+" nil t)
  658. (let* ((nam (buffer-substring (match-beginning 1) (match-end 1)))
  659. (m (member nam nmlst)))
  660. (message "Scanned username %s" nam)
  661. (if m
  662. (let ((cell (nthcdr (- (length nmlst) (length m)) cntlst)))
  663. (setcar cell (1+ (car cell))))
  664. (setq nmlst (cons nam nmlst)
  665. cntlst (cons 1 cntlst))))))
  666. (chart-bar-quickie 'vertical "Username Occurrence in RMAIL box"
  667. nmlst "User Names"
  668. cntlst "# of occurrences"
  669. 10
  670. (lambda (a b) (> (cdr a) (cdr b))))
  671. ))
  672. (provide 'chart)
  673. ;;; chart.el ends here