A command-line tool for rendering bar charts that can be displayed directly in the terminal or in text-based files like Markdown.
By default, chart
accepts line-based data with a numeric value and label text separated by a space or common tabular
symbols. It also tolerates additional whitespace, empty lines, common punctuation, and currency symbols:
$ cat examples/quarterly-stats.csv
18.67,2021-Q1
19.45,2021-Q2
20.89,2021-Q3
21.34,2021-Q4
21.78,2022-Q1
22.45,2022-Q2
23.89,2022-Q3
24.34,2022-Q4
23.54,2023-Q1
24.78,2023-Q2
25.89,2023-Q3
26.34,2023-Q4
28.15,2024-Q1
25.44,2023-Q2
$ chart -i examples/quarterly-stats.csv
2021-Q1 ββββββββββββββββββββββββββββββββββββββββββββ 18.67
2021-Q2 ββββββββββββββββββββββββββββββββββββββββββββββ 19.45
2021-Q3 βββββββββββββββββββββββββββββββββββββββββββββββββ 20.89
2021-Q4 ββββββββββββββββββββββββββββββββββββββββββββββββββ 21.34
2022-Q1 βββββββββββββββββββββββββββββββββββββββββββββββββββ 21.78
2022-Q2 βββββββββββββββββββββββββββββββββββββββββββββββββββββ 22.45
2022-Q3 ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ 23.89
2022-Q4 βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ 24.34
2023-Q1 βββββββββββββββββββββββββββββββββββββββββββββββββββββββ 23.54
2023-Q2 ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ 25.44
2023-Q3 βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ 25.89
2023-Q4 ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ 26.34
2024-Q1 ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ 28.15
You can also chart the occurrences of lines using the -c/--count
flag:
$ cat examples/count-occurrences.txt
Three
Two
Four
Five
Four
One
Three
Three
Five
Two
Five
Four
Five
Four
Five
$ chart -i examples/count-occurrences.txt --count
Three βββββββββββββββββββββββββββββββββββββββββββ 3
Two βββββββββββββββββββββββββββββ 2
Four ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ 4
Five ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ 5
One ββββββββββββββ 1
Pipes allow you to feed data to chart
, making it easy to chart the output of other terminal commands. For example,
if you have a file with newline-delimited JSON data from a source code security scanner, you can chart statistics on
identified CWEs. Process the data with jq
, count the lines with sort
and uniq
, and pipe it into chart
for
visualization:
$ jq -r '.cwe' examples/sast-findings.jsonld | sort | uniq -c | chart
CWE-200 ββββββββββββββββββββ 2
CWE-22 ββββββββββ 1
CWE-23 ββββββββββ 1
CWE-248 ββββββββββ 1
CWE-284 ββββββββββ 1
CWE-307 ββββββββββ 1
CWE-312 ββββββββββββββββββββββββββββββ 3
CWE-327 ββββββββββββββββββββββββββββββ 3
CWE-362 ββββββββββ 1
CWE-400 ββββββββββ 1
CWE-434 ββββββββββ 1
CWE-502 ββββββββββ 1
CWE-532 ββββββββββββββββββββ 2
CWE-601 ββββββββββ 1
CWE-611 ββββββββββ 1
CWE-676 ββββββββββ 1
CWE-78 ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ 6
CWE-79 ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ 7
CWE-798 ββββββββββ 1
CWE-89 ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ 7
CWE-918 ββββββββββ 1
CWE-94 ββββββββββββββββββββββββββββββββββββββββββββββββββ 5
By default, the chart displays bars in the order of insertion. This is usually fine, but sorting the chart differently can make the data easier to read and understand. Let's order the CWE chart by label:
$ jq -r '.cwe' examples/sast-findings.jsonld | sort | uniq -c | chart --sort label
CWE-200 ββββββββββββββββββββ 2
CWE-22 ββββββββββ 1
CWE-23 ββββββββββ 1
CWE-248 ββββββββββ 1
CWE-284 ββββββββββ 1
CWE-307 ββββββββββ 1
CWE-312 ββββββββββββββββββββββββββββββ 3
CWE-327 ββββββββββββββββββββββββββββββ 3
CWE-362 ββββββββββ 1
CWE-400 ββββββββββ 1
CWE-434 ββββββββββ 1
CWE-502 ββββββββββ 1
CWE-532 ββββββββββββββββββββ 2
CWE-601 ββββββββββ 1
CWE-611 ββββββββββ 1
CWE-676 ββββββββββ 1
CWE-78 ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ 6
CWE-79 ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ 7
CWE-798 ββββββββββ 1
CWE-89 ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ 7
CWE-918 ββββββββββ 1
CWE-94 ββββββββββββββββββββββββββββββββββββββββββββββββββ 5
The chart is now sorted alphabetically, but it's more natural for this data to be sorted by the numbers in the CWE
identifiers. By using labelnum
as the sorting option, numeric characters are extracted from the labels and sorted as
integers:
$ jq -r '.cwe' examples/sast-findings.jsonld | sort | uniq -c | chart --sort labelnum
CWE-22 ββββββββββ 1
CWE-23 ββββββββββ 1
CWE-78 ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ 6
CWE-79 ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ 7
CWE-89 ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ 7
CWE-94 ββββββββββββββββββββββββββββββββββββββββββββββββββ 5
CWE-200 ββββββββββββββββββββ 2
CWE-248 ββββββββββ 1
CWE-284 ββββββββββ 1
CWE-307 ββββββββββ 1
CWE-312 ββββββββββββββββββββββββββββββ 3
CWE-327 ββββββββββββββββββββββββββββββ 3
CWE-362 ββββββββββ 1
CWE-400 ββββββββββ 1
CWE-434 ββββββββββ 1
CWE-502 ββββββββββ 1
CWE-532 ββββββββββββββββββββ 2
CWE-601 ββββββββββ 1
CWE-611 ββββββββββ 1
CWE-676 ββββββββββ 1
CWE-798 ββββββββββ 1
CWE-918 ββββββββββ 1
You can also sort the chart by value and specify the order. Let's sort by value in descending order to easily identify the most and least common weaknesses:
$ jq -r '.cwe' examples/sast-findings.jsonld | sort | uniq -c | chart --sort value --desc
CWE-89 ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ 7
CWE-79 ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ 7
CWE-78 ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ 6
CWE-94 ββββββββββββββββββββββββββββββββββββββββββββββββββ 5
CWE-327 ββββββββββββββββββββββββββββββ 3
CWE-312 ββββββββββββββββββββββββββββββ 3
CWE-532 ββββββββββββββββββββ 2
CWE-200 ββββββββββββββββββββ 2
CWE-918 ββββββββββ 1
CWE-798 ββββββββββ 1
CWE-676 ββββββββββ 1
CWE-611 ββββββββββ 1
CWE-601 ββββββββββ 1
CWE-502 ββββββββββ 1
CWE-434 ββββββββββ 1
CWE-400 ββββββββββ 1
CWE-362 ββββββββββ 1
CWE-307 ββββββββββ 1
CWE-284 ββββββββββ 1
CWE-248 ββββββββββ 1
CWE-23 ββββββββββ 1
CWE-22 ββββββββββ 1
Sometimes, smaller values are overshadowed by larger values in the distribution:
$ cat examples/statistics.txt
1.2 Sales leads
3.8 Conversion rate
12.6 Monthly subs
34.9 Quarterly revenue
79.4 Client meetings
158.2 Product launches
367.1 Employees
952.5 Support calls
2764.8 Website traffic
7360.4 Marketing budget
$ chart -i examples/statistics.txt
Sales leads β 1.2
Conversion rate β 3.8
Monthly subs β 12.6
Quarterly revenue β 34.9
Client meetings β 79.4
Product launches β 158.2
Employees βββ 367.1
Support calls βββββββ 952.5
Website traffic βββββββββββββββββββββ 2764.8
Marketing budget βββββββββββββββββββββββββββββββββββββββββββββββββββββββ 7360.4
Using the --scale
flag scales the chart logarithmically, improving readability in these situations:
$ chart -i examples/statistics.txt --scale
Sales leads βββββ 1.2
Conversion rate ββββββββββ 3.8
Monthly subs ββββββββββββββββ 12.6
Quarterly revenue ββββββββββββββββββββββ 34.9
Client meetings βββββββββββββββββββββββββββ 79.4
Product launches βββββββββββββββββββββββββββββββ 158.2
Employees ββββββββββββββββββββββββββββββββββββ 367.1
Support calls ββββββββββββββββββββββββββββββββββββββββββ 952.5
Website traffic βββββββββββββββββββββββββββββββββββββββββββββββββ 2764.8
Marketing budget βββββββββββββββββββββββββββββββββββββββββββββββββββββββ 7360.4
In addition to text-based charts, chart
can generate Mermaid XY charts using the --mermaid
flag:
$ chart -i examples/count-occurrences.txt --count --sort value --mermaid --title 'Occurrences'
xychart-beta
title "Occurrences"
x-axis ["One", "Two", "Three", "Four", "Five"]
bar [1, 2, 3, 4, 5]
See the rendered chart here.
It can also generate a basic configuration for a Chart.js bar chart using the --chartjs
flag:
$ chart -i examples/count-occurrences.txt --count --sort value --chartjs --title 'Occurrences'
// Chart.js configuration (https://www.chartjs.org/docs/latest/configuration/).
// Generated by chart (https://github.com/michenriksen/chart).
const config = {
type: "bar",
data: {
datasets: [{
data: [1,2,3,4,5],
}],
labels: ["One","Two","Three","Four","Five"],
},
options: {
plugins: {
title: {
display: true,
text: "Occurrences"
}
}
}
}
See chart --help
for additional flags and options.
Download the latest pre-compiled binary for your operating system from the releases page.
If you have Go installed, you can also install the latest binary with:
go install github.com/michenriksen/chart/cmd/chart@latest