1 /*
2  * This file is part of ELKI:
3  * Environment for Developing KDD-Applications Supported by Index-Structures
4  *
5  * Copyright (C) 2018
6  * ELKI Development Team
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU Affero General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Affero General Public License for more details.
17  *
18  * You should have received a copy of the GNU Affero General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 package de.lmu.ifi.dbs.elki.visualization.visualizers.visunproj;
22 
23 import org.apache.batik.util.SVGConstants;
24 import org.w3c.dom.Element;
25 
26 import de.lmu.ifi.dbs.elki.visualization.VisualizationTask;
27 import de.lmu.ifi.dbs.elki.visualization.VisualizerContext;
28 import de.lmu.ifi.dbs.elki.visualization.css.CSSClass;
29 import de.lmu.ifi.dbs.elki.visualization.gui.VisualizationPlot;
30 import de.lmu.ifi.dbs.elki.visualization.projections.Projection;
31 import de.lmu.ifi.dbs.elki.visualization.style.StyleLibrary;
32 import de.lmu.ifi.dbs.elki.visualization.svg.SVGUtil;
33 import de.lmu.ifi.dbs.elki.visualization.visualizers.StaticVisualizationInstance;
34 import de.lmu.ifi.dbs.elki.visualization.visualizers.VisFactory;
35 import de.lmu.ifi.dbs.elki.visualization.visualizers.Visualization;
36 
37 /**
38  * Trivial "visualizer" that displays a static label. The visualizer is meant to
39  * be used for dimension labels in the overview.
40  *
41  * @author Erich Schubert
42  * @since 0.3
43  *
44  * @stereotype factory
45  * @navassoc - create - StaticVisualizationInstance
46  */
47 public class LabelVisualization implements VisFactory {
48   /**
49    * The label to render
50    */
51   private String label = "undefined";
52 
53   /**
54    * Flag to indicate rotated labels (90 deg to the left)
55    */
56   private boolean rotated = false;
57 
58   /**
59    * Constructor. Solely for API purposes (Parameterizable!)
60    */
LabelVisualization()61   public LabelVisualization() {
62     super();
63   }
64 
65   /**
66    * The actually used constructor - with a static label.
67    *
68    * @param label Label to use
69    */
LabelVisualization(String label)70   public LabelVisualization(String label) {
71     this(label, false);
72   }
73 
74   /**
75    * Constructor.
76    *
77    * @param label Label to use
78    * @param rotated Rotated 90 deg to the left
79    */
LabelVisualization(String label, boolean rotated)80   public LabelVisualization(String label, boolean rotated) {
81     super();
82     this.label = label;
83     this.rotated = rotated;
84   }
85 
86   @Override
processNewResult(VisualizerContext context, Object start)87   public void processNewResult(VisualizerContext context, Object start) {
88     // No auto discovery supported.
89   }
90 
91   @Override
makeVisualization(VisualizerContext context, VisualizationTask task, VisualizationPlot plot, double width, double height, Projection proj)92   public Visualization makeVisualization(VisualizerContext context, VisualizationTask task, VisualizationPlot plot, double width, double height, Projection proj) {
93     CSSClass cls = new CSSClass(plot, "unmanaged");
94     StyleLibrary style = context.getStyleLibrary();
95     double fontsize = style.getTextSize("overview.labels") / StyleLibrary.SCALE;
96     cls.setStatement(SVGConstants.CSS_FONT_SIZE_PROPERTY, SVGUtil.fmt(fontsize));
97     cls.setStatement(SVGConstants.CSS_FILL_PROPERTY, style.getTextColor("overview.labels"));
98     cls.setStatement(SVGConstants.CSS_FONT_FAMILY_PROPERTY, style.getFontFamily("overview.labels"));
99 
100     Element layer;
101     if(!rotated) {
102       layer = plot.svgText(width * .5, height * .5 + .35 * fontsize, this.label);
103       SVGUtil.setAtt(layer, SVGConstants.SVG_STYLE_ATTRIBUTE, cls.inlineCSS());
104       SVGUtil.setAtt(layer, SVGConstants.SVG_TEXT_ANCHOR_ATTRIBUTE, SVGConstants.SVG_MIDDLE_VALUE);
105     }
106     else {
107       layer = plot.svgText(height * -.5, width * .5 + .35 * fontsize, this.label);
108       SVGUtil.setAtt(layer, SVGConstants.SVG_STYLE_ATTRIBUTE, cls.inlineCSS());
109       SVGUtil.setAtt(layer, SVGConstants.SVG_TEXT_ANCHOR_ATTRIBUTE, SVGConstants.SVG_MIDDLE_VALUE);
110       SVGUtil.setAtt(layer, SVGConstants.SVG_TRANSFORM_ATTRIBUTE, "rotate(-90)");
111     }
112     return new StaticVisualizationInstance(context, task, plot, width, height, layer);
113   }
114 
115   @Override
allowThumbnails(VisualizationTask task)116   public boolean allowThumbnails(VisualizationTask task) {
117     return false;
118   }
119 }
120