React Native ART和D3:如何使d3.path的角落四舍五入?

问题描述 投票:1回答:1

有没有办法围绕我用d3.path制作的角落或酒吧?我正在使用React Native ART。

现在我有这个:

my square bar

但我想要圆角。我想做这样的事情:my rounded square bar

_createBarChart (x, y, w, h) {
  var path = d3.path.path()
  path.rect(x, y, w, h)
  return path
}

return (
  <View style={styles.container}>
    <Surface width={screen.width} height={200}>
      <Group x={0} y={180}>
        <Shape d={this._createBarChart(7, -35, 70, 35)} fill={color.purple} />
      </Group>
    </Surface>
  </View>
)
d3.js react-native
1个回答
0
投票

我使用此实用程序功能绘制带有圆角的rect(您可以指定哪些角不应该舍入):

function drawRoundedRect(
  xCoord,
  yCoord,
  width,
  height,
  radius,
  needRoundingTopLeft = true,
  needRoundingTopRight = true,
  needRoundingBottomLeft = true,
  needRoundingBottomRight = true
) {
  let retval;
  retval = 'M' + (xCoord + radius) + ',' + yCoord;
  retval += 'h' + (width - 2 * radius);

  if (needRoundingTopRight) {
    retval += 'a' + radius + ',' + radius + ' 0 0 1 ' + radius + ',' + radius;
  } else { retval += 'h' + radius; retval += 'v' + radius; }

  retval += 'v' + (height - 2 * radius);

  if (needRoundingBottomRight) {
    retval += 'a' + radius + ',' + radius + ' 0 0 1 ' + -radius + ',' + radius;
  } else { retval += 'v' + radius; retval += 'h' + -radius; }

  retval += 'h' + (2 * radius - width);

  if (needRoundingBottomLeft) {
    retval += 'a' + radius + ',' + radius + ' 0 0 1 ' + -radius + ',' + -radius;
  } else { retval += 'h' + -radius; retval += 'v' + -radius; }

  retval += 'v' + (2 * radius - height);

  if (needRoundingTopLeft) {
    retval += 'a' + radius + ',' + radius + ' 0 0 1 ' + radius + ',' + -radius;
  } else { retval += 'v' + -radius; retval += 'h' + radius; }

  retval += 'z';

  return retval;
}

您可以使用此代码在组件中创建方法,并以这种方式使用它:

_createBarChart(x, y, w, h) {
  return this._drawRoundedRect(x, y, w, h, 10)
}

_drawRoundedRect(xCoord, yCoord, width, height, radius, needRoundingTopLeft = true, needRoundingTopRight = true, needRoundingBottomLeft = true, needRoundingBottomRight = true) {
    /* code above */
}

return (
  <View style={styles.container}>
    <Surface width={screen.width} height={200}>
      <Group x={0} y={180}>
        <Shape d={this._createBarChart(7, -35, 70, 35)} fill={color.purple} />
      </Group>
    </Surface>
  </View>
)

在下面隐藏的代码段中查看工作演示:

function drawRoundedRect(
  xCoord,
  yCoord,
  width,
  height,
  radius,
  needRoundingTopLeft = true,
  needRoundingTopRight = true,
  needRoundingBottomLeft = true,
  needRoundingBottomRight = true
) {
  let retval;
  retval = 'M' + (xCoord + radius) + ',' + yCoord;
  retval += 'h' + (width - 2 * radius);

  if (needRoundingTopRight) {
    retval += 'a' + radius + ',' + radius + ' 0 0 1 ' + radius + ',' + radius;
  } else { retval += 'h' + radius; retval += 'v' + radius; }

  retval += 'v' + (height - 2 * radius);

  if (needRoundingBottomRight) {
    retval += 'a' + radius + ',' + radius + ' 0 0 1 ' + -radius + ',' + radius;
  } else { retval += 'v' + radius; retval += 'h' + -radius; }

  retval += 'h' + (2 * radius - width);

  if (needRoundingBottomLeft) {
    retval += 'a' + radius + ',' + radius + ' 0 0 1 ' + -radius + ',' + -radius;
  } else { retval += 'h' + -radius; retval += 'v' + -radius; }

  retval += 'v' + (2 * radius - height);

  if (needRoundingTopLeft) {
    retval += 'a' + radius + ',' + radius + ' 0 0 1 ' + radius + ',' + -radius;
  } else { retval += 'v' + -radius; retval += 'h' + radius; }

  retval += 'z';

  return retval;
}

d3.select('svg#all-round')
	.append('path')
  .attr('d', drawRoundedRect(0, 0, 50, 50, 10))
  
  d3.select('svg#only-top-round')
	.append('path')
  .attr('d', drawRoundedRect(0, 0, 50, 50, 10, true, true, false, false))
body {
  display: flex;
}

div {
  margin-right: 15px;
}

svg {
  fill: green;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.13.0/d3.js"></script>
<div>
  <h3>Round all:</h3>
  <svg id="all-round" width="60" height="60"></svg>
</div>
<div>
  <h3>Round only top:</h3>
  <svg id="only-top-round" width="60" height="60"></svg>
</div>
© www.soinside.com 2019 - 2024. All rights reserved.