如何在凹坑js中创建泡泡饼图

时间:2016-10-07 04:07:18

标签: javascript charts dimple.js

我是新来的酒窝,我在制作泡泡饼图时遇到了麻烦。我希望每个气泡也是一个显示区域百分比的饼图。我尝试使用myChart.addSeries(["North America", "Latin America"], dimple.plot.pie);插入饼图,但它不起作用。有什么建议?谢谢!

function draw(data) {
  /*
  D3.js setup code
  */
  "use strict";
  var margin = 75,
      width = 1400 - margin,
      height = 600 - margin;

  var svg = d3.select("body")
  .append("svg")
  .attr("width", width + margin)
  .attr("height", height + margin)
  .append('g')
  .attr('class','chart');         
  /*
          Dimple.js Chart construction code
        */
  var myChart = new dimple.chart(svg, data);
  myChart.addCategoryAxis("x", "Target Date");
  myChart.addCategoryAxis("y", "Target Reduction");
  myChart.addMeasureAxis("z", "Number of Cities");

  myChart.addSeries(
    ["Cities",
     "North America",
     "Latin America"],
    dimple.plot.bubble);

  //myChart.addSeries(["North America", "Latin America"], dimple.plot.pie);

  myChart.addLegend(180, 10, 360, 20, "right");
  myChart.draw();
  /*
  Add horizontal line at 50%
  */
  svg.append("line")
    .attr("x1", margin+65)
    .attr("x2", 1250)
    .attr("y1", 325)
    .attr("y2", 325)
    .style("stroke", "red")
    .style("stroke-dasharray", "3");
  /*
          Add horizontal line at 85%
        */
  svg.append("line")
    .attr("x1", margin+65)
    .attr("x2", 1250)
    .attr("y1", 180)
    .attr("y2", 180)
    .style("stroke", "red")
    .style("stroke-dasharray", "3");
  /*
            Add chrt title
          */
  svg.append("text")
    .attr("x", myChart._xPixels() + myChart._widthPixels() / 2)
    .attr("y", myChart._yPixels() - 20)
    .style("text-anchor", "middle")
    .style("font-family", "sans-serif")
    .style("font-weight", "bold")
    .style("font-size", "20px")
    .text("Emissions Targets set by CDP Cities");


};

d3.csv("data.csv", draw);
<style></style>
<body></body>  

2 个答案:

答案 0 :(得分:1)

这引起了我的兴趣所以我用RGraph创建了它的演示。这不是一个微不足道的代码 - 但它确实起到了作用:

http://www.rgraph.net/demos/scatter-bubble-pie-chart.html

答案 1 :(得分:0)

从dimples-master下载中有一个饼图泡泡演示(下面的代码)。或者在这里查看演示http://dimplejs.org/examples_viewer.html?id=pie_bubble

<!DOCTYPE html>
<meta charset="utf-8">
<html>
<div id="chartContainer">
  <script src="/lib/d3.v3.4.8.js"></script>
  <script src="/dist/dimple.v2.2.0.js"></script>
  <script type="text/javascript">
    var svg = dimple.newSvg("#chartContainer", 590, 400);
      d3.tsv("/data/example_data.tsv", function (data) {
        data = dimple.filterData(data, "Date", "01/12/2012");
        var myChart = new dimple.chart(svg, data);
        myChart.setBounds(60, 30, 500, 330)
        myChart.addMeasureAxis("y", "Unit Sales Monthly Change");
        myChart.addMeasureAxis("x", "Price Monthly Change");
        myChart.addMeasureAxis("p", "Operating Profit");
        myChart.addMeasureAxis("z", "Operating Profit");
        myChart.addSeries(["Owner", "Channel"], dimple.plot.pie);
        myChart.addLegend(200, 10, 360, 20, "right");
        myChart.draw();
      });
  </script>
</div>
</html>