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,121 @@
package org.knowm.xchart.standalone.issues;

import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import org.knowm.xchart.HeatMapChart;
import org.knowm.xchart.HeatMapChartBuilder;
import org.knowm.xchart.HeatMapSeries;
import org.knowm.xchart.SwingWrapper;
import org.knowm.xchart.style.Styler.LegendLayout;
import org.knowm.xchart.style.Styler.LegendPosition;

/**
* Demonstrates issue #585 — a symmetric colormap definition produces an asymmetric legend.
*
* <p>The gradient legend bar for the Vertical layout has its LinearGradientPaint "start" point
* placed {@code fontSize} pixels above the actual bottom of the rectangle. This leaves a strip at
* the bottom that is always clamped to {@code rangeColors[0]} (the min colour), making the
* min-colour area visually larger than the max-colour area even when the colormap and data range
* are perfectly symmetric.
*
* <p>Two windows open side by side — one for each LegendPosition — so the asymmetry is visible in
* both orientations. The colourmap intentionally goes blue → dark → blue to make the off-centre
* midpoint obvious.
*/
public class TestForIssue585 {

public static void main(String[] args) {

new SwingWrapper<>(getChartVertical()).displayChart();
new SwingWrapper<>(getChartHorizontal()).displayChart();
}

/** Vertical legend — bug is visible here: blue band at the bottom is wider than at the top. */
public static HeatMapChart getChartVertical() {

HeatMapChart chart = buildChart("Issue #585 – Vertical legend (bug: asymmetric gradient)");
chart.getStyler().setLegendPosition(LegendPosition.OutsideE);
chart.getStyler().setLegendLayout(LegendLayout.Vertical);
chart.getStyler().setGradientColorColumnHeight(200);
return chart;
}

/** Horizontal legend — shown for comparison. */
public static HeatMapChart getChartHorizontal() {

HeatMapChart chart = buildChart("Issue #585 – Horizontal legend (for comparison)");
chart.getStyler().setLegendPosition(LegendPosition.OutsideS);
chart.getStyler().setLegendLayout(LegendLayout.Horizontal);
chart.getStyler().setGradientColorColumnHeight(200);
return chart;
}

public static HeatMapChart getChart() {

return getChartVertical();
}

private static HeatMapChart buildChart(String title) {

// 21-colour symmetric palette: full-blue → no-blue → full-blue
Color[] colors =
new Color[] {
new Color(0.0f, 0.5f, 1.0f),
new Color(0.0f, 0.5f, 0.9f),
new Color(0.0f, 0.5f, 0.8f),
new Color(0.0f, 0.5f, 0.7f),
new Color(0.0f, 0.5f, 0.6f),
new Color(0.0f, 0.5f, 0.5f),
new Color(0.0f, 0.5f, 0.4f),
new Color(0.0f, 0.5f, 0.3f),
new Color(0.0f, 0.5f, 0.2f),
new Color(0.0f, 0.5f, 0.1f),
new Color(0.0f, 0.5f, 0.0f), // middle — darkest
new Color(0.0f, 0.5f, 0.1f),
new Color(0.0f, 0.5f, 0.2f),
new Color(0.0f, 0.5f, 0.3f),
new Color(0.0f, 0.5f, 0.4f),
new Color(0.0f, 0.5f, 0.5f),
new Color(0.0f, 0.5f, 0.6f),
new Color(0.0f, 0.5f, 0.7f),
new Color(0.0f, 0.5f, 0.8f),
new Color(0.0f, 0.5f, 0.9f),
new Color(0.0f, 0.5f, 1.0f),
};

List<String> xData = new ArrayList<>();
List<String> yData = new ArrayList<>();
for (int i = 0; i < 5; i++) {
xData.add("X" + i);
yData.add("Y" + i);
}

// Data ranges symmetrically from -5 to +5
List<Number[]> heatData = new ArrayList<>();
double step = 10.0 / (xData.size() * yData.size() - 1);
int idx = 0;
for (int x = 0; x < xData.size(); x++) {
for (int y = 0; y < yData.size(); y++) {
heatData.add(new Number[] {x, y, -5.0 + step * idx});
idx++;
}
}

HeatMapChart chart =
new HeatMapChartBuilder().width(700).height(500).title(title).build();

HeatMapSeries series = chart.addSeries("heat", xData, yData, heatData);
series.setMin(-5.0);
series.setMax(5.0);

chart
.getStyler()
.setRangeColors(colors)
.setMin(-5.0)
.setMax(5.0)
.setLegendVisible(true);

return chart;
}
}
2 changes: 1 addition & 1 deletion xchart/src/main/java/org/knowm/xchart/HeatMapSeries.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void replaceData(List<?> xData, List<?> yData, List<Number[]> heatData) {
protected void calculateMinMax() {

min = Double.MAX_VALUE;
max = Double.MIN_VALUE;
max = -Double.MAX_VALUE;
Number number = null;
for (Number[] numbers : heatData) {
if (numbers == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,12 @@ private void paintGradient(
Rectangle2D rect = null;
// paint gradient color Column
if (chart.getStyler().getLegendLayout() == Styler.LegendLayout.Vertical) {
start = new Point2D.Double(startx, starty + chart.getStyler().getGradientColorColumnHeight());
start =
new Point2D.Double(
startx,
starty
+ chart.getStyler().getLegendFont().getSize()
+ chart.getStyler().getGradientColorColumnHeight());
end = new Point2D.Double(startx, starty + chart.getStyler().getLegendFont().getSize());
rect =
new Rectangle2D.Double(
Expand Down
5 changes: 1 addition & 4 deletions xchart/src/main/java/org/knowm/xchart/style/BoxStyler.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ public BoxStyler() {
super.setAllStyles();
}

/**
* @deprecated Use the builder's {@code .theme(Theme)} method instead.
*/
@Deprecated
@Override
public void setTheme(Theme theme) {

this.theme = theme;
Expand Down
9 changes: 0 additions & 9 deletions xchart/src/main/java/org/knowm/xchart/style/BubbleStyler.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,4 @@ public BubbleStyler setDefaultSeriesRenderStyle(
*
* @param theme
*/
/**
* @deprecated Use the builder's {@code .theme(Theme)} method instead.
*/
@Deprecated
public void setTheme(Theme theme) {

this.theme = theme;
setAllStyles();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -259,13 +259,4 @@ public CategoryStyler setLabelsFontColorAutomaticDark(Color labelsFontColorAutom
*
* @param theme
*/
/**
* @deprecated Use the builder's {@code .theme(Theme)} method instead.
*/
@Deprecated
public void setTheme(Theme theme) {

this.theme = theme;
setAllStyles();
}
}
16 changes: 0 additions & 16 deletions xchart/src/main/java/org/knowm/xchart/style/DialStyler.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,6 @@ void setAllStyles() {
labelsFont = theme.getBaseFont();
}

/**
* Set the theme the styler should use
*
* @param theme
*/
/**
* @deprecated Use the builder's {@code .theme(Theme)} method instead.
*/
@Deprecated
public DialStyler setTheme(Theme theme) {

this.theme = theme;
setAllStyles();
return this;
}

public boolean isCircular() {

return isCircular;
Expand Down
18 changes: 2 additions & 16 deletions xchart/src/main/java/org/knowm/xchart/style/HeatMapStyler.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,6 @@ public class HeatMapStyler extends AxesChartStyler {

private Function<Double, String> heatMapDecimalValueFormatter;

/**
* Set the theme the styler should use
*
* @param theme
*/
/**
* @deprecated Use the builder's {@code .theme(Theme)} method instead.
*/
@Deprecated
public void setTheme(Theme theme) {

this.theme = theme;
setAllStyles();
}

@Override
public void setAllStyles() {

Expand Down Expand Up @@ -127,8 +112,9 @@ public HeatMapStyler setRangeColors(Color[] rangeColors) {
this.rangeColors = new Color[2];
this.rangeColors[0] = rangeColors[0];
this.rangeColors[1] = rangeColors[0];
} else {
this.rangeColors = rangeColors;
}
this.rangeColors = rangeColors;
} else {
this.rangeColors = DEFAULT_RANGE_COLORS;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,4 @@ public HorizontalBarStyler setLabelsFontColorAutomaticDark(Color labelsFontColor
*
* @param theme
*/
/**
* @deprecated Use the builder's {@code .theme(Theme)} method instead.
*/
@Deprecated
public void setTheme(Theme theme) {

this.theme = theme;
setAllStyles();
}
}
9 changes: 0 additions & 9 deletions xchart/src/main/java/org/knowm/xchart/style/OHLCStyler.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,4 @@ public OHLCStyler setDefaultSeriesRenderStyle(OHLCSeriesRenderStyle ohlcSeriesRe
*
* @param theme
*/
/**
* @deprecated Use the builder's {@code .theme(Theme)} method instead.
*/
@Deprecated
public void setTheme(Theme theme) {

this.theme = theme;
setAllStyles();
}
}
16 changes: 0 additions & 16 deletions xchart/src/main/java/org/knowm/xchart/style/PieStyler.java
Original file line number Diff line number Diff line change
Expand Up @@ -346,22 +346,6 @@ public PieStyler setLabelsFontColorAutomaticDark(Color labelsFontColorAutomaticD
return this;
}

/**
* Set the theme the styler should use
*
* @param theme
*/
/**
* @deprecated Use the builder's {@code .theme(Theme)} method instead.
*/
@Deprecated
public PieStyler setTheme(Theme theme) {

this.theme = theme;
setAllStyles();
return this;
}

public ClockwiseDirectionType getClockwiseDirectionType() {
return clockwiseDirectionType;
}
Expand Down
16 changes: 0 additions & 16 deletions xchart/src/main/java/org/knowm/xchart/style/RadarStyler.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,22 +84,6 @@ public RadarStyler setStartAngleInDegrees(double startAngleInDegrees) {
return this;
}

/**
* Set the theme the styler should use
*
* @param theme
*/
/**
* @deprecated Use the builder's {@code .theme(Theme)} method instead.
*/
@Deprecated
public RadarStyler setTheme(Theme theme) {

this.theme = theme;
setAllStyles();
return this;
}

public int getMarkerSize() {

return markerSize;
Expand Down
14 changes: 13 additions & 1 deletion xchart/src/main/java/org/knowm/xchart/style/Styler.java
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,7 @@ public boolean isToolTipsAlwaysVisible() {
/**
* When true, all data-point labels are rendered into the chart image (visible in BitmapEncoder
* output and Swing panels alike). This is a rendering/visual property; enable hover tooltips
* separately via {@link XChartPanel#setToolTipsEnabled(boolean)}.
* separately via {@link org.knowm.xchart.XChartPanel#setToolTipsEnabled(boolean)}.
*
* @param toolTipsAlwaysVisible true to render labels for every data point
*/
Expand Down Expand Up @@ -1029,4 +1029,16 @@ public Theme getTheme() {

return theme;
}

/**
* Sets the theme for this styler and re-applies all styles. Prefer constructing the chart via its
* builder ({@code .theme(Theme)}) over calling this method after construction.
*
* @param theme the theme to apply
*/
public void setTheme(Theme theme) {

this.theme = theme;
setAllStyles();
}
}
13 changes: 0 additions & 13 deletions xchart/src/main/java/org/knowm/xchart/style/XYStyler.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,6 @@ protected void setAllStyles() {
this.cursorBackgroundColor = theme.getCursorBackgroundColor();
}

/**
* Set the theme the styler should use
*
* @param theme
* @deprecated Use the builder's {@code .theme(Theme)} method instead.
*/
@Deprecated
public void setTheme(Theme theme) {

this.theme = theme;
setAllStyles();
}

public XYSeriesRenderStyle getDefaultSeriesRenderStyle() {

return xySeriesRenderStyle;
Expand Down
Loading