编辑d3图表元素

时间:2016-05-08 13:45:52

标签: javascript css json d3.js sunburst-diagram

我从某个网站获得了这个旭日形图代码,我非常喜欢它,我想让它符合我的需求。它比我现在所做的要复杂得多,所以我不知道如何做出我想要的某些改变。我一直在谷歌的帮助下尝试各种各样的东西,但我无法让它发挥作用。有一些JSON文件需要完成,但这不是问题。 无论如何,我想做出以下更改:

1)所有文字都是180度,不要改变圆圈的程度

2)内圈的3个部分大小相同(蛋白质,Ugljikohidrati,Masti),然后当我们为内圈的下列元素(Biljni izvori,Zivotinjski izvori,Suplementi)输入一个(例如:蛋白质)时同样大小。所有内部部件始终具有相同的尺寸,直到我们到达最外部部分,该外部部分基于单个元件的值而被设计为不同的尺寸。我尝试将尺寸放在父母身上,但没有做任何事。

3)适合分区的文本。是的,即使对于初学者来说这也是一件非常简单的事情,但每当我尝试以某种方式改变它时,我最终会弄乱其余的代码。

HTML文件:

<!DOCTYPE html>
<meta charset="utf-8">
<style>

circle,
path {
  cursor: pointer;
}

circle {
  fill: none;
  pointer-events: all;
}

#tooltip { background-color: white;
        padding: 3px 5px;
        border: 1px solid black;
        text-align: center;}

html {
  font-family: sans-serif;

}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>

var margin = {top: 350, right: 480, bottom: 350, left: 480},
    radius = Math.min(margin.top, margin.right, margin.bottom, margin.left) - 10;

function filter_min_arc_size_text(d, i) {return (d.dx*d.depth*radius/3)>14}; 

var hue = d3.scale.category10();

var luminance = d3.scale.sqrt()
    .domain([0, 1e6])
    .clamp(true)
    .range([90, 20]);

var svg = d3.select("body").append("svg")
    .attr("width", margin.left + margin.right)
    .attr("height", margin.top + margin.bottom)
  .append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

var partition = d3.layout.partition()
    .sort(function(a, b) { return d3.ascending(a.name, b.name); })
    .size([2 * Math.PI, radius]);

var arc = d3.svg.arc()
    .startAngle(function(d) { return d.x; })
    .endAngle(function(d) { return d.x + d.dx - .01 / (d.depth + .5); })
    .innerRadius(function(d) { return radius / 3 * d.depth; })
    .outerRadius(function(d) { return radius / 3 * (d.depth + 1) - 1; });

//Tooltip description
var tooltip = d3.select("body")
    .append("div")
    .attr("id", "tooltip")
    .style("position", "absolute")
    .style("z-index", "10")
    .style("opacity", 0);

function format_number(x) {
  return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}


function format_description(d) {
  var description = d.description;
      return  '<b>' + d.name + '</b></br>'+ d.description + '<br> (' + format_number(d.vrijednost) + ')';
}

function computeTextRotation(d) {
  var angle=(d.x +d.dx/2)*180/Math.PI - 90  

  return angle;
}

function mouseOverArc(d) {
       d3.select(this).attr("stroke","black")

          tooltip.html(format_description(d));
          return tooltip.transition()
            .duration(50)
            .style("opacity", 0.9);
        }

function mouseOutArc(){
  d3.select(this).attr("stroke","")
  return tooltip.style("opacity", 0);
}

function mouseMoveArc (d) {
          return tooltip
            .style("top", (d3.event.pageY-10)+"px")
            .style("left", (d3.event.pageX+10)+"px");
}

