如何绘制三角形符号?

时间:2017-04-02 22:51:50

标签: javascript d3.js

我正在尝试实施the triangle example但它失败了d3 v4对我来说

triangles = [];
triangles.push({x : 250});
triangles.push({x : 350});

var group = svgContainer.append('g').attr('transform','translate('+ xOffset +','+ 30 +')');
var arc = d3.symbol().type('triangle');

var line = group.selectAll('path')
            .data(triangles)
            .enter()
            .append('path')
            .attr('d',arc)
            .attr('fill','red')
            .attr('stroke','#000')
            .attr('stroke-width',1)
            .attr('transform',function(d){ return "translate("+(d.x)+",0)"; }); 

错误:

  

未捕获的TypeError:type.apply(...)。draw不是函数

1 个答案:

答案 0 :(得分:8)

而不是......

type("triangle")

......必须是:

type(d3.symbolTriangle)

以下是D3 v4.x中符号的the list

以下是演示:

var svg = d3.select("svg");

triangles = [];
triangles.push({
  x: 150
});
triangles.push({
  x: 50
});

var arc = d3.symbol().type(d3.symbolTriangle);

var line = svg.selectAll('path')
  .data(triangles)
  .enter()
  .append('path')
  .attr('d', arc)
  .attr('fill', 'red')
  .attr('stroke', '#000')
  .attr('stroke-width', 1)
  .attr('transform', function(d) {
    return "translate(" + d.x + ",30)";
  });
<script src="https://d3js.org/d3.v4.min.js"></script>
<svg></svg>

旋转三角形的相同代码(如your comment中所述):

var svg = d3.select("svg");

triangles = [];
triangles.push({
  x: 150
});
triangles.push({
  x: 50
});

var arc = d3.symbol().type(d3.symbolTriangle);

var line = svg.selectAll('path')
  .data(triangles)
  .enter()
  .append('path')
  .attr('d', arc)
  .attr('fill', 'red')
  .attr('stroke', '#000')
  .attr('stroke-width', 1)
  .attr('transform', function(d) {
    return "translate(" + d.x + ",30) rotate(180)";
  });
<script src="https://d3js.org/d3.v4.min.js"></script>
<svg></svg>