我正在动态创建SVG条形图(使用SQL和PHP),为了创建优化大小的图表,每个图表我会创建3个SVG图表,并使用CSS媒体查询来显示适合设备的图表。
显然,我希望输出一个SVG图表,并按比例缩放以适应每个设备的大小,这样可以减少我的代码量三分之一!
但是,每次尝试使用viewBox或width/height属性缩放我的内联代码时,我的图表要么被裁剪掉,要么根本无法显示。
希望我的 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>
任何建议或帮助将不胜感激!