From 782ce7b2ec5c68f84ab319b43f528f8710cdab26 Mon Sep 17 00:00:00 2001 From: Tim Molter Date: Tue, 9 Jun 2026 10:23:16 +0200 Subject: [PATCH] Add setChartTitleFontColor() to Styler (issue #706) Adds a dedicated chartTitleFontColor field to Styler that allows independent control of the chart title text color. When not set it falls back to chartFontColor, preserving existing behavior. ChartTitle now uses getChartTitleFontColor() instead of getChartFontColor() so the override is respected during rendering. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../standalone/issues/TestForIssue706.java | 105 ++++++++++++++++++ .../xchart/internal/chartpart/ChartTitle.java | 4 +- .../java/org/knowm/xchart/style/Styler.java | 17 +++ .../xchart/StylerChartTitleFontColorTest.java | 46 ++++++++ 4 files changed, 170 insertions(+), 2 deletions(-) create mode 100644 xchart-demo/src/main/java/org/knowm/xchart/standalone/issues/TestForIssue706.java create mode 100644 xchart/src/test/java/org/knowm/xchart/StylerChartTitleFontColorTest.java diff --git a/xchart-demo/src/main/java/org/knowm/xchart/standalone/issues/TestForIssue706.java b/xchart-demo/src/main/java/org/knowm/xchart/standalone/issues/TestForIssue706.java new file mode 100644 index 000000000..63d2abda6 --- /dev/null +++ b/xchart-demo/src/main/java/org/knowm/xchart/standalone/issues/TestForIssue706.java @@ -0,0 +1,105 @@ +package org.knowm.xchart.standalone.issues; + +import java.awt.Color; +import java.awt.Font; +import java.util.ArrayList; +import java.util.List; +import org.knowm.xchart.SwingWrapper; +import org.knowm.xchart.XYChart; +import org.knowm.xchart.XYChartBuilder; +import org.knowm.xchart.style.Styler; + +/** + * Demonstrates independent chart title font color control (issue #706). + * + *

Three charts are shown side by side: + * + *

    + *
  1. Default — title color follows chartFontColor (no override). + *
  2. Red title — setChartTitleFontColor(Color.RED) while body text stays dark. + *
  3. Custom color + larger font — orange title, bold 20pt, dark body text. + *
+ */ +public class TestForIssue706 { + + public static XYChart getChartDefault() { + + XYChart chart = + new XYChartBuilder() + .title("Default Title Color") + .xAxisTitle("X") + .yAxisTitle("Y") + .width(400) + .height(350) + .build(); + + chart.addSeries("sin", xData(), sinData()); + chart.getStyler().setLegendPosition(Styler.LegendPosition.InsideSW); + return chart; + } + + public static XYChart getChartRedTitle() { + + XYChart chart = + new XYChartBuilder() + .title("Red Title (Issue #706)") + .xAxisTitle("X") + .yAxisTitle("Y") + .width(400) + .height(350) + .build(); + + chart.addSeries("sin", xData(), sinData()); + chart.getStyler().setChartTitleFontColor(Color.RED); + chart.getStyler().setLegendPosition(Styler.LegendPosition.InsideSW); + return chart; + } + + public static XYChart getChartOrangeTitle() { + + XYChart chart = + new XYChartBuilder() + .title("Orange Bold Title") + .xAxisTitle("X") + .yAxisTitle("Y") + .width(400) + .height(350) + .build(); + + chart.addSeries("sin", xData(), sinData()); + chart + .getStyler() + .setChartTitleFontColor(new Color(220, 100, 0)) + .setChartTitleFont(new Font("Arial", Font.BOLD, 20)); + chart.getStyler().setLegendPosition(Styler.LegendPosition.InsideSW); + return chart; + } + + public static void main(String[] args) { + + List charts = new ArrayList<>(); + charts.add(getChartDefault()); + charts.add(getChartRedTitle()); + charts.add(getChartOrangeTitle()); + + new SwingWrapper<>(charts).displayChartMatrix(); + } + + private static double[] xData() { + + double[] x = new double[100]; + for (int i = 0; i < 100; i++) { + x[i] = i * 2 * Math.PI / 99; + } + return x; + } + + private static double[] sinData() { + + double[] y = new double[100]; + for (int i = 0; i < 100; i++) { + y[i] = Math.sin(i * 2 * Math.PI / 99); + } + return y; + } +} diff --git a/xchart/src/main/java/org/knowm/xchart/internal/chartpart/ChartTitle.java b/xchart/src/main/java/org/knowm/xchart/internal/chartpart/ChartTitle.java index baf4c139d..535c84989 100644 --- a/xchart/src/main/java/org/knowm/xchart/internal/chartpart/ChartTitle.java +++ b/xchart/src/main/java/org/knowm/xchart/internal/chartpart/ChartTitle.java @@ -78,9 +78,9 @@ public void paint(Graphics2D g) { if (TexRenderer.isTeX(title)) { TexRenderer.render( g, title, xOffset, yOffset - textBounds.getHeight(), - chart.getStyler().getChartTitleFont(), chart.getStyler().getChartFontColor()); + chart.getStyler().getChartTitleFont(), chart.getStyler().getChartTitleFontColor()); } else { - g.setColor(chart.getStyler().getChartFontColor()); + g.setColor(chart.getStyler().getChartTitleFontColor()); FontRenderContext frc = g.getFontRenderContext(); TextLayout textLayout = new TextLayout(title, chart.getStyler().getChartTitleFont(), frc); diff --git a/xchart/src/main/java/org/knowm/xchart/style/Styler.java b/xchart/src/main/java/org/knowm/xchart/style/Styler.java index 8b624f51a..8ce77aedd 100644 --- a/xchart/src/main/java/org/knowm/xchart/style/Styler.java +++ b/xchart/src/main/java/org/knowm/xchart/style/Styler.java @@ -34,6 +34,7 @@ public abstract class Styler { // Chart Title /////////////////////////////// private Font chartTitleFont; + private Color chartTitleFontColor; private boolean isChartTitleVisible; private boolean isChartTitleBoxVisible; private Color chartTitleBoxBackgroundColor; @@ -300,6 +301,22 @@ public Styler setChartTitleFont(Font chartTitleFont) { return this; } + public Color getChartTitleFontColor() { + + return chartTitleFontColor != null ? chartTitleFontColor : chartFontColor; + } + + /** + * Set the chart title font color. When not set, falls back to {@link #getChartFontColor()}. + * + * @param chartTitleFontColor the color to use for the chart title text + */ + public Styler setChartTitleFontColor(Color chartTitleFontColor) { + + this.chartTitleFontColor = chartTitleFontColor; + return this; + } + public boolean isChartTitleVisible() { return isChartTitleVisible; diff --git a/xchart/src/test/java/org/knowm/xchart/StylerChartTitleFontColorTest.java b/xchart/src/test/java/org/knowm/xchart/StylerChartTitleFontColorTest.java new file mode 100644 index 000000000..b59b7e15c --- /dev/null +++ b/xchart/src/test/java/org/knowm/xchart/StylerChartTitleFontColorTest.java @@ -0,0 +1,46 @@ +package org.knowm.xchart; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.knowm.xchart.style.Styler.ChartTheme.GGPlot2; + +import java.awt.Color; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class StylerChartTitleFontColorTest { + + private XYChart chart; + + @BeforeEach + void setUp() { + chart = new XYChart(800, 600, GGPlot2); + } + + @Test + void defaultFallsBackToChartFontColor() { + Color expected = chart.getStyler().getChartFontColor(); + assertThat(chart.getStyler().getChartTitleFontColor()).isEqualTo(expected); + } + + @Test + void setChartTitleFontColorOverridesChartFontColor() { + Color titleColor = Color.RED; + chart.getStyler().setChartTitleFontColor(titleColor); + assertThat(chart.getStyler().getChartTitleFontColor()).isEqualTo(titleColor); + } + + @Test + void chartFontColorStillUnaffectedByTitleColorOverride() { + Color originalFontColor = chart.getStyler().getChartFontColor(); + chart.getStyler().setChartTitleFontColor(Color.BLUE); + assertThat(chart.getStyler().getChartFontColor()).isEqualTo(originalFontColor); + } + + @Test + void setChartTitleFontColorToNullFallsBackToChartFontColor() { + chart.getStyler().setChartTitleFontColor(Color.GREEN); + chart.getStyler().setChartTitleFontColor(null); + assertThat(chart.getStyler().getChartTitleFontColor()) + .isEqualTo(chart.getStyler().getChartFontColor()); + } +}