简体   繁体   English

从下拉列表中选择路径元素的值时,如何更改其颜色

[英]How do I change color of path element when its value is selected from dropdown

I am making a Sunburst, I have a drop down menu from which I can select name of the countries.我正在制作森伯斯特,我有一个下拉菜单,我可以从中选择国家/地区名称。 The sunburst represents values of these countries.旭日代表这些国家的价值观。 But the drop down and sunburst are disconnected.但是下拉和森伯斯特是断开的。

Requirements - What I want to achieve is when I select the name of country from drop down all of the paths in sunburst change to grey except for the path representing the country.要求- 我想要实现的是当我从下拉菜单中选择国家名称时,除了代表国家的路径之外,旭日形中的所有路径都变为灰色。

Problem - I tried to achieve this by my code but it changes all of the sunburst to grey.问题- 我试图通过我的代码来实现这一点,但它把所有的森伯斯特都变成了灰色。

Fiddle of my code:小提琴我的代码:

CODE :代码

var width = 960,
    height = 700,
    radius = Math.min(width, height) / 2;
var b = {
  w: 130, h: 30, s: 3, t: 10
};

var x = d3.scale.linear()
    .range([0, 2 * Math.PI]);

var y = d3.scale.sqrt()
    .range([0, radius]);
var changeArray = [100,80,60,0, -60, -80, -100];
var colorArray = ["#67000d", "#b73e43", "#d5464a", "#f26256", "#fb876e", "#fca78e", "#fcbba1", "#fee0d2", "#fff5f0"];

var svg = d3.select("#diagram").append("svg")
    .attr("width", width)
    .attr("height", height)
  .append("g")
    .attr("transform", "translate(" + width / 2 + "," + (height / 2 + 10) + ")");

var partition = d3.layout.partition()
                  .value(function(d) { return d.Total; });

var arc = d3.svg.arc()
            .startAngle(function(d) { return Math.max(0, Math.min(2 * Math.PI, x(d.x))); })
            .endAngle(function(d) { return Math.max(0, Math.min(2 * Math.PI, x(d.x + d.dx))); })
            .innerRadius(function(d) { return Math.max(0, y(d.y)); })
            .outerRadius(function(d) { return Math.max(0, y(d.y + d.dy)); });
            console.log(arc)

    function checkIt(error, root){
      initializeBreadcrumbTrail();

      //intilaize dropdown
      if (error) throw error;

      var g = svg.selectAll("g")
             .data(partition.nodes(root))
            .enter().append("g");

     var path = g.append("path")
    .attr("d", arc)
    .style("fill", function(d) { var color;
                if(d.Total_Change > 200)
                             {color = colorArray[0]

                         }
                else if(d.Total_Change > 150 && d.Total_Change < 100)
                             {color = colorArray[1]

                         }
                else if(d.Total_Change > 100 && d.Total_Change < 80)
                             {color = colorArray[1]

                         }
                else if(d.Total_Change < 80 && d.Total_Change > 60 )
                 {
                    color = colorArray[2]
                     }
                   else if(d.Total_Change < 60 && d.Total_Change > 20 )
                 {
                    color = colorArray[3]
                     }
                   else if(d.Total_Change < 20 && d.Total_Change > 0 )
                 {
                    color = colorArray[4]
                     }
                   else if(d.Total_Change < 0 && d.Total_Change > -60)
                 {
                    color = colorArray[5]
                     }
                   else if(d.Total_Change < -60 && d.Total_Change > -80)
                 {
                    color = colorArray[6]
                     }
                   else
                 {
                    color = colorArray[7]
                     }
                return color})
    .on("click", click)
    .on("mouseover", mouseover);

    var tooltip = d3.select("body")
                     .append("div")
                     .attr("id", "tooltips")
                     .style("position", "absolute")
                     .style("background-color", "#fff")
                     .style("z-index", "10")
                     .style("visibility", "hidden");
      g.on("mouseover", function(d){return tooltip.style("visibility", "visible")
                                                   .html("<div class=" + "tooltip_container>"+"<h4 class=" + "tooltip_heading>" +d.name.replace(/[_-]/g, " ") +"</h4>" +"<br>" + "<p> Emissions 2013:" + " " + "<br>" + d.Total + " " +"<span>in Million Tons</span></p>"+ "<br>"+ "<p> Change in Emissions: <span>" + (d.Total_Change/d.Total*100).toPrecision(3) + "%" + "</span></p>"+"</div>" );})
       .on("mousemove", function(){return tooltip.style("top",(d3.event.pageY-10)+"px").style("left",(d3.event.pageX+10)+"px");})
       .on("mouseout", function(){return tooltip.style("visibility", "hidden");});

 //creating a dropdown

    var dropDown = d3.select("#dropdown_container")
                   .append("select")
                   .attr("class", "selection")
                    .attr("name", "country-list");

    var nodeArr = partition.nodes(root);
    var options = dropDown.selectAll("option")
                  .data(nodeArr)
                  .enter()
                  .append("option");

    options.text(function (d) {
            var prefix = new Array(d.depth + 1);
            var dropdownValues =  d.name.replace(/[_-]/g, " ");
            return dropdownValues;
        }).attr("value", function (d) {
            var dropdownValues = d.name;
            return dropdownValues;
        });

    d3.select(".selection").on("change", function changePie() {
        var value = this.value;
        var index = this.selectedIndex;
        var dataObj = nodeArr[index];
        console.log(this.value + " : " + dataObj["Iron and steel"] + " in " + (dataObj.parent && dataObj.parent.name));
    });


// transition on click
  function click(d) {
    // fade out all text elements

    path.transition()
      .duration(750)
      .attrTween("d", arcTween(d))
      .each("end", function(e, i) {
          // check if the animated element's data e lies within the visible angle span given in d
          if (e.x >= d.x && e.x < (d.x + d.dx)) {
            // get a selection of the associated text element
            var arcText = d3.select(this.parentNode).select("text");
            // fade in the text element and recalculate positions
            arcText.transition().duration(750)
              .attr("opacity", 1)
              .attr("transform", function() { return "rotate(" + computeTextRotation(e) + ")" })
              .attr("x", function(d) { return y(d.y); });
          }
      });
    };


};

