Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion scout-ui/src/minicharts/d3fns/few.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var d3 = require('d3');
var _ = require('lodash');
var tooltipHtml = require('./tooltip.jade');
var shared = require('./shared');
var debug = require('debug')('scout-ui:minicharts:few');

require('d3-tip')(d3);
Expand All @@ -25,7 +26,7 @@ module.exports = function(data, g, width, height, options) {
}
return d.tooltip || tooltipHtml({
label: d.label,
value: Math.round(d.value / sumValues * 100)
value: shared.percentFormat(d.value / sumValues)
});
})
.direction('n')
Expand Down
15 changes: 7 additions & 8 deletions scout-ui/src/minicharts/d3fns/many.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var d3 = require('d3');
var _ = require('lodash');
var tooltipHtml = require('./tooltip.jade');
var shared = require('./shared');
var debug = require('debug')('scout-ui:minicharts:many');

require('d3-tip')(d3);
Expand All @@ -18,13 +19,13 @@ module.exports = function(data, g, width, height, options) {
.rangeBands([0, width], 0.3, 0.0);

var values = _.pluck(data, 'value');
var maxValue = d3.max(values);
var sumValues = d3.sum(values);

var y = d3.scale.linear()
.domain([0, d3.max(values)])
.domain([0, maxValue])
.range([height, 0]);

var sumY = d3.sum(values);

// set up tooltips
var tip = d3.tip()
.attr('class', 'd3-tip')
Expand All @@ -34,7 +35,7 @@ module.exports = function(data, g, width, height, options) {
}
return d.tooltip || tooltipHtml({
label: d.label,
value: d.value
value: shared.percentFormat(d.value / sumValues)
});
})
.direction('n')
Expand All @@ -46,8 +47,6 @@ module.exports = function(data, g, width, height, options) {

if (options.scale) {
var maxVal = d3.max(y.domain());
var format = d3.format('%.1f');
var legendValues = [format(maxVal), format(maxVal / 2)];

// @todo use a scale and wrap both text and line in g element
var legend = g.append('g')
Expand All @@ -60,7 +59,7 @@ module.exports = function(data, g, width, height, options) {
.attr('y', 0)
.attr('dy', '0.3em')
.attr('text-anchor', 'end')
.text(d3.max(y.domain()) + '%');
.text(shared.percentFormat(maxValue / sumValues));

legend.append('text')
.attr('class', 'legend')
Expand All @@ -69,7 +68,7 @@ module.exports = function(data, g, width, height, options) {
.attr('y', height / 2)
.attr('dy', '0.3em')
.attr('text-anchor', 'end')
.text(d3.max(y.domain()) / 2 + '%');
.text(shared.percentFormat(maxValue / sumValues / 2));

legend.append('text')
.attr('class', 'legend')
Expand Down
78 changes: 47 additions & 31 deletions scout-ui/src/minicharts/d3fns/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,46 @@ module.exports = function(opts) {
var height = opts.height - margin.top - margin.bottom;
var el = opts.el;

// use the linear scale just to get nice binning values
var x = d3.scale.linear()
.domain(d3.extent(values))
.range([0, width]);
if (opts.data.unique < 20) {
var data = _(values)
.groupBy(function(d) {
return d;
})
.map(function(v, k) {
v.label = k;
v.value = v.length;
return v;
})
.value();

// Generate a histogram using approx. twenty uniformly-spaced bins
var ticks = x.ticks(20);
} else {
// use the linear scale just to get nice binning values
var x = d3.scale.linear()
.domain(d3.extent(values))
.range([0, width]);

var hist = d3.layout.histogram()
.bins(ticks);
// Generate a histogram using approx. twenty uniformly-spaced bins
var ticks = x.ticks(20);
var hist = d3.layout.histogram()
.bins(ticks);

var data = hist(values);
var sumY = d3.sum(_.pluck(data, 'y'));
var data = hist(values);
var sumY = d3.sum(_.pluck(data, 'y'));

_.each(data, function(d, i) {
var label;
if (i === 0) {
label = '< ' + (d.x + d.dx);
} else if (i === data.length - 1) {
label = '&ge; ' + d.x;
} else {
label = d.x + '-' + (d.x + d.dx);
}
// remapping keys to conform with all other types
d.value = d.y;
d.label = label;
d.tooltip = tooltipHtml({
label: label,
value: Math.round(d.y / sumY * 100)
_.each(data, function(d, i) {
var label;
if (i === 0) {
label = '< ' + (d.x + d.dx);
} else if (i === data.length - 1) {
label = '&ge; ' + d.x;
} else {
label = d.x + '-' + (d.x + d.dx);
}
// remapping keys to conform with all other types
d.value = d.y;
d.label = label;
});
});
}

// clear element first
d3.select(el).selectAll('*').remove();
Expand All @@ -52,16 +61,23 @@ module.exports = function(opts) {
.append('g')
.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');

many(data, g, width, height - 10, {
scale: true,
bgbars: false,
labels: {
var labels;
if (opts.data.unique < 20) {
labels = true;
} else {
labels = {
text: function(d, i) {
if (i === 0) return 'min: ' + d3.min(values);
if (i === data.length - 1) return 'max: ' + d3.max(values);
return '';
}
}
};
}

many(data, g, width, height - 10, {
scale: true,
bgbars: false,
labels: labels
});
};

4 changes: 4 additions & 0 deletions scout-ui/src/minicharts/d3fns/shared.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var d3 = require('d3');

module.exports = {

margin: {
Expand All @@ -7,4 +9,6 @@ module.exports = {
left: 40
},

percentFormat: d3.format('%.1f')

};
2 changes: 1 addition & 1 deletion scout-ui/src/minicharts/d3fns/tooltip.jade
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.tooltip-wrapper
.tooltip-label!= label
.tooltip-value #{value}%
.tooltip-value #{value}