2013-01-11 17 views
13

मैं डी 3 का उपयोग कर फ़्लोटिंग लेबल के साथ एक पाई चार्ट बनाना चाहता हूं। मैं डी 3 के लिए नया हूं और मुझे यह भी यकीन नहीं है कि यह संभव है? क्या आप किसी ग्राफ के लेबल किसी अन्य तरीके से उपयोग कर सकते हैं? यदि आप कर सकते हैं, तो क्या आप मुझे एक उदाहरण के लिए इंगित कर सकते हैं?डी 3 - पाई चार्ट और फोर्स निर्देशित लेबल

छोटा स्पष्टीकरण: एक पाई चार्ट पर होने की http://bl.ocks.org/1691430

enter image description here ...: मैं से लेबल चाहते हैं। अगर मैं अपने कोड सही ढंग से समझ इस खंड है कि लेबल कहते है http://jsbin.com/awilak/1/edit

,:

यहाँ कोड मैं नीचे चल रहा था है: या एक JSBIN में। मुझे समझ में नहीं आता कि labelForce.update क्या करता है। वहां से, मुझे संक्रमण की परवाह नहीं है, इसलिए लाइन की आवश्यकता नहीं है। तो बाकी सिर्फ मंडलियों को चित्रित कर रहे हैं और एक लिंक/लाइन जोड़ते हैं? अगर कोई एकीकृत हो सकता है तो यह आश्चर्यजनक होगा लेकिन अगर आप मुझे समझने में मदद कर सकते हैं कि क्या हो रहा है और मैं क्या खो रहा हूं, तो मैं आभारी हूं।

// Now for the labels 
// This is the only function call needed, the rest is just drawing the labels 
anchors.call(labelForce.update) 

labels = svg.selectAll(".labels") 
    .data(data, function(d,i) {return i;}) 
labels.exit() 
    .attr("class","exit") 
    .transition() 
    .delay(0) 
    .duration(500) 
    .style("opacity",0) 
    .remove(); 

// Draw the labelbox, caption and the link 
newLabels = labels.enter().append("g").attr("class","labels") 

newLabelBox = newLabels.append("g").attr("class","labelbox") 
newLabelBox.append("circle").attr("r",11) 
newLabelBox.append("text").attr("class","labeltext").attr("y",6) 
newLabels.append("line").attr("class","link") 

labelBox = svg.selectAll(".labels").selectAll(".labelbox") 
links = svg.selectAll(".link") 
labelBox.selectAll("text").text(function(d) { return d.num}) 
} 

<!DOCTYPE html> 
<html> 
<head>  
    <meta http-equiv="Content-type" content="text/html; charset=utf-8"> 
    <title>Testing Pie Chart</title> 
    <script type="text/javascript" src="http://mbostock.github.com/d3/d3.js?2.1.3"></script> 
    <script type="text/javascript" src="http://mbostock.github.com/d3/d3.geom.js?2.1.3"></script> 
    <script type="text/javascript" src="http://mbostock.github.com/d3/d3.layout.js?2.1.3"></script> 

    <style type="text/css"> 
    .slice text { 
     font-size: 16pt; 
     font-family: Arial; 
    } 
    </style> 
