10 package com.generalrobotix.ui.view.graph;
12 import org.eclipse.swt.SWT;
13 import org.eclipse.swt.events.PaintEvent;
14 import org.eclipse.swt.events.PaintListener;
15 import org.eclipse.swt.graphics.Color;
16 import org.eclipse.swt.graphics.FontMetrics;
17 import org.eclipse.swt.graphics.GC;
18 import org.eclipse.swt.graphics.RGB;
19 import org.eclipse.swt.widgets.Canvas;
20 import org.eclipse.swt.widgets.Composite;
33 public class XYLineGraph extends Canvas implements PaintListener {
98 super(parent,SWT.NO_BACKGROUND | SWT.DOUBLE_BUFFERED);
100 leftMargin_ = leftMargin;
101 rightMargin_ = rightMargin;
102 topMargin_ = topMargin;
103 bottomMargin_ = bottomMargin;
121 dsList_ =
new ArrayList<DataSeries>();
122 dsInfoMap_ =
new HashMap<DataSeries, DataSeriesInfo>();
127 addPaintListener(
this);
162 borderColor_ = color;
185 dsInfoMap_.put(ds, dsi);
196 int ind = dsList_.indexOf(ds);
200 dsInfoMap_.remove(ds);
209 return (Iterator)dsList_.listIterator();
224 axisInfo_[axis] = ai;
237 return axisInfo_[axis];
260 return dsi.
color.getRGB();
302 int width = getSize().x;
304 e.gc.setBackground(backColor_);
305 e.gc.fillRectangle(0, 0, width, height);
308 if (width < minWidth) {
311 if (height < minHeight) {
317 int xr = width - rightMargin_ - 1;
319 int yb = height - bottomMargin_ - 1;
338 drawAxis(e.gc, xl, yt, xr, yb, AXIS_LEFT, flag);
339 drawAxis(e.gc, xl, yt, xr, yb, AXIS_RIGHT, flag);
340 drawAxis(e.gc, xl, yt, xr, yb, AXIS_TOP, flag);
341 drawAxis(e.gc, xl, yt, xr, yb, AXIS_BOTTOM, flag);
345 e.gc.setClipping(xl, yt, xr - xl, yb - yt);
351 ListIterator li = dsList_.listIterator();
352 while (li.hasNext()) {
370 boolean connect =
false;
371 e.gc.setForeground(dsi.
color);
372 int iofs = - headPos;
376 if (Double.isNaN(data[
i])) {
382 e.gc.drawLine(ox, oy, ox, oy);
386 nx = xl + (
int)(((xStep * (i + iofs) + xOffset) - xbase) * xscale);
393 ny = yb - (
int)((data[i] * factor - ybase) * yscale);
396 e.gc.drawLine(ox, oy, nx, ny);
406 iofs = length - headPos;
407 for (
int i = 0;
i < headPos;
i++) {
408 if (Double.isNaN(data[
i])) {
411 e.gc.drawLine(ox, oy, ox, oy);
415 nx = xl + (
int)(((xStep * (i + iofs) + xOffset) - xbase) * xscale);
416 ny = yb - (
int)((data[i] * factor - ybase) * yscale);
419 e.gc.drawLine(ox, oy, nx, ny);
440 e.gc.setBackground(borderColor_);
441 e.gc.fillRectangle(0, 0, xl, height);
442 e.gc.fillRectangle(xr + 1, 0, width, height);
443 e.gc.fillRectangle(0, 0, width, yt);
444 e.gc.fillRectangle(0, yb + 1, width, height);
448 flag = DRAW_AXIS + DRAW_TICK + DRAW_LABEL +
DRAW_MARKER;
449 drawAxis(e.gc, xl, yt, xr, yb, AXIS_LEFT, flag);
450 drawAxis(e.gc, xl, yt, xr, yb, AXIS_RIGHT, flag);
451 drawAxis(e.gc, xl, yt, xr, yb, AXIS_TOP, flag);
452 drawAxis(e.gc, xl, yt, xr, yb, AXIS_BOTTOM, flag);
513 if ((flag & DRAW_AXIS) != 0) {
514 gc.setForeground(ai.
color);
517 gc.drawLine(xl, yt, xl, yb);
520 gc.drawLine(xr, yt, xr, yb);
523 gc.drawLine(xl, yt, xr, yt);
526 gc.drawLine(xl, yb, xr, yb);
530 double base = ai.
base;
531 double extent = ai.
extent;
536 scale = (yb - yt) / extent;
540 scale = (xr - xl) / extent;
544 double max = base + extent;
553 if (every > 0.0 && ((flag & DRAW_TICK) != 0)) {
554 gc.setForeground(ai.
color);
555 int cfrom = (
int)(Math.ceil(min / every));
556 int cto = (
int)(Math.floor(max / every));
560 for (
int i = cfrom;
i <= cto;
i ++) {
561 pos = yb - (
int)((every *
i - base) * scale);
562 gc.drawLine(xl, pos, xl - tickLength, pos);
566 for (
int i = cfrom;
i <= cto;
i ++) {
567 pos = yb - (
int)((every *
i - base) * scale);
568 gc.drawLine(xr, pos, xr + tickLength, pos);
572 for (
int i = cfrom;
i <= cto;
i ++) {
573 pos = xl + (
int)((every *
i - base) * scale);
574 gc.drawLine(pos, yt, pos, yt - tickLength);
578 for (
int i = cfrom;
i <= cto;
i ++) {
579 pos = xl + (
int)((every *
i - base) * scale);
580 gc.drawLine(pos, yb, pos, yb + tickLength);
587 if (every > 0.0 && ((flag & DRAW_GRID) != 0)) {
589 int cfrom = (
int)(Math.ceil(min / every));
590 int cto = (
int)(Math.floor(max / every));
595 for (
int i = cfrom;
i <= cto;
i ++) {
596 pos = yb - (
int)((every *
i - base) * scale);
597 gc.drawLine(xl + 1, pos, xr - 1, pos);
602 for (
int i = cfrom;
i <= cto;
i ++) {
603 pos = xl + (
int)((every *
i - base) * scale);
604 gc.drawLine(pos, yt + 1, pos, yb - 1);
611 if (every > 0.0 && ((flag & DRAW_LABEL) != 0)) {
612 DecimalFormat lformat =
new DecimalFormat(ai.
labelFormat);
615 FontMetrics lmetrics = gc.getFontMetrics();
616 int cfrom = (
int)(Math.ceil(min / every));
617 int cto = (
int)(Math.floor(max / every));
622 for (
int i = cfrom;
i <= cto;
i ++) {
623 lstr = lformat.format(every *
i);
624 xpos = xl - tickLength - ascale * LABEL_GAP_LEFT
625 - ascale * lmetrics.getAverageCharWidth();
626 ypos = yb - (
int)((every * i - base) * scale)
627 + (
int)(ascale * lmetrics.getHeight() / 3.5);
628 gc.drawString(lstr, xpos, ypos);
632 for (
int i = cfrom;
i <= cto;
i ++) {
633 lstr = lformat.format(every *
i);
635 ypos = yb - (
int)((every * i - base) * scale)
636 + (
int)(ascale * lmetrics.getHeight() / 3.5);
637 gc.drawString(lstr, xpos, ypos);
642 for (
int i = cfrom;
i <= cto;
i ++) {
643 lstr = lformat.format(every *
i);
644 xpos = xl + (
int)((every * i - base) * scale)
645 - ascale * lmetrics.getAverageCharWidth() / 2;
646 gc.drawString(lstr, xpos, ypos);
650 ypos = yb + tickLength
651 + ascale * LABEL_GAP_BOTTOM
652 + ascale * lmetrics.getHeight();
653 for (
int i = cfrom;
i <= cto;
i ++) {
654 lstr = lformat.format(every *
i);
655 xpos = xl + (
int)((every * i - base) * scale)
656 - ascale * lmetrics.getAverageCharWidth() / 2;
657 gc.drawString(lstr, xpos, ypos);
663 FontMetrics umetrics = gc.getFontMetrics();
669 ux = xl - unitXOfs - ascale * umetrics.getAverageCharWidth();
685 uy = yb + unitYOfs + ascale * umetrics.getHeight();
693 && ((flag & DRAW_MARKER) != 0)) {
702 gc.drawLine(xl + 1, pos - 1, xr - 1, pos - 1);
703 gc.drawLine(xl + 1, pos, xr - 1, pos);
704 gc.drawLine(xl + 1, pos + 1, xr - 1, pos + 1);
711 gc.drawLine(pos - 1, yt + 1, pos - 1, yb - 1);
712 gc.drawLine(pos, yt + 1, pos, yb - 1);
713 gc.drawLine(pos + 1, yt + 1, pos + 1, yb - 1);
void setBorderColor(Color color)
void setAxisInfo(int axis, AxisInfo ai)
XYLineGraph(Composite parent, int leftMargin, int rightMargin, int topMargin, int bottomMargin)
#define null
our own NULL pointer
static final int AXIS_BOTTOM
static final int DRAW_AXIS
static int min(int a, int b)
static final int LABEL_GAP_BOTTOM
static final int MIN_WIDTH
static final int AXIS_TOP
void removeDataSeries(DataSeries ds)
static final int AXIS_RIGHT
void paintControl(PaintEvent e)
void addLegend(LegendInfo legend)
png_bytep png_bytep png_size_t length
void drawAxis(GC gc, int xl, int yt, int xr, int yb, int axis, int flag)
static final int MIN_HEIGHT
png_infop png_uint_32 * width
static final int DRAW_TICK
static Activator getDefault()
ArrayList< DataSeries > dsList_
png_infop png_uint_32 png_uint_32 * height
AxisInfo getAxisInfo(int axis)
DataSeriesInfo(AxisInfo xAxisInfo, AxisInfo yAxisInfo, Color color, LegendInfo legend)
void setLegend(LegendPanel legend)
static final int LABEL_GAP_RIGHT
void addDataSeries(DataSeries ds, AxisInfo xai, AxisInfo yai, RGB rgb, String legend)
static final int LABEL_GAP_LEFT
static final int AXIS_LEFT
static final int DRAW_GRID
void removeLegend(LegendInfo legend)
static final int EPS_SCALE
void setStyle(DataSeries ds, RGB rgb)
static final int DRAW_MARKER
static final int DRAW_LABEL
String getLegendLabel(DataSeries ds)
RGB getStyle(DataSeries ds)
HashMap< DataSeries, DataSeriesInfo > dsInfoMap_
void setEPSMode(boolean flag)
static final int LABEL_GAP_TOP
png_infop png_uint_32 flag
void setBackColor(Color color)
static int max(int a, int b)
void setLegendLabel(DataSeries ds, String legend)