--- title: "Bar Chart" id: 64586 type: "computer_media" slug: "bar-chart" url: "http://localhost/computer_media/bar-chart/" markdown_url: "http://localhost/computer_media/bar-chart.md" published_at: "2026-03-03T01:01:34+00:00" modified_at: "2026-03-30T21:41:03+00:00" author: "David Anderson" featured_image: url: "http://localhost/wp-content/uploads/2023/06/10-BarChart.png" excerpt: "A dot-density bar chart routine reads paired height and density values from DATA lines to render variable-fill bars using only PLOT commands." category: - name: "Archived Media" slug: "archived-media" taxonomy: "category" url: "http://localhost/category/archived-media/" post_tag: - name: "Downloadable" slug: "downloadable" taxonomy: "post_tag" url: "http://localhost/tag/downloadable/" - name: "TS 2068" slug: "ts2068" taxonomy: "post_tag" url: "http://localhost/tag/ts2068/" model: - name: "Timex/Sinclair 2068" slug: "ts-2068" taxonomy: "model" url: "http://localhost/model/ts-2068/" indiv: - name: "Nick Hampshire" slug: "nick-hampshire" taxonomy: "indiv" url: "http://localhost/indiv/nick-hampshire/" genre: - name: "Business" slug: "business" taxonomy: "genre" url: "http://localhost/type/business/" - name: "Graphics" slug: "graphics" taxonomy: "genre" url: "http://localhost/type/graphics/" media_contents: - id: 64513 title: "CATS Library Tape 2" type: "computer_media" url: "http://localhost/computer_media/cats-library-tape-2/" media_type: "Program" programmers: - name: "Nick Hampshire" slug: "nick-hampshire" taxonomy: "indiv" url: "http://localhost/indiv/nick-hampshire/" download_url: "https://archive.org/download/timex-sinclair-software-archive/BarChart%20%281983%29%28Hampshire%2C%20Nick%29%28TS2068%29%28US%29%28Program%29.zip" mediadate: "1983" images: - url: "http://localhost/wp-content/uploads/2023/06/10-BarChart.png" media_type_tags: "Business, Graphics" --- # Bar Chart This program draws a dot-density bar chart on screen using PLOT and FOR/NEXT loops, reading bar data from DATA statements in pairs: bar height and dot spacing. Each bar is rendered as a grid of plotted pixels, with the dot density controlling the spacing between each plotted point in both the horizontal and vertical axes. A border is drawn first via a subroutine at line 400 using DRAW commands. The technique originates from [Nick Hampshire’s](http://localhost/indiv/nick-hampshire/) [Color Graphics](http://localhost/book/timex-sinclair-color-graphics/) book. *** ## Program Structure The program is divided into four logical sections: 1. Lines 1–4: Introduction text and keypress wait before starting. 2. Lines 60–90: Colour setup (INK, PAPER, BORDER) and subroutine call to draw the border. 3. Lines 100–300: Main bar-chart drawing loop reading DATA pairs and plotting bars. 4. Lines 400–460: Border-drawing subroutine using `PLOT`/`DRAW`. ## Data Format Bar data is stored in lines 130–155 as pairs `h,d` where `h` is the bar height and `d` is the dot density (step size). A sentinel pair `0,0` at line 170 terminates the read loop. The six groups of three bars each (18 bars total) are laid out across the screen. | Line | Bar 1 (h,d) | Bar 2 (h,d) | Bar 3 (h,d) | | --- | --- | --- | --- | | 130 | 20, 1 | 45, 2 | 80, 3 | | 135 | 22, 1 | 50, 2 | 70, 3 | | 140 | 25, 1 | 35, 2 | 90, 3 | | 145 | 35, 1 | 50, 2 | 100, 3 | | 150 | 30, 1 | 45, 2 | 75, 3 | | 155 | 40, 1 | 55, 2 | 140, 3 | ## Bar Drawing Technique Each bar occupies a horizontal span of 6 pixels (`b` to `b+5`), and the variable `b` is incremented by 6 at line 290 after each bar is drawn. The dot density value `d` is used as the `STEP` in both the outer `x` loop (line 240) and the inner `y` loop (line 250), so a density of 1 produces a solid-looking bar, 2 a medium-density dotted fill, and 3 a sparse dotted fill. This elegantly encodes fill style directly into the plotting loops without requiring any additional conditional logic. ## Border Subroutine The subroutine at lines 400–460 draws a rectangular border around the full 256×176 pixel plotting area using a sequence of `DRAW` commands. Note that the screen plotting area is 255×175 pixels (0–255 wide, 0–175 tall), so the `DRAW 255,0` and `DRAW 0,175` calls reach the far edges correctly from the origin at `PLOT 0,0`. ## Key BASIC Idioms - Line 3 uses `IF INKEY$="" THEN GO TO 3` as a simple keypress wait — a standard Sinclair idiom. - The sentinel `DATA 0,0` at line 170 with the check `IF d=0 THEN STOP` at line 230 is a clean loop termination pattern for DATA-driven programs. - `GO TO 220` at line 300 re-enters the READ loop without a FOR/NEXT counter, relying on the DATA pointer advancing automatically. ## Source Code ``` 1 PRINT "This barchart routine is one that can be adapted to your own programs. The data for the barsis stored in lines 130-155 in pairs, the first being the bar height and the second the dot density. From Nick Hampshire's ""Color Graphics""." 2 PRINT : PRINT "Press any key for a demo." 3 IF INKEY$="" THEN GO TO 3 4 CLS 60 INK 0 70 PAPER 7: BORDER 3 90 GO SUB 400 100 REM data 130 DATA 20,1,45,2,80,3 135 DATA 22,1,50,2,70,3 140 DATA 25,1,35,2,90,3 145 DATA 35,1,50,2,100,3 150 DATA 30,1,45,2,75,3 155 DATA 40,1,55,2,140,3 170 DATA 0,0 200 REM draw barchart 210 LET b=10 220 READ h,d 230 IF d=0 THEN STOP 240 FOR x=b TO b+5 STEP d 250 FOR y=20 TO h+10 STEP d 260 PLOT x,y 270 NEXT y 280 NEXT x 290 LET b=b+6: REM set start next bar 300 GO TO 220 400 REM border 410 PLOT 0,0 420 DRAW 255,0 430 DRAW 0,175 440 DRAW -255,0 450 DRAW 0,-175 460 RETURN ```