css - SVG : viewbox property strech text inside

标签 css svg text viewbox

我在 svg 元素上有一个带有 viewbox 属性的 histo 条形图。

我找不到内部文本被拉伸(stretch)的解决方案,如下所示。

显然,我不想从 g 元素中外部化文本元素。

svg path {
    fill: none;
    stroke: #003B56;
}
<svg width="100%" height="100" viewBox="0 0 200 100" preserveAspectRatio="none">
    <g>
        <g transform="translate(0, 0)" class="2013">
            <rect class="ca" width="33.333333333333336" height="100" stroke-width="1" stroke="#ffffff" style="fill: rgb(239, 239, 239);" />
            <rect class="ca" width="16.666666666666668" shape-rendering="crispEdges" x="8.333333333333334" height="0" y="95" style="fill: rgb(58, 58, 58);" />
            <text shape-rendering="crispEdges" dy="10" dx="16.666666666666668" style="text-anchor: middle; font-size: 8px; fill: rgb(140, 140, 140); width: 33.3333%; height: 66.6667%;">2013</text>
            <rect class="marge" x="14.583333333333334" width="4.166666666666667" height="8.333333333333334" y="85" shape-rendering="crispEdges" visibility="hidden" style="fill: rgb(210, 210, 210);" />
        </g>
        <g transform="translate(33.333333333333336, 0)" class="2014">
            <rect class="ca" width="33.333333333333336" height="100" stroke-width="1" stroke="#ffffff" style="fill: rgb(239, 239, 239);" />
            <rect class="ca" width="16.666666666666668" shape-rendering="crispEdges" x="8.333333333333334" height="0" y="95" style="fill: rgb(58, 58, 58);" />
            <text shape-rendering="crispEdges" dy="10" dx="16.666666666666668" style="text-anchor: middle; font-size: 8px; fill: rgb(140, 140, 140); width: 33.3333%; height: 66.6667%;">2014</text>
            <rect class="marge" x="14.583333333333334" width="4.166666666666667" height="8.333333333333334" y="85" shape-rendering="crispEdges" visibility="hidden" style="fill: rgb(210, 210, 210);" />
        </g>
        <g transform="translate(66.66666666666667, 0)" class="2015">
            <rect class="ca" width="33.333333333333336" height="100" stroke-width="1" stroke="#ffffff" style="fill: rgb(239, 239, 239);" />
            <rect class="ca" width="16.666666666666668" shape-rendering="crispEdges" x="8.333333333333334" height="0" y="95" style="fill: rgb(58, 58, 58);" />
            <text shape-rendering="crispEdges" dy="10" dx="16.666666666666668" style="text-anchor: middle; font-size: 8px; fill: rgb(140, 140, 140); width: 33.3333%; height: 66.6667%;">2015</text>
            <rect class="marge" x="14.583333333333334" width="4.166666666666667" height="8.333333333333334" y="85" shape-rendering="crispEdges" visibility="hidden" style="fill: rgb(210, 210, 210);" />
        </g>
        <g transform="translate(100, 0)" class="2015">
            <rect class="ca" width="33.333333333333336" height="100" stroke-width="1" stroke="#ffffff" style="fill: rgb(239, 239, 239);" />
            <rect class="ca" width="16.666666666666668" shape-rendering="crispEdges" x="8.333333333333334" height="65.32792098352175" y="29.67207901647825" style="fill: rgb(58, 58, 58);" />
            <text shape-rendering="crispEdges" dy="10" dx="16.666666666666668" style="text-anchor: middle; font-size: 8px; fill: rgb(140, 140, 140); width: 33.3333%; height: 66.6667%;">2015</text>
            <rect class="marge" x="14.583333333333334" width="4.166666666666667" height="8.333333333333334" y="75.76149333828752" shape-rendering="crispEdges" visibility="visible" style="fill: rgb(210, 210, 210);" />
        </g>
        <g transform="translate(133.33333333333334, 0)" class="2016">
            <rect class="ca" width="33.333333333333336" height="100" stroke-width="1" stroke="#ffffff" style="fill: rgb(239, 239, 239);" />
            <rect class="ca" width="16.666666666666668" shape-rendering="crispEdges" x="8.333333333333334" height="70" y="25" style="fill: rgb(58, 58, 58);" />
            <text shape-rendering="crispEdges" dy="10" dx="16.666666666666668" style="text-anchor: middle; font-size: 8px; fill: rgb(140, 140, 140); width: 33.3333%; height: 66.6667%;">2016</text>
            <rect class="marge" x="14.583333333333334" width="4.166666666666667" height="8.333333333333334" y="73.65632455308725" shape-rendering="crispEdges" visibility="visible" style="fill: rgb(210, 210, 210);" />
        </g>
        <g transform="translate(166.66666666666669, 0)" class="2017">
            <rect class="ca" width="33.333333333333336" height="100" stroke-width="1" stroke="#ffffff" style="fill: rgb(239, 239, 239);" />
            <rect class="ca" width="16.666666666666668" shape-rendering="crispEdges" x="8.333333333333334" height="27.544350652682226" y="67.45564934731777" style="fill: rgb(58, 58, 58);" />
            <text shape-rendering="crispEdges" dy="10" dx="16.666666666666668" style="text-anchor: middle; font-size: 8px; fill: rgb(140, 140, 140); width: 33.3333%; height: 66.6667%;">2017</text>
            <rect class="marge" x="14.583333333333334" width="4.166666666666667" height="8.333333333333334" y="81.50817238857238" shape-rendering="crispEdges" visibility="visible" style="fill: rgb(210, 210, 210);" />
        </g>
        <path transform="translate(114.58333333333333, 20)" class="data" d="M0,60.761493338287515L33.333333333333336,58.656324553087245L66.66666666666667,66.50817238857238" style="stroke: rgb(210, 210, 210);" />
    </g>
