[PATCH] Moved chart metadata line to top of chart. Minor cleanup
Wald Commits
scm-commit at wald.intevation.org
Thu Jul 26 11:16:11 CEST 2018
# HG changeset patch
# User gernotbelger
# Date 1532596566 -7200
# Node ID 7c7f73e5e01ebe47f8e93e2e973a9ea6c1c73c76
# Parent 819dbd0736247887ae13580b237887dd8a6e6493
Moved chart metadata line to top of chart. Minor cleanup.
diff -r 819dbd073624 -r 7c7f73e5e01e artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/util/CalculationUtils.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/util/CalculationUtils.java Thu Jul 26 11:08:11 2018 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/util/CalculationUtils.java Thu Jul 26 11:16:06 2018 +0200
@@ -18,7 +18,6 @@
*/
public final class CalculationUtils {
-
private CalculationUtils() {
throw new UnsupportedOperationException("Helper class");
}
@@ -31,6 +30,17 @@
* @param context
*/
public static String findArtifactUser(final CallContext context, final Artifact artifact) {
+
+ // REMARK: can't do that, because sometimes the user inside the collection is a fake user
+ // if (context instanceof CollectionCallContext) {
+ // /* avoid db access if we already know the user */
+ // final CollectionCallContext ccc = (CollectionCallContext) context;
+ // final ArtifactCollection collection = ccc.getCollection();
+ // final User user = collection.getUser();
+ // if (user != null)
+ // return user.getName();
+ // }
+
final ArtifactDatabase database = context.getDatabase();
return database.findArtifactUser(artifact.identifier());
}
diff -r 819dbd073624 -r 7c7f73e5e01e artifacts/src/main/java/org/dive4elements/river/exports/AbstractChartGenerator.java
--- a/artifacts/src/main/java/org/dive4elements/river/exports/AbstractChartGenerator.java Thu Jul 26 11:08:11 2018 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/AbstractChartGenerator.java Thu Jul 26 11:16:06 2018 +0200
@@ -21,6 +21,8 @@
import java.io.OutputStream;
import java.text.DateFormat;
import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Locale;
@@ -30,10 +32,13 @@
import javax.xml.xpath.XPathConstants;
+import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
+import org.dive4elements.artifactdatabase.CollectionCallContext;
import org.dive4elements.artifactdatabase.state.ArtifactAndFacet;
import org.dive4elements.artifactdatabase.state.Settings;
import org.dive4elements.artifacts.Artifact;
+import org.dive4elements.artifacts.ArtifactCollection;
import org.dive4elements.artifacts.ArtifactNamespaceContext;
import org.dive4elements.artifacts.CallContext;
import org.dive4elements.artifacts.CallMeta;
@@ -67,6 +72,7 @@
import org.jfree.data.Range;
import org.jfree.data.general.Series;
import org.jfree.data.xy.XYDataset;
+import org.jfree.ui.HorizontalAlignment;
import org.jfree.ui.RectangleInsets;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
@@ -467,22 +473,72 @@
}
}
+ protected final void generateTitles(final JFreeChart chart) {
+ /* add metadata title before real title */
+ addMetadataSubtitle(chart);
+
+ /* add the real chart title, but as subtitle after the metadata */
+ final String chartTitle = getChartTitle(this.context);
+ if (chartTitle != null) {
+ final TextTitle title = new TextTitle(chartTitle, JFreeChart.DEFAULT_TITLE_FONT);
+ chart.addSubtitle(title);
+ }
+
+ addSubtitles(this.context, chart);
+ }
+
+
/**
* Adds a metadata sub-title to the chart if it gets exported
*/
- protected final void addMetadataSubtitle(final CallContext context, final JFreeChart chart) {
+ protected final void addMetadataSubtitle(final JFreeChart chart) {
if ((!isExport() || !isExportMetadata()))
return;
+ final Collection<String> metadata = buildMetadata();
+ final String text = StringUtils.join(metadata, " - ");
+
+ /** The default font. */
+ final Font titleFont = new Font("SansSerif", Font.ITALIC, 10);
+
+ final TextTitle subtitle = new TextTitle(text, titleFont);
+ subtitle.setHorizontalAlignment(HorizontalAlignment.LEFT);
+ subtitle.setMargin(new RectangleInsets(10,10,10,10));
+
+ chart.addSubtitle(subtitle);
+ }
+
+ private Collection<String> buildMetadata() {
+
+ final CallMeta meta = this.context.getMeta();
+
+ if (!(this.context instanceof CollectionCallContext)) {
+ /* should never happen */
+ return Collections.emptyList();
+ }
+
+ final CollectionCallContext ccc = (CollectionCallContext) this.context;
+ final ArtifactCollection collection = ccc.getCollection();
+
+ final List<String> subtitles = new ArrayList<>();
+
+ /* version */
final String version = FLYS.VERSION;
- final String user = CalculationUtils.findArtifactUser(context, getArtifact());
- final Locale locale = Resources.getLocale(context.getMeta());
+ subtitles.add(Resources.getMsg(meta, "chart.subtitle.metadata.version", "default", version));
+
+ /* user */
+ // REMARK: the use inside the collection is a fake user and hence we cant use it here.
+ // final User user = collection.getUser();
+ final String userName = CalculationUtils.findArtifactUser(this.context, getArtifact());
+ subtitles.add(Resources.getMsg(meta, "chart.subtitle.metadata.user", "default", userName));
+
+ /* creation date */
+ final Locale locale = Resources.getLocale(meta);
final DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
final String dateText = df.format(new Date());
+ subtitles.add(Resources.getMsg(meta, "chart.subtitle.metadata.creationdate", "default", dateText));
- final String text = Resources.getMsg(context.getMeta(), "chart.subtitle.metadata", "default", version, user, dateText);
-
- chart.addSubtitle(new TextTitle(text));
+ return subtitles;
}
private boolean isExportMetadata() {
diff -r 819dbd073624 -r 7c7f73e5e01e artifacts/src/main/java/org/dive4elements/river/exports/DiagramGenerator.java
--- a/artifacts/src/main/java/org/dive4elements/river/exports/DiagramGenerator.java Thu Jul 26 11:08:11 2018 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/DiagramGenerator.java Thu Jul 26 11:16:06 2018 +0200
@@ -51,6 +51,7 @@
import org.jfree.chart.plot.Marker;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
+import org.jfree.chart.title.TextTitle;
import org.jfree.data.Range;
import org.jfree.data.general.Series;
import org.jfree.data.xy.XYDataset;
@@ -151,7 +152,7 @@
postProcess();
JFreeChart chart = ChartFactory.createXYLineChart(
- getChartTitle(context),
+ null,
"",
"",
null,
@@ -160,14 +161,14 @@
false,
false);
+ generateTitles(chart);
+
XYPlot plot = (XYPlot) chart.getPlot();
ValueAxis axis = createXAxis(context, getXAxisLabel());
plot.setDomainAxis(axis);
chart.setBackgroundPaint(Color.WHITE);
plot.setBackgroundPaint(Color.WHITE);
- addSubtitles(context, chart);
- addMetadataSubtitle(context, chart);
adjustPlot(plot);
//debugAxis(plot);
diff -r 819dbd073624 -r 7c7f73e5e01e artifacts/src/main/java/org/dive4elements/river/exports/TimeseriesChartGenerator.java
--- a/artifacts/src/main/java/org/dive4elements/river/exports/TimeseriesChartGenerator.java Thu Jul 26 11:08:11 2018 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/TimeseriesChartGenerator.java Thu Jul 26 11:16:06 2018 +0200
@@ -98,7 +98,7 @@
log.info("Generate Timeseries Chart.");
JFreeChart chart = ChartFactory.createTimeSeriesChart(
- getChartTitle(context),
+ null,
getXAxisLabel(),
getYAxisLabel(0),
null,
@@ -106,13 +106,13 @@
false,
false);
+ generateTitles(chart);
+
XYPlot plot = (XYPlot) chart.getPlot();
chart.setBackgroundPaint(Color.WHITE);
plot.setBackgroundPaint(Color.WHITE);
- addSubtitles(context, chart);
- addMetadataSubtitle( context, chart );
adjustPlot(plot);
addDatasets(plot);
adjustAxes(plot);
diff -r 819dbd073624 -r 7c7f73e5e01e artifacts/src/main/java/org/dive4elements/river/exports/XYChartGenerator.java
--- a/artifacts/src/main/java/org/dive4elements/river/exports/XYChartGenerator.java Thu Jul 26 11:08:11 2018 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/XYChartGenerator.java Thu Jul 26 11:16:06 2018 +0200
@@ -106,7 +106,7 @@
log.debug("XYChartGenerator.generateChart");
JFreeChart chart = ChartFactory.createXYLineChart(
- getChartTitle(context),
+ null,
getXAxisLabel(),
getYAxisLabel(0),
null,
@@ -115,14 +115,14 @@
false,
false);
+ generateTitles(chart);
+
XYPlot plot = (XYPlot) chart.getPlot();
ValueAxis axis = createXAxis(getXAxisLabel());
plot.setDomainAxis(axis);
chart.setBackgroundPaint(Color.WHITE);
plot.setBackgroundPaint(Color.WHITE);
- addSubtitles(context, chart);
- addMetadataSubtitle(context, chart);
adjustPlot(plot);
//debugAxis(plot);
diff -r 819dbd073624 -r 7c7f73e5e01e artifacts/src/main/resources/messages.properties
--- a/artifacts/src/main/resources/messages.properties Thu Jul 26 11:08:11 2018 +0200
+++ b/artifacts/src/main/resources/messages.properties Thu Jul 26 11:16:06 2018 +0200
@@ -1232,7 +1232,9 @@
common.client.ui.from = from
common.client.ui.to = to
-chart.subtitle.metadata = FLYS-Version: {0} - Bearbeiter: {1} - Datum der Erstellung: {2}
+chart.subtitle.metadata.version = FLYS-Version: {0}
+chart.subtitle.metadata.user = Bearbeiter: {0}
+chart.subtitle.metadata.creationdate = Datum der Erstellung: {0}
state.title.distance_state = Choose calculation range [km]
state.title.location_distance_state = Choose calculation location(s) / range [km]
diff -r 819dbd073624 -r 7c7f73e5e01e artifacts/src/main/resources/messages_de.properties
--- a/artifacts/src/main/resources/messages_de.properties Thu Jul 26 11:08:11 2018 +0200
+++ b/artifacts/src/main/resources/messages_de.properties Thu Jul 26 11:16:06 2018 +0200
@@ -1232,7 +1232,9 @@
common.client.ui.from = von
common.client.ui.to = bis
-chart.subtitle.metadata = FLYS-Version: {0} - Bearbeiter: {1} - Datum der Erstellung: {2}
+chart.subtitle.metadata.version = FLYS-Version: {0}
+chart.subtitle.metadata.user = Bearbeiter: {0}
+chart.subtitle.metadata.creationdate = Datum der Erstellung: {0}
state.title.distance_state = Berechnungsstrecke w\u00e4hlen [km]
state.title.location_distance_state = Berechnungsort(e) / strecke w\u00e4hlen [km]
More information about the Dive4Elements-commits
mailing list