Skip to content

Commit 0dbd627

Browse files
committed
Drawing squares and circles in the legend taking account the scale of the map
1 parent 1b15729 commit 0dbd627

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

js/jquery.mapael.js

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,6 @@
7272
$.fn.mapael.initElem(paper, plots[id], elemOptions, $tooltip);
7373
}
7474

75-
// Create the legends for areas and plots
76-
if (options.legend.area.slices && options.legend.area.display)
77-
areaLegend = $.fn.mapael.createLegend($(this), options, 'area', areas);
78-
79-
if (options.legend.plot.slices && options.legend.plot.display)
80-
plotLegend = $.fn.mapael.createLegend($(this), options, 'plot', plots);
81-
8275
// Enable zoom
8376
if (options.map.zoom.enabled)
8477
$.fn.mapael.initZoom($container, paper, mapConf.width, mapConf.height, options.map.zoom);
@@ -184,18 +177,35 @@
184177
// Handle resizing of the map
185178
if (options.map.width) {
186179
paper.setSize(options.map.width, mapConf.height * (options.map.width / mapConf.width));
180+
181+
// Create the legends for areas and plots taking into account the scale of the map
182+
if (options.legend.area.slices && options.legend.area.display)
183+
areaLegend = $.fn.mapael.createLegend($(this), options, 'area', areas, 1);
184+
if (options.legend.plot.slices && options.legend.plot.display)
185+
plotLegend = $.fn.mapael.createLegend($(this), options, 'plot', plots, (options.map.width / mapConf.width));
187186
} else {
188187
$(window).on('resize', function(){
189188
clearTimeout(resizeTO);
190189
resizeTO = setTimeout(function(){$container.trigger('resizeEnd');}, 150);
191190
});
192-
$(document).on('ready', function(){$container.trigger('resizeEnd');});
193191
$container.on('resizeEnd', function(e) {
194192
var containerWidth = $container.width();
195193
if (paper.width != containerWidth) {
196194
paper.setSize(containerWidth, mapConf.height * (containerWidth / mapConf.width));
197195
}
198196
});
197+
(function(self, $container, options, areas, plots) {
198+
$(document).on('ready', function(){
199+
$container.trigger('resizeEnd');
200+
201+
// Create the legends for areas and plots taking into account the scale of the map
202+
if (options.legend.area.slices && options.legend.area.display)
203+
areaLegend = $.fn.mapael.createLegend($(self), options, 'area', areas, 1);
204+
if (options.legend.plot.slices && options.legend.plot.display)
205+
plotLegend = $.fn.mapael.createLegend($(self), options, 'plot', plots, ($container.width() / mapConf.width));
206+
});
207+
})(this, $container, options, areas, plots);
208+
199209
$container.trigger('resizeEnd');
200210
}
201211

@@ -374,7 +384,7 @@
374384
* @param options map options
375385
* @param legendType the type of the legend : 'area' or 'plot'
376386
*/
377-
$.fn.mapael.createLegend = function ($container, options, legendType, elems) {
387+
$.fn.mapael.createLegend = function ($container, options, legendType, elems, scale) {
378388
var legendOptions = options.legend[legendType]
379389
, $legend = (legendType == 'plot') ? $('.' + options.legend.plot.cssClass, $container).empty() : $('.' + options.legend.area.cssClass, $container).empty()
380390
, paper = new Raphael($legend.get(0))
@@ -413,25 +423,25 @@
413423
elem = paper.rect(
414424
legendOptions.marginLeft
415425
, height
416-
, legendOptions.slices[i].size
417-
, legendOptions.slices[i].size
426+
, scale * (legendOptions.slices[i].size)
427+
, scale * (legendOptions.slices[i].size)
418428
).attr(legendOptions.slices[i].attrs);
419429
} else {
420430
elem = paper.circle(
421-
legendOptions.marginLeft + legendOptions.slices[i].size / 2
422-
, height + legendOptions.slices[i].size / 2
423-
, legendOptions.slices[i].size / 2
431+
legendOptions.marginLeft + scale * (legendOptions.slices[i].size / 2)
432+
, height + scale * (legendOptions.slices[i].size / 2)
433+
, scale * (legendOptions.slices[i].size / 2)
424434
).attr(legendOptions.slices[i].attrs);
425435
}
426436

427437
label = paper.text(
428-
legendOptions.marginLeft + legendOptions.slices[i].size + legendOptions.marginLeftLabel
429-
, height + legendOptions.slices[i].size / 2
438+
legendOptions.marginLeft + scale * legendOptions.slices[i].size + legendOptions.marginLeftLabel
439+
, height + scale * (legendOptions.slices[i].size / 2)
430440
, legendOptions.slices[i].label
431441
).attr(legendOptions.labelAttrs);
432442

433-
height += legendOptions.marginBottom + legendOptions.slices[i].size;
434-
width = Math.max(width, legendOptions.marginLeft + legendOptions.slices[i].size + legendOptions.marginLeftLabel + label.getBBox().width);
443+
height += legendOptions.marginBottom + scale * legendOptions.slices[i].size;
444+
width = Math.max(width, legendOptions.marginLeft + scale * legendOptions.slices[i].size + legendOptions.marginLeftLabel + label.getBBox().width);
435445

436446
if (legendOptions.hideElemsOnClick.enabled) {
437447
// Hide/show elements when user clicks on a legend element

0 commit comments

Comments
 (0)