-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathheatmap_controller.js
134 lines (108 loc) · 4.02 KB
/
heatmap_controller.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
var _ = require('lodash');
var module = require('ui/modules').get('heatmap');
module.controller('HeatmapController', function ($scope, Private) {
var tabifyAggResponse = Private(require('ui/agg_response/tabify/tabify'));
function getLabel(agg, name) {
return agg.bySchemaName[name] ? agg.bySchemaName[name][0].makeLabel() : '';
}
function processTableGroups(tableGroups, $scope) {
var columnAggId = _.first(_.pluck($scope.vis.aggs.bySchemaName['columns'], 'id'));
var rowAggId = _.first(_.pluck($scope.vis.aggs.bySchemaName['rows'], 'id'));
var metricsAggId = _.first(_.pluck($scope.vis.aggs.bySchemaName['metric'], 'id'));
var dataLabels = { [columnAggId]: 'col', [rowAggId]: 'row', [metricsAggId]: 'value' };
var cells = [];
tableGroups.tables.forEach(function (table) {
table.rows.forEach(function (row) {
var cell = {};
table.columns.forEach(function (column, i) {
var fieldFormatter = table.aggConfig(column).fieldFormatter();
// Median metric aggs use the parentId and not the id field
var key = column.aggConfig.parentId ? dataLabels[column.aggConfig.parentId] : dataLabels[column.aggConfig.id];
if (key) {
cell[key] = key !== 'value' ? fieldFormatter(row[i]) : row[i];
}
});
// if no columns or rows, then return '_all'
if (!cell.col && !cell.row) {
cell['col'] = '_all';
}
cells.push(cell);
});
});
return cells;
};
$scope.$watch('esResponse', function (resp) {
if (!resp) {
$scope.data = null;
return;
}
// Add row, column, and metric titles as vis parameters
_.merge($scope.vis.params, {
rowAxis: { title: getLabel($scope.vis.aggs, 'rows') },
columnAxis: { title: getLabel($scope.vis.aggs, 'columns') },
legendTitle: getLabel($scope.vis.aggs, 'metric')
});
$scope.data = [{
cells: processTableGroups(tabifyAggResponse($scope.vis, resp), $scope)
}];
$scope.eventListeners = {
mouseover: [ mouseover ],
mouseout: [ mouseout ],
mousemove: [ mousemove ]
};
function mouseover(event) {
mousemove(event);
};
function mousemove(event){
var target = d3.select(event.target);
var isHeatmapCell = (target.attr("class") === "cell");
if (isHeatmapCell) {
// get data bound to heatmap cell
var d = _.first(target.data());
// Custom code for tooltip functionality goes here
$scope.$apply(function () {
var params = $scope.vis.params;
$scope.tooltipItems = Object.keys(d)
.filter(function (key) { return key !== "data"; })
.map(function (key) {
var title = d3.selectAll('text.title');
var value = d[key];
if (key.toUpperCase() === 'ROW'){
key = params.columnAxis.title || 'ROW';
}
if (key.toUpperCase() === 'COL'){
key = params.rowAxis.title || 'COL';
}
return {
key: key.toUpperCase(),
value: value
};
});
var svgParent = $(".parent");
var tooltip = $('.heatmap-tooltip');
var width = event.clientX + tooltip.width();
var height = event.clientY + tooltip.height();
var top = event.pageY - svgParent.offset().top;
var left = event.pageX - svgParent.offset().left;
if ($(window).width() < width){
$scope.left = left - tooltip.width() / 2;
}else{
$scope.left = left;
}
if ($(window).height() < height){
$scope.top = top - tooltip.height();
}else{
$scope.top = top;
}
});
}
};
function mouseout(event){
$scope.$apply(function () {
$scope.tooltipItems = [];
$scope.top = 0;
$scope.left = 0;
});
}
});
});