1
package org.eclipse.linuxtools.internal.threadprofiler.graphs;
3
import java.util.Iterator;
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;
11
public class MultiGraph extends GraphModel {
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;
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;
25
public MultiGraph(String name, String units, int x, int y, int type) {
26
super(name, units, x, y, type);
29
public void setDefaultStyle(int style) {
33
public void setDefaultSize(int size) {
38
public void draw(GC gc) {
39
Color color = colorScheme.defaultColor;
40
Iterator<Color> it = colorScheme.getIterator();
42
for (CircularPointBuffer b : data) {
43
if (! (b instanceof GraphPointBuffer)) {
49
GraphPointBuffer buf = (GraphPointBuffer) b;
51
switch (buf.getStyle()) {
52
case GRAPH_STYLE_LINE :
53
gc.setForeground(color);
54
drawLineGraph(gc, buf.getIterator());
56
case GRAPH_STYLE_FILL :
57
gc.setBackground(color);
58
drawFillPolygonGraph(gc, buf.getIterator());
68
private void drawLegend(GC gc) {
69
int x = getXOffset() + gc.getFontMetrics().getAverageCharWidth() * getTitle().length() + LEGEND_SEPARATION + LEGEND_WIDTH_AND_HALF;
71
Color color = colorScheme.defaultColor;
72
Iterator<Color> it = colorScheme.getIterator();
74
for (CircularPointBuffer b : data) {
75
if (! (b instanceof GraphPointBuffer)) {
80
GraphPointBuffer buf = (GraphPointBuffer) b;
82
switch(buf.getStyle()) {
83
case GRAPH_STYLE_LINE :
85
gc.setForeground(color);
86
gc.drawLine(x, getYOffset() + LEGEND_WIDTH_AND_HALF, x + LEGEND_WIDTH, getYOffset() + LEGEND_WIDTH_AND_HALF);
88
case GRAPH_STYLE_FILL :
89
gc.setBackground(color);
90
gc.fillRectangle(x, getYOffset() + LEGEND_WIDTH, LEGEND_WIDTH, LEGEND_WIDTH);
96
gc.setForeground(colorScheme.getFontColor());
97
gc.drawText(buf.getName(), x + LEGEND_WIDTH_AND_HALF, getYOffset() + LEGEND_WIDTH/2, true);
99
x += gc.getFontMetrics().getAverageCharWidth() * buf.getName().length() + LEGEND_SEPARATION + LEGEND_WIDTH_AND_HALF;
104
public void addBuffer(String name) {
105
addBuffer(bufferSize, name, defaultStyle);
108
public void addBuffer(int bufferSize, String name) {
109
addBuffer(bufferSize, name, defaultStyle);
112
public void addBuffer(String name, int graphStyle) {
113
addBuffer(bufferSize, name, graphStyle);
116
public void addBuffer(int bufferSize, String name, int graphStyle) {
117
data.add(new GraphPointBuffer(bufferSize, graphStyle, name));
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()) {
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()});
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);
139
while (nextPointBuffer.hasNext()) {
140
DataPoint nextPoint = new DataPoint((int) (xSeg + 0.5), this.transform(nextPointBuffer.next()).getY(), 0);
142
drawLine(gc, pp, nextPoint);