https://github.com/gribnoysup/wunderbar
Simple horizontal bar chart printer for your terminal
https://github.com/gribnoysup/wunderbar
bar bar-chart chart cli library nodejs terminal
Last synced: 8 months ago
JSON representation
Simple horizontal bar chart printer for your terminal
- Host: GitHub
- URL: https://github.com/gribnoysup/wunderbar
- Owner: gribnoysup
- License: mit
- Created: 2018-03-05T17:56:25.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2023-01-06T01:32:50.000Z (over 3 years ago)
- Last Synced: 2025-08-19T06:53:46.662Z (10 months ago)
- Topics: bar, bar-chart, chart, cli, library, nodejs, terminal
- Language: JavaScript
- Homepage: https://npm.im/@gribnoysup/wunderbar
- Size: 1.46 MB
- Stars: 572
- Watchers: 11
- Forks: 12
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @gribnoysup/wunderbar
Have you ever wanted to print some pretty bar charts in your terminal? You are
in the right place!

Wunderbar can print horisontal bar chart with legend and chart scale straight to
your terminal. Or you can use it as a module in your code and get all the
building blocks to print chart yourself.
Wunderbar uses partial block characters (`▏▎▍▌▋▊▉█`) to print the most precise
charts possible:

## Use in terminal
```
echo "[1, 2, 3, 4, 5]" | npx @gribnoysup/wunderbar --min 0
```

```
cat data.json | npx @gribnoysup/wunderbar --min 0
```

## Use in your code
```
npm i --save @gribnoysup/wunderbar
```
```js
const wunderbar = require('@gribnoysup/wunderbar');
const data = require('./data.json');
const printData = () => {
const { chart, legend, scale, __raw } = wunderbar(data, {
min: 0,
length: 42,
});
console.log();
console.log(chart);
console.log();
console.log(scale);
console.log();
};
printData();
```
## Limitations
* Wunderbar supports only node >= 6
* Wunderbar uses [`chalk.hex`][1] to add color to your charts. Chalk is pretty
smart to downgrade the color if it is not supported by your terminal, but
output may vary in different terminals.
## API
### wunderbar(values, [options]) ⇒ [`OutputValue`][4]
| Param | Type | Default | Description |
| ---------------------------- | -------------------------------------------------------------------------------------------------------- | --------------------- | --------------------------------------------------------------------- |
| values | `Array` | | Values to draw on a chart |
| [options] | `Object` | | Chart drawing options |
| [options.min] | `number` | min value from values | Min chart value (inclusive) |
| [options.max] | `number` | max value from values | Max chart value (inclusive) |
| [options.length] | `number` | terminal width | Chart length |
| [options.sort] | "min" | "max" | "none" | (a: NormalizedValue, b: NormalizedValue) => number | `"none"` | Sort method for chart values |
| [options.randomColorOptions] | `Object` | `{}` | [randomColor options][2] for color generation |
| [options.format] | string | (a: number) => string | `"0.00"` | Value format method. String values are [Numeral.js format][7] strings |
All options are also supported in the cli version:
```
echo "[1000, 3000, 5000, 7000, 9000, 11000]" | \
npx @gribnoysup/wunderbar --min 0 --max 15000 --length 42 --sort min --randomColorOptions '{ "seed": "unicorn" }' --format "0a"
```

### Types
**InputValue** :
`{ value: number, color?: string, label?: string } | number | string`
**OutputValue** :
`{ legend: string, scale: string, chart: string, __raw: RawData }`
**RawData** :
`{ chartLength: number, minValue: number, minValueFormatted: string, maxValue: number, maxValueFormatted: string, normalizedValues: NormalizedValue[] }`
**NormalizedValue** :
`{ normalizedValue: number, rawValue: number, formattedValue: string, color: string, label: string, lineLength: number, chartBar: string, coloredChartBar: string }`
[1]: https://github.com/chalk/chalk#256-and-truecolor-color-support
[2]: https://github.com/davidmerfield/randomColor#options
[3]: #InputValue
[4]: #OutputValue
[5]: #NormalizedValue
[6]: #RawData
[7]: http://numeraljs.com/#format
## License
[MIT](./LICENSE)