I am totally new to SVG, so please bear with me. I read a lot of articles on the subject, and everyone is pointing to solutions like d3.js, which is, in my opinion, way to complex for the simple task that I have.
I need to make a graph with a Cartesian coordinate system, where (0,0) is at the lower left corner. It needs to have width, height and data within expressed in percentages, so everything scales with the page.
So, here is my code (to make things simple, only part of the graph is there):
<style>
.grid {stroke: white; stroke-width: 1; stroke-dasharray: 1 2}
.label{font-family: courier new; fill: white; font-size: 14px}
.data {stroke: white; stroke-width: 1}
</style>
<svg width="100%" height="100%">
<g class="x grid">
<line x1="0%" x2="0%" y1="80%" y2="100%"></line>
<line x1="10%" x2="10%" y1="80%" y2="100%"></line>
<line x1="20%" x2="20%" y1="80%" y2="100%"></line>
</g>
<g class="y grid">
<line x1="0%" x2="20%" y1="80%" y2="80%" ></line>
<line x1="0%" x2="20%" y1="90%" y2="90%" ></line>
<line x1="0%" x2="20%" y1="100%" y2="100%"></line>
</g>
<g class="x label">
<text x="10%" y="100%"> 1 minute </text>
<text x="20%" y="100%"> 2 minutes</text>
</g>
<g class="y label">
<text x="0%" y="80%"> 20% </text>
<text x="0%" y="90%"> 10% </text>
</g>
<g class="data">
<line x1="0%" x2="10%" y1="85%" y2="92%" ></line>
<line x1="10%" x2="20%" y1="92%" y2="88%" ></line>
</g>
</svg>
I wanted to use polygon
and path
for the data, so I can fill the area below the curve, but it doesn't like the percentages as values. Someone suggested using viewbox
to translate percentages to pixels, and then use pixels, but that messes up my grid. I would also like to have (0,0) at the lower left corner, so that my CGI doesn't have to do math on all the points that it needs to display. I tried transform="translate(0,100) scale(1,-1)"
but that doesn't work with percentages. I also tried transform="rotate(270)"
but when you reduce window width, graph height is reduced...
So, can someone kick-start me here, and help me set up a fluid, resizable graph with the origin in the lower left corner and colored area below the curve?
g
, it makes code simpler and cleaner. But there is one more thing that I would like to fix but I don't know how. I don't need to maintain aspect ratio. I want the graph to fill the page (or parentdiv
) and be flexible. Is that possible to achieve withviewport
? – Jesuitism