Skip to content

Commit 620fc44

Browse files
committed
Fix SVG rotated labels
1 parent 2ca3475 commit 620fc44

File tree

4 files changed

+40
-11
lines changed

4 files changed

+40
-11
lines changed

org.gitools.heatmap/src/main/java/org/gitools/heatmap/decorator/Decoration.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,15 @@ public class Decoration implements Serializable {
3131
public static final String NONE = "None";
3232

3333
private String value;
34+
35+
private boolean rotate;
36+
3437
private Color bgColor;
3538

3639
public Decoration(String value, Color bgColor) {
3740
this.value = value;
3841
this.bgColor = bgColor;
42+
this.rotate = false;
3943
}
4044

4145
public Decoration() {
@@ -45,6 +49,7 @@ public Decoration() {
4549
public Decoration reset() {
4650
this.value = "";
4751
this.bgColor = Color.WHITE;
52+
this.rotate = false;
4853
return this;
4954
}
5055

@@ -71,4 +76,11 @@ public void setBgColor(Color bgColor) {
7176
this.bgColor = bgColor;
7277
}
7378

79+
public boolean isRotate() {
80+
return rotate;
81+
}
82+
83+
public void setRotate(boolean rotate) {
84+
this.rotate = rotate;
85+
}
7486
}

org.gitools.heatmap/src/main/java/org/gitools/heatmap/header/HeatmapColoredLabelsHeader.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141

4242
@XmlAccessorType(XmlAccessType.NONE)
4343
@XmlRootElement
44-
public class HeatmapColoredLabelsHeader extends HeatmapHeader {
44+
public class
45+
HeatmapColoredLabelsHeader extends HeatmapHeader {
4546

4647
private static final String THICKNESS_CHANGED = "thickness";
4748
private static final String SEPARATION_GRID_CHANGED = "separationGrid";
@@ -71,7 +72,7 @@ public HeatmapColoredLabelsHeader(HeatmapDimension hdim) {
7172

7273
size = 20;
7374
thickness = 14;
74-
margin = 1;
75+
margin = 10;
7576
separationGrid = true;
7677

7778
labelVisible = true;

org.gitools.ui.app/src/main/java/org/gitools/ui/app/heatmap/drawer/AbstractHeatmapDrawer.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public abstract class AbstractHeatmapDrawer {
4646
public static final int HIGHLIGHT_POLICY_FORCE = 1;
4747
public static final int HIGHLIGHT_POLICY_AVOID = 2;
4848

49+
private static final double radianAngle90 = (90.0 / 180.0) * Math.PI;
4950

5051
private boolean pictureMode;
5152

@@ -140,7 +141,7 @@ protected static void paintCell(Decoration decoration, Color gridColor, int grid
140141

141142
Font font = g.getFont();
142143

143-
boolean isRotated = !font.getTransform().isIdentity();
144+
boolean isRotated = decoration.isRotate();
144145

145146
int fontHeight = (int) font.getSize2D();
146147

@@ -166,11 +167,28 @@ protected static void paintCell(Decoration decoration, Color gridColor, int grid
166167
int superscriptEnd = text.length();
167168
AttributedString attText = new AttributedString(text);
168169
attText.addAttribute(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUPER, e_pos, superscriptEnd);
169-
g.drawString(attText.getIterator(), x + leftMargin, y + height - bottomMargin);
170+
if (isRotated) {
171+
g.rotate(radianAngle90);
172+
g.drawString(attText.getIterator(), y + height - bottomMargin, - x - leftMargin - 1);
173+
g.rotate(-radianAngle90);
174+
} else {
175+
g.drawString(attText.getIterator(), x + leftMargin, y + height - bottomMargin);
176+
}
170177
} else {
171-
g.drawString(text, x + leftMargin, y + height - bottomMargin);
172-
}
173178

179+
if (isRotated) {
180+
g.rotate(radianAngle90);
181+
g.drawString(text, y + height - bottomMargin, - x - leftMargin - 1);
182+
183+
if ("CoCA-08".equals(text)) {
184+
System.out.println("x = " + x + " leftMargin = " + leftMargin + " width = " + width + " fontHeight = " + fontHeight);
185+
}
186+
187+
g.rotate(-radianAngle90);
188+
} else {
189+
g.drawString(text, x + leftMargin, y + height - bottomMargin);
190+
}
191+
}
174192
}
175193
}
176194
}

org.gitools.ui.app/src/main/java/org/gitools/ui/app/heatmap/drawer/header/HeatmapColoredLabelsDrawer.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@
3737

3838
public class HeatmapColoredLabelsDrawer extends AbstractHeatmapHeaderDrawer<HeatmapColoredLabelsHeader> {
3939

40-
private static final double radianAngle90 = (90.0 / 180.0) * Math.PI;
41-
4240

4341
public HeatmapColoredLabelsDrawer(Heatmap heatmap, HeatmapDimension heatmapDimension, HeatmapColoredLabelsHeader header) {
4442
super(heatmap, heatmapDimension, header);
@@ -50,7 +48,7 @@ public void draw(Graphics2D g, Rectangle box, Rectangle clip) {
5048

5149

5250
prepareDraw(g, box);
53-
Font previousFont = rotateFont(g);
51+
Font previousFont = changeFont(g);
5452

5553
HeatmapColoredLabelsHeader header = getHeader();
5654
header.reset();
@@ -79,6 +77,7 @@ public void draw(Graphics2D g, Rectangle box, Rectangle clip) {
7977
decoration.reset();
8078
boolean highlighted = isHighlightedIndex(startGroupIndex);
8179
header.decorate(decoration, groupLabel, false);
80+
decoration.setRotate(true);
8281

8382
int fullSize = getHeatmapDimension().getFullSize();
8483
//gridSize = 0 (no grid) if same group
@@ -115,13 +114,12 @@ public HeatmapPosition getPosition(Point p) {
115114
return (isHorizontal() ? new HeatmapPosition(getHeatmap(), -1, index, label) : new HeatmapPosition(getHeatmap(), index, -1, label));
116115
}
117116

118-
private Font rotateFont(Graphics2D g) {
117+
private Font changeFont(Graphics2D g) {
119118

120119
Font previousFont = g.getFont();
121120
Font headerFont = getHeader().getLabelFont();
122121
g.setFont(headerFont);
123122
AffineTransform fontAT = new AffineTransform();
124-
fontAT.rotate(radianAngle90);
125123
g.setFont(getHeader().getLabelFont().deriveFont(fontAT));
126124

127125
return previousFont;

0 commit comments

Comments
 (0)