forked from apache/superset
-
Notifications
You must be signed in to change notification settings - Fork 0
/
histogram.js
136 lines (119 loc) · 3.82 KB
/
histogram.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import d3 from 'd3';
import { getColorFromScheme } from '../javascripts/modules/colors';
require('./histogram.css');
function histogram(slice, payload) {
const data = payload.data;
const div = d3.select(slice.selector);
const numBins = Number(slice.formData.link_length) || 10;
const normalized = slice.formData.normalized;
const xAxisLabel = slice.formData.x_axis_label;
const yAxisLabel = slice.formData.y_axis_label;
const draw = function () {
// Set Margins
const margin = {
top: 50,
right: 10,
bottom: 20,
left: yAxisLabel ? 70 : 50,
};
const navBarHeight = 36;
const navBarBuffer = 10;
const width = slice.width() - margin.left - margin.right;
const height = slice.height() - margin.top - margin.bottom - navBarHeight - navBarBuffer;
// set number of ticks
const maxTicks = 20;
const numTicks = d3.min([maxTicks, numBins]);
// Set Histogram objects
const x = d3.scale.linear();
const y = d3.scale.linear();
const xAxis = d3.svg.axis()
.scale(x)
.orient('bottom')
.ticks(numTicks, 's');
const yAxis = d3.svg.axis()
.scale(y)
.orient('left')
.ticks(numTicks, 's');
// Calculate bins for the data
let bins = d3.layout.histogram().bins(numBins)(data);
if (normalized) {
const total = data.length;
bins = bins.map(d => ({ ...d, y: d.y / total }));
}
// Set the x-values
const max = d3.max(data);
const min = d3.min(data);
x.domain([min, max])
.range([0, width], 0.1);
// Set the y-values
y.domain([0, d3.max(bins, d => d.y)])
.range([height, 0]);
// Create the svg value with the bins
const svg = div.selectAll('svg')
.data([bins])
.enter()
.append('svg');
// Make a rectangular background fill
svg.append('rect')
.attr('width', '100%')
.attr('height', '100%')
.attr('fill', '#f6f6f6');
// Transform the svg to make space for the margins
const gEnter = svg
.append('g')
.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
// Add the bars and the x axis
gEnter.append('g').attr('class', 'bars');
gEnter.append('g').attr('class', 'x axis');
// Add width and height to the svg
svg.attr('width', slice.width())
.attr('height', slice.height());
// Create the bars in the svg
const bar = svg.select('.bars').selectAll('.bar').data(bins);
bar.enter().append('rect');
bar.exit().remove();
// Set the Height and Width for each bar
bar.attr('width', (x(bins[0].dx) - x(0)) - 1)
.attr('x', d => x(d.x))
.attr('y', d => y(d.y))
.attr('height', d => y.range()[0] - y(d.y))
.style('fill', getColorFromScheme(1, slice.formData.color_scheme))
.order();
// Update the x-axis
svg.append('g')
.attr('class', 'axis')
.attr('transform', 'translate(' + margin.left + ',' + (height + margin.top) + ')')
.text('values')
.call(xAxis);
// Update the Y Axis and add minor lines
svg.append('g')
.attr('class', 'axis')
.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')')
.text('count')
.call(yAxis)
.selectAll('g')
.filter(function (d) { return d; })
.classed('minor', true);
// add axis labels if passed
if (xAxisLabel) {
svg.append('text')
.attr('transform',
'translate(' + ((width + margin.left) / 2) + ' ,' +
(height + margin.top + 50) + ')')
.style('text-anchor', 'middle')
.text(xAxisLabel);
}
if (yAxisLabel) {
svg.append('text')
.attr('transform', 'rotate(-90)')
.attr('y', '1em')
.attr('x', 0 - (height / 2))
.attr('dy', '1em')
.style('text-anchor', 'middle')
.text(yAxisLabel);
}
};
div.selectAll('*').remove();
draw();
}
module.exports = histogram;