Commit f1a41893 authored by Tiziano Munaro's avatar Tiziano Munaro

Replace SWT-based spider chart with a JavaFX-based visualization

Issue-Ref: 3452
Issue-Url: https://af3-developer.fortiss.org/issues/3452Signed-off-by: Tiziano Munaro's avatarTiziano Munaro <munaro@fortiss.org>
parent 035376eb
......@@ -80,6 +80,7 @@ Export-Package: aerofx,
org.fortiss.tooling.common.ui.javafx,
org.fortiss.tooling.common.ui.javafx.control.treetableview,
org.fortiss.tooling.common.ui.javafx.layout,
org.fortiss.tooling.common.ui.javafx.style,
org.fortiss.tooling.common.ui.javafx.lwfxef,
org.fortiss.tooling.common.ui.javafx.lwfxef.change,
org.fortiss.tooling.common.ui.javafx.lwfxef.controller,
......
ColorStyleBase.java ca05f515764870da3d814f0ac38b32186f582132 RED
FillStyle.java 523ca463b908d5075d32d3a66dc377d4583372b3 RED
FontStyle.java ab25b918bfee12eccbaad941b00adbc0bba29228 RED
LineStyle.java 16084082302cbb9149e6a6f1f26503a982539e88 RED
StrokeStyle.java 703b573d91c469f064e146b683e744eda6f3d8ed RED
/*******************************************************************************
* Copyright (c) 2016, 2018 fortiss GmbH.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*******************************************************************************/
package org.fortiss.tooling.common.ui.javafx.style;
import javafx.scene.paint.Color;
/** Base class for styles with a single {@link Color}. */
abstract class ColorStyleBase {
/** The RGBA color. */
private final Color rgbaColor;
/** Constructor. */
public ColorStyleBase(Color rgbaColor) {
this.rgbaColor = rgbaColor;
}
/** Returns the RGBA color. */
public final Color getColor() {
return rgbaColor;
}
}
/*******************************************************************************
* Copyright (c) 2016, 2018 fortiss GmbH.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*******************************************************************************/
package org.fortiss.tooling.common.ui.javafx.style;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
/** Represents a style to be used in for filling areas. */
public final class FillStyle extends ColorStyleBase {
/** Alpha transparency value (default is opaque 1.0). */
private final double alpha;
/** Constructor */
public FillStyle(Color rgbColor) {
this(rgbColor, 1);
}
/** Constructor */
public FillStyle(Color c, double alpha) {
super(new Color(c.getRed(), c.getBlue(), c.getGreen(), alpha));
this.alpha = alpha;
}
/** Returns alpha value. */
public double getAlpha() {
return alpha;
}
/** Applies the style to the given graphics context. */
public void applyStyle(GraphicsContext gc) {
gc.setFill(getColor());
}
}
/*******************************************************************************
* Copyright (c) 2016, 2018 fortiss GmbH.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*******************************************************************************/
package org.fortiss.tooling.common.ui.javafx.style;
import static javafx.scene.paint.Color.BLACK;
import static javafx.scene.text.Font.font;
import javafx.geometry.Bounds;
import javafx.geometry.Dimension2D;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
/**
* Represents style attributes of fonts and provides a corresponding
* {@link #drawText(GraphicsContext, String, double, double) drawText} method.
*/
public final class FontStyle extends ColorStyleBase {
/** Black Verdana 18 point. */
public static final FontStyle BLACK_VERDANA_18PT = new FontStyle("Verdana", 18, BLACK);
/** Black Verdana 16 point. */
public static final FontStyle BLACK_VERDANA_16PT = new FontStyle("Verdana", 16, BLACK);
/** Black Verdana 14 point. */
public static final FontStyle BLACK_VERDANA_14PT = new FontStyle("Verdana", 14, BLACK);
/** Black Verdana 12 point. */
public static final FontStyle BLACK_VERDANA_12PT = new FontStyle("Verdana", 12, BLACK);
/** Black Verdana 10 point. */
public static final FontStyle BLACK_VERDANA_10PT = new FontStyle("Verdana", 10, BLACK);
/** Black Verdana 8 point. */
public static final FontStyle BLACK_VERDANA_8PT = new FontStyle("Verdana", 8, BLACK);
/** The font data. */
private final Font font;
/** Constructor. */
public FontStyle(String fontFamily, double size, Color rgbColor) {
super(rgbColor);
this.font = font(fontFamily, size);
}
/** Applies the font style to the given graphics context. */
public void applyStyle(GraphicsContext gc) {
gc.setStroke(getColor());
gc.setFill(getColor());
gc.setFont(font);
}
/** Draws the given text at the given location. */
public void drawText(GraphicsContext gc, String text, double x, double y) {
gc.save();
applyStyle(gc);
gc.fillText(text, x, y);
gc.strokeText(text, x, y);
gc.restore();
}
/** Returns the dimensions of the text when drawn with this style. */
public Dimension2D getTextBounds(String text) {
Text t = new Text(text);
t.setFont(font);
Bounds b = t.getBoundsInLocal();
Dimension2D dim = new Dimension2D(b.getWidth(), b.getHeight());
return dim;
}
}
/*******************************************************************************
* Copyright (c) 2016, 2018 fortiss GmbH.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*******************************************************************************/
package org.fortiss.tooling.common.ui.javafx.style;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
/**
* Represents a style to be used for lines and provides a corresponding
* {@link #drawLine(GraphicsContext, double, double, double, double) drawLine} method.
*/
public final class LineStyle extends ColorStyleBase {
/** Solid, black line with width of one point. */
public static final LineStyle SOLID_BLACK_1PT =
new LineStyle(Color.BLACK, 1, StrokeStyle.SOLID);
/** The style of the stroke of the line. */
private final StrokeStyle strokeStyle;
/** The width of the line. */
private final double width;
/** Constructor */
public LineStyle(Color rgbColor, double width, StrokeStyle strokeStyle) {
super(rgbColor);
this.width = width;
this.strokeStyle = strokeStyle;
}
/** Constructor */
public LineStyle(Color rgbColor) {
this(rgbColor, 1, StrokeStyle.SOLID);
}
/** Applies the line style to the given graphics context. */
public void applyStyle(GraphicsContext gc) {
gc.setStroke(getColor());
gc.setLineWidth(width);
strokeStyle.applyStyle(gc);
}
/** Draws a line */
public void drawLine(GraphicsContext gc, double x1, double y1, double x2, double y2) {
gc.save();
applyStyle(gc);
gc.moveTo(x1, y1);
gc.lineTo(x2, y2);
gc.restore();
}
}
/*******************************************************************************
* Copyright (c) 2017, 2018 fortiss GmbH.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*******************************************************************************/
package org.fortiss.tooling.common.ui.javafx.style;
import javafx.scene.canvas.GraphicsContext;
/** Represents the style of the stroke of a line. */
public final class StrokeStyle {
/** Stroke style of the solid line. */
public static final StrokeStyle SOLID = new StrokeStyle(0, null);
/** The line dash offset. */
private final double offset;
/** The dashes array. */
private final double[] dashes;
/** Constructor. */
public StrokeStyle(double offset, double[] dashes) {
this.offset = offset;
this.dashes = dashes;
}
/** Set the stroke style for the given graphics context. */
public void applyStyle(GraphicsContext gc) {
gc.setLineDashOffset(offset);
gc.setLineDashes(dashes);
}
}
......@@ -8,5 +8,6 @@
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="test-src"/>
<classpathentry kind="src" path="res"/>
<classpathentry kind="output" path="build"/>
</classpath>
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Spider Chart
Bundle-SymbolicName: org.fortiss.tooling.spiderchart.ui
Bundle-SymbolicName: org.fortiss.tooling.spiderchart.ui;singleton:=true
Bundle-Version: 2.17.0.qualifier
Bundle-Vendor: fortiss GmbH
Bundle-RequiredExecutionEnvironment: JavaSE-11
......@@ -10,8 +10,9 @@ Github-Project-Url: https://github.com/amitjoy/Spider-Chart-SWT.git
Export-Package: org.fortiss.tooling.spiderchart.model,
org.fortiss.tooling.spiderchart.style,
org.fortiss.tooling.spiderchart.util,
org.fortiss.tooling.spiderchart.widget
org.fortiss.tooling.spiderchart.control
Require-Bundle: org.eclipse.swt,
org.fortiss.tooling.base.ui;bundle-version="2.17.0";visibility:=reexport
org.fortiss.tooling.base.ui,
org.fortiss.tooling.common.ui
Bundle-ActivationPolicy: lazy
Automatic-Module-Name: org.fortiss.tooling.spiderchart.ui
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.0"?>
<!-- (c) 2017 fortiss GmbH -->
<plugin>
<extension point="org.eclipse.ui.views">
<view
class="test.org.fortiss.tooling.spiderchart.SpiderChartExampleFXViewPart"
id="test.org.fortiss.tooling.spiderchart.SpiderChartExampleFXViewPart"
name="SpiderChartExample"
restorable="true">
</view>
</extension>
</plugin>
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane fx:id="anchorPane" prefHeight="216.0" prefWidth="345.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" />
SpiderChartCanvas.java fd348bdd520fbab08b4c910ec0b76fc6512ea408 RED
SpiderChartControl.java 15417ba85f751c306802ab1d64c5481071f0ad5b RED
SpiderChartControlBase.java 12e98b77abfe0eeb6de22964b33bdfce056f157d RED
SpiderChartLegendControl.java c2ef64fa4bbeb18cd02dc1a855532e241fd1ef69 RED
SpiderChartTitleControl.java 64f026ea79a39c0e82d840e1920f1873a367e6ce RED
SpiderChartViewer.java 72f2db133eec606fd9e993fca3f6a0f03b6f528c RED
/*******************************************************************************
* Copyright (c) 2016, 2018 fortiss GmbH.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*******************************************************************************/
package org.fortiss.tooling.spiderchart.control;
import org.fortiss.tooling.spiderchart.model.SpiderChart;
import org.fortiss.tooling.spiderchart.style.ChartStyle;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
/** Represents a {@link Canvas} on which chart will be drawn. */
final class SpiderChartCanvas {
/** The canvas. */
private final Canvas canvas;
/** The chart style. */
private final ChartStyle style;
/** The chart widget. */
private final SpiderChartControl chartControl;
/** The title widget. */
private final SpiderChartTitleControl titleControl;
/** The legend widget. */
private final SpiderChartLegendControl legendControl;
/** Constructor */
public SpiderChartCanvas(SpiderChart chart, ChartStyle style) {
this.style = style;
this.chartControl = new SpiderChartControl(chart, style);
this.titleControl = new SpiderChartTitleControl(chart, style);
this.legendControl = new SpiderChartLegendControl(chart, style);
this.canvas = new Canvas();
canvas.widthProperty().addListener(evt -> {
layoutAndPaint();
});
canvas.heightProperty().addListener(evt -> {
layoutAndPaint();
});
}
/** Returns canvas. */
public Canvas getCanvasControl() {
return canvas;
}
/** Paints the spider chart. */
private void layoutAndPaint() {
double width = canvas.getWidth();
double height = canvas.getHeight();
layoutChart(width, height);
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.clearRect(0, 0, width, height);
drawChart(gc);
}
/** Computes the bounding boxes of the chart components. */
private void layoutChart(double width, double height) {
double chartMarginPercent = style.getRelativeMargin();
double widthMargin = width * chartMarginPercent;
double heightMargin = height * chartMarginPercent;
double chartY = 0;
double widthRemaining = width;
double heightRemaining = height;
if(style.isShowTitle()) {
titleControl.setBounds(0, 0, width - 1, heightMargin - 1);
heightRemaining -= heightMargin;
chartY = heightMargin;
}
if(style.isShowLegend()) {
if(style.getLegendStyle().isVerticalLayout()) {
legendControl.setBounds(width - widthMargin, height - heightRemaining,
widthMargin - 1, heightRemaining - 1);
widthRemaining -= widthMargin;
} else {
legendControl.setBounds(0, height - heightMargin, width - 1, heightMargin - 1);
heightRemaining -= heightMargin;
}
}
chartControl.setBounds(0, chartY, widthRemaining, heightRemaining);
}
/** Draws the spider chart. */
private void drawChart(GraphicsContext gc) {
if(style.isShowTitle()) {
titleControl.draw(gc);
}
chartControl.draw(gc);
if(style.isShowLegend()) {
legendControl.draw(gc);
}
}
}
/*-------------------------------------------------------------------------+
| Copyright 2016 fortiss GmbH |
| |
| Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. |
| You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
+--------------------------------------------------------------------------*/
package org.fortiss.tooling.spiderchart.widget;
/*******************************************************************************
* Copyright (c) 2016, 2018 fortiss GmbH.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*******************************************************************************/
package org.fortiss.tooling.spiderchart.control;
import org.eclipse.swt.graphics.Rectangle;
import org.fortiss.tooling.spiderchart.model.SpiderChart;
import org.fortiss.tooling.spiderchart.style.ChartStyle;
/**
* Base class for elements of the spider chart.
*
* @author mondal
* @author hoelzl
*/
public abstract class SpiderChartWidgetBase {
import javafx.geometry.Rectangle2D;
/** Base class for elements of the spider chart. */
public abstract class SpiderChartControlBase {
/** The spider chart this component belongs to. */
protected final SpiderChart chart;
/** The spider chart style information. */
protected final ChartStyle style;
/** The height of the component. */
protected int height;
protected double height;
/** The width of the component. */
protected int width;
protected double width;
/** The x location. */
protected int x;
protected double x;
/** The y location. */
protected int y;
protected double y;
/** Constructor. */
public SpiderChartWidgetBase(SpiderChart chart, ChartStyle style) {
public SpiderChartControlBase(SpiderChart chart, ChartStyle style) {
this.chart = chart;
this.style = style;
}
/** Sets the bounding rectangle area of this widget. */
public void setBounds(int x, int y, int width, int height) {
public void setBounds(double x, double y, double width, double height) {
this.x = x;
this.y = y;
this.width = width;
......@@ -54,27 +45,27 @@ public abstract class SpiderChartWidgetBase {
}
/** Returns x. */
public final int getX() {
public final double getX() {
return x;
}
/** Returns y. */
public final int getY() {
public final double getY() {
return y;
}
/** Returns width. */
public final int getWidth() {
public final double getWidth() {
return width;
}
/** Returns height. */
public final int getHeight() {
public final double getHeight() {
return height;
}
/** Returns the bounding rectangle. */
public final Rectangle getBounds() {
return new Rectangle(x, y, width, height);
public final Rectangle2D getBounds() {
return new Rectangle2D(x, y, width, height);
}
}
/*******************************************************************************
* Copyright (c) 2016, 2018 fortiss GmbH.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*******************************************************************************/
package org.fortiss.tooling.spiderchart.control;
import org.fortiss.tooling.spiderchart.model.SpiderChart;
import org.fortiss.tooling.spiderchart.style.ChartStyle;
import org.fortiss.tooling.common.ui.javafx.style.FontStyle;
import javafx.geometry.Dimension2D;
import javafx.scene.canvas.GraphicsContext;
/** Class used for drawing the spider chart title. */
public final class SpiderChartTitleControl extends SpiderChartControlBase {
/** Constructor */
public SpiderChartTitleControl(SpiderChart chart, ChartStyle style) {
super(chart, style);
}
/** Used to draw the title */
public void draw(GraphicsContext gc) {
if(!style.isShowTitle()) {
return;
}
String text = chart.getTitle();
if(text == null || text.trim().length() == 0) {
return;
}
gc.save();
FontStyle fontStyle = style.getTitleStyle();
Dimension2D extent = fontStyle.getTextBounds(text);
double x = getX() + getWidth() / 2 - extent.getWidth() / 2;
double y = getY() + getHeight() / 2 - extent.getHeight() / 2;
fontStyle.drawText(gc, text, x, y);
gc.restore();
}
}
/*******************************************************************************
* Copyright (c) 2016, 2018 fortiss GmbH.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*******************************************************************************/
package org.fortiss.tooling.spiderchart.control;
import org.fortiss.tooling.spiderchart.model.SpiderChart;
import org.fortiss.tooling.spiderchart.style.ChartStyle;
import javafx.geometry.Bounds;
import javafx.scene.canvas.Canvas;
import javafx.scene.layout.Pane;
/** Represents a viewer on the canvas to display the spider chart. */
public final class SpiderChartViewer {
/** The viewer main pane. */
private final Pane viewerPane;
/** The actual canvas to be used for drawing. */
private final SpiderChartCanvas canvas;
/** Constructor */
public SpiderChartViewer(SpiderChart chart, ChartStyle style) {
canvas = new SpiderChartCanvas(chart, style);
viewerPane = new Pane();
viewerPane.getChildren().add(canvas.getCanvasControl());
viewerPane.widthProperty().addListener(evt -> {
updateCanvasSize();
});
viewerPane.heightProperty().addListener(evt -> {
updateCanvasSize();
});
}
/** Updates the canvas size after viewer size changed. */
protected void updateCanvasSize() {