</svg>

最佳答案

您最好将 SVG 分成两部分:拉伸(stretch)条和未拉伸(stretch)标题。对条形图使用带有 preserveAspectRatio="none" 的内部 svg,并使用百分比定位标题。您不能为此使用 transform 属性。

svg path {
    fill: none;
    stroke: #003B56;
}
<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100">
    <svg width="100%" height="100" viewBox="0 0 200 100" preserveAspectRatio="none">
        <g transform="translate(0, 0)" class="2013">
            <rect class="ca" width="33.333333333333336" height="100" stroke-width="1" stroke="#ffffff" style="fill: rgb(239, 239, 239);" />
            <rect class="ca" width="16.666666666666668" shape-rendering="crispEdges" x="8.333333333333334" height="0" y="95" style="fill: rgb(58, 58, 58);" />
            <rect class="marge" x="14.583333333333334" width="4.166666666666667" height="8.333333333333334" y="85" shape-rendering="crispEdges" visibility="hidden" style="fill: rgb(210, 210, 210);" />
        </g>
        <g transform="translate(33.333333333333336, 0)" class="2014">
            <rect class="ca" width="33.333333333333336" height="100" stroke-width="1" stroke="#ffffff" style="fill: rgb(239, 239, 239);" />
            <rect class="ca" width="16.666666666666668" shape-rendering="crispEdges" x="8.333333333333334" height="0" y="95" style="fill: rgb(58, 58, 58);" />
            <rect class="marge" x="14.583333333333334" width="4.166666666666667" height="8.333333333333334" y="85" shape-rendering="crispEdges" visibility="hidden" style="fill: rgb(210, 210, 210);" />
        </g>
        <g transform="translate(66.66666666666667, 0)" class="2015">
            <rect class="ca" width="33.333333333333336" height="100" stroke-width="1" stroke="#ffffff" style="fill: rgb(239, 239, 239);" />
            <rect class="ca" width="16.666666666666668" shape-rendering="crispEdges" x="8.333333333333334" height="0" y="95" style="fill: rgb(58, 58, 58);" />
            <rect class="marge" x="14.583333333333334" width="4.166666666666667" height="8.333333333333334" y="85" shape-rendering="crispEdges" visibility="hidden" style="fill: rgb(210, 210, 210);" />
        </g>
        <g transform="translate(100, 0)" class="2015">
            <rect class="ca" width="33.333333333333336" height="100" stroke-width="1" stroke="#ffffff" style="fill: rgb(239, 239, 239);" />
            <rect class="ca" width="16.666666666666668" shape-rendering="crispEdges" x="8.333333333333334" height="65.32792098352175" y="29.67207901647825" style="fill: rgb(58, 58, 58);" />
            <rect class="marge" x="14.583333333333334" width="4.166666666666667" height="8.333333333333334" y="75.76149333828752" shape-rendering="crispEdges" visibility="visible" style="fill: rgb(210, 210, 210);" />
        </g>
        <g transform="translate(133.33333333333334, 0)" class="2016">
            <rect class="ca" width="33.333333333333336" height="100" stroke-width="1" stroke="#ffffff" style="fill: rgb(239, 239, 239);" />
            <rect class="ca" width="16.666666666666668" shape-rendering="crispEdges" x="8.333333333333334" height="70" y="25" style="fill: rgb(58, 58, 58);" />
            <rect class="marge" x="14.583333333333334" width="4.166666666666667" height="8.333333333333334" y="73.65632455308725" shape-rendering="crispEdges" visibility="visible" style="fill: rgb(210, 210, 210);" />
        </g>
        <g transform="translate(166.66666666666669, 0)" class="2017">
            <rect class="ca" width="33.333333333333336" height="100" stroke-width="1" stroke="#ffffff" style="fill: rgb(239, 239, 239);" />
            <rect class="ca" width="16.666666666666668" shape-rendering="crispEdges" x="8.333333333333334" height="27.544350652682226" y="67.45564934731777" style="fill: rgb(58, 58, 58);" />
            <rect class="marge" x="14.583333333333334" width="4.166666666666667" height="8.333333333333334" y="81.50817238857238" shape-rendering="crispEdges" visibility="visible" style="fill: rgb(210, 210, 210);" />
        </g>
        <path transform="translate(114.58333333333333, 20)" class="data" d="M0,60.761493338287515L33.333333333333336,58.656324553087245L66.66666666666667,66.50817238857238" style="stroke: rgb(210, 210, 210);" />
    </svg>
    <g shape-rendering="crispEdges" style="text-anchor: middle; font-size: 8px; fill: rgb(140, 140, 140);">
        <text dy="10" dx="8.3333333333333333%">2013</text>
        <text dy="10" dx="25%">2014</text>
        <text dy="10" dx="41.666666666666667%">2015</text>
        <text dy="10" dx="58.333333333333333%">2015</text>
        <text dy="10" dx="75%">2016</text>
        <text dy="10" dx="91.666666666666667%">2017</text>
    </g>
</svg>

您可以使用水平值的百分比(xwidth,同样没有任何地方的 transform 属性)同样定位矩形,并避免将它们放在内部 svg 中。尽管如此,对于路径来说,这是不可能的。

关于css - SVG : viewbox property strech text inside,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44655427/

相关文章:

css - 停止 CSS3 动画跳跃

css - 内联 SVG 在 IE 中不能用作背景图像

python - WebDriver:为什么 send_keys 在 find_element_by_xpath 之后不起作用?

基于 Java 文本的应用程序

javascript - 需要帮助在 Javascript 中重复更新一些文本

javascript - 使隐藏元素出现 X 秒

css - 彩色网页字体

html - 用于 Web 开发的 9-patch 图像

css - 图像不使用 css 自动宽度缩放

javascript - 节点大小与 D3 中的 child 数量成正比