Skip to content
Snippets Groups Projects
Commit 02427c9c authored by Florian Hölzl's avatar Florian Hölzl
Browse files

Legend entries in spider chart can be clicked to bring the corresponding data series to the front.

refs 2604
parent 13ba93aa
No related branches found
No related tags found
No related merge requests found
......@@ -97,4 +97,13 @@ public final class SpiderChart {
public String getTitle() {
return title;
}
/**
* Moves the given {@link DataSeries} to the end of the list (causing drawing to be performed
* last).
*/
public void moveSeriesToEnd(DataSeries ds) {
dataSeries.remove(ds);
dataSeries.add(ds);
}
}
......@@ -17,6 +17,9 @@ $Id$
+--------------------------------------------------------------------------*/
package org.fortiss.tooling.spiderchart.widget;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
......@@ -43,6 +46,9 @@ public final class SpiderChartLegendWidget extends SpiderChartWidgetBase {
/** The space between line and text. */
private static final int LINE_SPACE = 3;
/** Click areas of the legend entries. */
private final Map<DataSeries, Rectangle> clickAreas = new HashMap<DataSeries, Rectangle>();
/** Constructor. */
public SpiderChartLegendWidget(SpiderChart chart, ChartStyle style) {
super(chart, style);
......@@ -85,6 +91,9 @@ public final class SpiderChartLegendWidget extends SpiderChartWidgetBase {
gc.drawString(text, xs, y, true);
xDelta = xDelta + extent.x + margin;
gcState.foregroundRestoreAndDispose();
Rectangle clickArea = new Rectangle(xl, y, xDelta - xl, extent.y);
clickAreas.put(data, clickArea);
}
}
......@@ -144,6 +153,10 @@ public final class SpiderChartLegendWidget extends SpiderChartWidgetBase {
gcState.foregroundRestoreAndDispose();
cnt++;
int totalWidth = margin + LINE_SIZE + textWidth;
Rectangle clickArea = new Rectangle(getX(), getY() + dY, totalWidth, textHeight);
clickAreas.put(data, clickArea);
}
}
......@@ -169,4 +182,18 @@ public final class SpiderChartLegendWidget extends SpiderChartWidgetBase {
return textExtent.y + margin;
}
/**
* Scans the legend entries for a hit at the given point and returns the corresponding
* {@link DataSeries}.
*/
public DataSeries legendEntryHitScan(int x, int y) {
for(DataSeries d : clickAreas.keySet()) {
Rectangle a = clickAreas.get(d);
if(a != null && a.contains(x, y)) {
return d;
}
}
return null;
}
}
......@@ -27,6 +27,7 @@ import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.fortiss.tooling.base.ui.utils.GCUtils;
import org.fortiss.tooling.spiderchart.model.DataSeries;
import org.fortiss.tooling.spiderchart.model.SpiderChart;
import org.fortiss.tooling.spiderchart.style.ChartStyle;
......@@ -41,6 +42,8 @@ import org.fortiss.tooling.spiderchart.style.ChartStyle;
*/
final class SpiderChartSWTCanvas extends Canvas {
/** The chart. */
private final SpiderChart chart;
/** The chart style. */
private final ChartStyle style;
/** The chart widget. */
......@@ -53,6 +56,7 @@ final class SpiderChartSWTCanvas extends Canvas {
/** Constructor */
public SpiderChartSWTCanvas(Composite parent, SpiderChart chart, ChartStyle style) {
super(parent, SWT.NONE);
this.chart = chart;
this.style = style;
this.chartWidget = new SpiderChartWidget(chart, style);
this.titleWidget = new SpiderChartTitleWidget(chart, style);
......@@ -114,4 +118,15 @@ final class SpiderChartSWTCanvas extends Canvas {
legendWidget.draw(gcState);
}
}
/** Handles mouse clicks on the canvas. */
public void mouseClickedAt(int x, int y) {
if(legendWidget.getBounds().contains(x, y)) {
DataSeries ds = legendWidget.legendEntryHitScan(x, y);
if(ds != null) {
chart.moveSeriesToEnd(ds);
redraw();
}
}
}
}
......@@ -20,6 +20,8 @@ package org.fortiss.tooling.spiderchart.widget;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Composite;
import org.fortiss.tooling.spiderchart.model.SpiderChart;
......@@ -34,7 +36,7 @@ import org.fortiss.tooling.spiderchart.style.ChartStyle;
* @version $Rev$
* @ConQAT.Rating YELLOW Hash: D1124BA1A8766A1C3F3FA90766803DE1
*/
public final class SpiderChartViewer extends Composite {
public final class SpiderChartViewer extends Composite implements MouseListener {
/** The actual canvas to be used for drawing. */
private final SpiderChartSWTCanvas canvas;
......@@ -43,6 +45,7 @@ public final class SpiderChartViewer extends Composite {
super(parent, SWT.NONE);
canvas = new SpiderChartSWTCanvas(this, chart, style);
canvas.addMouseListener(this);
addControlListener(new ControlListener() {
@Override
......@@ -76,4 +79,22 @@ public final class SpiderChartViewer extends Composite {
canvas.dispose();
super.dispose();
}
/** {@inheritDoc} */
@Override
public void mouseDoubleClick(MouseEvent e) {
// nothing to do
}
/** {@inheritDoc} */
@Override
public void mouseDown(MouseEvent e) {
// nothing to do
}
/** {@inheritDoc} */
@Override
public void mouseUp(MouseEvent e) {
canvas.mouseClickedAt(e.x, e.y);
}
}
......@@ -17,6 +17,7 @@ $Id$
+--------------------------------------------------------------------------*/
package org.fortiss.tooling.spiderchart.widget;
import org.eclipse.swt.graphics.Rectangle;
import org.fortiss.tooling.spiderchart.model.SpiderChart;
import org.fortiss.tooling.spiderchart.style.ChartStyle;
......@@ -76,4 +77,9 @@ public abstract class SpiderChartWidgetBase {
public final int getHeight() {
return height;
}
/** Returns the bounding rectangle. */
public final Rectangle getBounds() {
return new Rectangle(x, y, width, height);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment