0

I have an SVG on which I want to add some labels. With my code the labels somehow do not appear on the SVG while I can see them through console.

fiddle example

Current code:

var debug = 0;
var debugWarning = "";
var changeArray = [-60,-40,-20,0,20,40,60];
/*var colorArray = ["#333366", "#3366CC", "#66CCFF", "#C1EAFF", "#EEEEEE", "#FFCC33", "#FF9900", "#FF6600", "#CC0033", "#990033"];*/
var colorArray = ["#009870", "#36b18c", "#99cfb8", "#ffffbf", "#f39a93", "#e95058", "#e4003a"];

var colorDark2S = new Array();
var colorDark4S = new Array();
var invariant = 0;

var dataset;
    function checkIt(data){
     var data_clean = []; //prepared variable for clear data
    for (var i in data) {
      data_clean.push(data[i].Change_total);
      }

       var countriesByName = d3.nest()
        .key(function (d) {
        return d.Country_Names;
    })
        .entries(data);

// creating dropdown
    var data = JSON.stringify(countriesByName)
    var data = JSON.parse(data);



  //move initialisation here, to be sure dataset is there
  var main_svg =  d3.select("#level_0")
                 .selectAll("path")

  var svg =     main_svg.data(data_clean)
               .attr("stroke", "#888888")
               .attr("stroke-width", "1px")
               .attr("stroke-linejoin","round")
               .attr("stroke-linecap","round")
               .style("fill", function fill(data_clean){
                var color;
                if(data_clean < changeArray[0])
                             {color = colorArray[0]
                           colorDark4S[invariant] = true;}
                else {
                if(data_clean < changeArray[1])
                   {
                    color = colorArray[1]
                    colorDark4S[invariant] = true;
                     } else {
                    if(data_clean < changeArray[2])
                        {
                    color = colorArray[2]
                    colorDark4S[invariant] = true;
                     } else {
                      if(data_clean < changeArray[3])

                     {color = colorArray[3]
                       } else {
                      if(data_clean < changeArray[4])
                        {
                       color = colorArray[4]
                      } else {
                      if(data_clean < changeArray[5])
                       {
                         color = colorArray[5]
                       } else  {
                    color = colorArray[6]
                    colorDark4S[invariant] = true;
                  }
                }
              }
            }
        }
      }
                return color
               })
 .on("mouseout", function () {
    d3.select(this)
    .style("stroke-width", "1px")
    .style("stroke", "#888888");
})
 .on("mouseover", function(d) {
           d3.select(this)
          .style("stroke-width", "3px")
          .style("stroke", "#000")
          .attr("stroke-linejoin","round")
          .attr("stroke-linecap","round");

            });

    textNodes = main_svg.selectAll("text").data(data);

    textNodes.enter().append( "text" )
        .attr( "class" , "label")
        .text( function( d , i ) {
            return d.key;
        } );

   foreignObjects = textNodes.enter().append("foreignObject");

  htmlDOMs = foreignObjects.append("xhtml:body")
    .style("margin",0)
    .style("padding",0)

htmlLabels = htmlDOMs.append("div")
    .attr("class","htmlLabel");

htmlLabels.append("p")
    .attr("class","labels")
    .text(function(d,i) { return d.key });


var zoom = d3.behavior.zoom()
    .scaleExtent([1, 10])
    .on("zoom", zoomed);

var main_svg1 = d3.select("#clipPart")
                .selectAll("path")
                .call(zoom);

var container = main_svg1;

function zoomed() {
  container.attr("transform", "translate(" + d3.event.translate + ")scale(" + d3.event.scale + ")");
}

}

function myJson(){d3.json("https://gist.githubusercontent.com/heenaI/cbbc5c5f49994f174376/raw/82cd19eff7db367193cf8ce00144a40ea8d140ac/data.json", checkIt);};

var svg_0 = d3.xml("https://gist.githubusercontent.com/heenaI/cbbc5c5f49994f174376/raw/bcfa09cbf6394e7ea2e87c0fcd0df636116aeb7d/picture.svg", "image/svg+xml", function(xml) {
    var importedNode = document.importNode(xml.documentElement, true);
    d3.select("#viz").node().appendChild(importedNode)

    myJson();
});

Chunk that adds labels to the svg

textNodes = main_svg.selectAll("text").data(data);

    textNodes.enter().append( "text" )
        .attr( "class" , "label")
        .text( function( d , i ) {
            return d.key;
        } );

   foreignObjects = textNodes.enter().append("foreignObject");

  htmlDOMs = foreignObjects.append("xhtml:body")
    .style("margin",0)
    .style("padding",0)

htmlLabels = htmlDOMs.append("div")
    .attr("class","htmlLabel");

htmlLabels.append("p")
    .attr("class","labels")
    .text(function(d,i) { return d.key });
Bennett McElwee
  • 24,740
  • 6
  • 54
  • 63
Imo
  • 1,455
  • 4
  • 28
  • 53
  • You should try to add a `textpath` after the `text` elements. See here some examples. http://stackoverflow.com/questions/16246186/d3-svg-and-textpath-how-to-move-the-text-down http://stackoverflow.com/questions/16164348/js-d3-textpath-isnt-displayed – kwoxer Nov 19 '15 at 21:47
  • @kwoxer still does not work with text path, fiddle link https://jsfiddle.net/n5v84svm/47/ – Imo Nov 20 '15 at 05:05
  • Well I cannot see the real issue right now without having a huge look into it. I can just say that you somehow add the `text` things to all of your pathes. That's definitily not intended and wrong! You should maybe start with a smaller example in smaller steps to figure out the real issue on that. Also use this basic example http://bl.ocks.org/mbostock/950642 I btw have done something similiar on my project http://arda-maps.org/ages/third/ maybe you can get it from there. But I excluded the text from the pathes completely e.g. so it's another approach. Hope it helps. – kwoxer Nov 20 '15 at 08:25

0 Answers0