;TIP1318.LSP: GRAPH.LSP Graph Data (c)1997, Yuqun Lian ;;; This routine will plot the data in graph.csv file and spline the ;;; curves. The data can have any number of rows and columns. The first ;;; row is the title, the second row is the x and y labels, and the ;;; third row is x values. Modify the following settings to your needs. ;;; Yuqun Lian - SimpleCAD http://www.simplecad.com 2/7/97 ;;;------------------------------------------------------------------- (setq ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; DATA_FILE "graph.csv" ; data file name ;; CHANGE FILE NAME TO SUIT BD_WIDTH 11.0 ; border width BD_HEIGHT 8.5 ; border height BD_THICK 0.02 ; border line thickness BD_OFFSET 1.2 ; border offset X_GRID 11 ; x grids number Y_GRID 10 ; y grids number Y_START 0.0 ; y start value Y_STOP 100.0 ; y stop value CURVE_THICK 0.02 ; curve thickness TICK_MARK 0.1 ; tick mark height TEXT_HEIGHT 0.2 ; label text height SPLINE_FLAG t ; change T to nil for non spline GRID_FLAG t ; change T to nil for no grids ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (defun C:GRAPH () (if (setq FILE (findfile DATA_FILE)) (progn (SET-VAR) (READ-DATA FILE) (DRAW-BORDER) (DRAW-CURVE) ) (alert (strcat "Data file " DATA_FILE " is not found")) ) (command "zoom" "extents" "zoom" ".95x") (princ) ) (defun SET-VAR () (setvar "blipmode" 0) (setvar "cmdecho" 0) (setvar "blipmode" 0) (MAKELA "grid" "8" "continuous") (MAKELA "border" "7" "continuous" ) (command "style" "h2" "complex.shx" (* TEXT_HEIGHT 1.4) "0.8" "" "" "" "" ) (command "style" "t1" "complex.shx" TEXT_HEIGHT "0.8" "" "" "" "" ) (command "style" "t2" "complex.shx" (* 0.8 TEXT_HEIGHT) "0.8" "" "" "" "" ) ) (defun READ-DATA (DATA_FILE) (setq LINE_LIST nil LEGEND_LIST nil) (setq FP (open DATA_FILE "r") ) ;; read headers (setq HEADER_LST (PARSE (read-line FP))) (setq TITLE (nth 0 HEADER_LST) SQFT_TON (nth 1 HEADER_LST) ) (setq HEADER_LST (PARSE (read-line FP))) (setq CATEGORY_LABEL (nth 0 HEADER_LST) VALUE_LABEL (nth 1 HEADER_LST) ) ;; read x values (setq TXT_LST (PARSE (read-line FP))) (setq TXT_COUNT (length TXT_LST) ) (setq XLST (DATA-LIST TXT_LST)) (setq X_START (nth 0 XLST) X_STOP (nth (1- (length XLST)) XLST) ) ;; read data (while (setq TXT (read-line FP)) (setq TXT_LST (PARSE TXT)) (if (= (length TXT_LST) TXT_COUNT) (progn (setq LST (DATA-LIST TXT_LST)) (setq LINE_A (CONSTRUCT XLST LST) ) (setq LINE_LIST (append LINE_LIST (list LINE_A))) (setq LEGEND_LIST (append LEGEND_LIST (list (nth 0 TXT_LST) ) ) ) ) ;progn ) ) (close FP) ) (defun DRAW-CURVE (/ LINE) (setq COUNT 1) (foreach LINE LINE_LIST (setq LANAME (strcat "la" (itoa COUNT))) (MAKELA LANAME (itoa COUNT) "continuous" ) (setvar "clayer" LANAME) (DRAW_LINE LINE) (PRINT_LEGEND) (setq COUNT (1+ COUNT)) ) ) (defun DATA-LIST (LST / DATA_LST) ;cut off first one and convert to float (setq I 0 N (length LST) DATA_LST nil) (foreach X LST (if (/= I 0) (setq DATA_LST (append DATA_LST (list (atof X))) ) ) (setq I (1+ I)) ) (setq DATA_LST DATA_LST) ) (defun PRINT_LEGEND (/ X1 X2 X3 Y PT1 PT2 PT) (setq X1 (+ X0 (* 5.0 TICK_MARK))) (setq X2 (+ X0 (* 13.0 TICK_MARK)) ) (setq X3 (+ X0 (* 17.0 TICK_MARK))) (setq Y (- YLAST (+ TEXT_HEIGHT TICK_MARK)) ) (setq PT1 (list X1 Y) PT2 (list X2 Y)) (command ".line" PT1 PT2 "" ) (setq PT (list X3 Y)) (setq LEGEND (nth (1- COUNT) LEGEND_LIST) ) (setvar "clayer" "border") (command ".text" "s" "t2" "j" "ml" PT "0" LEGEND ) (setq YLAST Y) ) (defun DRAW_LINE (LINE_LST / PTA ENT_OLD ENT_NEW PTB) (setq PTA (CONVERT (nth 0 LINE_LST))) (setq ENT_OLD nil) (foreach PTB LINE_LST (setq PTB (CONVERT PTB)) (command ".pline" PTA "w" CURVE_THICK "" PTB "" ) (setq ENT_NEW (ssget "l")) (if ENT_OLD (progn (command ".pedit" ENT_OLD "j" ENT_NEW "" "") (setq ENT_NEW (ssget "l") ) ) ) (setq ENT_OLD ENT_NEW) (setq PTA PTB) ) (if SPLINE_FLAG (command ".pedit" "l" "s" "")) ) (defun CONVERT (PT) ; convert point to drawing scale (setq X (car PT) Y (cadr PT)) (setq X (FX X)) ;conversion (setq Y (FY Y)) ;conversion (list X Y) ) (defun CONSTRUCT (X_LST Y_LST / NEWLST COUNT X Y) (setq LEN (length X_LST) NEWLST nil COUNT 0) (while (< COUNT LEN) (setq X (nth COUNT X_LST)) (setq Y (nth COUNT Y_LST)) ;(setq x (fx x)) ;conversion ;(setq y (fy y)) ;conversion (setq NEWLST (append NEWLST (list (list X Y)))) (setq COUNT (1+ COUNT) ) ) (setq NEWLST NEWLST) ) (defun PARSE (TXT / N COUNT WORD LST) (setq N (strlen TXT) COUNT 1 WORD "" LST nil) (while (<= COUNT N) (setq CHAR (substr TXT COUNT 1)) (if (= CHAR ",") (progn (setq LST (append LST (list WORD))) (setq WORD "") ) (progn (setq WORD (strcat WORD CHAR)) (if (= COUNT N) (setq LST (append LST (list WORD))) ) ) ) (setq COUNT (1+ COUNT)) ) (setq LST LST) ) (defun MAKELA (LAN LAC LALT) (command "layer" "n" LAN "c" LAC LAN "lt" LALT LAN "") ) (defun DRAW-BORDER () (setvar "clayer" "border") (setq X00 0.0 Y00 0.0) (setq X_LABEL (MAKE-LABEL X_START X_STOP X_GRID) ) (setq Y_LABEL (MAKE-LABEL Y_START Y_STOP Y_GRID)) ; consider graph position (setq BD_XX (+ BD_WIDTH X00) BD_YY (+ BD_HEIGHT Y00)) (setq X0 (+ BD_OFFSET X00) Y0 (+ BD_OFFSET Y00) XX (- BD_XX BD_OFFSET) YY (- BD_YY BD_OFFSET) ) ; calculate convertion factors (setq *AX (/ (- XX X0) (- X_STOP X_START))) (setq *BX (/ (- (* X0 X_STOP) (* XX X_START)) (- X_STOP X_START ) ) ) (setq *AY (/ (- YY Y0) (- Y_STOP Y_START))) (setq *BY (/ (- (* Y0 Y_STOP) (* YY Y_START)) (- Y_STOP Y_START ) ) ) (BORDER X00 Y00 BD_XX BD_YY "7" "0") (BORDER X0 Y0 XX YY "7" BD_THICK ) (setvar "clayer" "grid") (if GRID_FLAG (PLOT-GRID)) (setvar "clayer" "border") (WRITE-TITLE) (LABEL X_LABEL Y_LABEL TICK_MARK) (setq YLAST (- YY TEXT_HEIGHT) ) ) (defun WRITE-TITLE () (setq X (* 0.5 (+ X0 XX))) (setq Y (* (+ YY BD_YY) 0.5)) (setq PT (list X Y)) (command ".text" "s" "h2" "j" "mc" PT "0" TITLE ) (setq Y (- Y0 (+ TEXT_HEIGHT (* 3.0 TICK_MARK)))) (setq PT (list X Y) ) (command ".text" "s" "t1" "j" "tc" PT "0" CATEGORY_LABEL) (setq X (- X0 (+ TEXT_HEIGHT (* 5.0 TICK_MARK))) ) (setq Y (* 0.5 (+ Y0 YY))) (setq PT (list X Y)) (command ".text" "s" "t1" "j" "bc" PT "90" VALUE_LABEL ) (setq X (+ X0 (* 4.0 TICK_MARK))) (setq Y (+ Y0 (* 2.0 TICK_MARK)) ) (setq PT (list X Y)) (command ".text" "s" "t2" "j" "bl" PT "0" SQFT_TON ) ) ;; make a list base on start stop and number of points (defun MAKE-LABEL (START STOP N / LST I ITEM) (setq I 0 LST nil) (while (<= I N) (setq ITEM (+ START (/ (* (- STOP START) I) N)) ) (setq LST (append LST (list ITEM))) (setq I (1+ I)) ) (setq LST LST) ) (defun PLOT-GRID (/ I N X Y PT1 PT2) (setq I 1 N (length X_LABEL)) (foreach X X_LABEL (if (and (/= I 1) (/= I N)) (progn (setq X (FX X)) (setq PT1 (list X Y0) PT2 (list X YY) ) (command ".line" PT1 PT2 "") ) ) (setq I (1+ I)) ) (setq I 1 N (length Y_LABEL)) (foreach Y Y_LABEL (if (and (/= I 1) (/= I N)) (progn (setq Y (FY Y)) (setq PT1 (list X0 Y) PT2 (list XX Y) ) (command ".line" PT1 PT2 "") ) ) (setq I (1+ I)) ) ) (defun LABEL (X_LABEL Y_LABEL DT / Y Y1 Y2 Y3 X X1 X2 X3 PT1 PT2 TXTPT) (setq Y (FY (nth 0 Y_LABEL))) (setq Y1 (- Y DT) Y2 (+ Y DT) Y3 (- Y1 DT) ) (foreach X X_LABEL (setq TXT (rtos X 2 0)) (setq X (FX X)) (setq PT1 (list X Y1) PT2 (list X Y2)) (setq TXTPT (list X Y3) ) (command ".line" PT1 PT2 "") (command ".text" "s" "t2" "j" "tc" TXTPT "0" TXT ) ) (setq X (FX (nth 0 X_LABEL))) (setq X1 (- X DT) X2 (+ X DT) X3 (- X1 DT) ) (foreach Y Y_LABEL (setq TXT (rtos Y 2 1)) (setq Y (FY Y)) (setq PT1 (list X1 Y) PT2 (list X2 Y)) (setq TXTPT (list X3 Y) ) (command ".line" PT1 PT2 "") (command ".text" "s" "t2" "j" "mr" TXTPT "0" TXT ) ) ) (defun FX (X) (setq X (+ (* *AX X) *BX))) (defun FY (Y) (setq Y (+ (* *AY Y) *BY)) ) (defun BORDER (X1 Y1 X2 Y2 COLOR WIDTH / PT1 PT2 PT3 PT4 LAST_BD ) (setq PT1 (list X1 Y1) PT2 (list X2 Y1) PT3 (list X2 Y2) PT4 (list X1 Y2) ) (command "pline" PT1 "w" WIDTH WIDTH PT2 PT3 PT4 "c") (setq LAST_BD (ssget "l") ) (command "chprop" LAST_BD "" "c" COLOR "") ) (princ "Type GRAPH to run.") (princ) ;end graph.lsp