d3.json("https://gist.githubusercontent.com/heenaI/cbbc5c5f49994f174376/raw/55c672bbca7991442f1209cfbbb6ded45d5e8c8e/data.json", checkIt);

d3.select(self.frameElement).style("height", height + "px");

// Interpolate the scales!
function arcTween(d) {
  var xd = d3.interpolate(x.domain(), [d.x, d.x + d.dx]),
      yd = d3.interpolate(y.domain(), [d.y, 1]),
      yr = d3.interpolate(y.range(), [d.y ? 20 : 0, radius]);
  return function(d, i) {
    return i
        ? function(t) { return arc(d); }
        : function(t) { x.domain(xd(t)); y.domain(yd(t)).range(yr(t)); return arc(d); };
  };
}

function initializeBreadcrumbTrail() {
  // Add the svg area.
  var trail = d3.select("#sequence").append("svg:svg")
      .attr("width", width)
      .attr("height", 50)
      .attr("id", "trail");
  // Add the label at the end, for the percentage.
  trail.append("svg:text")
    .attr("id", "endlabel")
    .style("fill", "#000");
}

function breadcrumbPoints(d, i) {
  var points = [];
  points.push("0,0");
  points.push(b.w + ",0");
  points.push(b.w + b.t + "," + (b.h / 2));
  points.push(b.w + "," + b.h);
  points.push("0," + b.h);
  if (i > 0) { // Leftmost breadcrumb; don't include 6th vertex.
    points.push(b.t + "," + (b.h / 2));
  }
  return points.join(" ");
}

