2016-06-24 116 views
1

我想有一個響應SVG的折線圖。這應該有三個結合了固定和可伸縮元素的領域。SVG縮放的折線圖

chart scaling

粉色元件應的縮放子SVG(I需要定義一個子座標系用於繪製折線),灰色比特用於牽引軸,並應具有有限的縮放,因爲文本應該以相對簡單的字體呈現。

最後,我想這樣做沒有JavaScript。


一些我所面臨和存在的問題可能與解決方案幫助:

  1. 如果我可以給<svg>元素是以下x="20px" width="calc(100% - 20px)" height="calc(100% - 20px)"的粉紅色區域,這將在很大程度上解決了我的問題,但是這會觸發解析錯誤。我還沒有設法使用CSS來做任何變化。

回答

0

(另一個答案是非常值得的,如果有人可以用「純」svg做到這一點)。

我使用HTML佈局和多個SVG攻擊了它。這是我使用的代碼:

<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>