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
9
* IBM Corporation - Jeff Briggs, Henry Hughes, Ryan Morse
10
*******************************************************************************/
12
package org.eclipse.linuxtools.systemtap.ui.graphingapi.ui.widgets;
14
import java.text.DecimalFormat;
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;
26
* The Axis primitive, used to draw gridlines and axes on graphs.
27
* @author Henry Hughes
30
public class GraphAxis implements IGraphPrimitive {
31
public GraphAxis(AGraph graph, String title, int tickCount, int type) {
33
this.type = type&1; //Ensure type matches one of the possible values
34
this.tickCount = tickCount;
36
this.color = graph.axisColor;
39
public void setTickCount(int count) {
43
public int getType() {
47
public boolean isVisible() {
52
* Determines if the given point is inside this axis' bounds.
54
public boolean isUnder(Point loc) {
55
if(type==VERTICAL && loc.x < graph.getXPadding() ||
56
type==HORIZONTAL && loc.y > graph.getSize().y-graph.getYPadding())
61
public void calculateBounds() {
62
x1 = graph.getXPadding();
63
y2 = graph.getSize().y-graph.getYPadding();
64
locationX = graph.getXPadding();
67
if(type == HORIZONTAL) {
68
locationY = graph.getYPadding();
69
y1 = graph.getSize().y-graph.getYPadding();
70
x2 = graph.getSize().x-graph.getXPadding();
72
y2a = graph.getSize().y-graph.getYPadding();
74
widthX = graph.getSize().x - (graph.getXPadding()<<1);
76
tickIncrament = ((graph.getLocalXMax()-graph.getLocalXMin())/(double)tickCount);
77
range = graph.getLocalXMax() - graph.getLocalXMin();
78
localMin = graph.getLocalXMin();
80
locationY = graph.getSize().y - graph.getYPadding();
81
y1 = graph.getYPadding();
82
x2 = graph.getXPadding();
83
x2a = graph.getSize().x-graph.getXPadding();
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();
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.
100
protected int stringWidth(GC gc, String s) {
102
for(int i=0; i<s.length(); i++)
103
width += gc.getCharWidth(s.charAt(i));
109
* Draws the grid line for the given coordinates if grid lines are enabled.
111
protected void drawGridLine(GC gc, int x1, int y1, int x2, int y2) {
113
gc.setLineStyle(SWT.LINE_DOT);
114
gc.drawLine(x1,y1,x2,y2);
119
* Graphs the tick at the given location. Places the given string near the tick.
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));
130
else if(x > graph.getSize().x-strWidth)
131
x = graph.getSize().x-strWidth;
136
else if(y > graph.getSize().y-strHeight)
137
y = graph.getSize().y-strHeight;
138
gc.drawText(val, x, y);
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
147
* Converts units on the input value using SI prefixes (1 million becomes 1M, etc)
149
protected String getLabel(double val, int range) {
162
DecimalFormat format = new DecimalFormat(pattern);
163
return format.format(val) + PREFIXES[metric];
166
public void paint(GC gc) {
169
Color foreground = gc.getForeground();
170
gc.setForeground(color);
172
gc.drawLine(x1, y1, x2, y2);
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));
178
locationX += (widthX/(double)tickCount);
179
x2a += (widthX/(double)tickCount);
180
locationY -= (widthY/(double)tickCount);
181
y2a -= (widthY/(double)tickCount);
182
tickAmount += tickIncrament;
185
gc.setForeground(foreground);
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") } ;
194
protected int tickCount;
195
protected final AGraph graph;
196
protected Color color;
197
@SuppressWarnings("unused")
198
private String title;
200
protected int x1, y1, x2, y2;
201
protected int widthX, widthY, range, localMin;
202
protected double locationX, locationY, x2a, y2a, tickAmount, tickIncrament;
b'\\ No newline at end of file'