From 2e3f380e9842a1d190f5c9f03caa3f2c88eb7e95 Mon Sep 17 00:00:00 2001 From: sylvain <8940871+punx120@users.noreply.github.com> Date: Sat, 12 Mar 2022 17:57:58 -0500 Subject: [PATCH] Chart - add check box to show legend on chart --- src/main/studio/ui/chart/Chart.java | 14 +++--- .../studio/ui/chart/ChartConfigPanel.java | 46 +++++++++++-------- src/main/studio/ui/chart/ChartType.java | 6 +-- src/main/studio/ui/chart/SquareIcon.java | 4 +- 4 files changed, 39 insertions(+), 31 deletions(-) diff --git a/src/main/studio/ui/chart/Chart.java b/src/main/studio/ui/chart/Chart.java index f1111c27..ad5650b1 100755 --- a/src/main/studio/ui/chart/Chart.java +++ b/src/main/studio/ui/chart/Chart.java @@ -94,7 +94,7 @@ private interface KBase2RegularTimePeriod { regularTimePeriodConverters.put(K.KMinuteVector.class, v -> new Minute(((K.Minute)v).toDate())); } - private static StandardChartTheme currentTheme = new StandardChartTheme("JFree"); + private static final StandardChartTheme currentTheme = new StandardChartTheme("JFree"); static { currentTheme.setXYBarPainter(new StandardXYBarPainter()); } @@ -110,7 +110,7 @@ private void initComponents() { yIndex = new ArrayList<>(); for (int index = 0; index clazz = table.getColumnClass(index); if (domainKClass.contains(clazz)) xIndex.add(index); if (rangeKClass.contains(clazz)) yIndex.add(index); } @@ -125,7 +125,7 @@ private void initComponents() { pnlConfig = new ChartConfigPanel(this, names, xIndex, yIndex); contentPane.add(pnlConfig, BorderLayout.EAST); - createPlot(); + createPlot(false); configUpdateTimer = new Timer(CONFIG_UPDATE_DELAY, e -> saveFrameBounds()); @@ -197,13 +197,13 @@ public void componentHidden(ComponentEvent e) { updateFrameBounds(); } - void createPlot() { + void createPlot(boolean showLegend) { if (chartPanel !=null ) { contentPane.remove(chartPanel); chartPanel = null; } - JFreeChart chart = createChart(); + JFreeChart chart = createChart(showLegend); if (chart != null) { chart.addChangeListener(e -> updateTitle(chart) ); chartPanel = new ChartPanel(chart); @@ -217,7 +217,7 @@ void createPlot() { contentPane.repaint(); } - private JFreeChart createChart() { + private JFreeChart createChart(boolean showLegend) { NumberAxis yAxis = new NumberAxis(""); yAxis.setAutoRangeIncludesZero(false); @@ -270,7 +270,7 @@ private JFreeChart createChart() { if (xAxis == null) return null; JFreeChart chart = new JFreeChart("", JFreeChart.DEFAULT_TITLE_FONT, - plot, false); + plot, showLegend); currentTheme.apply(chart); return chart; } diff --git a/src/main/studio/ui/chart/ChartConfigPanel.java b/src/main/studio/ui/chart/ChartConfigPanel.java index 4cc3ce06..9606edae 100644 --- a/src/main/studio/ui/chart/ChartConfigPanel.java +++ b/src/main/studio/ui/chart/ChartConfigPanel.java @@ -11,27 +11,27 @@ public class ChartConfigPanel extends Box implements ActionListener { - private Chart chart; - private List names; - private List xIndex; - private List yIndex; + private final Chart chart; + private final List xIndex; + private final List yIndex; - private JComboBox comboCharType; - private JComboBox comboX; - private JCheckBox chkAll; - private JCheckBox[] chkY; - private LegendIcon[] icons; - private JPanel pnlLagend; + private final JComboBox comboCharType; + private final JComboBox comboX; + private final JCheckBox chkAll; + private final JCheckBox chkShowLegend; + private final JCheckBox[] chkY; + private final LegendIcon[] icons; + private final JPanel pnlLagend; - private JColorChooser colorChooser; - private LegendIcon colorChoosePreviewIcon; + private final JColorChooser colorChooser; + private final LegendIcon colorChoosePreviewIcon; private final static Border EMPTY_BORDER = BorderFactory.createEmptyBorder(2,0,2,0); private final static Border SELECTED_BORDER = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED); - private static Paint[] colors = DefaultDrawingSupplier.DEFAULT_PAINT_SEQUENCE; - private static Shape[] shapes = DefaultDrawingSupplier.DEFAULT_SHAPE_SEQUENCE; - private static BasicStroke[] strokes = new BasicStroke[] { + private static final Paint[] colors = DefaultDrawingSupplier.DEFAULT_PAINT_SEQUENCE; + private static final Shape[] shapes = DefaultDrawingSupplier.DEFAULT_SHAPE_SEQUENCE; + private static final BasicStroke[] strokes = new BasicStroke[] { new BasicStroke(1f), new BasicStroke(1f,BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL,1f,new float[] {10,10},0f @@ -59,7 +59,7 @@ private static class StrokeWidth { } } - private static StrokeWidth[] strokeWidths = new StrokeWidth[] { + private static final StrokeWidth[] strokeWidths = new StrokeWidth[] { new StrokeWidth("x 1", 1), new StrokeWidth("x 1.5", 1.5f), new StrokeWidth("x 2", 2), @@ -75,12 +75,11 @@ private static BasicStroke strokeWithWidth(BasicStroke stroke, float width) { stroke.getMiterLimit(), stroke.getDashArray(), stroke.getDashPhase()); } - private static BasicStroke defaultStroke = strokeWithWidth(strokes[0], 2f); + private static final BasicStroke defaultStroke = strokeWithWidth(strokes[0], 2f); public ChartConfigPanel(Chart chart, List names, List xIndex, List yIndex) { super(BoxLayout.Y_AXIS); this.chart = chart; - this.names = names; this.xIndex = xIndex; this.yIndex = yIndex; int count = yIndex.size(); @@ -109,6 +108,15 @@ public ChartConfigPanel(Chart chart, List names, List xIndex, L boxDomain.add(Box.createHorizontalGlue()); add(boxDomain); + Box legendBox = Box.createHorizontalBox(); + legendBox.add(new JLabel("Show legend: ")); + chkShowLegend = new JCheckBox("", false); + chkShowLegend.addActionListener(this::actionPerformed); + legendBox.add(Box.createHorizontalGlue()); + legendBox.add(chkShowLegend); + + add(legendBox); + Box boxSeries = Box.createHorizontalBox(); boxSeries.add(new JLabel("Series:")); boxSeries.add(Box.createHorizontalGlue()); @@ -219,7 +227,7 @@ public ChartType getChartType(int index) { public void actionPerformed(ActionEvent e) { pnlLagend.repaint(); validateState(); - chart.createPlot(); + chart.createPlot(chkShowLegend.isSelected()); } private void validateState() { diff --git a/src/main/studio/ui/chart/ChartType.java b/src/main/studio/ui/chart/ChartType.java index ed95ad61..6ca8abc5 100644 --- a/src/main/studio/ui/chart/ChartType.java +++ b/src/main/studio/ui/chart/ChartType.java @@ -8,9 +8,9 @@ public enum ChartType { BAR("bar", false, false); - private String title; - private boolean line; - private boolean shape; + private final String title; + private final boolean line; + private final boolean shape; ChartType(String title, boolean line, boolean shape) { diff --git a/src/main/studio/ui/chart/SquareIcon.java b/src/main/studio/ui/chart/SquareIcon.java index efa950f9..7ee8ed43 100644 --- a/src/main/studio/ui/chart/SquareIcon.java +++ b/src/main/studio/ui/chart/SquareIcon.java @@ -5,8 +5,8 @@ public class SquareIcon implements Icon { - private Paint color; - private int size; + private final Paint color; + private final int size; public SquareIcon(Paint color, int size) { this.color = color;