Skip to content

Commit 61f2206

Browse files
committed
Merge pull request #13 from agallou/optimization
remove some calls to jQuery or data
2 parents 7f91fd1 + ae18866 commit 61f2206

File tree

1 file changed

+40
-31
lines changed

1 file changed

+40
-31
lines changed

jquery.highchartTable.js

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
}
5454
graphContainer = graphContainer[0];
5555
} else {
56-
$table.before('<div ></div>');
56+
$table.before('<div></div>');
5757
graphContainer = $table.prev();
5858
graphContainer = graphContainer[0];
5959
}
@@ -81,48 +81,52 @@
8181
var skippedColumns = 0;
8282
var graphIsStacked = false;
8383
ths.each(function(indexTh, th) {
84-
var columnScale = $(th).data('graph-value-scale');
84+
var $th = $(th);
85+
var columnScale = $th.data('graph-value-scale');
8586

86-
var serieGraphType = $(th).data('graph-type');
87+
var serieGraphType = $th.data('graph-type');
8788
if($.inArray(serieGraphType, allowedGraphTypes) == -1) {
8889
serieGraphType = globalGraphType;
8990
}
9091

91-
var serieStackGroup = $(th).data('graph-stack-group');
92+
var serieStackGroup = $th.data('graph-stack-group');
9293
if(serieStackGroup) {
9394
graphIsStacked = true;
9495
}
9596

96-
if (typeof $(th).data('graph-yaxis') != 'undefined' && $(th).data('graph-yaxis') == '1') {
97+
var yaxis = $th.data('graph-yaxis');
98+
99+
if (typeof yaxis != 'undefined' && yaxis == '1') {
97100
nbYaxis = 2;
98101
}
99102

100-
var isColumnSkipped = $(th).data('graph-skip') == 1;
103+
var isColumnSkipped = $th.data('graph-skip') == 1;
101104
if (isColumnSkipped)
102105
{
103106
skippedColumns = skippedColumns + 1;
104107
}
105108

106109
var thGraphConfig = {
107-
libelle: $(th).text(),
110+
libelle: $th.text(),
108111
skip: isColumnSkipped,
109112
indexTd: indexTh - skippedColumns - 1,
110-
color: $(th).data('graph-color'),
111-
visible: !$(th).data('graph-hidden'),
112-
yAxis: typeof $(th).data('graph-yaxis') != 'undefined' ? $(th).data('graph-yaxis') : 0,
113-
dashStyle: $(th).data('graph-dash-style') || 'solid'
113+
color: $th.data('graph-color'),
114+
visible: !$th.data('graph-hidden'),
115+
yAxis: typeof yaxis != 'undefined' ? yaxis : 0,
116+
dashStyle: $th.data('graph-dash-style') || 'solid'
114117
};
115118

116-
if (typeof $(th).data('graph-vline-x') == 'undefined') {
119+
var vlinex = $th.data('graph-vline-x');
120+
if (typeof vlinex == 'undefined') {
117121
thGraphConfig.scale = typeof columnScale != 'undefined' ? parseFloat(columnScale) : 1;
118122
thGraphConfig.graphType = serieGraphType;
119123
thGraphConfig.stack = serieStackGroup;
120-
thGraphConfig.unit = $(th).data('graph-unit');
124+
thGraphConfig.unit = $th.data('graph-unit');
121125
columns[indexTh] = thGraphConfig;
122126
} else {
123-
thGraphConfig.x = $(th).data('graph-vline-x');
124-
thGraphConfig.height = $(th).data('graph-vline-height');
125-
thGraphConfig.name = $(th).data('graph-vline-name');
127+
thGraphConfig.x = vlinex;
128+
thGraphConfig.height = $th.data('graph-vline-height');
129+
thGraphConfig.name = $th.data('graph-vline-name');
126130
vlines[indexTh] = thGraphConfig;
127131
}
128132
});
@@ -145,7 +149,7 @@
145149
dataLabels: {
146150
x: isGraphInverted ? 15 : 0,
147151
enabled: $table.data('graph-datalabels-enabled') == 1,
148-
align: typeof $table.data('graph-datalabels-align') != 'undefined' ? $table.data('graph-datalabels-align') : 'center'
152+
align: $table.data('graph-datalabels-align') || 'center'
149153
}
150154
});
151155
}
@@ -184,11 +188,12 @@
184188
if (column.skip) {
185189
return;
186190
}
191+
var $td = $(td);
187192
if (indexTd==0) {
188-
cellValue = $(td).text();
193+
cellValue = $td.text();
189194
xValues.push(cellValue);
190195
} else {
191-
var rawCellValue = $(td).text();
196+
var rawCellValue = $td.text();
192197
var serie = series[column.indexTd];
193198

194199
if (rawCellValue.length==0) {
@@ -197,7 +202,7 @@
197202
var cleanedCellValue = rawCellValue.replace(/ /g, '').replace(/,/, '.');
198203
cellValue = Math.round(parseFloat(cleanedCellValue) * column.scale * 100) / 100;
199204

200-
var dataGraphX = $(td).data('graph-x');
205+
var dataGraphX = $td.data('graph-x');
201206

202207
if (isGraphDatetime) {
203208
dataGraphX = $('td', $(row)).first().text();
@@ -206,8 +211,9 @@
206211
dataGraphX = date.getTime() - date.getTimezoneOffset()*60*1000;
207212
}
208213

214+
var tdGraphName = $td.data('graph-name');
209215
var serieDataItem = {
210-
name: typeof $(td).data('graph-name') != 'undefined' ? $(td).data('graph-name') : rawCellValue,
216+
name: typeof tdGraphName != 'undefined' ? tdGraphName : rawCellValue,
211217
y: cellValue,
212218
x: dataGraphX //undefined if no x defined in table
213219
};
@@ -221,13 +227,14 @@
221227
}
222228

223229
if (column.graphType === 'pie') {
224-
if ($(td).data('graph-item-highlight')) {
230+
if ($td.data('graph-item-highlight')) {
225231
serieDataItem.sliced = 1;
226232
}
227233
}
228234

229-
if (typeof $(td).data('graph-item-color') != 'undefined') {
230-
serieDataItem.color = $(td).data('graph-item-color');
235+
var tdGraphItemColor = $td.data('graph-item-color');
236+
if (typeof tdGraphItemColor != 'undefined') {
237+
serieDataItem.color = tdGraphItemColor;
231238
}
232239

233240
serie.data.push(serieDataItem);
@@ -280,6 +287,8 @@
280287
var colors = [];
281288

282289
var themeColors = typeof Highcharts.theme != 'undefined' && typeof Highcharts.theme.colors != 'undefined' ? Highcharts.theme.colors : [];
290+
var lineShadow = $table.data('graph-line-shadow');
291+
var lineWidth = $table.data('graph-line-width') || 2;
283292

284293
for(var i=0; i<9; i++) {
285294
var dataname = 'graph-color-' + (i+1);
@@ -306,7 +315,7 @@
306315
},
307316
legend: {
308317
enabled: $table.data('graph-legend-disabled') != '1',
309-
layout: typeof $table.data('graph-legend-layout') != 'undefined' ? $table.data('graph-legend-layout') : 'horizontal',
318+
layout: $table.data('graph-legend-layout') || 'horizontal',
310319
symbolWidth: $table.data('graph-legend-width') || 30,
311320
x: $table.data('graph-legend-x') || 15,
312321
y: $table.data('graph-legend-y') || 0
@@ -340,7 +349,7 @@
340349
title: {
341350
text: $table.data('graph-xaxis-title-text') || null
342351
},
343-
gridLineWidth: typeof $table.data('graph-xaxis-gridLine-width') != 'undefined' ? $table.data('graph-xaxis-gridLine-width') : 0,
352+
gridLineWidth: $table.data('graph-xaxis-gridLine-width') || 0,
344353
gridLineDashStyle: $table.data('graph-xaxis-gridLine-style') || 'ShortDot'
345354
},
346355
yAxis: yAxisConfig,
@@ -361,12 +370,12 @@
361370
dataLabels: {
362371
enabled: true
363372
},
364-
lineWidth: $table.data('graph-line-width') || 2
373+
lineWidth: lineWidth
365374
},
366375
area: {
367-
lineWidth: $table.data('graph-line-width') || 2,
368-
shadow: typeof $table.data('graph-line-shadow') != 'undefined' ? $table.data('graph-line-shadow') : true,
369-
fillOpacity: typeof $table.data('graph-area-fillOpacity') != 'undefined' ? $table.data('graph-area-fillOpacity') : 0.75
376+
lineWidth: lineWidth,
377+
shadow: typeof lineShadow != 'undefined' ? lineShadow : true,
378+
fillOpacity: $table.data('graph-area-fillOpacity') || 0.75
370379
},
371380
pie: {
372381
allowPointSelect: true,
@@ -380,7 +389,7 @@
380389
animation: false,
381390
stickyTracking : false,
382391
stacking: graphIsStacked ? stackingType : null,
383-
groupPadding: typeof $table.data('graph-group-padding') != 'undefined' ? $table.data('graph-group-padding') : 0
392+
groupPadding: $table.data('graph-group-padding') || 0
384393
}
385394
},
386395
series: series,

0 commit comments

Comments
 (0)