// Update the breadcrumb trail to show the current sequence and percentage.
function updateBreadcrumbs(nodeArray) {

  // Data join; key function combines name and depth (= position in sequence).
  var g = d3.select("#trail")
      .selectAll("g")
      .data(nodeArray, function(d) { return d.name.replace(/[_-]/g, " ") + d.Total; });

  // Add breadcrumb and label for entering nodes.
  var entering = g.enter().append("svg:g");

  entering.append("svg:polygon")
      .attr("points", breadcrumbPoints)
      .style("fill", "#d3d3d3");

  entering.append("svg:text")
      .attr("x", (b.w + b.t) / 2)
      .attr("y", b.h / 2)
      .attr("dy", "0.35em")
      .attr("text-anchor", "middle")
      .text(function(d) { return d.name.replace(/[_-]/g, " "); });

  // Set position for entering and updating nodes.
  g.attr("transform", function(d, i) {
    return "translate(" + i * (b.w + b.s) + ", 0)";
  });

  // Remove exiting nodes.
  g.exit().remove();

  // Now move and update the percentage at the end.
  d3.select("#trail").select("#endlabel")
      .attr("x", (nodeArray.length + 0.5) * (b.w + b.s))
      .attr("y", b.h / 2)
      .attr("dy", "0.35em")
      .attr("text-anchor", "middle");

  // Make the breadcrumb trail visible, if it's hidden.
  d3.select("#trail")
      .style("visibility", "");
}
function getAncestors(node) {
  var path = [];
  var current = node;
  while (current.parent) {
    path.unshift(current);
    current = current.parent;
  }
  return path;
}

function mouseover(d) {

var sequenceArray = getAncestors(d);
updateBreadcrumbs(sequenceArray);}

The part that presents the dropdown and changes in the dropdown where I would like to change fill: /creating a dropdown显示下拉列表的部分以及我想更改填充的下拉列表中的更改:/创建下拉列表

  var dropDown = d3.select("#dropdown_container")
                   .append("select")
                   .attr("class", "selection")
                    .attr("name", "country-list");

    var nodeArr = partition.nodes(root);
    var options = dropDown.selectAll("option")
                  .data(nodeArr)
                  .enter()
                  .append("option");

    options.text(function (d) {
            var prefix = new Array(d.depth + 1);
            var dropdownValues =  d.name.replace(/[_-]/g, " ");
            return dropdownValues;
        }).attr("value", function (d) {
            var dropdownValues = d.name;
            return dropdownValues;
        });

    d3.select(".selection").on("change", function changePie() {
        var value = this.value;
        var index = this.selectedIndex;
        var dataObj = nodeArr[index];
        console.log(this.value + " : " + dataObj["Iron and steel"] + " in " + (dataObj.parent && dataObj.parent.name));
    });

To do this first store the color inside the data like this:为此,首先将颜色存储在数据中,如下所示:

                 {
                    color = colorArray[6]
                     }
                   else
                 {
                    color = colorArray[7]
                     }
                d.color = color; //store the color inside the data

On select do在选择做

d3.select(".selection").on("change", function changePie() {
    var value = this.value;
    var index = this.selectedIndex;
    var dataObj = nodeArr[index];
    path.style("fill", "gray");//make all the path gray
    path[0].forEach(function(p){
        var data = d3.select(p).data();//get the data from the path
        if (data[0].name === value){
            d3.select(p).style("fill", data[0].color);//set the color from the data stored before

        }
    });
    console.log(this.value + " :c " + dataObj["Iron and steel"] + " in " + (dataObj.parent && dataObj.parent.name));
});

Working code here工作代码在这里

In case you wish to see the full path highlighted from child to parent check here如果您希望查看从孩子到父母的完整路径,请在此处查看

Hope this helps!希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将表单元素的值更改为所选元素的ID? - How do I change the value of a form element to the ID of a selected element? 如何在更改下拉列表中选择的值时更改数据背景颜色 - How to change the data-backgound-color on change on the value selected in the dropdown 选择下拉列表中的值时,如何完成自动填充? - How to do autocomplete when a value from dropdown is selected? 从下拉菜单中选择银行时,如何回显值(价格)? - How do I echo a value (a price) when a bank is selected from a dropdown menu? Knockoutjs:更改下拉列表中所选值的颜色 - Knockoutjs: Change the color of selected value in dropdown list 选择下拉菜单中的某个元素时,单击按钮时更改文本 - Change text on button click when a certain element from a dropdown is selected 如何根据所选下拉列表从SQL更改下拉列表值 - how to change dropdown value take from sql based on selected dropdown 如何更改按钮的颜色,以便在使用jQuery单击按钮时将其更改为另一种颜色? - How do I change the color of a button so that it changes to another color when its clicked using jQuery? 使用角度,如何在下拉列表中获取ng-change上的选定值? - Using angular, How do I get selected value on ng-change on a dropdown? 如果选择的值不是第一个值,如何自动提交下拉列表? - How do I auto submit a dropdown when a value is selected other than the first value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM