source: branches/MetisMQI/src/main/java/weka/clusterers/forOPTICSAndDBScan/OPTICS_GUI/GraphPanel.java

Last change on this file was 29, checked in by gnappo, 15 years ago

Taggata versione per la demo e aggiunto branch.

File size: 12.1 KB
Line 
1/*
2 *    This program is free software; you can redistribute it and/or modify
3 *    it under the terms of the GNU General Public License as published by
4 *    the Free Software Foundation; either version 2 of the License, or
5 *    (at your option) any later version.
6 *
7 *    This program is distributed in the hope that it will be useful,
8 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 *    GNU General Public License for more details.
11 *
12 *    You should have received a copy of the GNU General Public License
13 *    along with this program; if not, write to the Free Software
14 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15 */
16
17/*
18 *    Copyright (C) 2004
19 *    & Matthias Schubert (schubert@dbs.ifi.lmu.de)
20 *    & Zhanna Melnikova-Albrecht (melnikov@cip.ifi.lmu.de)
21 *    & Rainer Holzmann (holzmann@cip.ifi.lmu.de)
22 */
23
24package weka.clusterers.forOPTICSAndDBScan.OPTICS_GUI;
25
26import weka.clusterers.forOPTICSAndDBScan.DataObjects.DataObject;
27import weka.core.FastVector;
28import weka.core.RevisionHandler;
29import weka.core.RevisionUtils;
30import weka.core.Utils;
31
32import java.awt.Color;
33import java.awt.Dimension;
34import java.awt.Graphics;
35import java.awt.event.MouseEvent;
36import java.awt.event.MouseMotionAdapter;
37
38import javax.swing.JComponent;
39
40/**
41 * <p>
42 * GraphPanel.java <br/>
43 * Authors: Rainer Holzmann, Zhanna Melnikova-Albrecht <br/>
44 * Date: Sep 16, 2004 <br/>
45 * Time: 10:28:19 AM <br/>
46 * $ Revision 1.4 $ <br/>
47 * </p>
48 *
49 * @author Zhanna Melnikova-Albrecht (melnikov@cip.ifi.lmu.de)
50 * @author Rainer Holzmann (holzmann@cip.ifi.lmu.de)
51 * @version $Revision: 1.4 $
52 */
53public class GraphPanel
54    extends JComponent
55    implements RevisionHandler {
56
57    /** for serialization */
58    private static final long serialVersionUID = 7917937528738361470L;
59
60    /**
61     * Holds the clustering results
62     */
63    private FastVector resultVector;
64
65    /**
66     * Holds the value that is multiplied with the original values of core- and reachability
67     * distances in order to get better graphical views
68     */
69    private int verticalAdjustment;
70
71    /**
72     * Specifies the color for displaying core-distances
73     */
74    private Color coreDistanceColor;
75
76    /**
77     * Specifies the color for displaying reachability-distances
78     */
79    private Color reachabilityDistanceColor;
80
81    /**
82     * Specifies the width for displaying the distances
83     */
84    private int widthSlider;
85
86    /**
87     * Holds the flag for showCoreDistances
88     */
89    private boolean showCoreDistances;
90
91    /**
92     * Holds the flag for showrRechabilityDistances
93     */
94    private boolean showReachabilityDistances;
95
96    /**
97     * Holds the index of the last toolTip
98     */
99    private int recentIndex = -1;
100
101    // *****************************************************************************************************************
102    // constructors
103    // *****************************************************************************************************************
104
105    public GraphPanel(FastVector resultVector,
106                      int verticalAdjustment,
107                      boolean showCoreDistances,
108                      boolean showReachbilityDistances) {
109        this.resultVector = resultVector;
110        this.verticalAdjustment = verticalAdjustment;
111        coreDistanceColor = new Color(100, 100, 100);
112        reachabilityDistanceColor = Color.orange;
113        widthSlider = 5;
114        this.showCoreDistances = showCoreDistances;
115        this.showReachabilityDistances = showReachbilityDistances;
116
117        addMouseMotionListener(new MouseHandler());
118    }
119
120    // *****************************************************************************************************************
121    // methods
122    // *****************************************************************************************************************
123
124    /**
125     * Draws the OPTICS Plot
126     * @param g
127     */
128    protected void paintComponent(Graphics g) {
129        if (isOpaque()) {
130            Dimension size = getSize();
131            g.setColor(getBackground());
132            g.fillRect(0, 0, size.width, size.height);
133        }
134
135        int stepSize = 0;
136        int cDist = 0;
137        int rDist = 0;
138
139        for (int vectorIndex = 0; vectorIndex < resultVector.size(); vectorIndex++) {
140            double coreDistance = ((DataObject) resultVector.elementAt(vectorIndex)).getCoreDistance();
141            double reachDistance = ((DataObject) resultVector.elementAt(vectorIndex)).getReachabilityDistance();
142
143            if (coreDistance == DataObject.UNDEFINED)
144                cDist = getHeight();
145            else
146                cDist = (int) (coreDistance * verticalAdjustment);
147
148            if (reachDistance == DataObject.UNDEFINED)
149                rDist = getHeight();
150            else
151                rDist = (int) (reachDistance * verticalAdjustment);
152
153            int x = vectorIndex + stepSize;
154
155            if (isShowCoreDistances()) {
156                /**
157                 * Draw coreDistance
158                 */
159                g.setColor(coreDistanceColor);
160                g.fillRect(x, getHeight() - cDist, widthSlider, cDist);
161            }
162
163            if (isShowReachabilityDistances()) {
164                int sizer = widthSlider;
165                if (!isShowCoreDistances()) sizer = 0;
166                /**
167                 * Draw reachabilityDistance
168                 */
169                g.setColor(reachabilityDistanceColor);
170                g.fillRect(x + sizer, getHeight() - rDist, widthSlider, rDist);
171            }
172
173            if (isShowCoreDistances() && isShowReachabilityDistances()) {
174                stepSize += (widthSlider * 2);
175            } else
176                stepSize += widthSlider;
177        }
178    }
179
180    /**
181     * Sets a new resultVector
182     * @param resultVector
183     */
184    public void setResultVector(FastVector resultVector) {
185        this.resultVector = resultVector;
186    }
187
188    /**
189     * Displays a toolTip for the selected DataObject
190     * @param toolTip
191     */
192    public void setNewToolTip(String toolTip) {
193        setToolTipText(toolTip);
194    }
195
196    /**
197     * Adjusts the size of this panel in respect of the shown content
198     * @param serObject SERObject that contains the OPTICS clustering results
199     */
200    public void adjustSize(SERObject serObject) {
201        int i = 0;
202        if (isShowCoreDistances() && isShowReachabilityDistances())
203            i = 10;
204        else if ((isShowCoreDistances() && !isShowReachabilityDistances()) ||
205                !isShowCoreDistances() && isShowReachabilityDistances())
206            i = 5;
207        setSize(new Dimension((i * serObject.getDatabaseSize()) +
208                serObject.getDatabaseSize(), getHeight()));
209        setPreferredSize(new Dimension((i * serObject.getDatabaseSize()) +
210                serObject.getDatabaseSize(), getHeight()));
211    }
212
213    /**
214     * Returns the flag for showCoreDistances
215     * @return True or false
216     */
217    public boolean isShowCoreDistances() {
218        return showCoreDistances;
219    }
220
221    /**
222     * Sets the flag for showCoreDistances
223     * @param showCoreDistances
224     */
225    public void setShowCoreDistances(boolean showCoreDistances) {
226        this.showCoreDistances = showCoreDistances;
227    }
228
229    /**
230     * Returns the flag for showReachabilityDistances
231     * @return True or false
232     */
233    public boolean isShowReachabilityDistances() {
234        return showReachabilityDistances;
235    }
236
237    /**
238     * Sets the flag for showReachabilityDistances
239     * @param showReachabilityDistances
240     */
241    public void setShowReachabilityDistances(boolean showReachabilityDistances) {
242        this.showReachabilityDistances = showReachabilityDistances;
243    }
244
245    /**
246     * Sets a new value for the vertical verticalAdjustment
247     * @param verticalAdjustment
248     */
249    public void setVerticalAdjustment(int verticalAdjustment) {
250        this.verticalAdjustment = verticalAdjustment;
251    }
252
253    /**
254     * Sets a new color for the coreDistance
255     * @param coreDistanceColor
256     */
257    public void setCoreDistanceColor(Color coreDistanceColor) {
258        this.coreDistanceColor = coreDistanceColor;
259        repaint();
260    }
261
262    /**
263     * Sets a new color for the reachabilityDistance
264     * @param reachabilityDistanceColor
265     */
266    public void setReachabilityDistanceColor(Color reachabilityDistanceColor) {
267        this.reachabilityDistanceColor = reachabilityDistanceColor;
268        repaint();
269    }
270
271    // *****************************************************************************************************************
272    // inner classes
273    // *****************************************************************************************************************
274
275    private class MouseHandler
276         extends MouseMotionAdapter
277         implements RevisionHandler {
278     
279        /**
280         * Invoked when the mouse button has been moved on a component
281         * (with no buttons no down).
282         */
283        public void mouseMoved(MouseEvent e) {
284            showToolTip(e.getX());
285        }
286
287        /**
288         * Shows a toolTip with the dataObjects parameters (c-dist, r-dist, key, attributes . . .)
289         * @param x MouseCoordinate X
290         * @return boolean
291         */
292        private boolean showToolTip(int x) {
293            int i = 0;
294            if (isShowCoreDistances() && isShowReachabilityDistances())
295                i = 11;
296            else if ((isShowCoreDistances() && !isShowReachabilityDistances()) ||
297                    !isShowCoreDistances() && isShowReachabilityDistances() ||
298                    !isShowCoreDistances() && !isShowReachabilityDistances())
299                i = 6;
300            if ((x / i) == recentIndex)
301                return false;
302            else
303                recentIndex = x / i;
304            DataObject dataObject = null;
305            try {
306                dataObject = (DataObject) resultVector.elementAt(recentIndex);
307            } catch (Exception e) {
308            }
309            if (dataObject != null) {
310                if (!isShowCoreDistances() && !isShowReachabilityDistances()) {
311                    setNewToolTip("<html><body><b>Please select a distance" +
312                            "</b></body></html>"
313                    );
314                } else
315                    setNewToolTip("<html><body><table>" +
316                            "<tr><td>DataObject:</td><td>" + dataObject + "</td></tr>" +
317                            "<tr><td>Key:</td><td>" + dataObject.getKey() + "</td></tr>" +
318                            "<tr><td>" +
319                            (isShowCoreDistances() ? "<b>" : "") + "Core-Distance:" +
320                            (isShowCoreDistances() ? "</b>" : "") +
321                            "</td><td>" +
322                            (isShowCoreDistances() ? "<b>" : "") +
323                            ((dataObject.getCoreDistance() == DataObject.UNDEFINED) ? "UNDEFINED" :
324                            Utils.doubleToString(dataObject.getCoreDistance(), 3, 5)) +
325                            (isShowCoreDistances() ? "</b>" : "") +
326                            "</td></tr>" +
327                            "<tr><td>" +
328                            (isShowReachabilityDistances() ? "<b>" : "") + "Reachability-Distance:" +
329                            (isShowReachabilityDistances() ? "</b>" : "") +
330                            "</td><td>" +
331                            (isShowReachabilityDistances() ? "<b>" : "") +
332                            ((dataObject.getReachabilityDistance() == DataObject.UNDEFINED) ? "UNDEFINED" :
333                            Utils.doubleToString(dataObject.getReachabilityDistance(), 3, 5)) +
334                            (isShowReachabilityDistances() ? "</b>" : "") +
335                            "</td></tr>" +
336                            "</table></body></html>"
337                    );
338            }
339            return true;
340        }
341       
342        /**
343         * Returns the revision string.
344         *
345         * @return              the revision
346         */
347        public String getRevision() {
348          return RevisionUtils.extract("$Revision: 1.4 $");
349        }
350    }
351   
352    /**
353     * Returns the revision string.
354     *
355     * @return          the revision
356     */
357    public String getRevision() {
358      return RevisionUtils.extract("$Revision: 1.4 $");
359    }
360}
Note: See TracBrowser for help on using the repository browser.