~ubuntu-branches/ubuntu/trusty/eclipse-linuxtools/trusty

« back to all changes in this revision

Viewing changes to systemtap/org.eclipse.linuxtools.systemtap.ui.graphingapi.ui/src/org/eclipse/linuxtools/systemtap/ui/graphingapi/ui/widgets/GraphAxis.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2012-06-29 12:07:30 UTC
  • Revision ID: package-import@ubuntu.com-20120629120730-bfri1xys1i71dpn6
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*******************************************************************************
 
2
 * Copyright (c) 2006 IBM Corporation.
 
3
 * All rights reserved. This program and the accompanying materials
 
4
 * are made available under the terms of the Eclipse Public License v1.0
 
5
 * which accompanies this distribution, and is available at
 
6
 * http://www.eclipse.org/legal/epl-v10.html
 
7
 *
 
8
 * Contributors:
 
9
 *     IBM Corporation - Jeff Briggs, Henry Hughes, Ryan Morse
 
10
 *******************************************************************************/
 
11
 
 
12
package org.eclipse.linuxtools.systemtap.ui.graphingapi.ui.widgets;
 
13
 
 
14
import java.text.DecimalFormat;
 
15
 
 
16
import org.eclipse.linuxtools.internal.systemtap.ui.graphingapi.ui.Localization;
 
17
import org.eclipse.linuxtools.systemtap.ui.graphingapi.ui.graphs.AGraph;
 
18
import org.eclipse.swt.SWT;
 
19
import org.eclipse.swt.graphics.Color;
 
20
import org.eclipse.swt.graphics.GC;
 
21
import org.eclipse.swt.graphics.Point;
 
22
 
 
23
 
 
24
 
 
25
/**
 
26
 * The Axis primitive, used to draw gridlines and axes on graphs.
 
27
 * @author Henry Hughes
 
28
 * @author Ryan Morse
 
29
 */
 
