dchart

package module
v0.0.0-...-35e6875 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 29, 2024 License: CC-BY-4.0 Imports: 10 Imported by: 1

README

dchart - charts for deck

The dchart package generates deck markup for various chart types, reading from an io.ReadCloser and writing to an io.Writer. The chart types and attributes defined by manipulating settings.

API

NewChart(chartype string, top, bottom, left, right float64) => settings
settings.[thing] = ...
settings.GenerateChart(deck *generate.Deck, io.ReadCloser)

Chart Data		[]ChartData
Chart Settings	Settings

Read CSV or TSV 	Getdata(r io.ReadCloser, readcsv bool, cols string) ([]ChartData,float64,float64,string)
Read TSV 			TSVdata(r io.ReadCloser) ([]ChartData, float64, float64, string)
Read CSV 			CSVdata(r io.ReadCloser, csvcols string) ([]ChartData, float64, float64, string)
Defne a Chart 		NewChart(chartType string, top, bottom, left, right float64) Settings
Define Standalone 	NewFullChart(chartType string, top, bottom, left, right float64) Settings
Make Chart 			(s *Settings) GenerateChart(deck *generate.Deck, r io.ReadCloser)
Write the Chart 	(s *Settings) Write(w io.Writer, r io.ReadCloser)

Example Client

package main
import (
	"fmt"
	"os"
	"github.com/ajstarks/dchart"
	"github.com/ajstarks/deck/generate"
)
func main() {
	chart := dchart.NewFullChart("bar", 0, 0, 0, 0)
	chart.ShowTitle = true
	chart.XLabelInterval = 2
	deck := generate.NewSlides(os.Stdout, 0, 0)
	deck.StartDeck()
	for _, f := range os.Args[1:] {
		r, err := os.Open(f)
		if err != nil {
			fmt.Fprintf(os.Stderr, "%v\n", err)
			continue
		}
		chart.GenerateChart(deck, r)
	}
	deck.EndDeck()
}

creates this chart.

example output

Command line client

cmd/dchart is the command line tool for dchart. It generates deck markup for bar, line, scatter, dot, volume, donut, proportional and fan charts, reading data from the standard input or specified files. Unless specified otherwise, each input source generates a slide in the deck.

The input data format a tab-separated or CSV formatted list of label,data pairs where label is an arbitrary string, and data is intepreted as a floating point value.

A line beginning with "#" is parsed as a title, with the title text beginning after the "#". If a third column is present, it serves as an annotation. label strings with \n characters denote multi-line labels.

Here is an example input data file:

# GOOG Stock Volume (Millions of Shares)
2017-01-01	33.1916
2017-02-01	25.6825
2017-03-01	33.8351	Peak
2017-04-01	25.1619
2017-05-01	32.1801

Example CSV file:

#,GOOG Stock Volume (Millions of Shares)
2017-01-01,33.1916
2017-02-01,25.6825
2017-03-01,33.8351,Peak
2017-04-01,25.1619
2017-05-01,32.1801

Typically dchart generates input for deck clients like pdfdeck, or pdi (a shell script for pdfdeck which reads deck markup on the standard input and produces PDF on the standard output).

$ dchart foo.d bar.d baz.d > fbb.xml && pdfdeck fbb.xml && open fbb.pdf
$ dchart -min=0 -max=700 -datafmt %0.2f -line -bar=f -vol -dot [A-Z]*.d | pdi > allvol.pdf
$ ls -lS | awk 'BEGIN {print "# File Size"} NR > 1 {print $NF "\t" $5}' | dchart -hbar | pdi > fs.pdf

Defaults

With no options, dchart makes a bar graph, showing data values and every data label.

Placement

The plot is positioned and scaled on the deck canvas with the -top, -bottom, -left, and -right flags. These flag values represent percentages on the deck canvas.

Chart types and elements

The -bar, -hbar, -line, -dot, -scatter, -vol, -pgrid, -pmap,-donut, and -radial. flags specify the chart types.

The -grid, -title, -val, and -yaxis flags control the visibility of plot components.

Command line options

-dmim        data minimum (default false, min=0)
-min         set the minimum value
-max         set the maximum value
-csv         read CSV files (default false)
-csvcol      specify the columns to use for label,value

-bar         show bar chart (default true)
-wbar        show "word" bar chart (default false)
-hbar        horizontal chart layout (default false)
-scatter     show a scatter chart (default false)
-dot         show dot plot (default false)
-lego        show lego chart (default false)
-bowtie      show bowtie chart (default false)
-fan         show fan chart (default false)
-line        show line chart (default false)
-slope       show a slope chart (default false)
-frame       show a frame outlining the chart (default false)
-datacond    conditional coloring (low,high,color)
-rline       show regression line (default false)
-vol         show volume plot (default false)
-pgrid       show a proportional grid (default false)
-pmap        show proportional map (default false)
-donut       show a donut chart (default false)
-radial      show a radial chart (default false)
-spokes      show a radial chart with spokes (default false)

-grid        show gridlines on the y axis (default false)
-val         show values (default true)
-pct         show percentages with values (default false)
-valpos      value position (t=top, b=bottom, m=middle) (default "t")
-yaxis       show a y axis (default true)
-yrange      specify the y axis labels (min,max,step)
-fulldeck    generate full deck markup (default true)
-title       show title (default true)
-chartitle   specify the title (overiding title in the data)
-hline       horizontal line with optional label (value,label)
-noteloc     note location (c-center, r-right, l-left, default c)

-top         top of the plot (default 80)
-bottom      bottom of the plot (default 30)
-left        left margin (default 20)
-right       right margin (default 80)

-psize       diameter of the donut (default 30)
-pwidth      width of the donut or proportional map (default 3 time textsize)
-solidpmap   use solid colors for pmaps
-barwidth    barwidth (default computed from the number of data points)
-linewidth   linewidth for line charts (default 0.2)
-ls          linespacing (default 2.4)
-textsize    text size (default 1.5)
-xlabel      x axis label interval (default 1, 0 to supress all labels)
-xlabrot     x axis label rotation (default 0, no rotation)
-xstagger    stagger x axis labels
-xlast       show the last x label
-color       data color (default "lightsteelblue")
-framecolor  frame color (default "rgb(127,0,0)")
-rlcolor     regression line color (default "rgb(127,0,0)")
-vcolor      value color (default "rgb(127,0,0)")
-lcolor      axis label color (default "rgb(75,75,75)")
-volop       volume opacity (default 50)
-datafmt     data format for values (default "%.1f")
-note        show annotations (default true)

Examples

Using this data in AAPL.d

# AAPL Volume
2017-01-01	563.122
2017-02-01	574.969
2017-03-01	561.628
2017-04-01	373.304
2017-05-01	653.755
2017-06-01	684.178
2017-07-01	421.992
2017-08-01	661.069
2017-09-01	679.879
2017-10-01	504.291
2017-11-01	600.663
2017-12-01	417.354

here are some variations.

$ dchart AAPL.d

no-args

$ dchart -yrange=0,700,50 AAPL.d

yrange

$ dchart -xlabel=2 -left 30 -right 70 -top 70 -bottom 40 -yaxis=f AAPL.d

pos

$ dchart -color gray AAPL.d

bar-gray

$ dchart -grid AAPL.d # add a y axis grid

bar-grid

$ dchart -grid -barwidth=1 AAPL.d # adjust the bar width

barwidth

$ dchart -bar=f -dot AAPL.d # no bars, dot plot

dot

$ dchart -bar=f -vol AAPL.d # no bars, volume plot

vol

$ dchart -datafmt %0.2f -bar=f -dot -line AAPL.d

dot-line

$ dchart -bar=f -line AAPL.d # line chart

connect

$ dchart -bar=f -line -yaxis=f -val=f AAPL.d # only show line and x axis

connect-no-axis-no-val

$ dchart -scatter -val=f -bar=f -yaxis=f AAPL.d

scatter

$ dchart -bar=f -line -vol -dot AAPL.d # combine line, volume, and dot

vol-dot

$ dchart -bar=f -line -vol -dot -yaxis=f AAPL.d # as above, removing the y-axis

vol-dot-no-axis

$ dchart -bar=f -line -vol -dot -grid AAPL.d

connect-dot-vol-val-grid

$ dchart -hbar AAPL.d

hlayout

$ sort -k2 -nr pdf.d | dchart -left 20 -hbar

sorted-bar

Using this data in browser.d

# Browser Market Share Dec 2016-Dec 2017
Chrome	53.72
Safari	14.47
Other	9.36
UC	8.28
Firefox	6.23
IE	3.99
Opera	3.9

here are views of proportional data:

$ dchart -wbar browser.d

wbar

$ dchart -donut -color=std -pwidth=5 browser.d

donut

$ dchart -pmap -pwidth=5 -textsize=1 browser.d

pmap

Using this data in incar.d:

# US Incarceration Rate
White	39	antiquewhite
Hispanic	19	burlywood
Black	40	sienna
Other	2	gray

the note field may be used to specify the color

pgrid

$ dchart -ls 3 -val=f -pgrid incar.d

Here is the lego chart variation:

lego

$ dchart -left 30 -top 80 -lego -textsize 4 

Using this data in slope.d

# Test Slope Graphs
one     20      First
two     80

three   0       Second
four    0

five    100     Third
six     0

seven   0       Fourth
eight   100

nine    50      Fifth
ten     50

eleven  100     Sixth
twelve  100

slope

$ dchart -slope -left=10 -right=30 -top=80 -bottom=60 slope.d

Using this data in count.d:

# Count Of Things
One	10	red
Two	20	green
Three	30	blue
Four	40	purple
Five	50	yellow
Six	60	black
Seven	70	brown
Eight	80	silver
Nine	90	orange
Ten	100	pink

$ dchart -psize=10 -pwidth=40 -left=50 -top=50 -radial -textsize=3 data/incr.d|pdf -pagesize 800,800

radial

Using this data:

# Clockwise
twelve	12	red
one	1	green
two	2	blue
three	3	purple
four	4	maroon
five	5	black
six	6	brown
seven	7	silver
eight	8	orange
nine	9	pink
ten	10
eleven	11

$ dchart -psize=10 -pwidth=40 -left=50 -top=50 -radial -textsize=3 -spokes data/clock.d|pdf -pagesize 800,800

spoke

Using this data:

# Occupations of African-Americans and Whites (2019)
Management	31.8	crimson
Service	23.8	gold
Sales	22.4	steelblue
Construction	5.7	tan
Production	16.3	rgb(101,67,33)
Management	41.4	crimson
Service	15.9	gold
Sales	21.3	steelblue
Construction	10.1	tan
Production	11.3	rgb(101,67,33)

$ dchart -val -psize=20 -top=60 -bowtie occupation.d

bowtie

$ dchart 0val -psize=20 -top=60 -fan occupation.d

fan

Documentation

Overview

Package dchart - make charts in the deck format

Package dchart generates deck markup for bar, line, dot, and volume charts, reading data from the standard input or specified files. Unless specified otherwise, each input source generates a slide in the deck.

The input data format a tab-separated list of label,data pairs where label is an arbitrary string, and data is intepreted as a floating point value. A line beginning with "#" is parsed as a title, with the title text beginning after the "#". A third column specifies an annotation.

Here is an example input data file:

# GOOG Stock Volume (Millions of Shares)
2017-01-01	33.1916
2017-02-01	25.6825
2017-03-01	33.8351	Peak value
2017-04-01	25.1619
2017-05-01	32.1801

The command line options are:

-dmim        data minimum (default false, min=0)
-min         set the minimum value
-max         set the maximum value
-csv         read CSV files (default false)
-csvcol      specify the columns to use for label,value

-bar         show bars (default true)
-hbar        horizontal chart layout (default false)
-scatter     show scatter chart (default false)
-wbar        show "word" bar chart (default false)
-line        show line chart (default false)
-dot         show dot plot (default false)
-lego        show lego chart (default false)
-bowtie      show bowtie chart (default false)
-fan         show fanchart (default false)
-grid        show gridlines on the y axis (default false)
-val         show values (default true)
-rline       show a regression line (default false)
-frame       show a frame outlining the chart (default false)
-datacond    conditional colors (low,high,color)
-pct         show percentages with values (default false)
-valpos      value position (t=top, b=bottom, m=middle) (default "t")
-vol         show volume plot (default false)
-pgrid       show a proportional grid (default false)
-pmap        show proportional map (default false)
-donut       show a donut chart (default false)
-radial      show a radial chart (default false)
-spokes      show spokes on the radial chart (default false)
-yaxis       show a y axis (default true)
-yrange      define the y axis range (min,max,step)
-fulldeck    generate full markup (default true)
-title       show title (default true)
-chartitle   specify the title (overiding title in the data)
-hline       horizontal line with optional label (value,label)
-noteloc     note location (c-center, r-right, l-left, default c)

-top         top of the plot (default 80)
-bottom      bottom of the plot (default 30)
-left        left margin (default 20)
-right       right margin (default 80)
-x           x location of the donut chart (default 50)
-y           y location of the donut chart (default 50)
-psize       diameter of the donut (default 30)

-pwidth      width of the donut or proportional map (default 3)
-solidpmap   use solid colors for pmaps
-barwidth    barwidth (default computed from the number of data points)
-linewidth   linewidth for line charts (default 0.2)
-ls          linespacing (default 2.4)
-textsize    text size (default 1.5)
-xlabel      x axis label interval (default 1, 0 to supress all labels)
-xlabrot     x axis label rotation (default 0, no rotation)
-xstagger    stagger x axis labels
-xlast       show the last x label
-color       data color (default "lightsteelblue")
-vcolor      value color (default "rgb(127,0,0)")
-lcolor      axis label color (default "rgb(75,75,75)")
-rlcolor     regression line color (default "rgb(127,0,0)")
-framecolor  frame color (default "rgb(127,0,0)")
-volop       volume opacity (default 50)
-datafmt     data format for values (default "%.1f")
-note        show annotation (default true)

