2015-08-23 69 views
1

這是我用於調用兩個塊(在兩個div標籤中)的相同js函數的代碼。第二個標籤(div id =「frame4」)的答案也會打印在第一個標籤(div id =「frame3」)內。我想單獨打印它們。我怎樣才能做到這一點?爲函數如何在同一個HTML中多次調用JS函數

function dsPieChart(x){ 
    var formatAsPercentage = d3.format("%") ; 
    var dataset = [ 
     {category: "", measure:x }, 
     {category: "", measure:(100-x)}, 

    ] 
    ; 

    var width = 100, 
     height = 100, 
     outerRadius = Math.min(width, height)/2, 
     innerRadius = outerRadius * .9, 
     // for animation 
     innerRadiusFinal = outerRadius * .8, 
     innerRadiusFinal3 = outerRadius* .7, 
     color = d3.scale.category20b() //builtin range of colors 
     ; 

    var vis = d3.select("#pieChart") 
     .append("svg:svg")    //create the SVG element inside the <body> 
     .data([dataset])     //associate our data with the document 
      .attr("width", width)   //set the width and height of our visualization (these will be attributes of the <svg> tag 
      .attr("height", height) 
      .append("svg:g")    //make a group to hold our pie chart 
      .attr("transform", "translate(" + outerRadius + "," + outerRadius + ")") //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(outerRadius).innerRadius(innerRadius); 

    // for animation 
    var arcFinal = d3.svg.arc().innerRadius(innerRadiusFinal).outerRadius(outerRadius); 
    var arcFinal3 = d3.svg.arc().innerRadius(innerRadiusFinal3).outerRadius(outerRadius); 

    var pie = d3.layout.pie()   //this will create arc data for us given a list of values 
      .value(function(d) { return d.measure; }); //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) 
       .on("mouseover", mouseover) 
       .on("mouseout", mouseout) 
       .on("click", up) 
       ; 

      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 
      .append("svg:title") //mouseover title showing the figures 
      // .text(function(d) { return d.data.category + ": " + d.data.measure ; }); 
      .text(function(d) { return d.data.measure ; }); 

      d3.selectAll("g.slice").selectAll("path").transition() 
      .duration(750) 
      .delay(10) 
      .attr("d", arcFinal) 
      ; 
     // Add a label to the larger arcs, translated to the arc centroid and rotated. 

     arcs.filter(function(d) { return d.endAngle - d.startAngle > .2; }) 
      .append("svg:text") 
      .attr("dy", ".35em") 
      .attr("text-anchor", "middle") 
      .attr("transform", function(d) { return "translate(" + arcFinal.centroid(d) + ")rotate(" + angle(d) + ")"; }) 
      .text(function(d) { return d.data.category; }) 
      ; 
     // Computes the label angle of an arc, converting from radians to degrees. 
     function angle(d) { 
      var a = (d.startAngle + d.endAngle) * 90/Math.PI - 90; 
      return a > 90 ? a - 180 : a; 
     } 
     // Pie chart title 
     vis.append("svg:text") 
      .attr("dy", ".35em") 
      .attr("text-anchor", "middle") 
      .text(x +"%") 
      .attr("class","title") 
      ; 

    function mouseover() { 
     d3.select(this).select("path").transition() 
      .duration(750) 
       .attr("d", arcFinal3) 
       ; 
    } 

    function mouseout() { 
     d3.select(this).select("path").transition() 
      .duration(750) 
       //.attr("stroke","blue") 
       //.attr("stroke-width", 1.5) 
       .attr("d", arcFinal) 
       ; 
    } 

    function up(d, i) { 
      /* update bar chart when user selects piece of the pie chart */ 
      //updateBarChart(dataset[i].category); 
      updateBarChart(d.data.category, color(i)); 
      updateLineChart(d.data.category, color(i)); 

    } 
    } 
+2

爲了能夠幫助您,我們需要知道dsPieChart的功能。您的代碼看起來像是第二次正確調用該函數,所以問題必須發生在代碼的其他部分。順便說一句,您使用的是什麼模板引擎? –

+0

該函數的參數是什麼?是'dsPieChart()'你正在談論的函數,那麼當你調用該函數時,它應該爲frame3和frame4打印同樣的東西,因爲你正在傳遞相同的參數'<%= coverage%>'。 – Imran

+0

dsPieChart()用於d3w餅圖使用d3 –

回答

3

更改功能

<div id="frame3"> 

    <! ----pieChart ----- !> 
    <h5><i>Code Coverage</i></h5> 
     <div id="pieChart"></div> 
     <script type="text/javascript"> 


     dsPieChart(<%=coverage %>); 
     </script> 

    </div> 

    <!test_density !> 
    <div id="frame3"> 
    <div id="pieChart"></div> 
    <script type="text/javascript"> 


    dsPieChart(<%=density %>); 
     </script> 
    </div> 

代碼傳遞給元件ID第二個參數。

function dsPieChart(x, selectorId){ 

更改硬編碼選擇:

var vis = d3.select("#pieChart"); 

var vis = d3.select("#" + selectorId); 

然後當你調用該函數也排在第二paramater標識id選擇。請注意,元素ID在頁面中必須是唯一的:

<div id="pieChart-1"></div> 
<script type="text/javascript"> 
    dsPieChart(<%=coverage %>, 'pieChart-1'); 
    </script> 
</div> 

<div id="pieChart-2"></div> 
<script type="text/javascript"> 
    dsPieChart(<%=density %>, 'pieChart-2'); 
    </script> 
</div> 
相關問題