2016-03-25 77 views
1

カラーブロック、その色に対応する名前、および色を変更するドロップダウンを示す凡例を作成しようとしています。ここに私のコードです:要素がD3に追加されたときに表示されない

function create_legend(){ 
    var legend = legendSVG.selectAll("g.legend") 
    .data(ext_color_domain) 
    .enter().append("g") 
    .attr("class", "legend"); 

    var ls_w = 20, ls_h = 20; 

    //create color blocks 
    legend.append("rect") 
    .attr("x", 20) 
    .attr("y", function(d, i){ return height - (i*ls_h) - 2*ls_h;}) 
    .attr("width", ls_w) 
    .attr("height", ls_h) 
    .style("fill", function(d, i) { return color_scale[i]; }) 
    .style("opacity", 0.8); 

    //create text 
    legend.append("text") 
    .attr("x", 50) 
    .attr("y", function(d, i){ return height - (i*ls_h) - ls_h - 4;}) 
    .text(function(d, i){ return segment_map[i]; }); 

    //create dropdown for colors 
    legend.append("div") 
    .append("select") 
    .attr("x", 80) 
    .attr("y", function(d, i){ return height - (i*ls_h) - ls_h - 4;}) 
    .selectAll("option") 
    .data(color_names) 
    .enter().append("option") 
    .attr("value", function (d) { return d; }) 
    .text(function (d) { return d; });  

} 

色&テキストが表示されますが、ドロップダウン要素にはありません。

enter image description here

更新:[OK]を、私は次の操作を実行しようとしたが、それは私にエラー「キャッチされないでSyntaxError:予期しないトークン」を与えるSVGで

'//create dropdown for colors 
    legend.append("foreignObject") 
    .attr("class", ".dropdown") 
    .append("div") 
    .append("select") 
    .attr("x", 80) 
    .attr("y", function(d, i){ return height - (i*ls_h) - ls_h - 4;}) 
    .selectAll("option") 
    .data(color_names) 
    .enter().append("option") 
    .attr("value", function (d) { return d; }) 
    .text(function (d) { return d; });' 
+0

はOK、私はそれを読んhttp://stackoverflow.com/questions/7458546/html-in-svg-in-html –

+0

を参照してください。それは私がしようとしていることは不可能だということですか?または可能ですか?それは本当にそのリンクから明らかではなかった... – NBC

+0

。後に追加した後に(無効です –

答えて

0

html要素のみの子であることが可能にforeignObject要素とforeignObject要素は、テキスト要素の子にすることはできません。 foreignObject要素はテキスト要素の兄弟ですが、DOMはこのように見える必要があります。異物のために

g 
|- rect 
|- text 
|- foreignObject 
     |- div 
+0

更新された記事を見る – NBC

0

あなたは、このように要素を追加する必要があります。 .append("xhtml:div")の代わり.append("div")

は、完全なスニペットを修正しました。

legend.append("foreignObject") 
    .attr("class", ".dropdown") 
    .append("xhtml:div") 
    .append("xhtml:select") 
    .attr("width", 80) 

    .attr("x",function(d, i){ return width * i}) 
    .attr("y", function(d, i){ return height - (i*ls_h) - ls_h - 4;}) 
    .selectAll("option") 
    .data(color_names) 
    .enter().append("xhtml:option") 
    .attr("value", function (d) { return d; }) 
    .text(function (d) { return d; } 
関連する問題