Index

Constants

View Source
const (
	// Titlecolor is the color of the title string
	Titlecolor = "black"
	// Dotlinecolor is the dotted line color
	Dotlinecolor = "lightgray"
	// Defaultfmt is the default number format
	Defaultfmt = "%.1f"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Attributes

type Attributes struct {
	BackgroundColor,
	DataColor,
	FrameColor,
	LabelColor,
	RegressionLineColor,
	ValueColor,
	ChartTitle,
	CSVCols,
	DataCondition,
	DataFmt,
	HLine,
	NoteLocation,
	ValuePosition,
	YAxisR string
}

Attributes define chart attributes

type ChartData

type ChartData struct {
	// contains filtered or unexported fields
}

ChartData defines the name,value pairs

func CSVdata

func CSVdata(r io.ReadCloser, csvcols string) ([]ChartData, float64, float64, string)

CSVdata reads CSV structured name,value pairs, with optional comments, returning a slice with the data, allong with min, max and title

func Getdata

func Getdata(r io.ReadCloser, readcsv bool, cols string) ([]ChartData, float64, float64, string)

Getdata reads input from a Reader, either tab-separated or CSV

func TSVdata

func TSVdata(r io.ReadCloser) ([]ChartData, float64, float64, string)

TSVdata reads tab-delimited name,value pairs, with optional comments, returning a slice with the data, allong with min, max and title

type Flags

type Flags struct {
	DataMinimum,
	FullDeck,
	ReadCSV,
	ShowAxis,
	ShowBar,
	ShowBowtie,
	ShowDonut,
	ShowDot,
	ShowFan,
	ShowFrame,
	ShowGrid,
	ShowHBar,
	ShowLine,
	ShowLego,
	ShowNote,
	ShowPercentage,
	ShowPGrid,
	ShowPMap,
	ShowRadial,
	ShowRegressionLine,
	ShowScatter,
	ShowSlope,
	ShowSpokes,
	ShowTitle,
	ShowValues,
	ShowVolume,
	ShowWBar,
	ShowXLast,
	ShowXstagger,
	SolidPMap bool
}

Flags define chart on/off switches

type Measures

type Measures struct {
	CanvasWidth,
	CanvasHeight,
	TextSize,
	Left,
	Right,
	Top,
	Bottom,
	LineSpacing,
	BarWidth,
	LineWidth,
	PSize,
	PWidth,
	UserMin,
	UserMax,
	VolumeOpacity,
	XLabelRotation float64
	XLabelInterval,
	PMapLength int
}

Measures define chart measures

type Settings

type Settings struct {
	Flags
	Attributes
	Measures
}

Settings is a collection of all chart settings

func NewChart

func NewChart(chartType string, top, bottom, left, right float64) Settings

NewChart initializes the settings required to make a chart chartType may be one of: "line", "slope", "bar", "wbar", "hbar", "volume, "scatter", "donut", "pmap", "pgrid", "lego", "radial", "bowtie", "fan"

func NewFullChart

func NewFullChart(chartType string, top, bottom, left, right float64) Settings

NewFullChart initializes the settings required to make a chart that includes the enclosing deck markup

func (*Settings) GenerateChart

func (s *Settings) GenerateChart(deck *generate.Deck, r io.ReadCloser)

GenerateChart makes charts according to the orientation: horizontal bar or line, bar, dot, or donut volume charts

func (*Settings) Hchart

func (s *Settings) Hchart(deck *generate.Deck, r io.ReadCloser)

Hchart makes horizontal bar charts using input from a Reader

func (*Settings) Pchart

func (s *Settings) Pchart(deck *generate.Deck, r io.ReadCloser)

Pchart draws proportional data, either a pmap, pgrid, radial or donut using input from a Reader

func (*Settings) Slopechart

func (s *Settings) Slopechart(deck *generate.Deck, r io.ReadCloser)

Slopechart draws a slope chart

func (*Settings) Vchart

func (s *Settings) Vchart(deck *generate.Deck, r io.ReadCloser)

Vchart makes charts using input from a Reader the types of charts are bar (column), dot, line, and volume

func (*Settings) Wbchart

func (s *Settings) Wbchart(deck *generate.Deck, r io.ReadCloser)

Wbchart makes a word bar chart

func (*Settings) Write

func (s *Settings) Write(w io.Writer, r io.ReadCloser)

Write performs chart I/O

Directories

Path Synopsis
cmd
dchart
dchart - make charts in the deck format
dchart - make charts in the deck format

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL