简体   繁体   中英

Can't render region labels as HTML with jVectorMap

I wrote my code down to a small fiddle: http://jsfiddle.net/V8dyd/266/

I am able to render region labels as strings on the map but unable to render them as HTML.

$(function(){
    var Jsondata = {
        LK: " Region :  Asia,  Feedback : 228, Good : 34.00%, Normal : 33.00%, Bad : 30.00% ",
        IN: "Total Responses : 228"
    };

    var map = $('#map').vectorMap({
        map: 'world_mill_en',
        zoomMin: 1,
        zoomMax: 1,
        regionLabelStyle: {
            initial: {
                fill: '#B90E32'
            },
            hover: {
                fill: 'black'
            }
        },
        labels: {
            regions: {
                render: function (code) {
                    if (code==="LK") {  
                        var a = Jsondata[code];
                        var array = a.split(',');
                        var s = "<html><body><div><small>" + array[0]  +"</small><br><small>" + array[1] + "</small><br><small>" + array[2] + "</small><br><small>" + array[3] + "</small></div</body></html>";
                        var htmlObject = document.createElement('div');
                        htmlObject.innerHTML = s;
                        return htmlObject;
                        // return  s
                    }
                }
            }
        }
    });
});

The labels are rendered using svg TEXT-Tags and do not allow HTML. In the current version this is not supported but i used a Hook in the Region Function for realize line breaks: https://github.com/bjornd/jvectormap/blob/master/src/region.js

Like this:

 jvm.Region = function(config){
      var bbox,
          text,
          offsets,
          labelDx,
          labelDy;

      this.config = config;
      this.map = this.config.map;

      this.shape = config.canvas.addPath({
        d: config.path,
        'data-code': config.code
      }, config.style, config.canvas.rootElement);
      this.shape.addClass('jvectormap-region jvectormap-element');

      bbox = this.shape.getBBox();

      myHook(bbox, text, offsets, config);
      return false;
      ...
 }

 function myHook(bbox, text, offsets, config){
     // Create your own Label like this
     text = this.getLabelText(config.code);
     if (this.config.label && text) {
        offsets = this.getLabelOffsets(config.code);
        this.labelX = bbox.x + bbox.width / 2 + offsets[0];
        this.labelY = bbox.y + bbox.height / 2 + offsets[1];
        this.label = config.canvas.addText({
            text: text,
            'text-anchor': 'middle',
            'alignment-baseline': 'central',
            x: this.labelX,
            y: this.labelY,
            'data-code': config.code
        }, config.labelStyle, config.labelsGroup);
        this.label.addClass('jvectormap-region jvectormap-element');
    } 
 }

Thats not the best way but maybe it helps you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM