2013-06-23 18:00:13 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Jquery Mapael - Dynamic maps jQuery plugin (based on raphael.js)
|
|
|
|
* Requires jQuery and raphael.js
|
|
|
|
*
|
2013-07-29 22:30:16 +02:00
|
|
|
* Version: 0.4.0 (29-07-2013)
|
2013-06-23 18:00:13 +02:00
|
|
|
*
|
|
|
|
* Copyright (c) 2013 Vincent Brouté (http://www.neveldo.fr/mapael)
|
|
|
|
* Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php).
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
(function($) {
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
$.fn.mapael = function(options) {
|
|
|
|
options = $.extend(true, {}, $.fn.mapael.defaultOptions, options);
|
|
|
|
|
|
|
|
return this.each(function() {
|
|
|
|
|
|
|
|
var $tooltip = $("<div>").addClass(options.map.tooltip.cssClass).css("display", "none")
|
2013-07-15 22:53:59 +02:00
|
|
|
, $container = $('.' + options.map.cssClass, this).empty().append($tooltip)
|
2013-06-23 18:00:13 +02:00
|
|
|
, mapConf = $.fn.mapael.maps[options.map.name]
|
2013-07-15 22:53:59 +02:00
|
|
|
, paper = new Raphael($container[0], mapConf.width, mapConf.height)
|
2013-07-29 22:30:16 +02:00
|
|
|
, elemOptions = {}
|
2013-06-30 22:55:03 +02:00
|
|
|
, coords = {}
|
2013-07-04 22:47:20 +02:00
|
|
|
, resizeTO = 0
|
2013-07-15 22:53:59 +02:00
|
|
|
, areas = {}
|
|
|
|
, plots = {}
|
2013-07-29 22:30:16 +02:00
|
|
|
, areaLegend = {}
|
|
|
|
, plotLegend = {}
|
2013-07-15 22:53:59 +02:00
|
|
|
, id = 0;
|
2013-06-23 18:00:13 +02:00
|
|
|
|
2013-06-30 22:55:03 +02:00
|
|
|
options.map.tooltip.css && $tooltip.css(options.map.tooltip.css);
|
|
|
|
paper.setViewBox(0, 0, mapConf.width, mapConf.height, false);
|
2013-06-23 18:00:13 +02:00
|
|
|
|
|
|
|
// Draw map areas
|
2013-07-15 22:53:59 +02:00
|
|
|
for (id in mapConf.elems) {
|
2013-07-29 22:30:16 +02:00
|
|
|
elemOptions = $.fn.mapael.getElemOptions(
|
2013-07-15 22:53:59 +02:00
|
|
|
options.map.defaultArea
|
2013-06-23 18:00:13 +02:00
|
|
|
, (options.areas[id] ? options.areas[id] : {})
|
2013-07-15 22:53:59 +02:00
|
|
|
, options.legend.area
|
2013-06-23 18:00:13 +02:00
|
|
|
);
|
2013-07-29 22:30:16 +02:00
|
|
|
areas[id] = {'mapElem' : paper.path(mapConf.elems[id]).attr(elemOptions.attrs)};
|
|
|
|
$.fn.mapael.initElem(paper, areas[id], elemOptions, $tooltip);
|
2013-06-23 18:00:13 +02:00
|
|
|
}
|
|
|
|
|
2013-06-30 22:55:03 +02:00
|
|
|
// Draw plots
|
2013-07-15 22:53:59 +02:00
|
|
|
for (id in options.plots) {
|
2013-07-29 22:30:16 +02:00
|
|
|
elemOptions = $.fn.mapael.getElemOptions(
|
2013-07-15 22:53:59 +02:00
|
|
|
options.map.defaultPlot
|
|
|
|
, (options.plots[id] ? options.plots[id] : {})
|
|
|
|
, options.legend.plot
|
2013-06-30 22:55:03 +02:00
|
|
|
);
|
|
|
|
|
2013-07-29 22:30:16 +02:00
|
|
|
if (elemOptions.x && elemOptions.y)
|
|
|
|
coords = {x : elemOptions.x, y : elemOptions.y};
|
2013-07-15 22:53:59 +02:00
|
|
|
else
|
2013-07-29 22:30:16 +02:00
|
|
|
coords = mapConf.getCoords(elemOptions.latitude, elemOptions.longitude);
|
2013-06-23 18:00:13 +02:00
|
|
|
|
2013-07-29 22:30:16 +02:00
|
|
|
if ("square" == elemOptions.type) {
|
2013-07-15 22:53:59 +02:00
|
|
|
plots[id] = {'mapElem' : paper.rect(
|
2013-07-29 22:30:16 +02:00
|
|
|
coords.x - (elemOptions.size / 2)
|
|
|
|
, coords.y - (elemOptions.size / 2)
|
|
|
|
, elemOptions.size
|
|
|
|
, elemOptions.size
|
|
|
|
).attr(elemOptions.attrs)};
|
2013-07-15 22:53:59 +02:00
|
|
|
} else { // Default = circle
|
2013-07-29 22:30:16 +02:00
|
|
|
plots[id] = {'mapElem' : paper.circle(coords.x, coords.y, elemOptions.size / 2).attr(elemOptions.attrs)};
|
2013-06-23 18:00:13 +02:00
|
|
|
}
|
|
|
|
|
2013-07-29 22:30:16 +02:00
|
|
|
$.fn.mapael.initElem(paper, plots[id], elemOptions, $tooltip);
|
2013-07-15 22:53:59 +02:00
|
|
|
}
|
|
|
|
|
2013-07-29 22:30:16 +02:00
|
|
|
// Create the legends for areas and plots
|
|
|
|
if (options.legend.area.slices && options.legend.area.display)
|
|
|
|
areaLegend = $.fn.mapael.createLegend($(this), options, 'area', areas);
|
|
|
|
|
|
|
|
if (options.legend.plot.slices && options.legend.plot.display)
|
|
|
|
plotLegend = $.fn.mapael.createLegend($(this), options, 'plot', plots);
|
|
|
|
|
2013-07-15 22:53:59 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Update the current map
|
|
|
|
* Refresh attributes and tooltips for areas and plots
|
2013-07-29 22:30:16 +02:00
|
|
|
* @param options options to refresh
|
|
|
|
* @param resetAreas true to reset previous areas options
|
|
|
|
* @param resetPlots true to reset previous plots options
|
|
|
|
* @param animDuration animation duration in ms
|
|
|
|
* @param easing easing type
|
2013-07-15 22:53:59 +02:00
|
|
|
*/
|
2013-07-29 22:30:16 +02:00
|
|
|
$(this).on('update', function(e, updateOptions, resetAreas, resetPlots, animDuration, easing) {
|
|
|
|
var elemOptions = {}
|
2013-07-15 22:53:59 +02:00
|
|
|
, legend = {}
|
|
|
|
, id = 0
|
|
|
|
, bbox = {}
|
|
|
|
, textPosition = {}
|
2013-07-29 22:30:16 +02:00
|
|
|
, plotOffset = 0
|
|
|
|
, resetHiddenElem = function(el) {
|
|
|
|
if(typeof el.hidden != "undefined" && el.hidden == true) {
|
|
|
|
$(el.node).trigger('click');
|
|
|
|
}
|
|
|
|
};
|
2013-06-23 18:00:13 +02:00
|
|
|
|
2013-07-15 22:53:59 +02:00
|
|
|
if (!animDuration) animDuration = 300;
|
|
|
|
if (!easing) easing = 'linear';
|
|
|
|
if (resetAreas) options.areas = {};
|
|
|
|
if (resetPlots) options.plots = {};
|
2013-06-23 18:00:13 +02:00
|
|
|
|
2013-07-15 22:53:59 +02:00
|
|
|
$.extend(true, options, updateOptions);
|
2013-07-04 22:47:20 +02:00
|
|
|
|
2013-07-29 22:30:16 +02:00
|
|
|
areaLegend.forEach && areaLegend.forEach(resetHiddenElem);
|
|
|
|
plotLegend.forEach && plotLegend.forEach(resetHiddenElem);
|
|
|
|
|
2013-07-15 22:53:59 +02:00
|
|
|
// Update areas attributes and tooltips
|
|
|
|
for (id in areas) {
|
2013-07-29 22:30:16 +02:00
|
|
|
elemOptions = $.fn.mapael.getElemOptions(
|
2013-07-15 22:53:59 +02:00
|
|
|
options.map.defaultArea
|
|
|
|
, (options.areas[id] ? options.areas[id] : {})
|
|
|
|
, options.legend.area
|
|
|
|
);
|
|
|
|
|
2013-07-29 22:30:16 +02:00
|
|
|
if (elemOptions.value)
|
|
|
|
areas[id].value = elemOptions.value;
|
2013-07-15 22:53:59 +02:00
|
|
|
|
2013-07-29 22:30:16 +02:00
|
|
|
$.fn.mapael.setHoverOptions(areas[id].mapElem, elemOptions.attrs, areas[id].mapElem.attrsHover);
|
|
|
|
areas[id].mapElem.animate(elemOptions.attrs, animDuration, easing);
|
|
|
|
|
|
|
|
if (elemOptions.tooltip && elemOptions.tooltip.content) {
|
|
|
|
areas[id].mapElem.tooltipContent = elemOptions.tooltip.content;
|
2013-07-15 22:53:59 +02:00
|
|
|
if (areas[id].textElem) {
|
2013-07-29 22:30:16 +02:00
|
|
|
areas[id].textElem.tooltipContent = elemOptions.tooltip.content;
|
2013-07-15 22:53:59 +02:00
|
|
|
}
|
2013-07-04 22:47:20 +02:00
|
|
|
}
|
2013-07-15 22:53:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update plots attributes and tooltips
|
|
|
|
for (id in plots) {
|
2013-07-29 22:30:16 +02:00
|
|
|
elemOptions = $.fn.mapael.getElemOptions(
|
2013-07-15 22:53:59 +02:00
|
|
|
options.map.defaultPlot
|
|
|
|
, (options.plots[id] ? options.plots[id] : {})
|
|
|
|
, options.legend.plot
|
|
|
|
);
|
2013-06-23 18:00:13 +02:00
|
|
|
|
2013-07-29 22:30:16 +02:00
|
|
|
if (elemOptions.value)
|
|
|
|
plots[id].value = elemOptions.value;
|
|
|
|
|
2013-07-15 22:53:59 +02:00
|
|
|
// Update text position
|
|
|
|
if (plots[id].textElem) {
|
|
|
|
bbox = plots[id].mapElem.getBBox();
|
2013-07-29 22:30:16 +02:00
|
|
|
plotOffset = (elemOptions.size - bbox.height) / 2;
|
2013-07-15 22:53:59 +02:00
|
|
|
bbox.x -= plotOffset;
|
|
|
|
bbox.x2 += plotOffset;
|
|
|
|
bbox.y -= plotOffset;
|
|
|
|
bbox.y2 += plotOffset;
|
2013-07-29 22:30:16 +02:00
|
|
|
textPosition = $.fn.mapael.getTextPosition(bbox, elemOptions.textPosition);
|
2013-07-15 22:53:59 +02:00
|
|
|
plots[id].textElem.animate({x : textPosition.x, y : textPosition.y}, animDuration, easing);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update plot size
|
2013-07-29 22:30:16 +02:00
|
|
|
if ("square" == elemOptions.type) {
|
|
|
|
elemOptions.attrs.width = elemOptions.size;
|
|
|
|
elemOptions.attrs.height = elemOptions.size;
|
2013-07-15 22:53:59 +02:00
|
|
|
} else { // Default : circle
|
2013-07-29 22:30:16 +02:00
|
|
|
elemOptions.attrs.r = elemOptions.size / 2;
|
2013-07-15 22:53:59 +02:00
|
|
|
}
|
|
|
|
|
2013-07-29 22:30:16 +02:00
|
|
|
$.fn.mapael.setHoverOptions(plots[id].mapElem, elemOptions.attrs, plots[id].mapElem.attrsHover);
|
|
|
|
plots[id].mapElem.animate(elemOptions.attrs, animDuration, easing);
|
2013-07-15 22:53:59 +02:00
|
|
|
|
2013-07-29 22:30:16 +02:00
|
|
|
if (elemOptions.tooltip && elemOptions.tooltip.content) {
|
|
|
|
plots[id].mapElem.tooltipContent = elemOptions.tooltip.content;
|
2013-07-15 22:53:59 +02:00
|
|
|
if (plots[id].textElem) {
|
2013-07-29 22:30:16 +02:00
|
|
|
plots[id].textElem.tooltipContent = elemOptions.tooltip.content;
|
2013-07-15 22:53:59 +02:00
|
|
|
}
|
|
|
|
}
|
2013-06-23 18:00:13 +02:00
|
|
|
}
|
2013-07-15 22:53:59 +02:00
|
|
|
});
|
2013-06-23 18:00:13 +02:00
|
|
|
|
2013-07-29 22:30:16 +02:00
|
|
|
// Handle resizing of the map
|
2013-07-02 22:04:41 +02:00
|
|
|
if (options.map.width) {
|
|
|
|
paper.setSize(options.map.width, mapConf.height * (options.map.width / mapConf.width));
|
|
|
|
} else {
|
2013-07-29 22:30:16 +02:00
|
|
|
$(window).on('resize', function(){
|
2013-07-02 22:04:41 +02:00
|
|
|
clearTimeout(resizeTO);
|
|
|
|
resizeTO = setTimeout(function(){$container.trigger('resizeEnd');}, 150);
|
|
|
|
});
|
2013-07-29 22:30:16 +02:00
|
|
|
$(document).on('ready', function(){$container.trigger('resizeEnd');});
|
|
|
|
$container.on('resizeEnd', function(e) {
|
2013-07-02 22:04:41 +02:00
|
|
|
var containerWidth = $container.width();
|
|
|
|
if (paper.width != containerWidth) {
|
|
|
|
paper.setSize(containerWidth, mapConf.height * (containerWidth / mapConf.width));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
$container.trigger('resizeEnd');
|
|
|
|
}
|
|
|
|
|
2013-06-30 22:55:03 +02:00
|
|
|
$(paper.desc).append(" and Mapael (http://neveldo.fr/mapael)");
|
2013-06-23 18:00:13 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-07-15 22:53:59 +02:00
|
|
|
/**
|
2013-07-29 22:30:16 +02:00
|
|
|
* Init the element of the map (drawing, setting attributes, events, tooltip, ...)
|
|
|
|
* @param paper
|
|
|
|
* @param elem
|
|
|
|
* @param params
|
|
|
|
* @param $tooltip
|
2013-07-15 22:53:59 +02:00
|
|
|
*/
|
2013-07-29 22:30:16 +02:00
|
|
|
$.fn.mapael.initElem = function(paper, elem, options, $tooltip) {
|
2013-07-15 22:53:59 +02:00
|
|
|
var bbox = {}, textPosition = {};
|
2013-07-29 22:30:16 +02:00
|
|
|
$.fn.mapael.setHoverOptions(elem.mapElem, options.attrs, options.attrsHover);
|
2013-07-15 22:53:59 +02:00
|
|
|
|
2013-07-29 22:30:16 +02:00
|
|
|
if (options.text) {
|
|
|
|
// Set a text label in the area
|
2013-07-15 22:53:59 +02:00
|
|
|
bbox = elem.mapElem.getBBox();
|
2013-07-29 22:30:16 +02:00
|
|
|
textPosition = $.fn.mapael.getTextPosition(bbox, options.textPosition);
|
|
|
|
options.textAttrs['text-anchor'] = textPosition.textAnchor;
|
|
|
|
elem.textElem = paper.text(textPosition.x, textPosition.y, options.text).attr(options.textAttrs);
|
2013-07-15 22:53:59 +02:00
|
|
|
|
2013-07-29 22:30:16 +02:00
|
|
|
options.attrs.href && (elem.textElem.attr({href: options.attrs.href}));
|
|
|
|
$.fn.mapael.setHoverOptions(elem.textElem, options.textAttrs, options.textAttrsHover);
|
2013-07-15 22:53:59 +02:00
|
|
|
$.fn.mapael.setHover(paper, elem.mapElem, elem.textElem);
|
2013-07-29 22:30:16 +02:00
|
|
|
$.fn.mapael.setCallbacks(options, elem.mapElem, elem.textElem);
|
2013-07-15 22:53:59 +02:00
|
|
|
} else {
|
|
|
|
$.fn.mapael.setHover(paper, elem.mapElem);
|
2013-07-29 22:30:16 +02:00
|
|
|
$.fn.mapael.setCallbacks(options, elem.mapElem);
|
2013-07-15 22:53:59 +02:00
|
|
|
}
|
|
|
|
|
2013-07-29 22:30:16 +02:00
|
|
|
if (options.tooltip && options.tooltip.content) {
|
|
|
|
elem.mapElem.tooltipContent = options.tooltip.content;
|
2013-07-15 22:53:59 +02:00
|
|
|
$.fn.mapael.setTooltip(elem.mapElem, $tooltip);
|
|
|
|
|
2013-07-29 22:30:16 +02:00
|
|
|
if (options.text) {
|
|
|
|
elem.textElem.tooltipContent = options.tooltip.content;
|
2013-07-15 22:53:59 +02:00
|
|
|
$.fn.mapael.setTooltip(elem.textElem, $tooltip);
|
|
|
|
}
|
|
|
|
}
|
2013-07-29 22:30:16 +02:00
|
|
|
|
|
|
|
if (options.value)
|
|
|
|
elem.value = options.value;
|
2013-07-15 22:53:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-07-29 22:30:16 +02:00
|
|
|
* Set a tooltip for the areas and plots
|
|
|
|
* @param elem area or plot element
|
|
|
|
* @param $tooltip the tooltip container
|
|
|
|
* @param content the content to set in the tooltip
|
2013-07-15 22:53:59 +02:00
|
|
|
*/
|
2013-07-29 22:30:16 +02:00
|
|
|
$.fn.mapael.setTooltip = function(elem, $tooltip) {
|
|
|
|
var tooltipTO = 0;
|
|
|
|
|
|
|
|
$(elem.node).on("mouseover", function() {
|
|
|
|
tooltipTO = setTimeout(function() {$tooltip.html(elem.tooltipContent).css("display", "block");}, 120);
|
|
|
|
}).on("mouseout", function() {
|
|
|
|
clearTimeout(tooltipTO);
|
|
|
|
$tooltip.css("display", "none");
|
|
|
|
}).on("mousemove", function(e) {
|
|
|
|
$tooltip.css("left", e.pageX + 15).css("top", e.pageY + 15 - $(window).scrollTop());
|
|
|
|
});
|
|
|
|
};
|
2013-07-15 22:53:59 +02:00
|
|
|
|
2013-06-23 18:00:13 +02:00
|
|
|
/**
|
|
|
|
* Set user defined callbacks on areas and plots
|
2013-07-29 22:30:16 +02:00
|
|
|
* @param elemOptions the element parameters
|
2013-06-23 18:00:13 +02:00
|
|
|
* @param mapElem the map element to set callback on
|
|
|
|
* @param textElem the optional text within the map element
|
|
|
|
*/
|
2013-07-29 22:30:16 +02:00
|
|
|
$.fn.mapael.setCallbacks = function(elemOptions, mapElem, textElem) {
|
2013-07-15 22:53:59 +02:00
|
|
|
var availableCallbacks = ['click', 'mouseover', 'mouseout']
|
|
|
|
, callbackFct = {};
|
|
|
|
|
|
|
|
for(var i = 0, length = availableCallbacks.length; i < length; ++i) {
|
2013-07-29 22:30:16 +02:00
|
|
|
if (elemOptions["on" + availableCallbacks[i]]) {
|
|
|
|
callbackFct = elemOptions["on" + availableCallbacks[i]];
|
|
|
|
$(mapElem.node).on(availableCallbacks[i], function() {callbackFct(elemOptions, mapElem, textElem)});
|
|
|
|
textElem && $(textElem.node).on(availableCallbacks[i], function() {callbackFct(elemOptions, mapElem, textElem)});
|
2013-07-15 22:53:59 +02:00
|
|
|
}
|
2013-06-23 18:00:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Draw a legend for areas and / or plots
|
|
|
|
* @param $container the legend container
|
|
|
|
* @param options map options
|
|
|
|
* @param legendType the type of the legend : 'area' or 'plot'
|
|
|
|
*/
|
2013-07-29 22:30:16 +02:00
|
|
|
$.fn.mapael.createLegend = function ($container, options, legendType, elems) {
|
|
|
|
var legendOptions = options.legend[legendType]
|
2013-07-15 22:53:59 +02:00
|
|
|
, $legend = (legendType == 'plot') ? $('.' + options.legend.plot.cssClass, $container).empty() : $('.' + options.legend.area.cssClass, $container).empty()
|
2013-06-23 18:00:13 +02:00
|
|
|
, paper = new Raphael($legend.get(0))
|
|
|
|
, width = 5
|
|
|
|
, height = 5
|
|
|
|
, title = {}
|
2013-07-29 22:30:16 +02:00
|
|
|
, defaultElemOptions = {}
|
2013-06-23 18:00:13 +02:00
|
|
|
, elem = {}
|
2013-07-15 22:53:59 +02:00
|
|
|
, label = {};
|
2013-06-23 18:00:13 +02:00
|
|
|
|
2013-07-29 22:30:16 +02:00
|
|
|
if(legendOptions.title) {
|
|
|
|
title = paper.text(legendOptions.marginLeftTitle, legendOptions.marginBottom, legendOptions.title)
|
|
|
|
.attr(legendOptions.titleAttrs);
|
2013-06-23 18:00:13 +02:00
|
|
|
|
2013-07-29 22:30:16 +02:00
|
|
|
width = legendOptions.marginLeftTitle + title.getBBox().width;
|
|
|
|
height += legendOptions.marginBottom + title.getBBox().height;
|
2013-06-23 18:00:13 +02:00
|
|
|
}
|
|
|
|
|
2013-07-29 22:30:16 +02:00
|
|
|
for(var i = 0, length = legendOptions.slices.length; i < length; ++i) {
|
|
|
|
defaultElemOptions = (legendType == 'plot') ? options.map['defaultPlot'] : options.map['defaultArea'];
|
|
|
|
legendOptions.slices[i].attrs = $.extend(
|
2013-06-23 18:00:13 +02:00
|
|
|
{}
|
2013-07-29 22:30:16 +02:00
|
|
|
, defaultElemOptions.attrs
|
|
|
|
, legendOptions.slices[i].attrs
|
2013-06-23 18:00:13 +02:00
|
|
|
);
|
2013-07-29 22:30:16 +02:00
|
|
|
legendOptions.slices[i].attrsHover = $.extend(
|
2013-06-23 18:00:13 +02:00
|
|
|
{}
|
2013-07-29 22:30:16 +02:00
|
|
|
, defaultElemOptions.attrsHover
|
|
|
|
, legendOptions.slices[i].attrsHover
|
2013-06-23 18:00:13 +02:00
|
|
|
);
|
|
|
|
|
2013-07-29 22:30:16 +02:00
|
|
|
if(legendType == 'area' || legendOptions.slices[i].type == "square") {
|
2013-06-23 18:00:13 +02:00
|
|
|
// Draw a square for squared plots AND areas
|
2013-07-29 22:30:16 +02:00
|
|
|
!legendOptions.slices[i].size && (legendOptions.slices[i].size = 20);
|
2013-06-23 18:00:13 +02:00
|
|
|
|
|
|
|
elem = paper.rect(
|
2013-07-29 22:30:16 +02:00
|
|
|
legendOptions.marginLeft
|
2013-06-23 18:00:13 +02:00
|
|
|
, height
|
2013-07-29 22:30:16 +02:00
|
|
|
, legendOptions.slices[i].size
|
|
|
|
, legendOptions.slices[i].size
|
|
|
|
).attr(legendOptions.slices[i].attrs);
|
2013-07-15 23:22:12 +02:00
|
|
|
} else {
|
|
|
|
elem = paper.circle(
|
2013-07-29 22:30:16 +02:00
|
|
|
legendOptions.marginLeft + legendOptions.slices[i].size / 2
|
|
|
|
, height + legendOptions.slices[i].size / 2
|
|
|
|
, legendOptions.slices[i].size / 2
|
|
|
|
).attr(legendOptions.slices[i].attrs);
|
2013-07-15 23:22:12 +02:00
|
|
|
}
|
2013-06-30 22:55:03 +02:00
|
|
|
|
2013-06-23 18:00:13 +02:00
|
|
|
label = paper.text(
|
2013-07-29 22:30:16 +02:00
|
|
|
legendOptions.marginLeft + legendOptions.slices[i].size + legendOptions.marginLeftLabel
|
|
|
|
, height + legendOptions.slices[i].size / 2
|
|
|
|
, legendOptions.slices[i].label
|
|
|
|
).attr(legendOptions.labelAttrs);
|
2013-06-23 18:00:13 +02:00
|
|
|
|
2013-07-29 22:30:16 +02:00
|
|
|
height += legendOptions.marginBottom + legendOptions.slices[i].size;
|
|
|
|
width = Math.max(width, legendOptions.marginLeft + legendOptions.slices[i].size + legendOptions.marginLeftLabel + label.getBBox().width);
|
2013-06-23 18:00:13 +02:00
|
|
|
|
2013-07-29 22:30:16 +02:00
|
|
|
if (legendOptions.hideElemsOnClick.enabled) {
|
|
|
|
// Hide/show elements when user clicks on a legend element
|
|
|
|
label.attr({cursor:'pointer'});
|
|
|
|
|
|
|
|
$.fn.mapael.setHoverOptions(elem, legendOptions.slices[i].attrs, legendOptions.slices[i].attrsHover);
|
|
|
|
$.fn.mapael.setHoverOptions(label, legendOptions.labelAttrs, legendOptions.labelAttrs);
|
|
|
|
$.fn.mapael.setHover(paper, elem, label);
|
|
|
|
|
|
|
|
label.hidden = false;
|
|
|
|
(function(i, elem, label) {
|
|
|
|
$(label.node).on('click', function() {
|
|
|
|
if (!label.hidden) {
|
|
|
|
label.animate({'opacity':legendOptions.hideElemsOnClick.opacity}, 300);
|
|
|
|
elem.animate({'opacity':legendOptions.hideElemsOnClick.opacity}, 300);
|
|
|
|
} else {
|
|
|
|
label.animate({'opacity':typeof label.originalAttrs.opacity != "undefined" ? label.originalAttrs.opacity : 1}, 300);
|
|
|
|
elem.animate({'opacity':typeof elem.originalAttrs.opacity != "undefined" ? elem.originalAttrs.opacity : 1}, 300);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var id in elems) {
|
|
|
|
if ((!legendOptions.slices[i].min || elems[id].value >= legendOptions.slices[i].min)
|
|
|
|
&& (!legendOptions.slices[i].max || elems[id].value < legendOptions.slices[i].max)
|
|
|
|
) {
|
|
|
|
if (!label.hidden) {
|
|
|
|
elems[id].mapElem.animate({'opacity':legendOptions.hideElemsOnClick.opacity}, 300);
|
|
|
|
elems[id].textElem && elems[id].textElem.animate({'opacity':legendOptions.hideElemsOnClick.opacity}, 300);
|
|
|
|
} else {
|
|
|
|
elems[id].mapElem.animate({'opacity':typeof elems[id].mapElem.originalAttrs.opacity != "undefined" ? elems[id].mapElem.originalAttrs.opacity : 1}, 300);
|
|
|
|
elems[id].textElem && elems[id].textElem.animate({'opacity':typeof elems[id].textElem.originalAttrs.opacity != "undefined" ? elems[id].textElem.originalAttrs.opacity : 1}, 300);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
label.hidden = !label.hidden;
|
|
|
|
});
|
|
|
|
})(i, elem, label);
|
|
|
|
}
|
2013-06-23 18:00:13 +02:00
|
|
|
}
|
2013-07-02 22:04:41 +02:00
|
|
|
|
|
|
|
// VMLWidth option allows you to set static width for the legend
|
|
|
|
// only for VML render because text.getBBox() returns wrong values on IE6/7
|
2013-07-29 22:30:16 +02:00
|
|
|
if (Raphael.type != 'SVG' && legendOptions.VMLWidth)
|
|
|
|
width = legendOptions.VMLWidth;
|
2013-07-02 22:04:41 +02:00
|
|
|
|
2013-07-29 22:30:16 +02:00
|
|
|
paper.setSize(width, height)
|
|
|
|
return paper;
|
2013-06-23 18:00:13 +02:00
|
|
|
}
|
2013-07-29 22:30:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the attributes on hover and the attributes to restore for a map element
|
|
|
|
* @param elem the map element
|
|
|
|
* @param originalAttrs the original attributes to restore on mouseout event
|
|
|
|
* @param attrsHover the attributes to set on mouseover event
|
|
|
|
*/
|
|
|
|
$.fn.mapael.setHoverOptions = function (elem, originalAttrs, attrsHover) {
|
|
|
|
// Disable transform option on hover for VML (IE<9) because of several bugs
|
|
|
|
if (Raphael.type != 'SVG') delete attrsHover.transform;
|
|
|
|
elem.attrsHover = attrsHover;
|
|
|
|
|
|
|
|
if (elem.attrsHover.transform) elem.originalAttrs = $.extend({transform : "s1"}, originalAttrs);
|
|
|
|
else elem.originalAttrs = originalAttrs;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the hover behavior (mouseover & mouseout) for plots and areas
|
|
|
|
* @param paper Raphael paper object
|
|
|
|
* @param mapElem the map element
|
|
|
|
* @param textElem the optional text element (within the map element)
|
|
|
|
*/
|
|
|
|
$.fn.mapael.setHover = function (paper, mapElem, textElem) {
|
|
|
|
var $mapElem = {}
|
|
|
|
, $textElem = {}
|
|
|
|
, hoverTO = 0
|
|
|
|
, overBehaviour = function() {hoverTO = setTimeout(function () {$.fn.mapael.elemHover(paper, mapElem, textElem);}, 120);}
|
|
|
|
, outBehaviour = function () {clearTimeout(hoverTO);$.fn.mapael.elemOut(paper, mapElem, textElem);};
|
|
|
|
|
|
|
|
$mapElem = $(mapElem.node);
|
|
|
|
$mapElem.on("mouseover", overBehaviour);
|
|
|
|
$mapElem.on("mouseout", outBehaviour);
|
|
|
|
|
|
|
|
if (textElem) {
|
|
|
|
$textElem = $(textElem.node);
|
|
|
|
$textElem.on("mouseover", overBehaviour);
|
|
|
|
$(textElem.node).on("mouseout", outBehaviour);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-06-23 18:00:13 +02:00
|
|
|
/**
|
2013-06-30 22:55:03 +02:00
|
|
|
* Set he behaviour for 'mouseover' event
|
2013-06-23 18:00:13 +02:00
|
|
|
* @param paper paper Raphael paper object
|
|
|
|
* @param mapElem mapElem the map element
|
|
|
|
* @param textElem the optional text element (within the map element)
|
|
|
|
*/
|
2013-07-29 22:30:16 +02:00
|
|
|
$.fn.mapael.elemHover = function (paper, mapElem, textElem) {
|
2013-07-15 22:53:59 +02:00
|
|
|
mapElem.animate(mapElem.attrsHover, mapElem.attrsHover.animDuration);
|
|
|
|
textElem && textElem.animate(textElem.attrsHover, textElem.attrsHover.animDuration);
|
2013-07-02 22:04:41 +02:00
|
|
|
paper.safari();
|
2013-06-23 18:00:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-06-30 22:55:03 +02:00
|
|
|
* Set he behaviour for 'mouseout' event
|
2013-06-23 18:00:13 +02:00
|
|
|
* @param paper Raphael paper object
|
|
|
|
* @param mapElem the map element
|
|
|
|
* @param textElem the optional text element (within the map element)
|
|
|
|
*/
|
2013-07-29 22:30:16 +02:00
|
|
|
$.fn.mapael.elemOut = function (paper, mapElem, textElem) {
|
2013-07-15 22:53:59 +02:00
|
|
|
mapElem.animate(mapElem.originalAttrs, mapElem.attrsHover.animDuration);
|
|
|
|
textElem && textElem.animate(textElem.originalAttrs, textElem.attrsHover.animDuration);
|
2013-06-23 18:00:13 +02:00
|
|
|
paper.safari();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2013-07-29 22:30:16 +02:00
|
|
|
* Get element options by merging default options, element options and legend options
|
|
|
|
* @param defaultOptions
|
|
|
|
* @param elemOptions
|
|
|
|
* @param legendOptions
|
2013-06-23 18:00:13 +02:00
|
|
|
*/
|
2013-07-29 22:30:16 +02:00
|
|
|
$.fn.mapael.getElemOptions = function(defaultOptions, elemOptions, legendOptions) {
|
|
|
|
var options = $.extend(true, {}, defaultOptions, elemOptions);
|
|
|
|
if (options.value) {
|
|
|
|
$.extend(true, options, $.fn.mapael.getLegendSlice(options.value, legendOptions));
|
|
|
|
}
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the coordinates of the text relative to a bbox and a position
|
|
|
|
* @param bbox the boundary box of the element
|
|
|
|
* @param textPosition the wanted text position (inner, right, left, top or bottom)
|
|
|
|
*/
|
|
|
|
$.fn.mapael.getTextPosition = function(bbox, textPosition) {
|
|
|
|
var textX = 0
|
|
|
|
, textY = 0
|
|
|
|
, textAnchor = '';
|
2013-07-15 22:53:59 +02:00
|
|
|
|
2013-07-29 22:30:16 +02:00
|
|
|
switch (textPosition) {
|
|
|
|
case 'bottom' :
|
|
|
|
textX = (bbox.x + bbox.x2) / 2;
|
|
|
|
textY = bbox.y2 + 15;
|
|
|
|
textAnchor = "middle";
|
|
|
|
break;
|
|
|
|
case 'top' :
|
|
|
|
textX = (bbox.x + bbox.x2) / 2;
|
|
|
|
textY = bbox.y - 15;
|
|
|
|
textAnchor = "middle";
|
|
|
|
break;
|
|
|
|
case 'left' :
|
|
|
|
textX = bbox.x - 10;
|
|
|
|
textY = (bbox.y + bbox.y2) / 2;
|
|
|
|
textAnchor = "end";
|
|
|
|
break;
|
|
|
|
case 'right' :
|
|
|
|
textX = bbox.x2 + 10;
|
|
|
|
textY = (bbox.y + bbox.y2) / 2;
|
|
|
|
textAnchor = "start";
|
|
|
|
break;
|
|
|
|
default : // 'inner' position
|
|
|
|
textX = (bbox.x + bbox.x2) / 2;
|
|
|
|
textY = (bbox.y + bbox.y2) / 2;
|
|
|
|
textAnchor = "middle";
|
2013-06-23 18:00:13 +02:00
|
|
|
}
|
2013-07-29 22:30:16 +02:00
|
|
|
return {'x' : textX, 'y' : textY, 'textAnchor' : textAnchor};
|
|
|
|
}
|
2013-06-23 18:00:13 +02:00
|
|
|
|
|
|
|
/**
|
2013-07-29 22:30:16 +02:00
|
|
|
* Get the legend conf matching with the value
|
|
|
|
* @param value the value to match with a slice in the legend
|
|
|
|
* @param legend the legend params object
|
|
|
|
* @return the legend slice matching with the value
|
2013-06-23 18:00:13 +02:00
|
|
|
*/
|
2013-07-29 22:30:16 +02:00
|
|
|
$.fn.mapael.getLegendSlice = function (value, legend) {
|
|
|
|
for(var i = 0, length = legend.slices.length; i < length; ++i) {
|
|
|
|
if ((!legend.slices[i].min || value >= legend.slices[i].min)
|
|
|
|
&& (!legend.slices[i].max || value < legend.slices[i].max)
|
|
|
|
) {
|
|
|
|
return legend.slices[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {};
|
2013-06-23 18:00:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Default map options
|
|
|
|
$.fn.mapael.defaultOptions = {
|
2013-07-29 22:30:16 +02:00
|
|
|
map : {
|
|
|
|
cssClass : "map"
|
|
|
|
, tooltip : {
|
|
|
|
cssClass : "mapTooltip"
|
2013-06-23 18:00:13 +02:00
|
|
|
}
|
2013-07-29 22:30:16 +02:00
|
|
|
, defaultArea : {
|
|
|
|
attrs : {
|
|
|
|
fill : "#343434"
|
|
|
|
, stroke : "#5d5d5d"
|
|
|
|
, "stroke-width" : 1
|
|
|
|
, "stroke-linejoin" : "round"
|
2013-06-23 18:00:13 +02:00
|
|
|
}
|
2013-07-29 22:30:16 +02:00
|
|
|
, attrsHover : {
|
|
|
|
fill : "#f38a03"
|
2013-06-23 18:00:13 +02:00
|
|
|
, animDuration : 300
|
|
|
|
}
|
2013-07-29 22:30:16 +02:00
|
|
|
, textPosition : 'inner'
|
|
|
|
, textAttrs : {
|
|
|
|
"font-size" : 15
|
|
|
|
, fill : "#c7c7c7"
|
2013-06-23 18:00:13 +02:00
|
|
|
}
|
2013-07-29 22:30:16 +02:00
|
|
|
, textAttrsHover : {
|
|
|
|
fill : "#eaeaea"
|
2013-06-23 18:00:13 +02:00
|
|
|
, "animDuration" : 300
|
|
|
|
}
|
|
|
|
}
|
2013-07-29 22:30:16 +02:00
|
|
|
, defaultPlot : {
|
|
|
|
type : "circle"
|
|
|
|
, size : 15
|
|
|
|
, attrs : {
|
|
|
|
fill : "#0088db"
|
|
|
|
, stroke : "#fff"
|
|
|
|
, "stroke-width" : 0
|
|
|
|
, "stroke-linejoin" : "round"
|
2013-06-23 18:00:13 +02:00
|
|
|
}
|
2013-07-29 22:30:16 +02:00
|
|
|
, attrsHover : {
|
|
|
|
"stroke-width" : 3
|
2013-06-23 18:00:13 +02:00
|
|
|
, animDuration : 300
|
|
|
|
}
|
2013-07-29 22:30:16 +02:00
|
|
|
, textPosition : 'right'
|
|
|
|
, textAttrs : {
|
|
|
|
"font-size" : 15
|
|
|
|
, fill : "#c7c7c7"
|
2013-06-23 18:00:13 +02:00
|
|
|
},
|
2013-07-29 22:30:16 +02:00
|
|
|
textAttrsHover : {
|
|
|
|
fill : "#eaeaea"
|
2013-06-23 18:00:13 +02:00
|
|
|
, animDuration : 300
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-29 22:30:16 +02:00
|
|
|
, legend : {
|
|
|
|
area : {
|
|
|
|
cssClass : "areaLegend"
|
|
|
|
, display : false
|
|
|
|
, marginLeft : 15
|
|
|
|
, marginLeftTitle : 5
|
|
|
|
, marginLeftLabel : 10
|
|
|
|
, marginBottom : 15
|
|
|
|
, titleAttrs : {
|
2013-06-23 18:00:13 +02:00
|
|
|
"font-size" : 18
|
|
|
|
, fill : "#343434"
|
|
|
|
, "text-anchor" : "start"
|
|
|
|
}
|
2013-07-29 22:30:16 +02:00
|
|
|
, labelAttrs : {
|
2013-06-23 18:00:13 +02:00
|
|
|
"font-size" : 15
|
|
|
|
, fill : "#343434"
|
|
|
|
, "text-anchor" : "start"
|
|
|
|
}
|
2013-07-29 22:30:16 +02:00
|
|
|
, hideElemsOnClick : {
|
|
|
|
enabled : true
|
|
|
|
, opacity : 0.2
|
|
|
|
}
|
2013-06-23 18:00:13 +02:00
|
|
|
, slices : []
|
2013-07-15 22:53:59 +02:00
|
|
|
}
|
2013-07-29 22:30:16 +02:00
|
|
|
, plot : {
|
|
|
|
cssClass : "plotLegend"
|
|
|
|
, display : false
|
|
|
|
, marginLeft : 15
|
|
|
|
, marginLeftTitle : 5
|
|
|
|
, marginLeftLabel : 10
|
|
|
|
, marginBottom : 15
|
|
|
|
, titleAttrs : {
|
2013-06-23 18:00:13 +02:00
|
|
|
"font-size" : 18
|
|
|
|
, fill : "#343434"
|
|
|
|
, "text-anchor" : "start"
|
|
|
|
}
|
2013-07-29 22:30:16 +02:00
|
|
|
, labelAttrs : {
|
2013-06-23 18:00:13 +02:00
|
|
|
"font-size" : 15
|
|
|
|
, fill : "#343434"
|
|
|
|
, "text-anchor" : "start"
|
|
|
|
}
|
2013-07-29 22:30:16 +02:00
|
|
|
, hideElemsOnClick : {
|
|
|
|
enabled : true
|
|
|
|
, opacity : 0.2
|
|
|
|
}
|
2013-06-23 18:00:13 +02:00
|
|
|
, slices : []
|
|
|
|
}
|
|
|
|
}
|
2013-07-29 22:30:16 +02:00
|
|
|
, areas : {}
|
|
|
|
, plots : {}
|
2013-06-23 18:00:13 +02:00
|
|
|
};
|
|
|
|
})(jQuery);
|