/**
* Represents map legend.
* @constructor
* @param {Object} params Configuration parameters.
* @param {String} params.cssClass Additional CSS class to apply to legend element.
* @param {Boolean} params.vertical If true
legend will be rendered as vertical.
* @param {String} params.title Legend title.
* @param {Function} params.labelRender Method to convert series values to legend labels.
*/
jvm.Legend = function(params) {
this.params = params || {};
this.map = this.params.map;
this.series = this.params.series;
this.body = jvm.$('
');
this.body.addClass('jvectormap-legend');
if (this.params.cssClass) {
this.body.addClass(this.params.cssClass);
}
if (params.vertical) {
this.map.legendCntVertical.append( this.body );
} else {
this.map.legendCntHorizontal.append( this.body );
}
this.render();
}
jvm.Legend.prototype.render = function(){
var ticks = this.series.scale.getTicks(),
i,
inner = jvm.$('').addClass('jvectormap-legend-inner'),
tick,
sample,
label;
this.body.html('');
if (this.params.title) {
this.body.append(
jvm.$('').addClass('jvectormap-legend-title').html(this.params.title)
);
}
this.body.append(inner);
for (i = 0; i < ticks.length; i++) {
tick = jvm.$('').addClass('jvectormap-legend-tick');
sample = jvm.$('').addClass('jvectormap-legend-tick-sample');
switch (this.series.params.attribute) {
case 'fill':
if (jvm.isImageUrl(ticks[i].value)) {
sample.css('background', 'url('+ticks[i].value+')');
} else {
sample.css('background', ticks[i].value);
}
break;
case 'stroke':
sample.css('background', ticks[i].value);
break;
case 'image':
sample.css('background', 'url('+ticks[i].value+') no-repeat center center');
break;
case 'r':
jvm.$('').css({
'border-radius': ticks[i].value,
border: this.map.params.markerStyle.initial['stroke-width']+'px '+
this.map.params.markerStyle.initial['stroke']+' solid',
width: ticks[i].value * 2 + 'px',
height: ticks[i].value * 2 + 'px',
background: this.map.params.markerStyle.initial['fill']
}).appendTo(sample);
break;
}
tick.append( sample );
label = ticks[i].label;
if (this.params.labelRender) {
label = this.params.labelRender(label);
}
tick.append( jvm.$(''+label+'
').addClass('jvectormap-legend-tick-text') );
inner.append(tick);
}
inner.append( jvm.$('').css('clear', 'both') );
}