Derek Erb
Derek Erb

Reputation: 130

SVG rotate text 180 degrees (flip) in group

I have an SVG vertical bar chart I created. I have animated the vertical bars from bottom to top. In order to more easily do this I applied a transformation to the bar group with scale and translate modifications to make my rectangles go from bottom to top and left to right.

Each group looks like this:

<g class="bar" transform="scale(1,-1) translate(0,-390)">

The problem is that the text labels I want to appear on top of each vertical bar are (logically) flipped 180 degrees.

<text x="110" y="252.1042">31.039</text>

I would like to flip text elements 180 degrees to sit nicely atop my vertical bars and I just can't figure the syntax...

I made a CodePen with the working code here: CodePen

Any suggestions or assistance would be most appreciated including RTFM with a reference as to where I should read.

Upvotes: 0

Views: 4181

Answers (2)

Paul LeBeau
Paul LeBeau

Reputation: 101820

IMO, the simplest solution is to just transform the <rect> and leave the text alone.

<g class="bar">
    <rect x="110" y="-10" width="64" height="242.1042"
          transform="scale(1,-1) translate(0,-390)">
    <animate attributeName="height" from="-10" to="242.1042" dur="0.5s" fill="freeze" />
    </rect>
    <text x="110" y="147.8958">31.039</text>
</g>

To get the text in the right place, just set the y to (400 - val), instead of (val).

.wst_chart {

/*	display: none; */

	text-align: center;
	background-color: white;
	width: 90%;
	border: 1px solid darkgray;

	margin: 0 auto 2% auto;
	padding-bottom: 0;
}

.graph {
	font-family: "Lucida Grande", "Lucida Sans Unicode", Verdana, Arial, Helvetica, sans-serif;

	padding: 1% 0 1% 0;

	height: 440px;
	width:  880px;
}

.graph .grid {
	stroke: #cccccc;
	stroke-dasharray: 0;
	stroke-width: 1;
}

.graph .x-grid, .graph .y-grid {
	stroke: black;
	stroke-width: 2;
}

.labels {
	font-size: 13px;
}

.label-title {
	font-weight: bold;
	font-size: 12px;
	fill: black;
}

.graph .labels.x-labels {
	text-anchor: middle;
}

.graph .labels.y-labels {
	text-anchor: end;
}

.graph .labels.y-labels2 {
	text-anchor: start;
}

.graph .y-label1, .graph .y-label2 {
	text-anchor: middle;
}

.x-labels, .label-title, .y-label1, .y-label2, .CPI, .ARun, .G1G2 {
	font-family: "Lucida Grande", "Lucida Sans Unicode", Verdana, Arial, Helvetica, sans-serif;
}

.graph .line {
	fill: none;
	stroke-width: 3;

	transition: 0.5s;
}

.graph .line2 {
	fill: none;
	stroke-width: 3;
	stroke-dasharray: 5;
}

.graph .lineCPI {
	stroke: orange;
}

.graph .labels .CPI {
	fill: orange;
}

.graph .lineARun {
	stroke: blue;
}

.graph .ARun {
	fill: blue;
}

.graph .lineG1G2 {
	stroke: purple;
}

.graph .G1G2 {
	fill: purple;
}

.graph .bar {
	fill: darkred;
}

.graph .bar:hover, .graph .bar:focus {
	fill: black;
}

.graph .bar text {
	visibility: hidden;
}

.graph .bar:hover text, .graph .bar:focus text {
	visibility: visible;
	fill: black;
}
<svg version="1.2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" class="graph" aria-labelledby="Sales Figures" role="img">
  
<title id="title">Sales Figures</title>

  <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">50</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)">Euros 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">2012</text>
</g>

  
  
<g class="bar">
    <rect x="110" y="-10" width="64" height="242.1042" transform="scale(1,-1) translate(0,-390)">
    <animate attributeName="height" from="-10" to="242.1042" dur="0.5s" fill="freeze" />
    </rect>
    <text x="110" y="147.8958">31.039</text>
</g>

<g class="labels x-labels">
    <text x="260" y="424">2013</text>
</g>
<g class="bar">
    <rect x="230" y="-10" width="64" height="283.6041" transform="scale(1,-1) translate(0,-390)">
    <animate attributeName="height" from="-10" to="283.6041" dur="0.5s" fill="freeze" />
    </rect>
    <text x="230" y="106.3959">36.3595</text>
</g>

<g class="labels x-labels">
    <text x="380" y="424">2014</text>
</g>
<g class="bar">
    <rect x="350" y="-10" width="64" height="331.9329" transform="scale(1,-1) translate(0,-390)">
    <animate attributeName="height" from="-10" to="331.9329" dur="0.5s" fill="freeze" />
    </rect>
    <text x="350" y="58.0671">42.5555</text>
</g>

<g class="labels x-labels">
    <text x="500" y="424">2015</text>
</g>
<g class="bar">
    <rect x="470" y="-10" width="64" height="330.8292" transform="scale(1,-1) translate(0,-390)">
    <animate attributeName="height" from="-10" to="330.8292" dur="0.5s" fill="freeze" />
    </rect>
    <text x="470" y="59.1708">42.414</text>
</g>

<g class="labels x-labels">
    <text x="620" y="424">2016</text>
</g>
<g class="bar">
    <rect x="590" y="-10" width="64" height="302.8233" transform="scale(1,-1) translate(0,-390)">
    <animate attributeName="height" from="-10" to="302.8233" dur="0.5s" fill="freeze" />
    </rect>
    <text x="590" y="87.1767">38.8235</text>
</g>

<g class="labels x-labels">
    <text x="740" y="424">2017</text>
</g>
<g class="bar">
    <rect x="710" y="-10" width="64" height="374.5716" transform="scale(1,-1) translate(0,-390)">
    <animate attributeName="height" from="-10" to="374.5716" dur="0.5s" fill="freeze" />
    </rect>
    <text x="710" y="15.4284">48.022</text>
</g>
<g class="labels x-labels">
    <text x="440" y="448" class="label-title x-label">YEAR</text>
</g>
</svg>

Upvotes: 1

user8811940
user8811940

Reputation:

I would probably do it without flipping the scene, but to answer your question, you can transform this:

<text x="230" y="293.6041">36.3595</text>

into this:

<text transform="translate(230, 293.6041) scale(1,-1)">36.3595</text>

The text will end a bit higher than it was, you can adjusting by changing a bit the translate y coordinate.

Upvotes: 0

Related Questions