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,28 +352,30 @@ public class AWTGraphRenderer implements IGraphRenderer<AWTDrawer> {
int i = 0; int i = 0;
for (SeriesData series : graph.data().series()) { for (SeriesData series : graph.data().series()) {
var seriesStyleSize = graph.style().seriesStyles().size(); if (series.showInLegend()) {
if (graph.style().seriesStyles().isEmpty()) { var seriesStyleSize = graph.style().seriesStyles().size();
throw new IllegalArgumentException("No styles found"); if (graph.style().seriesStyles().isEmpty()) {
} throw new IllegalArgumentException("No styles found");
SeriesStyle style = graph.style().seriesStyles().get(i % seriesStyleSize); }
SeriesStyle style = graph.style().seriesStyles().get(i % seriesStyleSize);
var seriesName = series.name(); var seriesName = series.name();
var stroke = new BasicStroke((float) (strokeWidth * 2f), BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER); var stroke = new BasicStroke((float) (strokeWidth * 2f), BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER);
graphics2D.setColor(style.color().overrideOpacity(1.0f).toColor()); graphics2D.setColor(style.color().overrideOpacity(1.0f).toColor());
graphics2D.setStroke(stroke); graphics2D.setStroke(stroke);
var lineOffsetX = legendOffsetX + seriesPadding; var lineOffsetX = legendOffsetX + seriesPadding;
var currentOffsetY = legendOffsetY + seriesPadding / 2d var currentOffsetY = legendOffsetY + seriesPadding / 2d
+ i * (seriesPadding / 2d + singleSeriesHeight + seriesPadding / 2d) + i * (seriesPadding / 2d + singleSeriesHeight + seriesPadding / 2d)
+ seriesPadding / 2d; + seriesPadding / 2d;
var lineOffsetY = currentOffsetY + singleSeriesHeight / 2d; var lineOffsetY = currentOffsetY + singleSeriesHeight / 2d;
graphics2D.draw(new Line2D.Double(lineOffsetX, lineOffsetY, lineOffsetX + seriesPreviewLineWidth, lineOffsetY)); graphics2D.draw(new Line2D.Double(lineOffsetX, lineOffsetY, lineOffsetX + seriesPreviewLineWidth, lineOffsetY));
var textOffsetX = lineOffsetX + seriesPreviewLineWidth + seriesPadding; var textOffsetX = lineOffsetX + seriesPreviewLineWidth + seriesPadding;
var textOffsetY = currentOffsetY + seriesNameFontMetrics.getAscent(); var textOffsetY = currentOffsetY + seriesNameFontMetrics.getAscent();
graphics2D.setColor(fgColor); graphics2D.setColor(fgColor);
graphics2D.setFont(seriesNameFont); graphics2D.setFont(seriesNameFont);
graphics2D.fill(generateShapeFromText(graphics2D, seriesName, textOffsetX, textOffsetY)); graphics2D.fill(generateShapeFromText(graphics2D, seriesName, textOffsetX, textOffsetY));
i++; i++;
}
} }
} }
@ -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,10 +415,12 @@ 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()) {
var seriesName = series.name(); if (series.showInLegend()) {
var seriesNameRasterWidth = seriesNameFontMetrics.stringWidth(seriesName); var seriesName = series.name();
if (seriesTextMaxWidth < seriesNameRasterWidth) { var seriesNameRasterWidth = seriesNameFontMetrics.stringWidth(seriesName);
seriesTextMaxWidth = seriesNameRasterWidth; if (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();
}
}