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

« back to all changes in this revision

Viewing changes to systemtap/org.eclipse.linuxtools.threadprofiler/src/org/eclipse/linuxtools/internal/threadprofiler/graphs/MultiGraph.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
package org.eclipse.linuxtools.internal.threadprofiler.graphs;
 
2
 
 
3
import java.util.Iterator;
 
4
 
 
5
import org.eclipse.linuxtools.internal.threadprofiler.CircularPointBuffer;
 
6
import org.eclipse.linuxtools.internal.threadprofiler.DataPoint;
 
7
import org.eclipse.linuxtools.internal.threadprofiler.GraphPointBuffer;
 
8
import org.eclipse.swt.graphics.Color;
 
9
import org.eclipse.swt.graphics.GC;
 
10
 
 
11
public class MultiGraph extends GraphModel {
 
12
 
 
13
        /** Width of lines/squares for legend symbols */
 
14
        private static final int LEGEND_WIDTH = 10;
 
15
        /** Extra space to leave between legend entries */
 
16
        private static final int LEGEND_SEPARATION = LEGEND_WIDTH*2;
 
17
        /** 1.5 times LEGEND_WIDTH */
 
18
        private static final int LEGEND_WIDTH_AND_HALF = LEGEND_WIDTH + LEGEND_WIDTH/2;
 
19
        
 
20
        public static final int GRAPH_STYLE_FILL = 0;
 
21
        public static final int GRAPH_STYLE_LINE = 1;
 
22
        private int defaultStyle = GRAPH_STYLE_FILL;
 
23
        private int bufferSize = BUFFER_SIZE;
 
24
 
 
25
        public MultiGraph(String name, String units, int x, int y, int type) {
 
26
                super(name, units, x, y, type);
 
27
        }
 
28
        
 
29
        public void setDefaultStyle(int style) {
 
30
                defaultStyle = style;
 
31
        }
 
32
        
 
33
        public void setDefaultSize(int size) {
 
34
                bufferSize= size;
 
35
        }
 
36
 
 
37
        @Override
 
38
        public void draw(GC gc) {
 
39
                Color color = colorScheme.defaultColor;
 
40
                Iterator<Color> it = colorScheme.getIterator();
 
41
                
 
42
                for (CircularPointBuffer b : data) {
 
43
                        if (! (b instanceof GraphPointBuffer)) {
 
44
                                continue;
 
45
                        }
 
46
                        if (it.hasNext())
 
47
                                color = it.next();
 
48
                        gc.setLineWidth(2);
 
49
                        GraphPointBuffer buf = (GraphPointBuffer) b;
 
50
                        
 
51
                        switch (buf.getStyle()) {
 
52
                        case GRAPH_STYLE_LINE :
 
53
                                gc.setForeground(color);
 
54
                                drawLineGraph(gc, buf.getIterator());
 
55
                                break;
 
56
                        case GRAPH_STYLE_FILL :
 
57
                                gc.setBackground(color);
 
58
                                drawFillPolygonGraph(gc, buf.getIterator());
 
59
                                break;
 
60
                        default :
 
61
                                break;
 
62
                        }
 
63
                }
 
64
                drawAxis(gc);
 
65
                drawLegend(gc);
 
66
        }
 
67
        
 
68
        private void drawLegend(GC gc) {
 
69
                int x = getXOffset() + gc.getFontMetrics().getAverageCharWidth() * getTitle().length() + LEGEND_SEPARATION + LEGEND_WIDTH_AND_HALF;
 
70
                
 
71
                Color color = colorScheme.defaultColor;
 
72
                Iterator<Color> it = colorScheme.getIterator();
 
73
                
 
74
                for (CircularPointBuffer b : data) {
 
75
                        if (! (b instanceof GraphPointBuffer)) {
 
76
                                continue;
 
77
                        }
 
78
                        if (it.hasNext())
 
79
                                color = it.next(); 
 
80
                        GraphPointBuffer buf = (GraphPointBuffer) b;
 
81
                        
 
82
                        switch(buf.getStyle()) {
 
83
                        case GRAPH_STYLE_LINE :
 
84
                                gc.setLineWidth(2);
 
85
                                gc.setForeground(color);
 
86
                                gc.drawLine(x, getYOffset() + LEGEND_WIDTH_AND_HALF, x + LEGEND_WIDTH, getYOffset() + LEGEND_WIDTH_AND_HALF);
 
87
                                break;
 
88
                        case GRAPH_STYLE_FILL : 
 
89
                                gc.setBackground(color);
 
90
                                gc.fillRectangle(x, getYOffset() + LEGEND_WIDTH, LEGEND_WIDTH, LEGEND_WIDTH);
 
91
                                break;
 
92
                        default :
 
93
                                break;
 
94
                        }
 
95
                        
 
96
                        gc.setForeground(colorScheme.getFontColor());
 
97
                        gc.drawText(buf.getName(), x + LEGEND_WIDTH_AND_HALF, getYOffset() + LEGEND_WIDTH/2, true);
 
98
                        
 
99
                        x += gc.getFontMetrics().getAverageCharWidth() * buf.getName().length() + LEGEND_SEPARATION + LEGEND_WIDTH_AND_HALF;
 
100
                }
 
101
                
 
102
        }
 
103
 
 
104
        public void addBuffer(String name) {
 
105
                addBuffer(bufferSize, name, defaultStyle);
 
106
        }
 
107
        
 
108
        public void addBuffer(int bufferSize, String name) {
 
109
                addBuffer(bufferSize, name, defaultStyle);
 
110
        }
 
111
        
 
112
        public void addBuffer(String name, int graphStyle) {
 
113
                addBuffer(bufferSize, name, graphStyle);
 
114
        }
 
115
        
 
116
        public void addBuffer(int bufferSize, String name, int graphStyle) {
 
117
                data.add(new GraphPointBuffer(bufferSize, graphStyle, name));
 
118
        }
 
119
 
 
120
        protected void drawFillPolygonGraph(GC gc, Iterator<DataPoint> nextPointBuffer) {
 
121
                DataPoint pp = new DataPoint(getXOffset(), getYOffset(), 0);
 
122
                double xSeg = this.getXOffset();
 
123
                double increment = getXIncrement(gc);
 
124
                while (nextPointBuffer.hasNext()) {
 
125
                        gc.setAlpha(170);
 
126
                        DataPoint nextPoint = new DataPoint((int) (xSeg + 0.5), this.transform(nextPointBuffer.next()).getY(), 0);
 
127
                        //Use old point, new point, and the corresponding intersections with the x-axis to fill
 
128
                        gc.fillPolygon(new int[] {pp.getX(), pp.getY(), nextPoint.getX(), nextPoint.getY(), nextPoint.getX(), getYOffset(), pp.getX(), getYOffset()});
 
129
                        xSeg += increment;
 
130
                        pp = nextPoint;
 
131
        }
 
132
        }
 
133
        
 
134
        protected void drawLineGraph(GC gc, Iterator<DataPoint> nextPointBuffer) {
 
135
                DataPoint pp = new DataPoint(getXOffset(), getYOffset(), 0);
 
136
                double xSeg = this.getXOffset();
 
137
                double increment = getXIncrement(gc);
 
138
                
 
139
                while (nextPointBuffer.hasNext()) {
 
140
                        DataPoint nextPoint = new DataPoint((int) (xSeg + 0.5), this.transform(nextPointBuffer.next()).getY(), 0);
 
141
                        xSeg += increment;
 
142
                        drawLine(gc, pp, nextPoint);
 
143
                        pp = nextPoint;
 
144
        }
 
145
        }
 
146
 
 
147
}