</head> 
<body> 
    <button id="button"> Test </button> 
    <br> 
    <form id="controls"> 
     <div> 
      <h2>Y axis</h2> 
      <ul id="y-axis"> 
       <li><label><input checked="checked" type="radio" name="y-axis" value="Component">Component</label></li> 
       <li><label><input type="radio" name="y-axis" value="Browser">Browser</label></li> 
       <li><label><input type="radio" name="y-axis" value="Version">Version</label></li> 
      </ul> 
     </div> 
    </form> 
    <script type="text/javascript"> 
    // return a list of types which are currently selected 
    function plottableTypes() { 
     var types = [].map.call (document.querySelectorAll ("#coaster-types input:checked"), function (checkbox) { return checkbox.value;}); 
     return types; 
    } 


    var w = 600,      //width 
    h = 600,       //height 
    r = 100, 
    r2 = 200,       //radius 
    axis = getAxis(),     //axes 
    color = d3.scale.category20c();  //builtin range of colors 

    data = [ 
     {"Browser":"Internet Explorer ","Version":"8.0","Toatl":2000,"Component":"6077447412293130422"}, 
     {"Browser":"Internet Explorer ","Version":"9.0 ","Toatl":1852,"Component":"6077447412293130422"}, 
     {"Browser":"Internet Explorer ","Version":"6.0 ","Toatl":1754,"Component":"6077447412293130422"}, 
     {"Browser":"Firefox ","Version":"16.0 ","Toatl":1020,"Component":"6077447412293130422"}, 
     {"Browser":"Chrome ","Version":"23.0 ","Toatl":972,"Component":"6077447412293130422"}, 
     {"Browser":"Internet Explorer ","Version":"7.0 ","Toatl":700,"Component":"6077447412293130422"}, 
     {"Browser":"Mobile Safari ","Version":"6.0 ","Toatl":632,"Component":"6077447412293130422"}, 
     {"Browser":"BOT ","Version":"BOT ","Toatl":356,"Component":"6077447412293130422"}, 
     {"Browser":"Firefox ","Version":"8.0 ","Toatl":196,"Component":"6077447412293130422"}, 
     {"Browser":"Mobile Safari ","Version":"5.1 ","Toatl":184,"Component":"6077447412293130422"} 
    ]; 

    var vis = d3.select("body") 
     .append("svg:svg")    //create the SVG element inside the <body> 
     .data([data])     //associate our data with the document 
     .attr("width", w)   //set the width and height of our visualization (these will be attributes of the <svg> tag 
     .attr("height", h) 
     .append("svg:g")    //make a group to hold our pie chart 
     .attr("transform", "translate(" + r2 + "," + r2 + ")") //move the center of the pie chart from 0, 0 to radius, radius 

    var arc = d3.svg.arc()    //this will create <path> elements for us using arc data 
     .outerRadius(r); 


    var pie = d3.layout.pie()   //this will create arc data for us given a list of values 
     .value(function(d) { return d.Toatl; }); //we must tell it out to access the value of each element in our data array 

    var arcs = vis.selectAll("g.slice")  //this selects all <g> elements with class slice (there aren't any yet) 
     .data(pie)       //associate the generated pie data (an array of arcs, each having startAngle, endAngle and value properties) 
     .enter()       //this will create <g> elements for every "extra" data element that should be associated with a selection. The result is creating a <g> for every object in the data array 
     .append("svg:g")    //create a group to hold each slice (we will have a <path> and a <text> element associated with each slice) 
     .attr("class", "slice"); //allow us to style things in the slices (like text) 


    arcs.append("svg:path") 
     .attr("fill", function(d, i) { return color(i); }) //set the color for each slice to be chosen from the color function defined above 
     .attr("d", arc);         //this creates the actual SVG path using the associated data (pie) with the arc drawing function 


    arcs.append("svg:text")          //add a label to each slice 
     .attr("transform", function(d) {     //set the label's origin to the center of the arc 
      //we have to make sure to set these before calling arc.centroid 
      d.innerRadius = r2; 
      d.outerRadius = r; 
      return "translate(" + arc.centroid(d) + ")";  //this gives us a pair of coordinates like [50, 50] 
     }) 
     .attr("text-anchor", "middle")       //center the text on it's origin 
     .text(function(d, i) { 
      if(axis.yAxis == "Component"){ 
       return data[i].Component; 
      } 
      return data[i].Browser;  //get the label from our original data array 
     });  

     d3.select('#button').on('click', reColor); 

     var arcOver = d3.svg.arc() 
      .outerRadius(r + 30) 
      .innerRadius(0); 
     var arc = d3.svg.arc() 
      .outerRadius(r) 
      .innerRadius(0); 

     var arcs = vis.selectAll("g.slice") 
      .attr("class", "slice") 
      .on("mouseover", function(d) { 
       getAxis(); 
       d3.select(this) 
        .select("path") 
        .transition() 
        .duration(500) 
       .attr("d", arcOver); 
       d3.select(this).select("text") 
        .text(function(d, i) { 
         if(axis.yAxis == "Component"){ 
          return data[i].Component; 
         } 
        return data[i].Browser;  //get the label from our original data array 
       });  
      }) 
      .on("mouseout", function(d) { 
       getAxis(); 
       d3.select(this) 
        .select("path") 
        .transition() 
        .duration(500) 
        .attr("d", arc); 
       d3.select(this) 
        .select("text") 
        .text(function(d, i) { 
         if(axis.yAxis == "Component"){ 
          return data[i].Component; 
         } 
         return data[i].Browser;  //get the label from our original data array 
        }); 
       }); 


     function reColor(){ 
      var slices = d3.select('body').selectAll('path'); 
      slices.transition() 
       .duration(2000) 
       .attr("fill", function(d, i) { return color(i+2); }); 
      slices.transition() 
       .delay(2000) 
       .duration(2000) 
       .attr("fill", function(d, i) { return color(i+10); }) 
     } 
     function makeData(){ 

     } 
     // return an object containing the currently selected axis choices 
     function getAxis() { 
      var y = document.querySelector("#y-axis input:checked").value; 
      return { 
       yAxis: y, 
      }; 
     } 
     function update() { 
      axis = getAxis() 
      arcs.selectAll("text")   //add a label to each slice    
       .text(function(d, i) { 
        if(axis.yAxis == "Component"){ 
         return data[i].Component; 
        } 
        return data[i].Browser;  //get the label from our original data array 
       }); 
      } 

     document.getElementById("controls").addEventListener ("click", update, false); 
     document.getElementById("controls").addEventListener ("keyup", update, false); 
    </script> 
</body> 
</html> 
+0

हाँ यह संभव है। आपके द्वारा पोस्ट किया गया उदाहरण पहले से ही काफी करीब है जो मुझे लगता है कि आप करना चाहते हैं। क्या आपने कुछ कोशिश की है जो आपने कोशिश की है? –

+0

मुझे उलझन में है कि जब मुझे लेबल कॉल करने की आवश्यकता होती है, तो मैं ऊपर अपने पाई चार्ट में संपादित करूंगा। कॉल (labelForce.update) वास्तव में क्या कर रहा है? आपकी मदद के लिए धन्यवाद! – gbam

+1

उदाहरण में 'एंकर' लेबल की चीजें हैं। वे आपके पाई सेगमेंट होंगे। शुरुआत में पाई चार्ट को स्वतंत्र रूप से आकर्षित करना और उदाहरण में कोड का उपयोग करना आसान हो सकता है जैसा कि पाई सेगमेंट में कहीं भी बिंदुओं के डेटा के साथ है। –

उत्तर

-1

आपको दो आर्क बनाने की आवश्यकता है। पाई चार्ट ड्राइंग के लिए एक, और एक जो लेबल पर बैठने के लिए बड़ा है।

// first arc used for drawing the pie chart 
var arc = d3.svg.arc() 
    .outerRadius(radius - 10) 
    .innerRadius(0); 

// label attached to first arc 
g.append("text") 
    .attr("transform", function(d) { return "translate(" + arc.centroid(d) + ")"; }) 
    .attr("dy", ".35em") 
    .style("text-anchor", "middle") 
    .text(function(d) { return d.data.age; }); 

// second arc for labels 
var arc2 = d3.svg.arc() 
    .outerRadius(radius + 20) 
    .innerRadius(radius + 20); 

// label attached to second arc 
g.append("text") 
    .attr("transform", function(d) { return "translate(" + arc2.centroid(d) + ")"; }) 
    .attr("dy", ".35em") 
    .style("text-anchor", "middle") 
    .text(function(d) { return d.data.age; }); 
0

हां, आप निश्चित रूप से पाई चार्ट के साथ बल-लेबल जोड़ सकते हैं! आपके द्वारा शुरू किए गए पाई चार्ट लेबल के बारे में विशेष रूप से विशेष कुछ भी नहीं है, वे केवल टेक्स्ट तत्व हैं जिन्हें ट्रांसफॉर्म या एक्स/वाई का उपयोग करके किसी अन्य चीज़ की तरह रखा जा सकता है। ऐसा लगता है कि आप शुरू में इन लेबलों को लेबलिंग वाले आर्क के सिड्रोइड के अनुसार स्थिति में रखते थे, लेकिन आप आसानी से अन्य मानदंडों (जैसे बल लेआउट के आउटपुट) का उपयोग कर सकते हैं।

डी 3 का बल लेआउट निश्चित रूप से क्या है, जो चल रहा है, और जो जुड़े हुए हैं, के बारे में बाधाओं के सेट के आधार पर चीजों के लिए स्थितियों की गणना करता है। लेबल Force.update माइक के bl.ocks से विधि उदाहरण का उपयोग बल लेआउट को सूचित करने के लिए किया जा रहा है कि कितनी वस्तुओं को स्थानांतरित करने की आवश्यकता है, और जहां निश्चित "एंकर" बिंदु हैं। इसके बाद लेबल के लिए आरेखित स्थिति को आरेख के डेटा मॉडल में सहेजता है, और बाद में रेड्रालैबल्स फ़ंक्शन में उनका उपयोग किया जाता है।

2

जैसा कि दूसरों ने आपके परिचय-पोस्ट पर टिप्पणियों में उल्लेख किया है, जैसा कि आपने इसे वर्णित समाधान प्राप्त करना संभव है और यह आपके कोड और "चलने वाले लेबल" के उदाहरणों के साथ-साथ संभव है। अगर मैं आपको सही ढंग से समझता हूं, तो आप बल-लेआउट का उपयोग करके गैर-ओवरलैपिंग लेबल प्राप्त करना चाहते हैं, जो एक बहुत अच्छा विचार है कि मैंने अभी तक ठोकर खाई नहीं है।

उदाहरण से चिपका हुआ कोड-भाग केवल लेबल और रेखाएं खींचता है जैसा आपने पहले ही सही ढंग से समझाया है। अगला चरण को अपने पाई चार्ट के चारों ओर एक बल-जैसे लेआउट में लेबल पुनर्व्यवस्थित करना है।

function redrawLabels() { 
    labelBox 
     .attr("transform",function(d) { return "translate("+d.labelPos.x+" "+d.labelPos.y+")"}) 

    links 
     .attr("x1",function(d) { return d.anchorPos.x}) 
     .attr("y1",function(d) { return d.anchorPos.y}) 
     .attr("x2",function(d) { return d.labelPos.x}) 
     .attr("y2",function(d) { return d.labelPos.y}) 
}   

// Initialize the label-forces 
labelForce = d3.force_labels() 
    .linkDistance(0.0) 
    .gravity(0) 
    .nodes([]).links([]) 
    .charge(-60) 
    .on("tick",redrawLabels) 

समारोह एक है कि लेबल और लाइनों के पदों को बदल देता है:

बात यह है कि उदाहरण में लेबल (और लिंक) rearranges निम्नलिखित है। बल की गणना डी 3 द्वारा की जाती है और d3.force_labels()... से शुरू होती है। जैसा कि आप देख सकते हैं, फ़ंक्शन को टिक-ईवेंट के लिए इवेंट-हैंडलर के रूप में असाइन किया गया है। दूसरे शब्दों में: बल की गणना के हर चरण के बाद, डी 3 प्रत्येक लेबल के लिए 'दराज' को कॉल करता है और पदों को अद्यतन करता है।

दुर्भाग्य से मैं force_labels() डी 3 की विधि से बहुत परिचित नहीं हूं, लेकिन मुझे लगता है कि यह नियमित force() की तरह काम करता है। आपके मामले में एक एंकर, प्रत्येक लेबल के लिए प्रत्येक पाई-टुकड़े में कहीं भी रखा जाता है। प्रत्येक पाई-टुकड़ा (पाई स्वयं नहीं) के भीतर अधिक केंद्रित, बेहतर। दुर्भाग्यवश आपको इस एंकर-स्थिति को किसी भी तरह (पाप और कॉस सामान) की गणना करनी है और लाइन-सिरों को redrawLabels() के भीतर इस निश्चित स्थिति में सेट करना है।

ऐसा करने के बाद आपको पहला परिणाम दिखाई देगा। आपको अच्छे नतीजों को प्राप्त करने के लिए बल के गुरुत्वाकर्षण, लिंकडिस्टेंस आदि मूल्यों के साथ खेलना पड़ सकता है। (यही कारण है कि उदाहरण में silders करते हैं।)

अधिक जानकारी के लिए d3 डॉक्स देखें: https://github.com/mbostock/d3/wiki/Force-Layout

तो फिर तुम शायद समस्या यह है कि लेबल अतिव्यापी बिना लेकिन कुछ अजीब क्रम में पाई के आसपास का आदेश दिया जाता है पर ठोकर होगा । आप शुरुआत में लेबल को चारों ओर यादृच्छिक रूप से स्थिति के स्थान पर रखने के बजाय लेबल को अपने पाई के चारों ओर एक बड़े सर्कल पर सही क्रम में रखकर हल कर सकते हैं, जो समस्या का कारण है। इस तरह आप कम जिटर और गलत जगहों का अनुभव करेंगे।

विचार भी एक और ब्लॉक उदाहरण में वर्णित है: http://bl.ocks.org/mbostock/7881887

इस उदाहरण में, नोड्स शुरू में एक आभासी सर्कल पर रखा जाता है। स्थिति निम्नलिखित कार्य करके की जाती है:

एक्स: गणित.क्योंकि (मैं/मी * 2 * Math.PI) * 200 + चौड़ाई/2 + math.random(),

y: गणित। पाप (i/m * 2 * Math.PI) * 200 + ऊंचाई/2 + Math.random()

वे ड्राइंग-पैनल के केंद्र में 200 के त्रिज्या के साथ एक सर्कल का प्रतिनिधित्व करते हैं। सर्कल एम में समान रूप से बड़े टुकड़ों में बांटा गया है। i/m बस 'टुकड़े-स्थिति' की गणना करता है जहां मैं 0 से एम -1 तक है।

आशा है कि मैं मदद कर सकता हूं!

संबंधित मुद्दे