【发布时间】:2019-03-12 21:15:43
【问题描述】:
我正在制作这样的数据可视化:
使用viewBox来缩放SVG会很方便,这样图形的宽度就可以缩放到数据范围的宽度。
本例中的数据点是 12,549,因此我希望范围从 0 到 14,000,并使用宽度为 12,549 的 rect 将其呈现在宽度为 14,000 的 viewBox 中。换句话说,为我的数据使用自然单位。
但是当我这样做时,我的轴上的字体也缩放,10px的字体也缩放,变得非常小以至于看不到。
所以我需要一种在不缩放字体单位的情况下缩放绘图单位的方法,但我看不到这样做的方法。
更新
这里是有问题的代码:
<svg width="960" height="50" class="bullet2" style="margin-top: 10px;">
<svg viewBox="0 0 14000 25" preserveAspectRatio="none" width="100%" height="25" class="bars">
<rect x="0" y="0" width="14000" height="25" class="background"/>
<rect x="0" y="0" height="12.5" width="12549" class="item player"/>
<rect x="0" y="12.5" height="12.5" width="3750" class="item team-average"/>
<line x1="2000" x2="2000" y1="0" y2="25" class="marker" style="stroke-width: 29.1667px;"/>
</svg>
<g class="axis">
<g transform="translate(0, 25)" class="tick" style="opacity: 1;">
<line y1="0" y2="5"/>
<text text-anchor="middle" dy="1em" y="6">0</text>
</g>
<g transform="translate(140, 25)" class="tick" style="opacity: 1;">
<line y1="0" y2="5"/>
<text text-anchor="middle" dy="1em" y="6">2000</text>
</g>
<g transform="translate(280, 25)" class="tick" style="opacity: 1;">
<line y1="0" y2="5"/>
<text text-anchor="middle" dy="1em" y="6">4000</text>
</g>
<g transform="translate(420, 25)" class="tick" style="opacity: 1;">
<line y1="0" y2="5"/>
<text text-anchor="middle" dy="1em" y="6">6000</text>
</g>
<g transform="translate(560, 25)" class="tick" style="opacity: 1;">
<line y1="0" y2="5"/>
<text text-anchor="middle" dy="1em" y="6">8000</text>
</g>
<g transform="translate(700, 25)" class="tick" style="opacity: 1;">
<line y1="0" y2="5"/>
<text text-anchor="middle" dy="1em" y="6">10000</text>
</g>
<g transform="translate(840, 25)" class="tick" style="opacity: 1;">
<line y1="0" y2="5"/>
<text text-anchor="middle" dy="1em" y="6">12000</text>
</g>
<g transform="translate(980, 25)" class="tick" style="opacity: 1;">
<line y1="0" y2="5"/>
<text text-anchor="middle" dy="1em" y="6">14000</text>
</g>
</g>
</svg>
请注意内部svg 元素上的viewBox,按所述对其进行缩放。我尝试在外部svg 元素上使用相同的缩放比例,该元素还包括轴,但是当我这样做时,图像看起来像这样:
轴标签在那里,但它们也缩放了,14000像素缩放中的10px字体太小了,你看不到。
【问题讨论】: