Skip to content

Commit 9b0a346

Browse files
author
Yaron Naveh
committed
stacked bar chart
1 parent 9210c31 commit 9b0a346

File tree

8 files changed

+309
-35
lines changed

8 files changed

+309
-35
lines changed

README.md

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ You can use any of the default widgets of [blessed](https://github.com/chjj/bles
5656
}
5757
screen.append(line) //must append before setting data
5858
line.setData([data])
59-
59+
6060
screen.key(['escape', 'q', 'C-c'], function(ch, key) {
6161
return process.exit(0);
6262
});
@@ -73,6 +73,8 @@ See below for a complete list of widgets.
7373

7474
[Bar Chart](#bar-chart)
7575

76+
[Stacked Bar Chart](#stacked-bar-chart)
77+
7678
[Map](#map)
7779

7880
[Gauge](#gauge)
@@ -142,6 +144,31 @@ See below for a complete list of widgets.
142144
, data: [5, 10]})
143145
`````
144146

147+
### Stacked Bar Chart
148+
149+
<img src="./docs/images/stacked-bar.png" alt="stacked-bar" width="250">
150+
151+
`````javascript
152+
bar = contrib.stackedBar(
153+
{ label: 'Server Utilization (%)'
154+
, barWidth: 4
155+
, barSpacing: 6
156+
, xOffset: 0
157+
//, maxValue: 15
158+
, height: "40%"
159+
, width: "50%"
160+
, barBgColor: [ 'red', 'blue', 'green' ]})
161+
screen.append(bar)
162+
bar.setData(
163+
{ barCategory: ['Q1', 'Q2', 'Q3', 'Q4']
164+
, stackedCategory: ['US', 'EU', 'AP']
165+
, data:
166+
[ [ 7, 7, 5]
167+
, [8, 2, 0]
168+
, [0, 0, 0]
169+
, [2, 3, 2] ]
170+
})
171+
`````
145172

146173
### Map
147174

@@ -231,7 +258,7 @@ Updating the donut is as easy as passing in an array to `setData` using the same
231258

232259
lcd.setDisplay(23 + 'G'); // will display "23G"
233260
lcd.setOptions({}) // adjust options at runtime
234-
261+
235262
`````
236263

237264
Please see the **examples/lcd.js** for an example. The example provides keybindings to adjust the `segmentWidth` and `segmentInterval` and `strokeWidth` in real-time so that you can see how they manipulate the look and feel.
@@ -391,7 +418,7 @@ Every node is a hash and it can have custom properties that can be used in "sele
391418
* No effect when the node have no child
392419
* Default value for each node will be `treeInstance.options.extended` if the node `extended` option is not set
393420
* *Example* : <code>{'Fruit':{ name: 'Fruit', extended: true, children:{ 'Banana': {}, 'Cherry': {}}}}</code>
394-
421+
395422
### Markdown
396423
397424
<img src="./docs/images/markdown.png" alt="table">

docs/images/stacked-bar.png

25.7 KB
Loading

examples/stacked-bar.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var blessed = require('blessed')
2+
, contrib = require('../')
3+
, screen = blessed.screen()
4+
, bar = contrib.stackedBar(
5+
{ label: 'Server Utilization (%)'
6+
, barWidth: 4
7+
, barSpacing: 6
8+
, xOffset: 0
9+
//, maxValue: 15
10+
, height: "40%"
11+
, width: "50%"
12+
, barBgColor: [ 'red', 'blue', 'green' ]})
13+
14+
screen.append(bar)
15+
16+
bar.setData(
17+
{ barCategory: ['Q1', 'Q2', 'Q3', 'Q4']
18+
, stackedCategory: ['US', 'EU', 'AP']
19+
, data:
20+
[ [ 7, 7, 5]
21+
, [8, 2, 0]
22+
, [0, 0, 0]
23+
, [2, 3, 2] ]
24+
})
25+
26+
screen.render()

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ exports.tree = require('./lib/widget/tree.js')
1818
exports.markdown = require('./lib/widget/markdown.js')
1919

2020
exports.bar = require('./lib/widget/charts/bar')
21+
exports.stackedBar = require('./lib/widget/charts/stacked-bar')
2122
exports.line = require('./lib/widget/charts/line')
2223

2324
exports.OutputBuffer = require('./lib/server-utils').OutputBuffer

lib/utils.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Recursively merge properties of two objects
2+
* Recursively merge properties of two objects
33
*/
44
function MergeRecursive(obj1, obj2) {
55

@@ -33,5 +33,36 @@ function getTypeName(thing){
3333
return Object.prototype.toString.call(thing);
3434
}
3535

36+
function abbreviateNumber(value) {
37+
var newValue = value;
38+
if (value >= 1000) {
39+
var suffixes = ["", "k", "m", "b","t"];
40+
var suffixNum = Math.floor( (""+value).length/3 );
41+
var shortValue = '';
42+
for (var precision = 2; precision >= 1; precision--) {
43+
shortValue = parseFloat( (suffixNum != 0 ? (value / Math.pow(1000,suffixNum) ) : value).toPrecision(precision));
44+
var dotLessShortValue = (shortValue + '').replace(/[^a-zA-Z 0-9]+/g,'');
45+
if (dotLessShortValue.length <= 2) { break; }
46+
}
47+
if (shortValue % 1 != 0) shortNum = shortValue.toFixed(1);
48+
newValue = shortValue+suffixes[suffixNum];
49+
}
50+
return newValue;
51+
}
52+
53+
function getColorCode(color) {
54+
if (Array.isArray(color) && color.length == 3)
55+
{
56+
return x256(color[0],color[1],color[2]);
57+
}
58+
else
59+
{
60+
return color;
61+
}
62+
}
63+
3664
exports.MergeRecursive = MergeRecursive
3765
exports.getTypeName = getTypeName
66+
exports.abbreviateNumber = abbreviateNumber
67+
exports.getColorCode = getColorCode
68+

lib/widget/charts/line.js

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,6 @@ var blessed = require('blessed')
55
, x256 = require('x256')
66
, _ = require('lodash')
77

8-
function getColorCode(color) {
9-
if (Array.isArray(color) && color.length == 3)
10-
{
11-
return x256(color[0],color[1],color[2]);
12-
}
13-
else
14-
{
15-
return color;
16-
}
17-
}
18-
198
function Line(options) {
209

2110
var self = this
@@ -90,7 +79,7 @@ Line.prototype.setData = function(data) {
9079
var maxChars = legendWidth-2
9180
for (var i=0; i<data.length; i++) {
9281
var style = data[i].style || {}
93-
var color = getColorCode(style.line || self.options.style.line)
82+
var color = utils.getColorCode(style.line || self.options.style.line)
9483
legandText += '{'+color+'-fg}'+ data[i].title.substring(0, maxChars)+'{/'+color+'-fg}\r\n'
9584
}
9685
self.legend.setContent(legandText)
@@ -130,27 +119,10 @@ Line.prototype.setData = function(data) {
130119
return max;
131120
}
132121

133-
function abbreviateNumber(value) {
134-
var newValue = value;
135-
if (value >= 1000) {
136-
var suffixes = ["", "k", "m", "b","t"];
137-
var suffixNum = Math.floor( (""+value).length/3 );
138-
var shortValue = '';
139-
for (var precision = 2; precision >= 1; precision--) {
140-
shortValue = parseFloat( (suffixNum != 0 ? (value / Math.pow(1000,suffixNum) ) : value).toPrecision(precision));
141-
var dotLessShortValue = (shortValue + '').replace(/[^a-zA-Z 0-9]+/g,'');
142-
if (dotLessShortValue.length <= 2) { break; }
143-
}
144-
if (shortValue % 1 != 0) shortNum = shortValue.toFixed(1);
145-
newValue = shortValue+suffixes[suffixNum];
146-
}
147-
return newValue;
148-
}
149-
150122
function formatYLabel(value, max, min, numLabels, wholeNumbersOnly, abbreviate) {
151123
var fixed = (max/numLabels<1 && value!=0 && !wholeNumbersOnly) ? 2 : 0
152124
var res = value.toFixed(fixed)
153-
return abbreviate?abbreviateNumber(res):res
125+
return abbreviate?utils.abbreviateNumber(res):res
154126
}
155127

156128
function getMaxXLabelPadding(numLabels, wholeNumbersOnly, abbreviate, min) {

0 commit comments

Comments
 (0)