var root_ = null;
d3.json("dat.json", function(error, root) {
  if (error) return console.warn(error);
  // Compute the initial layout on the entire tree to sum sizes.
  // Also compute the full name and fill color for each node,
  // and stash the children so they can be restored as we descend.

  partition
      .value(function(d) { return d.size; })
      .nodes(root)
      .forEach(function(d) {
        d._children = d.children;
        d.sum = d.value;
        d.key = key(d);
        d.fill = fill(d);
      });

  // Now redefine the value function to use the previously-computed sum.
  partition
      .children(function(d, depth) { return depth < 2 ? d._children : null; })
      .value(function(d) { return d.sum; });


  var center = svg.append("circle")
      .attr("r", radius / 3)
      .on("click", zoomOut);

  center.append("title")
      .text("zoom out");

  var partitioned_data=partition.nodes(root).slice(1)

  var path = svg.selectAll("path")
      .data(partitioned_data)
    .enter().append("path")
      .attr("d", arc)
      .style("fill", function(d) { return d.fill; })
      .each(function(d) { this._current = updateArc(d); })
      .on("click", zoomIn)
    .on("mouseover", mouseOverArc)
      .on("mousemove", mouseMoveArc)
      .on("mouseout", mouseOutArc);


  var texts = svg.selectAll("text")
      .data(partitioned_data)
    .enter().append("text")
    .filter(filter_min_arc_size_text)     
      .attr("transform", function(d) { return "rotate(" + computeTextRotation(d) + ")"; })
    .attr("x", function(d) { return radius / 3 * d.depth; })  
    .attr("dx", "6") // margin
      .attr("dy", ".35em") // vertical-align  
    .text(function(d,i) {return d.name})

  function zoomIn(p) {
    if (p.depth > 1) p = p.parent;
    if (!p.children) return;
    zoom(p, p);
  }

  function zoomOut(p) {
    if (!p.parent) return;
    zoom(p.parent, p);
  }

  // Zoom to the specified new root.
  function zoom(root, p) {
    if (document.documentElement.__transition__) return;

    // Rescale outside angles to match the new layout.
    var enterArc,
        exitArc,
        outsideAngle = d3.scale.linear().domain([0, 2 * Math.PI]);

    function insideArc(d) {
      return p.key > d.key
          ? {depth: d.depth - 1, x: 0, dx: 0} : p.key < d.key
          ? {depth: d.depth - 1, x: 2 * Math.PI, dx: 0}
          : {depth: 0, x: 0, dx: 2 * Math.PI};
    }

    function outsideArc(d) {
      return {depth: d.depth + 1, x: outsideAngle(d.x), dx: outsideAngle(d.x + d.dx) - outsideAngle(d.x)};
    }

    center.datum(root);

    // When zooming in, arcs enter from the outside and exit to the inside.
    // Entering outside arcs start from the old layout.
    if (root === p) enterArc = outsideArc, exitArc = insideArc, outsideAngle.range([p.x, p.x + p.dx]);

   var new_data=partition.nodes(root).slice(1)

    path = path.data(new_data, function(d) { return d.key; });

   // When zooming out, arcs enter from the inside and exit to the outside.
    // Exiting outside arcs transition to the new layout.
    if (root !== p) enterArc = insideArc, exitArc = outsideArc, outsideAngle.range([p.x, p.x + p.dx]);

    d3.transition().duration(d3.event.altKey ? 7500 : 750).each(function() {
      path.exit().transition()
          .style("fill-opacity", function(d) { return d.depth === 1 + (root === p) ? 1 : 0; })
          .attrTween("d", function(d) { return arcTween.call(this, exitArc(d)); })
          .remove();

      path.enter().append("path")
          .style("fill-opacity", function(d) { return d.depth === 2 - (root === p) ? 1 : 0; })
          .style("fill", function(d) { return d.fill; })
          .on("click", zoomIn)
       .on("mouseover", mouseOverArc)
         .on("mousemove", mouseMoveArc)
         .on("mouseout", mouseOutArc)
          .each(function(d) { this._current = enterArc(d); });


      path.transition()
          .style("fill-opacity", 1)
          .attrTween("d", function(d) { return arcTween.call(this, updateArc(d)); });



    });


   texts = texts.data(new_data, function(d) { return d.key; })

   texts.exit()
           .remove()    
    texts.enter()
            .append("text")

    texts.style("opacity", 0)
      .attr("transform", function(d) { return "rotate(" + computeTextRotation(d) + ")"; })
    .attr("x", function(d) { return radius / 3 * d.depth; })  
    .attr("dx", "6") // margin
      .attr("dy", ".35em") // vertical-align
      .filter(filter_min_arc_size_text)     
      .text(function(d,i) {return d.name})
    .transition().delay(750).style("opacity", 1)


  }
});

function key(d) {
  var k = [], p = d;
  while (p.depth) k.push(p.name), p = p.parent;
  return k.reverse().join(".");
}

function fill(d) {
  var p = d;
  while (p.depth > 1) p = p.parent;
  var c = d3.lab(hue(p.name));
  c.l = luminance(d.sum);
  return c;
}

function arcTween(b) {
  var i = d3.interpolate(this._current, b);
  this._current = i(0);
  return function(t) {
    return arc(i(t));
  };
}

function updateArc(d) {
  return {depth: d.depth, x: d.x, dx: d.dx};
}

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

</script>

JSON文件:

