有没有办法围绕我用d3.path制作的角落或酒吧?我正在使用React Native ART。
现在我有这个:
_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>
)
答案 0 :(得分: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))
&#13;
body {
display: flex;
}
div {
margin-right: 15px;
}
svg {
fill: green;
}
&#13;
<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>
&#13;