|
| 1 | +package org.knowm.xchart.standalone.issues; |
| 2 | + |
| 3 | +import org.knowm.xchart.ChartButtonConfig; |
| 4 | +import org.knowm.xchart.ChartButtonConfig.ChartButtonPosition; |
| 5 | +import org.knowm.xchart.SwingWrapper; |
| 6 | +import org.knowm.xchart.XChartPanel; |
| 7 | +import org.knowm.xchart.XYChart; |
| 8 | +import org.knowm.xchart.XYChartBuilder; |
| 9 | +import org.knowm.xchart.style.colors.ChartColor; |
| 10 | + |
| 11 | +/** |
| 12 | + * Demonstrates issue #975 — chart button styling moved out of {@code Styler}/{@code Theme} into |
| 13 | + * {@link ChartButtonConfig} on {@link XChartPanel}. |
| 14 | + * |
| 15 | + * <p>Previously, {@code Styler} carried six {@code chartButton*} fields that were exclusively used |
| 16 | + * by the Swing layer ({@code ChartButton}, {@code ChartZoom}), polluting every headless render |
| 17 | + * path. These have been removed from {@code Styler} and {@code Theme} and replaced by the new |
| 18 | + * {@link ChartButtonConfig} class, which lives entirely within the Swing rendering layer. |
| 19 | + * |
| 20 | + * <p>This demo shows the zoom-reset button using a custom {@code ChartButtonConfig}: red |
| 21 | + * background, white text, and positioned in the bottom-right corner. Drag a selection on the chart |
| 22 | + * to trigger the zoom and reveal the styled reset button. |
| 23 | + */ |
| 24 | +public class TestForIssue975 { |
| 25 | + |
| 26 | + public static void main(String[] args) { |
| 27 | + |
| 28 | + SwingWrapper<XYChart> sw = new SwingWrapper<>(getChart()); |
| 29 | + sw.displayChart(); |
| 30 | + |
| 31 | + // Configure zoom and the custom button style on the panel created by SwingWrapper. |
| 32 | + sw.getXChartPanel() |
| 33 | + .setChartButtonConfig( |
| 34 | + new ChartButtonConfig() |
| 35 | + .setBackgroundColor(ChartColor.RED.getColor()) |
| 36 | + .setBorderColor(ChartColor.DARK_GREY.getColor()) |
| 37 | + .setFontColor(java.awt.Color.WHITE) |
| 38 | + .setMargin(8) |
| 39 | + .setPosition(ChartButtonPosition.InsideSE)) |
| 40 | + .setZoomEnabled(true) |
| 41 | + .setZoomResetByButton(true); |
| 42 | + } |
| 43 | + |
| 44 | + /** Constructs and returns the chart without launching a window (headless-safe). */ |
| 45 | + public static XYChart getChart() { |
| 46 | + |
| 47 | + XYChart chart = |
| 48 | + new XYChartBuilder() |
| 49 | + .width(700) |
| 50 | + .height(400) |
| 51 | + .title("Issue #975 – ChartButtonConfig demo") |
| 52 | + .xAxisTitle("X") |
| 53 | + .yAxisTitle("Y") |
| 54 | + .build(); |
| 55 | + |
| 56 | + chart.addSeries("series1", new double[]{1, 2, 3, 4, 5}, new double[]{2, 4, 3, 7, 5}); |
| 57 | + chart.addSeries("series2", new double[]{1, 2, 3, 4, 5}, new double[]{5, 3, 6, 2, 8}); |
| 58 | + return chart; |
| 59 | + } |
| 60 | +} |
0 commit comments