Hide series

This commit is contained in:
Andrea Cavalli 2022-09-15 22:13:57 +02:00
parent e3f579f38c
commit a148502cf5
2 changed files with 48 additions and 30 deletions

View File

@ -126,9 +126,9 @@ public class AWTGraphRenderer implements IGraphRenderer<AWTDrawer> {
legendSizeW = getLegendSizeW(graph, seriesNameFontMetrics); legendSizeW = getLegendSizeW(graph, seriesNameFontMetrics);
legendSizeH = getLegendSizeH(graph, seriesNameFontMetrics); legendSizeH = getLegendSizeH(graph, seriesNameFontMetrics);
if (legendSizeW > graphWidth / 3d || legendSizeH > graphHeight / 2.5d) { if (legendSizeW > graphWidth / 1.2d || legendSizeH > graphHeight / 1.5d) {
var newFontSizeW = (float) (seriesNameFont.getSize() * ((graphWidth / 3d) / legendSizeW)); var newFontSizeW = (float) (seriesNameFont.getSize() * ((graphWidth / 1.2d) / legendSizeW));
var newFontSizeH = (float) (seriesNameFont.getSize() * ((graphHeight / 2.5d) / legendSizeH)); var newFontSizeH = (float) (seriesNameFont.getSize() * ((graphHeight / 1.5d) / legendSizeH));
seriesNameFont = seriesNameFont.deriveFont(Math.min(newFontSizeW, newFontSizeH)); seriesNameFont = seriesNameFont.deriveFont(Math.min(newFontSizeW, newFontSizeH));
seriesNameFontMetrics = graphics2D.getFontMetrics(seriesNameFont); seriesNameFontMetrics = graphics2D.getFontMetrics(seriesNameFont);
} }
@ -352,6 +352,7 @@ public class AWTGraphRenderer implements IGraphRenderer<AWTDrawer> {
int i = 0; int i = 0;
for (SeriesData series : graph.data().series()) { for (SeriesData series : graph.data().series()) {
if (series.showInLegend()) {
var seriesStyleSize = graph.style().seriesStyles().size(); var seriesStyleSize = graph.style().seriesStyles().size();
if (graph.style().seriesStyles().isEmpty()) { if (graph.style().seriesStyles().isEmpty()) {
throw new IllegalArgumentException("No styles found"); throw new IllegalArgumentException("No styles found");
@ -376,6 +377,7 @@ public class AWTGraphRenderer implements IGraphRenderer<AWTDrawer> {
i++; i++;
} }
} }
}
private static double getSeriesMargin(FontMetrics seriesNameFontMetrics) { private static double getSeriesMargin(FontMetrics seriesNameFontMetrics) {
return seriesNameFontMetrics.getHeight() * 2d / 3d; return seriesNameFontMetrics.getHeight() * 2d / 3d;
@ -397,7 +399,12 @@ public class AWTGraphRenderer implements IGraphRenderer<AWTDrawer> {
} }
private static double getLegendSizeH(Graph graph, FontMetrics seriesNameFontMetrics) { private static double getLegendSizeH(Graph graph, FontMetrics seriesNameFontMetrics) {
int seriesCount = graph.data().series().size(); int seriesCount = 0;
for (SeriesData series : graph.data().series()) {
if (series.showInLegend()) {
seriesCount++;
}
}
double seriesPadding = getSeriesPadding(seriesNameFontMetrics); double seriesPadding = getSeriesPadding(seriesNameFontMetrics);
double singleSeriesHeight = seriesNameFontMetrics.getHeight(); double singleSeriesHeight = seriesNameFontMetrics.getHeight();
return seriesPadding / 2d return seriesPadding / 2d
@ -408,12 +415,14 @@ public class AWTGraphRenderer implements IGraphRenderer<AWTDrawer> {
private static double getSeriesTextMaxWidth(Graph graph, FontMetrics seriesNameFontMetrics) { private static double getSeriesTextMaxWidth(Graph graph, FontMetrics seriesNameFontMetrics) {
double seriesTextMaxWidth = 0; double seriesTextMaxWidth = 0;
for (SeriesData series : graph.data().series()) { for (SeriesData series : graph.data().series()) {
if (series.showInLegend()) {
var seriesName = series.name(); var seriesName = series.name();
var seriesNameRasterWidth = seriesNameFontMetrics.stringWidth(seriesName); var seriesNameRasterWidth = seriesNameFontMetrics.stringWidth(seriesName);
if (seriesTextMaxWidth < seriesNameRasterWidth) { if (seriesTextMaxWidth < seriesNameRasterWidth) {
seriesTextMaxWidth = seriesNameRasterWidth; seriesTextMaxWidth = seriesNameRasterWidth;
} }
} }
}
return seriesTextMaxWidth; return seriesTextMaxWidth;
} }

View File

@ -2,4 +2,13 @@ package it.cavallium.jlinegraph;
import java.util.List; import java.util.List;
public record SeriesData(List<Vertex> vertices, boolean isFunction, String name) {} public record SeriesData(List<Vertex> vertices, boolean isFunction, String name, boolean showInLegend) {
public SeriesData(List<Vertex> vertices, boolean isFunction, String name) {
this(vertices, isFunction, name, true);
}
public SeriesData {
showInLegend = showInLegend && !name.isBlank();
}
}