将d3与流星集成?试图绘制饼图 [英] Integrating d3 with meteor? Trying to draw pie charts

查看:269
本文介绍了将d3与流星集成?试图绘制饼图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Meteor中绘制饼图,但我对d3和Meteor都很新鲜,并且不能真正了解发生了什么。

I'm trying to draw pie charts in Meteor, but I'm very new to both d3 and Meteor and am not really understanding what is going on.

以下用于从csv文件中绘制饼图的d3代码适用于Meteor以外的我:

The following d3 code to draw pie charts from a csv file works for me outside of Meteor:

<!DOCTYPE html>
<meta charset="utf-8">
    <link href='http://fonts.googleapis.com/css?family=Montserrat:400,700' rel='stylesheet' type='text/css'>
<style>

body {
  font: 30px "Montserrat";
  text-transform:uppercase;
}

svg {
  padding: 10px 0 0 10px;
}

.arc {
  stroke: #fff;
}

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

var radius = 150,
    padding = 10;

var color = d3.scale.ordinal()
    .range(["#f65c55","#c8e7ec"]);

var arc = d3.svg.arc()
    .outerRadius(radius)
    .innerRadius(radius - 40);

var pie = d3.layout.pie()
    .sort(null)
    .value(function(d) { return d.population; });

d3.csv("data.csv", function(error, data) {
  color.domain(d3.keys(data[0]).filter(function(key) { return key !== "Criteria"; }));

  data.forEach(function(d) {
    d.ages = color.domain().map(function(name) {
      return {name: name, population: +d[name]};
    });
  });

  var legend = d3.select("body").append("svg")
      .attr("class", "legend")
      .attr("width", radius * 2)
      .attr("height", radius * 2)
    .selectAll("g")
      .data(color.domain().slice().reverse())
    .enter().append("g")
      .attr("transform", function(d, i) { return "translate(0," + i * 50 + ")"; });

  legend.append("rect")
      .attr("width", 40)
      .attr("height", 40)
      .style("fill", color);

  legend.append("text")
      .attr("x", 50)
      .attr("y", 20)
      .attr("dy", ".35em")
      .attr("font-size","20px")
      .text(function(d) { return d; });

  var svg = d3.select("body").selectAll(".pie")
      .data(data)
    .enter().append("svg")
      .attr("class", "pie")
      .attr("width", radius * 2)
      .attr("height", radius * 2)
    .append("g")
      .attr("transform", "translate(" + radius + "," + radius + ")");

  svg.selectAll(".arc")
      .data(function(d) { return pie(d.ages); })
    .enter().append("path")
      .attr("class", "arc")
      .attr("d", arc)
      .style("fill", function(d) { return color(d.data.name); });

  svg.append("text")
      .attr("dy", ".35em")
      .style("text-anchor", "middle")
      .text(function(d) { return d.Criteria; });

});

</script>

我也有一个Meteor模板如下,我想在这些图中绘制:

I also have a Meteor template as follows that I want to draw these charts in:

  <div class="tab-pane active" id="playback">
    {{> playback}}
  </div>

但是,当我尝试并按照Web教程来集成这两个时, 。任何人都可以帮助我明白为什么?提前感谢!

However, when I try and follow web tutorials to integrate the two, the graphs don't get drawn. Can anyone help me understand why? Thanks in advance!

编辑:忘记提及,data.csv如下所示:

forgot to mention, data.csv looks like this:

Criteria,Disapproval, Approval
Too Fast,1,2
Too Slow,5,6
Clarity,2,3
Legibility,202070,343207

第一行是图例,其余的是4个单独的图表。

The first line is for the legend, and the rest are for 4 separate graphs.

推荐答案

Template.chart.rendered = function(){

    Deps.autorun(function () {

       //d3 codeing here!!
    }
}

它为我工作如果你编码没有Deps.autorun()它不会渲染
哦!一个在html页面在你的情况下可能
但是对于我的情况,我使用nvd3.org http://nvd3.org/livecode/index.html #codemirrorNav 。我希望您能澄清。

It's working for me. If you're coding without Deps.autorun() it's will not render. Oh!! one morething at html page in you case maybe . However for my case I using nvd3.org http://nvd3.org/livecode/index.html#codemirrorNav. And this I hope you will clearify.

这篇关于将d3与流星集成?试图绘制饼图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