最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

css - Add borders to SVG path d3 javascript - Stack Overflow

programmeradmin3浏览0评论

I want to have my svg paths (lines) have borders such that outlines are black, but the inside is another color. Line code:

self.lineFunction = function(dat) {
  var self = this
  var line = d3.svg.line().interpolate('linear');

  var data = dat.map(function(d) {
    return [self.xScale(d[0]), self.yScale(d[1].mean)];
  });

  return line(data);
}

self.lines = self.svg.selectAll('.line')
  .data(d3.keys(self.data), function(d) {return d})
  .enter()
  .append('path')
  .attr('d', function(d) {return self.lineFunction(self.data[d])})
  .attr('class', 'line')
  .style('stroke', 'blue')
  .style('stroke-width', '2')
  .style('fill', 'none');

I want to have my svg paths (lines) have borders such that outlines are black, but the inside is another color. Line code:

self.lineFunction = function(dat) {
  var self = this
  var line = d3.svg.line().interpolate('linear');

  var data = dat.map(function(d) {
    return [self.xScale(d[0]), self.yScale(d[1].mean)];
  });

  return line(data);
}

self.lines = self.svg.selectAll('.line')
  .data(d3.keys(self.data), function(d) {return d})
  .enter()
  .append('path')
  .attr('d', function(d) {return self.lineFunction(self.data[d])})
  .attr('class', 'line')
  .style('stroke', 'blue')
  .style('stroke-width', '2')
  .style('fill', 'none');
Share Improve this question edited Feb 16, 2017 at 23:38 Preview 35.8k10 gold badges94 silver badges113 bronze badges asked Dec 11, 2013 at 18:31 mikemike 23.8k32 gold badges81 silver badges100 bronze badges 4
  • You can't do this with a single line, but you could use a path element instead. – Lars Kotthoff Commented Dec 11, 2013 at 18:53
  • I see what you're saying @LarsKotthoff -- is there a way to convert my lineFunction code into a path generator that I can then add a border to? – mike Commented Dec 11, 2013 at 19:10
  • You could take the area generator, use your actual y coordinate for y0 and for y1 take the actual + a margin. Similar for x. – Lars Kotthoff Commented Dec 11, 2013 at 19:15
  • I think this is a more elegant approach than 2 lines -- I'd rather mark this as the answer if you'd like to make it one @LarsKotthoff – mike Commented Dec 11, 2013 at 19:48
Add a comment  | 

3 Answers 3

Reset to default 11

A forward-looking answer: if SVG2 was supported you could use the paint-order property (assuming the fill is opaque):

.pathWithBorder {
  paint-order: stroke fill;
  stroke-width: 1.8;
  stroke: black;
  fill: blue;
}

Then there's no need to duplicate the path element, and the stroke will only be visible outside the shape.

You have to create a slightly thinner line along the same path:

inner = self.svg
  .selectAll('.inner')
  .data(d3.keys(self.data), function(d) { return d; })
  .enter().append('path')
  .attr('d', function(d) {return self.lineFunction(self.data[d])})
  .attr('class', 'inner')
  .style('stroke', 'black')
  .style('stroke-width', '1.8')
  .style('fill', 'none');

This means you have two lines on top on one another, the lower one slightly protruding from the other, giving the impression of a border.

You can do this by replacing the line with a path. For this, you can use D3's area generator. It requires two coordinates for each "point", but you can do this by passing in the actual coordinate to x0 and the actual plus a margin to x1 (and similarly for y). This margin will determine the "thickness" of the line. You can set fill/stroke as usual for the path.

发布评论

评论列表(0)

  1. 暂无评论