Skip to content

Commit

Permalink
abbreviate line chart
Browse files Browse the repository at this point in the history
  • Loading branch information
Yaron Naveh committed Oct 9, 2015
1 parent 13a4d8a commit ae51c4a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 12 deletions.
31 changes: 31 additions & 0 deletions examples/line-abbreviate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var blessed = require('blessed')
, contrib = require('../index')
, screen = blessed.screen()
, line = contrib.line(
{ width: 80
, height: 30
, left: 15
, top: 12
, xPadding: 5
, label: 'Title'
, abbreviate: true
})

, data = [ { title: 'us-east',
x: ['t1', 't2', 't3', 't4'],
y: [5, 8000, 99999, 3100000],
style: {
line: 'red'
}
}
]


screen.append(line) //must append before setting data
line.setData(data)

screen.key(['escape', 'q', 'C-c'], function(ch, key) {
return process.exit(0);
});

screen.render()
40 changes: 29 additions & 11 deletions lib/widget/charts/line.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Line.prototype.__proto__ = Canvas.prototype;
Line.prototype.type = 'line';

Line.prototype.setData = function(data) {

if (!this.ctx) {
throw "error: canvas context does not exist. setData() for line charts must be called after the chart has been added to the screen via screen.append()"
}
Expand Down Expand Up @@ -116,19 +116,37 @@ Line.prototype.setData = function(data) {

return max;
}

function formatYLabel(value, max, numLabels, wholeNumbersOnly) {

function abbreviateNumber(value) {
var newValue = value;
if (value >= 1000) {
var suffixes = ["", "k", "m", "b","t"];
var suffixNum = Math.floor( (""+value).length/3 );
var shortValue = '';
for (var precision = 2; precision >= 1; precision--) {
shortValue = parseFloat( (suffixNum != 0 ? (value / Math.pow(1000,suffixNum) ) : value).toPrecision(precision));
var dotLessShortValue = (shortValue + '').replace(/[^a-zA-Z 0-9]+/g,'');
if (dotLessShortValue.length <= 2) { break; }
}
if (shortValue % 1 != 0) shortNum = shortValue.toFixed(1);
newValue = shortValue+suffixes[suffixNum];
}
return newValue;
}

function formatYLabel(value, max, numLabels, wholeNumbersOnly, abbreviate) {
var fixed = (max/numLabels<1 && value!=0 && !wholeNumbersOnly) ? 2 : 0
return value.toFixed(fixed)
var res = value.toFixed(fixed)
return abbreviate?abbreviateNumber(res):res
}

function getMaxXLabelPadding(numLabels, wholeNumbersOnly) {
function getMaxXLabelPadding(numLabels, wholeNumbersOnly, abbreviate) {
var max = getMaxY()

return formatYLabel(max, max, numLabels, wholeNumbersOnly).length * 2;
return formatYLabel(max, max, numLabels, wholeNumbersOnly, abbreviate).length * 2;
}

if (getMaxXLabelPadding(this.options.numYLabels, this.options.wholeNumbersOnly) > xLabelPadding) {
if (getMaxXLabelPadding(this.options.numYLabels, this.options.wholeNumbersOnly, this.options.abbreviate) > xLabelPadding) {
xLabelPadding = getMaxXLabelPadding(this.options.numYLabels);
};

Expand All @@ -138,15 +156,15 @@ Line.prototype.setData = function(data) {

function getMaxX() {
var maxLength = 0;

for(var i = 0; i < labels.length; i++) {
if(labels[i] === undefined) {
console.log("label[" + i + "] is undefined");
} else if(labels[i].length > maxLength) {
maxLength = labels[i].length;
}
}

return maxLength;
}

Expand Down Expand Up @@ -197,7 +215,7 @@ Line.prototype.setData = function(data) {
// Draw the Y value texts
var maxY = getMaxY()
for(var i = 0; i < maxY; i += yLabelIncrement) {
c.fillText(formatYLabel(i, maxY, this.options.numYLabels, this.options.wholeNumbersOnly), xPadding - xLabelPadding, getYPixel(i));
c.fillText(formatYLabel(i, maxY, this.options.numYLabels, this.options.wholeNumbersOnly, this.options.abbreviate), xPadding - xLabelPadding, getYPixel(i));
}

for (var h=0; h<data.length; h++) {
Expand Down Expand Up @@ -259,7 +277,7 @@ Line.prototype.getOptionsPrototype = function() {
y: [22, 7, 12, 1],
style: {line: 'blue'}
}]

}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "blessed-contrib",
"version": "2.3.7",
"version": "2.3.8",
"description": "",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit ae51c4a

Please sign in to comment.