简体   繁体   English

线图的SVG缩放

[英]SVG Scaling for Line Chart

I would like to have a responsive SVG for a line chart. 我想为折线图提供响应式SVG。 This should have three areas that combine fixed and scalable elements. 这应该有三个区域,它们结合了固定和可扩展的元素。

图表缩放

The pink element should be a scaling child svg (I need to define a child coordinate system for drawing a polyline), the grey bits are used for drawing axis, and should have limited scaling, since text should render in a relativaly simple font. 粉红色元素应该是缩放子svg(我需要定义用于绘制折线的子坐标系),灰色位用于绘制轴,并且应该具有有限的缩放,因为文本应该以相对简单的字体呈现。

Finally, I would like to do this without JavaScript. 最后,我想在没有JavaScript的情况下这样做。


Some of the problems I have faced and which might help with a solution: 我遇到的一些问题可能有助于解决方案:

  1. If I could give the <svg> element that is the pink area the following x="20px" width="calc(100% - 20px)" height="calc(100% - 20px)" , this would largely solve my problem, but this triggers a parse error. 如果我可以给粉红色区域的<svg>元素以下x="20px" width="calc(100% - 20px)" height="calc(100% - 20px)" ,这将在很大程度上解决我的问题,但这会触发解析错误。 I haven't managed to get any variations using CSS to work either. 我还没有设法使用CSS来实现任何变化。

(Another answer would be well worth it if someone can do this with "pure" svg). (如果有人能用“纯粹的svg”做到这一点,那么另一个答案是值得的。

I hacked it using HTML layout and multiple SVGs. 我使用HTML布局和多个SVG攻击它。 This is the code I am using: 这是我正在使用的代码:

 <div style="width: 100%; height: 300px"> <!-- The Y axis --> <svg style="width: 40px; height: calc(100% - 20px); float: left;"> <g class="yaxis"> <line x1="99%" x2="99%" y1="0%" y2="100%" stroke="black" vector-effect="non-scaling-stroke"></line> <g class="tick tick-yaxis"> <text x="99%" y="100%" text-anchor="end">$0.0</text> <line x1="98%" x2="100%" y1="100%" y2="100%" stroke="black"></line> </g> <!-- ... --> </g> </g> </svg> <!-- The actual chart --> <svg viewBox="0 0 100 100" preserveAspectRatio="none" style="width: calc(100% - 40px); height: calc(100% - 20px); float: left;"> <polyline points="0,50 45.46574414322495,0 65,100 75,100 89.52839708894965,0 91.10141646672459,100 98.21939424857649,0 100,100" vector-effect="non-scaling-stroke" stroke="red" fill="none"></polyline> </svg> <!-- x axis --> <svg style="clear: both; margin-left: 40px; width: calc(100% - 40px); height: 20px;"> <g class="xaxis"> <line x1="0%" x2="100%" y1="1%" y2="1%" stroke="black" vector-effect="non-scaling-stroke"></line> <g class="tick tick-xaxis"> <text x="5%" y="100%" text-anchor="middle">28 Feb 2011</text> <line x1="5%" x2="5%" y1="94%" y2="96%" stroke="black"></line> </g> </svg> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM