【发布时间】:2019-07-20 22:59:31
【问题描述】:
我有动态创建的 SVG 条形图(SQL 和 PHP)。 目前,为了创建优化的尺寸,我为每个图表创建 3 个 SVG 图表,并使用带有媒体查询的 CSS 来根据设备显示正确的图表。
我显然更喜欢输出一个 SVG 图表并将其缩放到适用于每个设备的任何尺寸,这将使我的代码减少 3!
但每次我尝试使用 viewBox 或宽度/高度属性缩放我的内联代码时,我的图表要么显示被截断,要么根本不显示。
我希望我的 Code Pen 链接在这里可以正常工作:
https://codepen.io/DerekErb/pen/VowVaM
<div class="wst_chart grstd">
<svg version="1.2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" class="graph grstd" aria-labelledby="Total US Yearling Sales" role="img">
<g class="grid x-grid">
<line x1="80" x2="80" y1="5" y2="406"></line>
<line x1="800" x2="800" y1="5" y2="406"></line>
</g>
<g class="grid y-grid">
<line x1="70" x2="800" y1="400" y2="400"></line>
</g>
<g class="grid y-grid2">
<line x1="70" x2="800" y1="10" y2="10"></line>
<line x1="70" x2="800" y1="54" y2="54"></line>
<line x1="70" x2="800" y1="98" y2="98"></line>
<line x1="70" x2="800" y1="142" y2="142"></line>
<line x1="70" x2="800" y1="186" y2="186"></line>
<line x1="70" x2="800" y1="230" y2="230"></line>
<line x1="70" x2="800" y1="274" y2="274"></line>
<line x1="70" x2="800" y1="318" y2="318"></line>
<line x1="70" x2="800" y1="362" y2="362"></line>
</g>
<g class="labels y-labels">
<text x="60" y="16">530</text>
<text x="60" y="406">0</text>
</g>
<g class="labels label-title chartlabels">
<text x="60" y="160" class="y-label1" transform="rotate(270, 60,
200)" >US $ in Millions</text>
</g>
<g class="grid x-grid2" id="xGrid2">
<line x1="200" x2="200" y1="400" y2="411"></line>
<line x1="320" x2="320" y1="400" y2="411"></line>
<line x1="440" x2="440" y1="400" y2="411"></line>
<line x1="560" x2="560" y1="400" y2="411"></line>
<line x1="680" x2="680" y1="400" y2="411"></line>
</g>
<g class="labels x-labels">
<text x="140" y="424">2013</text>
</g>
<g class="bar">
<rect x="110" y="-10" width="64" height="285.76021132075"
transform="scale(1,-1) translate(0,-390)">
<animate attributeName="height" from="-10" to="285.76021132075"
dur="0.5s" fill="freeze" />
</rect>
</g>
<g class="labels x-labels">
<text x="260" y="424">2014</text>
</g>
<g class="bar">
<rect x="230" y="-10" width="64" height="287.41336981132"
transform="scale(1,-1) translate(0,-390)">
<animate attributeName="height" from="-10" to="287.41336981132"
dur="0.5s" fill="freeze" />
</rect>
</g>
<g class="labels x-labels">
<text x="380" y="424">2015</text>
</g>
<g class="bar">
<rect x="350" y="-10" width="64" height="299.57062641509"
transform="scale(1,-1) translate(0,-390)">
<animate attributeName="height" from="-10" to="299.57062641509"
dur="0.5s" fill="freeze" />
</rect>
</g>
<g class="labels x-labels">
<text x="500" y="424">2016</text>
</g>
<g class="bar">
<rect x="470" y="-10" width="64" height="282.86670566038"
transform="scale(1,-1) translate(0,-390)">
<animate attributeName="height" from="-10" to="282.86670566038"
dur="0.5s" fill="freeze" />
</rect>
</g>
<g class="labels x-labels">
<text x="620" y="424">2017</text>
</g>
<g class="bar">
<rect x="590" y="-10" width="64" height="323.23185283019"
transform="scale(1,-1) translate(0,-390)">
<animate attributeName="height" from="-10" to="323.23185283019"
dur="0.5s" fill="freeze" />
</rect>
</g>
<g class="labels x-labels">
<text x="740" y="424">2018</text>
</g>
<g class="bar">
<rect x="710" y="-10" width="64" height="384.99839716981"
transform="scale(1,-1) translate(0,-390)">
<animate attributeName="height" from="-10" to="384.99839716981"
dur="0.5s" fill="freeze" />
</rect>
</g>
<g class="labels x-labels">
<text x="440" y="448" class="label-title x-label">YEAR</text>
</g>
</svg>
</div>
非常感谢任何建议或帮助!
【问题讨论】: