chart element (does not work)
This commit is contained in:
parent
17a613541a
commit
08686de066
7 changed files with 227 additions and 28 deletions
|
@ -12,13 +12,18 @@ import java.util.List;
|
||||||
public final class QuickChart {
|
public final class QuickChart {
|
||||||
|
|
||||||
private final static int WIDTH = 600;
|
private final static int WIDTH = 600;
|
||||||
|
|
||||||
private final static int HEIGHT = 400;
|
private final static int HEIGHT = 400;
|
||||||
|
|
||||||
private QuickChart() {
|
private QuickChart() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static XYChart getChart(String chartTitle, String xTitle, String yTitle,
|
public static XYChart getChart(String chartTitle,
|
||||||
String seriesName, double[] xData, double[] yData) {
|
String xTitle,
|
||||||
|
String yTitle,
|
||||||
|
String seriesName,
|
||||||
|
double[] xData,
|
||||||
|
double[] yData) {
|
||||||
double[][] yData2d = { yData };
|
double[][] yData2d = { yData };
|
||||||
if (seriesName == null) {
|
if (seriesName == null) {
|
||||||
return getChart(chartTitle, xTitle, yTitle, null, xData, yData2d);
|
return getChart(chartTitle, xTitle, yTitle, null, xData, yData2d);
|
||||||
|
@ -27,8 +32,12 @@ public final class QuickChart {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static XYChart getChart(String chartTitle, String xTitle, String yTitle,
|
public static XYChart getChart(String chartTitle,
|
||||||
String[] seriesNames, double[] xData, double[][] yData) {
|
String xTitle,
|
||||||
|
String yTitle,
|
||||||
|
String[] seriesNames,
|
||||||
|
double[] xData,
|
||||||
|
double[][] yData) {
|
||||||
XYChart chart = new XYChart(WIDTH, HEIGHT);
|
XYChart chart = new XYChart(WIDTH, HEIGHT);
|
||||||
chart.setTitle(chartTitle);
|
chart.setTitle(chartTitle);
|
||||||
chart.setXAxisTitle(xTitle);
|
chart.setXAxisTitle(xTitle);
|
||||||
|
@ -46,8 +55,12 @@ public final class QuickChart {
|
||||||
return chart;
|
return chart;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static XYChart getChart(String chartTitle, String xTitle, String yTitle,
|
public static XYChart getChart(String chartTitle,
|
||||||
String seriesName, List<? extends Double> xData, List<? extends Double> yData) {
|
String xTitle,
|
||||||
|
String yTitle,
|
||||||
|
String seriesName,
|
||||||
|
List<? extends Double> xData,
|
||||||
|
List<? extends Double> yData) {
|
||||||
XYChart chart = new XYChart(WIDTH, HEIGHT);
|
XYChart chart = new XYChart(WIDTH, HEIGHT);
|
||||||
chart.setTitle(chartTitle);
|
chart.setTitle(chartTitle);
|
||||||
chart.setXAxisTitle(xTitle);
|
chart.setXAxisTitle(xTitle);
|
||||||
|
|
|
@ -18,4 +18,5 @@ module org.xbib.graphics.layout.pdfbox {
|
||||||
requires org.xbib.settings.datastructures;
|
requires org.xbib.settings.datastructures;
|
||||||
requires transitive java.desktop;
|
requires transitive java.desktop;
|
||||||
requires java.logging;
|
requires java.logging;
|
||||||
|
requires org.xbib.graphics.chart;
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,141 @@
|
||||||
|
package org.xbib.graphics.pdfbox.layout.elements;
|
||||||
|
|
||||||
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||||
|
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
||||||
|
import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject;
|
||||||
|
import org.apache.pdfbox.util.Matrix;
|
||||||
|
import org.xbib.graphics.chart.QuickChart;
|
||||||
|
import org.xbib.graphics.chart.xy.XYChart;
|
||||||
|
import org.xbib.graphics.pdfbox.PdfBoxGraphics2D;
|
||||||
|
import org.xbib.graphics.pdfbox.layout.text.DrawListener;
|
||||||
|
import org.xbib.graphics.pdfbox.layout.text.Position;
|
||||||
|
import org.xbib.graphics.pdfbox.layout.text.WidthRespecting;
|
||||||
|
|
||||||
|
import java.awt.Color;
|
||||||
|
import java.awt.geom.AffineTransform;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
public class ChartElement implements Element, Drawable, Dividable, WidthRespecting {
|
||||||
|
|
||||||
|
String chartTitle;
|
||||||
|
|
||||||
|
private double[] xData;
|
||||||
|
|
||||||
|
private double[] yData;
|
||||||
|
|
||||||
|
private float width;
|
||||||
|
|
||||||
|
private float height;
|
||||||
|
|
||||||
|
private float scaleX = 1.0f;
|
||||||
|
|
||||||
|
private float scaleY = 1.0f;
|
||||||
|
|
||||||
|
private float maxWidth = -1;
|
||||||
|
|
||||||
|
private Position absolutePosition;
|
||||||
|
|
||||||
|
public void setXData(double[] xData) {
|
||||||
|
this.xData = xData;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setYData(double[] yData) {
|
||||||
|
this.yData = yData;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setScaleX(float scaleX) {
|
||||||
|
this.scaleX = scaleX;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getScaleX() {
|
||||||
|
return scaleX;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setScaleY(float scaleY) {
|
||||||
|
this.scaleY = scaleY;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getScaleY() {
|
||||||
|
return scaleY;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setWidth(float width) {
|
||||||
|
this.width = width;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public float getWidth() throws IOException {
|
||||||
|
return width * scaleX;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setHeight(float height) {
|
||||||
|
this.height = height;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public float getHeight() throws IOException {
|
||||||
|
return height * scaleY;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Divided divide(float remainingHeight, float nextPageHeight)
|
||||||
|
throws IOException {
|
||||||
|
if (getHeight() <= nextPageHeight) {
|
||||||
|
return new Divided(new VerticalSpacer(remainingHeight), this);
|
||||||
|
}
|
||||||
|
return new Cutter(this).divide(remainingHeight, nextPageHeight);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public float getMaxWidth() {
|
||||||
|
return maxWidth;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setMaxWidth(float maxWidth) {
|
||||||
|
this.maxWidth = maxWidth;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Position getAbsolutePosition() {
|
||||||
|
return absolutePosition;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAbsolutePosition(Position absolutePosition) {
|
||||||
|
this.absolutePosition = absolutePosition;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void draw(PDDocument pdDocument,
|
||||||
|
PDPageContentStream contentStream,
|
||||||
|
Position upperLeft,
|
||||||
|
DrawListener drawListener) throws IOException {
|
||||||
|
float x = upperLeft.getX();
|
||||||
|
float y = upperLeft.getY() - getHeight();
|
||||||
|
PdfBoxGraphics2D pdfBoxGraphics2D = new PdfBoxGraphics2D(pdDocument, getWidth(), getHeight());
|
||||||
|
Logger.getLogger("").info("x=" + x + " y=" + y);
|
||||||
|
Logger.getLogger("").info("xData=" + Arrays.toString(xData));
|
||||||
|
Logger.getLogger("").info("yData=" + Arrays.toString(xData));
|
||||||
|
XYChart chart = QuickChart.getChart("Sample Chart",
|
||||||
|
"X", "Y", "y(x)", xData, yData);
|
||||||
|
chart.paint(pdfBoxGraphics2D, 600, 480);
|
||||||
|
PDFormXObject xFormObject = pdfBoxGraphics2D.getXFormObject();
|
||||||
|
xFormObject.setMatrix(AffineTransform.getTranslateInstance(x, y));
|
||||||
|
//Matrix matrix = new Matrix();
|
||||||
|
//matrix.translate(x, y);
|
||||||
|
contentStream.saveGraphicsState();
|
||||||
|
//contentStream.transform(matrix);
|
||||||
|
contentStream.drawForm(xFormObject);
|
||||||
|
contentStream.restoreGraphicsState();
|
||||||
|
if (drawListener != null) {
|
||||||
|
drawListener.drawn(this, upperLeft, getWidth(), getHeight());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Drawable removeLeadingEmptyVerticalSpace() {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,55 @@
|
||||||
|
package org.xbib.graphics.pdfbox.layout.script.command;
|
||||||
|
|
||||||
|
import org.xbib.graphics.pdfbox.layout.elements.ChartElement;
|
||||||
|
import org.xbib.graphics.pdfbox.layout.elements.render.VerticalLayoutHint;
|
||||||
|
import org.xbib.graphics.pdfbox.layout.script.Engine;
|
||||||
|
import org.xbib.graphics.pdfbox.layout.script.State;
|
||||||
|
import org.xbib.graphics.pdfbox.layout.text.Alignment;
|
||||||
|
import org.xbib.graphics.pdfbox.layout.text.Position;
|
||||||
|
import org.xbib.settings.Settings;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
import static org.xbib.graphics.pdfbox.layout.util.PdfUtil.mmToPt;
|
||||||
|
|
||||||
|
public class ChartCommand implements Command {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute(Engine engine, State state, Settings settings) throws IOException {
|
||||||
|
ChartElement element = new ChartElement();
|
||||||
|
if (settings.containsSetting("xdata")) {
|
||||||
|
String[] strings = settings.getAsArray("xdata");
|
||||||
|
element.setXData(Arrays.stream(strings).mapToDouble(Double::parseDouble).toArray());
|
||||||
|
}
|
||||||
|
if (settings.containsSetting("ydata")) {
|
||||||
|
String[] strings = settings.getAsArray("ydata");
|
||||||
|
element.setYData(Arrays.stream(strings).mapToDouble(Double::parseDouble).toArray());
|
||||||
|
}
|
||||||
|
if (settings.containsSetting("x") && settings.containsSetting("y")) {
|
||||||
|
element.setAbsolutePosition(new Position(mmToPt(settings.getAsFloat("x", 0f)), mmToPt(settings.getAsFloat("y", 0f))));
|
||||||
|
}
|
||||||
|
if (settings.containsSetting("width")) {
|
||||||
|
element.setWidth(settings.getAsFloat("width", element.getWidth()));
|
||||||
|
}
|
||||||
|
if (settings.containsSetting("height")) {
|
||||||
|
element.setHeight(settings.getAsFloat("height", element.getHeight()));
|
||||||
|
}
|
||||||
|
if (settings.containsSetting("scalex")) {
|
||||||
|
element.setScaleX(settings.getAsFloat("scalex", element.getScaleX()));
|
||||||
|
}
|
||||||
|
if (settings.containsSetting("scaley")) {
|
||||||
|
element.setScaleY(settings.getAsFloat("scaley", element.getScaleY()));
|
||||||
|
}
|
||||||
|
Alignment alignment = Alignment.valueOf(settings.get("alignment", "left").toUpperCase(Locale.ROOT));
|
||||||
|
String margin = settings.get("margin", "0 0 0 0");
|
||||||
|
String[] margins = margin.split(" ");
|
||||||
|
float marginleft = Float.parseFloat(margins[0]);
|
||||||
|
float marginright = Float.parseFloat(margins[1]);
|
||||||
|
float margintop = Float.parseFloat(margins[2]);
|
||||||
|
float marginbottom = Float.parseFloat(margins[3]);
|
||||||
|
VerticalLayoutHint verticalLayoutHint = new VerticalLayoutHint(alignment, marginleft, marginright, margintop, marginbottom, true);
|
||||||
|
state.elements.peek().add(element, verticalLayoutHint);
|
||||||
|
}
|
||||||
|
}
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
@ -1,5 +1,4 @@
|
||||||
dependencies {
|
dependencies {
|
||||||
api "org.apache.pdfbox:pdfbox:${project.property('pdfbox.version')}"
|
api "org.apache.pdfbox:pdfbox:${project.property('pdfbox.version')}"
|
||||||
testImplementation "org.jfree:jfreechart:${project.property('jfreechart.version')}"
|
testImplementation "org.jfree:jfreechart:${project.property('jfreechart.version')}"
|
||||||
//testImplementation "io.sf.carte:echosvg-bridge:${project.property('echosvg.version')}"
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue