Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.knowm.xchart.standalone.issues;

import java.util.Arrays;
import java.util.List;
import org.knowm.xchart.SwingWrapper;
import org.knowm.xchart.XYChart;
import org.knowm.xchart.XYChartBuilder;

/**
* Demonstrates that a very long series name is truncated in the legend rather than breaking the
* chart layout.
*
* @see <a href="https://github.com/knowm/XChart/issues/686">Issue #686</a>
*/
public class TestForIssue686 {

public static void main(String[] args) {
new SwingWrapper<>(getChart()).displayChart();
}

public static XYChart getChart() {
XYChart chart =
new XYChartBuilder()
.width(600)
.height(400)
.title("Issue 686 - Long Series Name")
.build();

List<Double> xData = Arrays.asList(1.0, 2.0, 3.0, 4.0, 5.0);
List<Double> yData = Arrays.asList(2.0, 4.0, 3.0, 5.0, 4.0);

String veryLongName =
"This is an extremely long series name that used to break the chart layout by pushing the plot area to zero width";
chart.addSeries(veryLongName, xData, yData);

return chart;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public abstract class Legend_<ST extends Styler, S extends Series> implements Ch
static final int BOX_OUTLINE_WIDTH = 5;
private static final int LEGEND_MARGIN = 6;
private static final int MULTI_LINE_SPACE = 3;
private static final double MAX_LEGEND_TEXT_WIDTH_RATIO = 0.45;
final Chart<ST, S> chart;
double xOffset = 0;
double yOffset = 0;
Expand Down Expand Up @@ -290,22 +291,22 @@ Map<String, Rectangle2D> getSeriesTextBounds(S series) {
// FontMetrics fontMetrics = g.getFontMetrics(getChartPainter().getstyler().getLegendFont());
// float fontDescent = fontMetrics.getDescent();

double maxTextWidth = chart.getWidth() * MAX_LEGEND_TEXT_WIDTH_RATIO;
FontRenderContext frc = new FontRenderContext(null, true, false);
String lines[] = series.getLabel().split("\\n");
Map<String, Rectangle2D> seriesTextBounds =
new LinkedHashMap<String, Rectangle2D>(lines.length);
for (String line : lines) {
TextLayout textLayout =
new TextLayout(
line, chart.getStyler().getLegendFont(), new FontRenderContext(null, true, false));
new TextLayout(line, chart.getStyler().getLegendFont(), frc);
Shape shape = textLayout.getOutline(null);
Rectangle2D bounds = shape.getBounds2D();
// System.out.println(tl.getAscent());
// System.out.println(tl.getDescent());
// System.out.println(tl.getBounds());
// seriesTextBounds.put(line, new Rectangle2D.Double(bounds.getX(), bounds.getY(),
// bounds.getWidth(), bounds.getHeight() - tl.getDescent()));
// seriesTextBounds.put(line, new Rectangle2D.Double(bounds.getX(), bounds.getY(),
// bounds.getWidth(), tl.getAscent()));
if (bounds.getWidth() > maxTextWidth) {
line = truncateLabel(line, frc, maxTextWidth);
textLayout = new TextLayout(line, chart.getStyler().getLegendFont(), frc);
shape = textLayout.getOutline(null);
bounds = shape.getBounds2D();
}
seriesTextBounds.put(line, bounds);
}
return seriesTextBounds;
Expand Down Expand Up @@ -381,4 +382,19 @@ public Rectangle2D getBounds() {
// is the width and height.
}
}

private String truncateLabel(String text, FontRenderContext frc, double maxWidth) {
String ellipsis = "…";
StringBuilder sb = new StringBuilder(text);
while (sb.length() > 0) {
TextLayout tl =
new TextLayout(
sb.toString() + ellipsis, chart.getStyler().getLegendFont(), frc);
if (tl.getOutline(null).getBounds2D().getWidth() <= maxWidth) {
return sb.toString() + ellipsis;
}
sb.deleteCharAt(sb.length() - 1);
}
return ellipsis;
}
}
Loading