chart.el 24 KB

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