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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
/**
* Matomo - free/libre analytics platform
*
* DataTable UI class for JqplotGraph/StackedBarEvolution.
*
* @link http://www.jqplot.com
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
(function ($, require) {
var exports = require('piwik/UI'),
JqplotGraphDataTable = exports.JqplotGraphDataTable,
JqplotGraphDataTablePrototype = JqplotGraphDataTable.prototype;
exports.JqplotStackedBarEvolutionGraphDataTable = function (element) {
JqplotGraphDataTable.call(this, element);
};
$.extend(exports.JqplotStackedBarEvolutionGraphDataTable.prototype, JqplotGraphDataTablePrototype, {
_setJqplotParameters: function (params) {
JqplotGraphDataTablePrototype._setJqplotParameters.call(this, params);
var defaultParams = {
axes: {
xaxis: {
pad: 1.0,
renderer: $.jqplot.CategoryAxisRenderer,
tickOptions: {
showGridline: false
}
}
},
piwikTicks: {
showTicks: true,
showGrid: true,
showHighlight: false,
tickColor: this.tickColor
}
};
defaultParams.seriesDefaults = {
renderer: $.jqplot.BarRenderer,
rendererOptions: {
shadowOffset: 1,
shadowDepth: 2,
shadowAlpha: .2,
fillToZero: true,
barMargin: this.data[0].length > 10 ? 2 : 10
}
};
defaultParams.stackSeries = true;
var overrideParams = {
legend: {
show: false
},
canvasLegend: {
show: true
}
};
this.jqplotParams = $.extend(true, {}, defaultParams, this.jqplotParams, overrideParams);
},
_bindEvents: function () {
JqplotGraphDataTablePrototype._bindEvents.call(this);
var self = this;
var lastTick = false;
$('#' + this.targetDivId)
.on('jqplotMouseLeave', function (e, s, i, d) {
$(this).css('cursor', 'default');
JqplotGraphDataTablePrototype._destroyDataPointTooltip.call(this, $(this));
})
.on('jqplotClick', function (e, s, i, d) {
if (lastTick !== false && typeof self.jqplotParams.axes.xaxis.onclick != 'undefined'
&& typeof self.jqplotParams.axes.xaxis.onclick[lastTick] == 'string') {
var url = self.jqplotParams.axes.xaxis.onclick[lastTick];
broadcast.propagateNewPage(url);
}
})
.on('jqplotPiwikTickOver', function (e, tick) {
lastTick = tick;
var label;
var dataByAxis = {};
var totalValue = 0;
for (var d = 0; d < self.data.length; ++d) {
var valueUnformatted = self.data[d][tick];
if (typeof valueUnformatted === 'undefined' || valueUnformatted === null) {
continue;
}
totalValue += valueUnformatted;
var axis = self.jqplotParams.series[d]._xaxis || 'xaxis';
if (!dataByAxis[axis]) {
dataByAxis[axis] = [];
}
var value = self.formatY(valueUnformatted, d);
var series = self.jqplotParams.series[d].label;
var seriesColor = self.jqplotParams.seriesColors[d];
dataByAxis[axis].push('<span class="tooltip-series-color" style="background-color: ' + seriesColor + ';"/>' + '<strong>' + value + '</strong> ' + piwikHelper.htmlEntities(series));
}
dataByAxis[axis].push('<span class="tooltip-series-color" style="background-color: #000;"/>' + '<strong>' + self.formatY(totalValue, 0) + '</strong> ' + _pk_translate('General_Total'));
var xAxisCount = 0;
Object.keys(self.jqplotParams.axes).forEach(function (axis) {
if (axis.substring(0, 1) === 'x') {
++xAxisCount;
}
});
var content = '';
for (var i = 0; i < xAxisCount; ++i) {
var axisName = i === 0 ? 'xaxis' : 'x' + (i + 1) + 'axis';
if (!dataByAxis[axisName] || !dataByAxis[axisName].length) {
continue;
}
if (typeof self.jqplotParams.axes[axisName].labels != 'undefined') {
label = self.jqplotParams.axes[axisName].labels[tick];
} else {
label = self.jqplotParams.axes[axisName].ticks[tick];
}
if (typeof label === 'undefined') { // sanity check
continue;
}
content += '<h3 class="evolution-tooltip-header">'+piwikHelper.htmlEntities(label)+'</h3>'+dataByAxis[axisName].join('<br />');
}
$(this).tooltip({
track: true,
items: 'div',
content: content,
show: false,
hide: false
}).trigger('mouseover');
if (typeof self.jqplotParams.axes.xaxis.onclick != 'undefined'
&& typeof self.jqplotParams.axes.xaxis.onclick[lastTick] == 'string') {
$(this).css('cursor', 'pointer');
}
});
this.setYTicks();
},
_destroyDataPointTooltip: function () {
// do nothing, tooltips are destroyed in the jqplotMouseLeave event
},
render: function () {
JqplotGraphDataTablePrototype.render.call(this);
if (initializeSparklines) {
initializeSparklines();
}
},
setYTicksForAxis: function (axisName, axis) {
// calculate maximum x value of all data sets
var maxCrossDataSets = 0;
for (var j = 0; j < this.data[0].length; j++) {
var sum = 0;
for (var i = 0; i < this.data.length; i++) {
if (this.jqplotParams.series[i].yaxis == axisName) {
sum += this.data[i][j];
}
}
sum = parseFloat(sum);
if (sum > maxCrossDataSets) {
maxCrossDataSets = sum;
}
}
// add little padding on top
maxCrossDataSets += Math.round(maxCrossDataSets * .03);
// round to the nearest multiple of ten
if (maxCrossDataSets > 15) {
maxCrossDataSets = maxCrossDataSets + 10 - maxCrossDataSets % 10;
}
if (maxCrossDataSets == 0) {
maxCrossDataSets = 1;
}
// make sure percent axes don't go above 100%
if (axis.tickOptions.formatString.substring(2, 3) == '%' && maxCrossDataSets > 100) {
maxCrossDataSets = 100;
}
// calculate y-values for ticks
var ticks = [];
var numberOfTicks = 2;
var tickDistance = Math.ceil(maxCrossDataSets / numberOfTicks);
for (var i = 0; i <= numberOfTicks; i++) {
ticks.push(i * tickDistance);
}
axis.ticks = ticks;
},
});
})(jQuery, require);
|