Skip to content

Commit

Permalink
Merge pull request #267 from jzy3d/feature/chart2D
Browse files Browse the repository at this point in the history
2D charts improvements
  • Loading branch information
jzy3d authored May 24, 2022
2 parents 7442aee + ca2cdd2 commit b70ed23
Show file tree
Hide file tree
Showing 501 changed files with 6,316 additions and 2,085 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ jobs:


macos-11-4-arm64:
#if: ${{ false }} # disable for now
if: ${{ false }} # disable for now

runs-on: ${{ matrix.os }}
strategy:
Expand Down
1 change: 1 addition & 0 deletions jzy3d-core-awt/doc/core-awt.drawio

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion jzy3d-core-awt/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
<artifactId>jzy3d-core</artifactId>
<version>${project.version}</version>
</dependency>

</dependencies>

</project>
23 changes: 12 additions & 11 deletions jzy3d-core-awt/src/main/java/org/jzy3d/chart/AWTChart.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.jzy3d.maths.Dimension;
import org.jzy3d.plot2d.primitive.AWTColorbarImageGenerator;
import org.jzy3d.plot3d.primitives.Drawable;
import org.jzy3d.plot3d.primitives.axis.layout.IAxisLayout;
import org.jzy3d.plot3d.primitives.axis.layout.AxisLayout;
import org.jzy3d.plot3d.rendering.canvas.Quality;
import org.jzy3d.plot3d.rendering.legends.colorbars.AWTColorbarLegend;
import org.jzy3d.plot3d.rendering.view.AWTRenderer2d;
Expand Down Expand Up @@ -32,19 +32,20 @@ public AWTView getAWTView() {
}

public AWTColorbarLegend colorbar(Drawable drawable) {
return colorbar(drawable, new Dimension(AWTColorbarImageGenerator.MIN_BAR_WIDTH,
AWTColorbarImageGenerator.MIN_BAR_HEIGHT), getView().getAxis().getLayout());
return colorbar(drawable, null, getView().getAxis().getLayout());
}

public AWTColorbarLegend colorbar(Drawable drawable, IAxisLayout layout) {
return colorbar(drawable, new Dimension(AWTColorbarImageGenerator.MIN_BAR_WIDTH,
AWTColorbarImageGenerator.MIN_BAR_HEIGHT), layout);
public AWTColorbarLegend colorbar(Drawable drawable, AxisLayout layout) {
return colorbar(drawable, null, layout);
}

public AWTColorbarLegend colorbar(Drawable drawable, Dimension d, IAxisLayout layout) {
AWTColorbarLegend cbar = new AWTColorbarLegend(drawable, layout);
cbar.setMinimumSize(d);
drawable.setLegend(cbar);
return cbar;
public AWTColorbarLegend colorbar(Drawable drawable, Dimension minDimension, AxisLayout layout) {
AWTColorbarLegend colorbar = new AWTColorbarLegend(drawable, layout);

if(minDimension!=null)
colorbar.setMinimumDimension(minDimension);

drawable.setLegend(colorbar);
return colorbar;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public AWTCameraMouseController() {}

public AWTCameraMouseController(Chart chart) {
register(chart);
addSlaveThreadController(chart.getFactory().newCameraThreadController(chart));
addThread(chart.getFactory().newCameraThreadController(chart));
}

public AWTCameraMouseController(Chart chart, RateLimiter limiter) {
Expand Down Expand Up @@ -57,7 +57,10 @@ public void dispose() {
*/
@Override
public void mousePressed(MouseEvent e) {
//
if(getChart().getView().is2D()) {
return;
}

if (handleSlaveThread(e))
return;

Expand All @@ -70,6 +73,10 @@ public void mousePressed(MouseEvent e) {
/** Compute shift or rotate */
@Override
public void mouseDragged(MouseEvent e) {
if(getChart().getView().is2D()) {
return;
}

// Check if mouse rate limiter wish to forbid this mouse drag instruction
if(rateLimiter!=null && !rateLimiter.rateLimitCheck()) {
return;
Expand All @@ -96,6 +103,10 @@ else if (AWTMouseUtilities.isRightDown(e)) {
/** Compute zoom */
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
if(getChart().getView().is2D()) {
return;
}

// Check if mouse rate limiter wish to forbid this mouse drag instruction
if(rateLimiter!=null && !rateLimiter.rateLimitCheck()) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.jzy3d.plot3d.rendering.scene.Scene;
import org.jzy3d.plot3d.rendering.view.AWTRenderer2d;
import org.jzy3d.plot3d.rendering.view.AWTView;
import org.jzy3d.plot3d.rendering.view.AbstractAWTRenderer2d;
import org.jzy3d.plot3d.rendering.view.AbstractViewportManager;
import org.jzy3d.plot3d.rendering.view.Camera;
import org.jzy3d.plot3d.rendering.view.View;
Expand Down Expand Up @@ -60,7 +61,7 @@ public void unregister() {
}

protected AWTRenderer2d initRenderer2d(final ICanvas c) {
return new AWTRenderer2d() {
return new AbstractAWTRenderer2d() {
@Override
public void paint(Graphics g, int canvasWidth, int canvasHeight) {
drawSelection((Graphics2D) g, c.getRendererWidth(), c.getRendererHeight());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,37 @@ public abstract class AWTAbstractImageGenerator implements AWTImageGenerator {
protected Color foregroundColor = Color.BLACK;


public void configureText(Graphics2D graphic) {
protected void configureText(Graphics2D graphic) {
graphic.setFont(awtFont); // Text for the numbers in the ColorBar is Size=12
}

public void drawBackground(int width, int height, Graphics2D graphic) {
protected void drawBackground(int width, int height, Graphics2D graphic) {
if (hasBackground) {
graphic.setColor(AWTColor.toAWT(backgroundColor));
graphic.fillRect(0, 0, width, height);
}
}

protected void drawBorder(Graphics2D graphic, int width, int height) {
graphic.setColor(AWTColor.toAWT(foregroundColor));
graphic.drawRect(0, 0, width - 1, height - 1);
}

@Override
public void setFont(Font font) {
// reset font only if necessary
if (this.font == null || !this.font.equals(font)) {
this.font = font;
this.textSize = font.getHeight();

setAWTFont(AWTFont.toAWT(font));
}
}

@Override
public Font getFont() {
return font;
}

@Override
public boolean hasBackground() {
Expand Down Expand Up @@ -60,11 +80,6 @@ public void setForegroundColor(Color foregroundColor) {
this.foregroundColor = foregroundColor;
}

public void drawLegendBorder(Graphics2D graphic, int width, int height) {
graphic.setColor(AWTColor.toAWT(foregroundColor));
graphic.drawRect(0, 0, width - 1, height - 1);
}

@Override
public java.awt.Font getAWTFont() {
return awtFont;
Expand All @@ -74,20 +89,4 @@ public java.awt.Font getAWTFont() {
public void setAWTFont(java.awt.Font font) {
this.awtFont = font;
}

@Override
public void setFont(Font font) {
// reset font only if necessary
if (this.font == null || !this.font.equals(font)) {
this.font = font;
this.textSize = font.getHeight();

setAWTFont(AWTFont.toAWT(font));
}
}

@Override
public Font getFont() {
return font;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.jzy3d.maths.Coord2d;
import org.jzy3d.painters.IPainter;
import org.jzy3d.plot2d.rendering.AWTGraphicsUtils;
import org.jzy3d.plot3d.primitives.axis.layout.IAxisLayout;
import org.jzy3d.plot3d.primitives.axis.layout.AxisLayout;
import org.jzy3d.plot3d.primitives.axis.layout.providers.ITickProvider;
import org.jzy3d.plot3d.primitives.axis.layout.renderers.ITickRenderer;

Expand All @@ -32,6 +32,8 @@ public class AWTColorbarImageGenerator extends AWTAbstractImageGenerator
public static int BAR_WIDTH_DEFAULT = 20;
protected int barWidth;
protected int textToBarHorizontalMargin = 2;

protected boolean addTextHeightToVerticalMargin = false;

protected Coord2d pixelScale = new Coord2d(1, 1);

Expand All @@ -48,40 +50,34 @@ public AWTColorbarImageGenerator(ColorMapper mapper, ITickProvider provider,
this.min = mapper.getMin();
this.max = mapper.getMax();

setFont(IAxisLayout.FONT_DEFAULT);
setFont(AxisLayout.FONT_DEFAULT);
}


// public

@Override
public BufferedImage toImage(int width, int height) {
return toImage(width, height, BAR_WIDTH_DEFAULT);
}

/** Renders the {@link AWTColorbarImageGenerator} to an image. */
/** Renders the colorbar to an image. */
public BufferedImage toImage(int width, int height, int barWidth) {
if (barWidth > width)
return null;

this.barWidth = width;
this.barWidth = barWidth;


BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphic = image.createGraphics();

// graphic.scale(pixelScale.x, pixelScale.y);

AWTGraphicsUtils.configureRenderingHints(graphic);

graphic.setColor(AWTColor.toAWT(Color.WHITE));
graphic.fillRect(0, 0, width, height);

configureText(graphic);
drawBackground(width, height, graphic);
drawBarColors(height, barWidth, graphic);
drawBarContour(height, barWidth, graphic);
drawTextAnnotations(height, barWidth, graphic);
drawBarColors(height, this.barWidth/*getScaledBarWidth()*/, graphic);
drawBarContour(height, this.barWidth/*getScaledBarWidth()*/, graphic);
drawTextAnnotations(height, this.barWidth/*getScaledBarWidth()*/, graphic);
return image;
}

Expand All @@ -96,9 +92,20 @@ public BufferedImage toImage(int width, int height, int barWidth) {
* @param barWidth
* @param graphic
*/
public void drawBarContour(int height, int barWidth, Graphics2D graphic) {
protected void drawBarContour(int height, int barWidth, Graphics2D graphic) {

int finalY = 0;
int finalH = height;

// add little space to avoid cutting the text
// on top and bottom of colorbar
if(addTextHeightToVerticalMargin) {
finalY = textSize/2;
finalH = height - textSize;
}

graphic.setColor(AWTColor.toAWT(foregroundColor));
graphic.drawRect(0, textSize / 2, barWidth, height - textSize);
graphic.drawRect(0, finalY, barWidth, finalH);
}

/**
Expand All @@ -108,8 +115,19 @@ public void drawBarContour(int height, int barWidth, Graphics2D graphic) {
* @param barWidth
* @param graphic
*/
public void drawBarColors(int height, int barWidth, Graphics2D graphic) {
for (int h = textSize / 2; h <= (height - textSize / 2); h++) {
protected void drawBarColors(int height, int barWidth, Graphics2D graphic) {

int finalFrom = 0;
int finalTo = height;

// add little space to avoid cutting the text
// on top and bottom of colorbar
if(addTextHeightToVerticalMargin) {
finalFrom = textSize/2;
finalTo = height - (textSize/2);
}

for (int h = finalFrom; h <= finalTo; h++) {
// Compute value & color
double v = min + (max - min) * (h) / (height - textSize);
Color c = mapper.getColor(v); // To allow the Color to be a variable independent of the
Expand All @@ -121,16 +139,27 @@ public void drawBarColors(int height, int barWidth, Graphics2D graphic) {
}
}

public void drawTextAnnotations(int height, int barWidth, Graphics2D graphic) {
protected void drawTextAnnotations(int height, int barWidth, Graphics2D graphic) {
if (provider != null) {
double[] ticks = provider.generateTicks(min, max);
int ypos;
String txt;

//System.out.println("AWTColorbarImageGen : min=" + min + " max=" + max);

int xpos = barWidth + textToBarHorizontalMargin;

for (int t = 0; t < ticks.length; t++) {
ypos = (int) (textSize
+ (height - textSize - (height - textSize) * ((ticks[t] - min) / (max - min))));
txt = renderer.format(ticks[t]);
graphic.drawString(txt, barWidth + textToBarHorizontalMargin, ypos);
// ???? HELP
double ratioOfRange = (ticks[t] - min) / (max - min);
double heightNoText = height;

//if(addTextHeightToVerticalMargin)
heightNoText -= textSize;

int ypos = (int) (height - (heightNoText * ratioOfRange));


String txt = renderer.format(ticks[t]);
graphic.drawString(txt, xpos, ypos);
}
}
}
Expand All @@ -144,6 +173,15 @@ public void setPixelScale(Coord2d pixelScale) {
}

/* */

protected int getScaledBarWidth() {
if(pixelScale!=null) {
return (int)(barWidth * pixelScale.x);
}
else {
return barWidth;
}
}

/**
* Compute the optimal image width to contain the text as defined by the tick provided and
Expand All @@ -154,7 +192,7 @@ public int getPreferedWidth(IPainter painter) {
return getPreferedWidth(maxWidth);
}

public int getPreferedWidth(int maxTextWidth) {
protected int getPreferedWidth(int maxTextWidth) {
return maxTextWidth + textToBarHorizontalMargin + BAR_WIDTH_DEFAULT;
}

Expand All @@ -166,7 +204,7 @@ public void setTextToBarHorizontalMargin(int textToBarHorizontalMargin) {
this.textToBarHorizontalMargin = textToBarHorizontalMargin;
}

public int getMaxTickLabelWidth(IPainter painter) {
protected int getMaxTickLabelWidth(IPainter painter) {
int maxWidth = 0;
if (provider != null) {
double[] ticks = provider.generateTicks(min, max);
Expand Down
Loading

0 comments on commit b70ed23

Please sign in to comment.