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,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).
*
* <p>Three charts are shown side by side:
*
* <ol>
* <li>Default — title color follows chartFontColor (no override).
* <li>Red title — setChartTitleFontColor(Color.RED) while body text stays dark.
* <li>Custom color + larger font — orange title, bold 20pt, dark body text.
* </ol>
*/
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<XYChart> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
17 changes: 17 additions & 0 deletions xchart/src/main/java/org/knowm/xchart/style/Styler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}
Loading