{
 "name": "flare",
 "description": "flare",
 "children": [
  {
   "name": "Protein",
   "description": "Protein",
   "children": [
    {
     "name": "Zivotinjski izvori",
     "description": "Zivotinjski izvori",
     "size": 25,
     "children": [
      {
        "name": "Meso", 
        "description": "",
        "children": [
        {"name": "Piletina", "description": "Pileća prsa", "size": 23, "vrijednost": "23g/100g"},
        {"name": "Puretina", "description": "Pureća prsa", "size": 25, "vrijednost": "25g/100g"},
        {"name": "Svinjetina", "description": "Svinjski kotlet", "size": 23, "vrijednost": "23g/100g"},
        {"name": "Teletina", "description": "Teleći but", "size": 29, "vrijednost": "29g/100g"},
        {"name": "Janjetina", "description": "Janječi kotlet", "size": 19, "vrijednost": "19g/100g"}
        ]
        },
      {
        "name": "Riba", 
        "description": "Riba",
        "children": [
        {"name": "Bakalar", "description": "", "size": 19, "vrijednost": "19g/100g"},
        {"name": "Losos", "description": "", "size": 20, "vrijednost": "20g/100g"},
        {"name": "Sardine", "description": "", "size": 24, "vrijednost": "24g/100g"},
        {"name": "Tunj", "description": "", "size": 24, "vrijednost": "24g/100g"},
        {"name": "Skuša", "description": "", "size": 19, "vrijednost": "19g/100g"},
        {"name": "Oslić", "description": "", "size": 17.2, "vrijednost": "17.2g/100g"}
        ]
        },
      {"name": "Jaja", "description": "Jaja", "size": 100, "vrijednost": "16g/100g"},
      {
        "name": "Mliječni proizvodi", 
        "description": "Mliječni proizvodi",
        "children": [
        {"name": "Posni sir", "description": "", "size": 12, "vrijednost": "12g/100g"},
        {"name": "Mekani sir", "description": "", "size": 14, "vrijednost": "14g/100g"},
        {"name": "Masni tvrdi sir", "description": "", "size": 20, "vrijednost": "20g/100g"},
        {"name": "Tvrdi sir", "description": "", "size": 25, "vrijednost": "25g/100g"},
        {"name": "Sir Ementaler", "description": "", "size": 30, "vrijednost": "30g/100g"},
        {"name": "Sir Parmezan", "description": "", "size": 34, "vrijednost": "34g/100g"}
        ]
    }
    ]
    },
    {
     "name": "Biljni izvori",
     "size": 25,
     "description": "Biljni izvori",
     "children": [
      {"name": "Povrće",
       "description": "",
       "children":[
       {"name": "Grašak", "description": "", "size": 7, "vrijednost": "7g/100g"},
       {"name": "Grah", "description": "", "size": 7, "vrijednost": "7g/100g"},
       {"name": "Leća", "description": "", "size": 8, "vrijednost": "8g/100g"},
       {"name": "Peršin", "description": "", "size": 5, "vrijednost": "5g/100g"},
       {"name": "Vlasac", "description": "", "size": 5, "vrijednost": "5g/100g"},
       {"name": "Prokulice", "description": "", "size": 4, "vrijednost": "4g/100g"},
       {"name": "Šampinjoni", "description": "", "size": 3, "vrijednost": "3g/100g"},
       {"name": "Kukuruz", "description": "", "size": 4, "vrijednost": "4g/100g"}
      ]},
      {"name": "Žitarice",
       "description": "Žitarice,kruh,riža",
       "children": [
       {"name": "Soja", "description": "Soja u zrnu", "size": 38, "vrijednost": "38g/100g"},
       {"name": "Sojin sir", "description": "", "size": 8, "vrijednost": "8g/100g"},
       {"name": "Zobene pahuljice", "description": "", "size": 14, "vrijednost": "14g/100g"},
       {"name": "Ječmena klica", "description": "", "size": 29, "vrijednost": "29g/100g"},
       {"name": "Dvopek", "description": "", "size": 10, "vrijednost": "10g/100g"},
       {"name": "Raženi kruh", "description": "", "size": 7, "vrijednost": "7g/100g"},
       {"name": "Crni kruh", "description": "", "size": 6, "vrijednost": "6g/100g"}
       ]},
      {"name": "Voće",
      "description": "i orašasti plodovi",
      "children":[
      {"name": "Badem", "description": "", "size": 20, "vrijednost": "20g/100g"},
      {"name": "Kikiriki", "description": "", "size": 23, "vrijednost": "23g/100g"},
      {"name": "Orasi", "description": "", "size": 13, "vrijednost": "13g/100g"},
      {"name": "Avokado", "description": "", "size": 2, "vrijednost": "2g/100g"},
      {"name": "Suho voće", "description": "marelice,grožđice,šljive", "size": 3, "vrijednost": "3g/100g"},
      {"name": "Banana", "description": "", "size": 2, "vrijednost": "1g/100g"}
      ]},
      {"name": "Sjemenke", 
      "description": "",
      "children":[
      {"name": "Sjemenke bundeve", "description": "pečene", "size": 33, "vrijednost": "33g/100g"},
      {"name": "Sjemenke lubenice", "description": "", "size": 28, "vrijednost": "28g/100g"},
      {"name": "Sjemenke suncokreta", "description": "", "size": 27, "vrijednost": "27g/100g"},
      {"name": "Chia", "description": "", "size": 14, "vrijednost": "14g/100g"},
      {"name": "Quinoa", "description": "", "size": 14, "vrijednost": "14g/100g"}
      ]}
     ]
    },
    {
     "name": "Suplementi",
     "size": 25,
     "description": "Suplementi",
     "children": [
      {"name": "Whey protein",
      "description": "",
      "children":[
      {"name": "Izolat", "description": "", "size": 90, "vrijednost": "oko 90% čisti protein"},
      {"name": "Koncentrat", "description": "", "size": 75, "vrijednost": "oko 75% čisti protein"}
      ]},
      {"name": "Weight Gaineri", "description": "", "size": 35, "vrijednost": "oko 30-40% čisti protein"}
     ]
    }
   ]
  },
  {
   "name": "Ugljikohidrati",
   "description": "Ugljikohidrati",
   "children": [
    {"name": "Jednostavni",
     "description": "Visoki GI,lako probavljivi",
      "size": 25,
      "children":[
       {"name": "Dekstroza", "description": "suplement", "size": 70, "vrijednost": "GI: 100"},
       {"name": "Banana", "description": "zrela voćka", "size": 23, "vrijednost": "23g/100g"},
       {"name": "Grožđe", "description": "", "size": 16, "vrijednost": "16g/100g"},
       {"name": "Med", "description": "prirodni pčelinji", "size": 77, "vrijednost": "77g/100g"},
       {"name": "Suho voće", "description": "", "size": 56, "vrijednost": "56g/100g"},
       {"name": "Riža", "description": "instant", "size": 77, "vrijednost": "77g/100g"},
       {"name": "Kruh/peciva", "description": "bijeli", "size": 50, "vrijednost": "50g/100g"},
       {"name": "Bijeli šećer", "description": "rafinirani", "size": 99, "vrijednost": "99g/100g"},
       {"name": "Šljiva", "description": "", "size": 14, "vrijednost": "14g/100g"}
       ]},
    {"name": "Složeni",
     "description": "Srednji i niski GI,teže se probavljaju",
      "size": 70,
      "children":[
      {"name": "Šljiva", "description": "", "size": 12, "vrijednost": "12g/100g"},
      {"name": "Tjestenina", "description": "", "size": 30, "vrijednost": "30g/100g"},
      {"name": "Jabuka", "description": "", "size": 13, "vrijednost": "13g/100g"},
      {"name": "Zobene pahuljice", "description": "", "size": 64, "vrijednost": "64g/100g"},
      {"name": "Crni kruh", "description": "", "size": 49, "vrijednost": "49g/100g"},
      {"name": "Mahunarke", "description": "", "size": 27, "vrijednost": "27g/100g"},
      {"name": "Kikiriki", "description": "", "size": 24, "vrijednost": "24g/100g"}
      ]}
   ]
  },
  {
   "name": "Masti",
   "description": "Masti",
   "children": [
    {
     "name": "Ulja",
     "description": "converters",
     "children": [
      {"name": "x", "description": "Converters", "size": 25},
      {"name": "x", "description": "DelimitedTextConverter", "size": 25},
      {"name": "x", "description": "GraphMLConverter", "size": 25},
      {"name": "x", "description": "IDataConverter", "size": 25},
      {"name": "x", "description": "JSONConverter", "size": 25},
      {"name": "x", "description": "JSONConverter", "size": 25}
     ]
    },
    {
      "name": "Ribe",
      "description": "",
      "children": [
    {"name": "x", "description": "DataField", "size": 25},
    {"name": "x", "description": "DataSchema", "size": 25},
    {"name": "x", "description": "DataSet", "size": 25},
    {"name": "x", "description": "DataSource", "size": 25},
    {"name": "x", "description": "DataTable", "size": 25},
    {"name": "x", "description": "DataUtil", "size": 25}
    ]},
    {
      "name": "Orašasti plodovi",
      "description": "",
      "children": [
      {"name": "x", "description": "DataField", "size": 25},
    {"name": "x", "description": "DataSchema", "size": 25},
    {"name": "x", "description": "DataSet", "size": 25},
    {"name": "x", "description": "DataSource", "size": 25},
    {"name": "x", "description": "DataTable", "size": 25},
    {"name": "x", "description": "DataUtil", "size": 25}
      ]
    }
   ]
  }
 ]
}

0 个答案:

没有答案