30
public class GraphAxis implements IGraphPrimitive {
 
31
        public GraphAxis(AGraph graph, String title, int tickCount, int type) {
 
32
                this.graph = graph;
 
33
                this.type = type&1;     //Ensure type matches one of the possible values
 
34
                this.tickCount = tickCount;
 
35
                this.title = title;
 
36
                this.color = graph.axisColor;
 
37
        }
 
38
 
 
39
        public void setTickCount(int count) {
 
40
                tickCount = count;
 
41
        }
 
42
        
 
43
        public int getType() {
 
44
                return type;
 
45
        }
 
46
        
 
47
        public boolean isVisible() {
 
48
                return true;
 
49
        }
 
50
 
 
51
        /**
 
52
         * Determines if the given point is inside this axis' bounds.
 
53
         */
 
54
        public boolean isUnder(Point loc) {
 
55
                if(type==VERTICAL && loc.x < graph.getXPadding() ||
 
56
                        type==HORIZONTAL && loc.y > graph.getSize().y-graph.getYPadding())
 
57
                        return true;
 
58
                return false;
 
59
        }
 
60
        
 
61
        public void calculateBounds() {
 
62
                x1 = graph.getXPadding();
 
63
                y2 = graph.getSize().y-graph.getYPadding();
 
64
                locationX = graph.getXPadding();
 
65
                tickAmount = 0;
 
66
 
 
67
                if(type == HORIZONTAL) {
 
68
                        locationY = graph.getYPadding();
 
69
                        y1 = graph.getSize().y-graph.getYPadding();
 
70
                        x2 = graph.getSize().x-graph.getXPadding();
 
71
                        x2a = (int)locationX;
 
72
                        y2a = graph.getSize().y-graph.getYPadding();
 
73
                        
 
74
                        widthX = graph.getSize().x - (graph.getXPadding()<<1);
 
75
                        widthY = 0;
 
76
                        tickIncrament = ((graph.getLocalXMax()-graph.getLocalXMin())/(double)tickCount);
 
77
                        range = graph.getLocalXMax() - graph.getLocalXMin();
 
78
                        localMin = graph.getLocalXMin();
 
79
                } else {
 
80
                        locationY = graph.getSize().y - graph.getYPadding();
 
81
                        y1 = graph.getYPadding();
 
82
                        x2 = graph.getXPadding();
 
83
                        x2a = graph.getSize().x-graph.getXPadding();
 
84
                        y2a = (int)locationY;
 
85
 
 
86
                        widthX = 0;
 
87
                        widthY = graph.getSize().y - (graph.getYPadding()<<1);
 
88
                        tickIncrament = ((graph.getLocalYMax()-graph.getLocalYMin())/(double)tickCount);
 
89
                        range = graph.getLocalYMax() - graph.getLocalYMin();
 
90
                        localMin = graph.getLocalYMin();
 
91
                }
 
92
        }
 
93
 
 
94
        /**
 
95
         * Calculates the width, in pixels, of the input string.
 
96
         * @param gc GC to use for the calculation.
 
97
         * @param s String to calculate.
 
98
         * @return Width of the string in pixels.
 
99
         */
 
100
        protected int stringWidth(GC gc, String s) {
 
101
                int width = 0;
 
102
                for(int i=0; i<s.length(); i++)
 
103
                        width += gc.getCharWidth(s.charAt(i));
 
104
                
 
105
                return width;
 
106
        }
 
107
        
 
108
        /**
 
109
         * Draws the grid line for the given coordinates if grid lines are enabled.
 
110
         */
 
111
        protected void drawGridLine(GC gc, int x1, int y1, int x2, int y2) {
 
112
                if(graph.showGrid) {
 
113
                        gc.setLineStyle(SWT.LINE_DOT);
 
114
                        gc.drawLine(x1,y1,x2,y2);
 
115
                }
 
116
        }
 
117
 
 
118
        /**
 
119
         * Graphs the tick at the given location. Places the given string near the tick.
 
120
         */
 
121
        protected void drawTick(GC gc, int x, int y, String val) {
 
122
                gc.setLineStyle(SWT.LINE_SOLID);
 
123
                int strWidth = stringWidth(gc, val);
 
124
                int strHeight = gc.getFontMetrics().getHeight();
 
125
                gc.drawLine(x, y, x+((HORIZONTAL==type) ? 0 : strWidth), y+((HORIZONTAL==type) ? -strHeight : 0));
 
126
                
 
127
                x -= (strWidth>>1);
 
128
                if(x < 0)
 
129
                        x = 0;
 
130
                else if(x > graph.getSize().x-strWidth)
 
131
                        x = graph.getSize().x-strWidth;
 
132
                
 
133
                y -= (strHeight>>1);
 
134
                if(y < 0)
 
135
                        y = 0;
 
136
                else if(y > graph.getSize().y-strHeight)
 
137
                        y = graph.getSize().y-strHeight;
 
138
                gc.drawText(val, x, y);
 
139
        }
 
140
 
 
141
        protected void drawTitle(GC gc) {
 
142
                //TODO: Implement this function.  Has to rotate text for vertical bars
 
143
                //http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/Rotateandflipanimage.htm
 
144
        }
 
145
        
 
146
        /**
 
147
         * Converts units on the input value using SI prefixes (1 million becomes 1M, etc)
 
148
         */
 
149
        protected String getLabel(double val, int range) {
 
150
                int metric = 0;
 
151
                String pattern=".0";
 
152
                range /= tickCount;
 
153
                while(val > 1000) {
 
154
                        val /= 1000;
 
155
                        metric++;
 
156
                        
 
157
                        range /= 10;
 
158
                        if(range < 1)
 
159
                                pattern += "0";
 
160
                }
 
161
 
 
162
                DecimalFormat format = new DecimalFormat(pattern);
 
163
                return format.format(val) + PREFIXES[metric];
 
164
        }
 
165
        
 
166
        public void paint(GC gc) {
 
167
                calculateBounds();
 
168
                
 
169
                Color foreground = gc.getForeground();
 
170
                gc.setForeground(color);
 
171
 
 
172
                gc.drawLine(x1, y1, x2, y2);
 
173
                drawTitle(gc);
 
174
                for(int i=0; i<=tickCount; i++) {
 
175
                        drawGridLine(gc, (int)locationX, (int)locationY, (int)x2a, (int)y2a);
 
176
                        drawTick(gc, (int)locationX, (int)y2a, getLabel(localMin+tickAmount, range));
 
177
 
 
178
                        locationX += (widthX/(double)tickCount);
 
179
                        x2a += (widthX/(double)tickCount);
 
180
                        locationY -= (widthY/(double)tickCount);
 
181
                        y2a -= (widthY/(double)tickCount);
 
182
                        tickAmount += tickIncrament;
 
183
 
 
184
                }
 
185
                gc.setForeground(foreground);
 
186
        }
 
187
 
 
188
        public static final int VERTICAL = 0;
 
189
        public static final int HORIZONTAL = 1;
 
190
        //kilo, mega, giga, tera, peta, exa, zetta, yotta
 
191
        protected static final String[] PREFIXES = { "", Localization.getString("GraphAxis.Kilo"), Localization.getString("GraphAxis.Mega"), Localization.getString("GraphAxis.Giga"), Localization.getString("GraphAxis.Tera"), Localization.getString("GraphAxis.Peta"), Localization.getString("GraphAxis.Exa"), Localization.getString("GraphAxis.Zetta"), Localization.getString("GraphAxis.Yotta") } ;
 
192
 
 
193
        protected int type;
 
194
        protected int tickCount;
 
195
        protected final AGraph graph;
 
196
        protected Color color;
 
197
        @SuppressWarnings("unused")
 
198
        private String title;
 
199
        
 
200
        protected int x1, y1, x2, y2;
 
201
        protected int widthX, widthY, range, localMin;
 
202
        protected double locationX, locationY, x2a, y2a, tickAmount, tickIncrament;
 
203
}
 
 
b'\\ No newline at end of file'