Skip to content

Commit

Permalink
Merge pull request #96 from punx120/chart_legend
Browse files Browse the repository at this point in the history
Chart - add check box to show legend on chart
  • Loading branch information
punx120 authored Mar 22, 2022
2 parents 2a46db2 + 2e3f380 commit 3a18cb6
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 31 deletions.
14 changes: 7 additions & 7 deletions src/main/studio/ui/chart/Chart.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -110,7 +110,7 @@ private void initComponents() {
yIndex = new ArrayList<>();
for (int index = 0; index<table.getColumnCount(); index++) {
names.add(table.getColumnName(index));
Class clazz = table.getColumnClass(index);
Class<?> clazz = table.getColumnClass(index);
if (domainKClass.contains(clazz)) xIndex.add(index);
if (rangeKClass.contains(clazz)) yIndex.add(index);
}
Expand All @@ -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());

Expand Down Expand Up @@ -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);
Expand All @@ -217,7 +217,7 @@ void createPlot() {
contentPane.repaint();
}

private JFreeChart createChart() {
private JFreeChart createChart(boolean showLegend) {
NumberAxis yAxis = new NumberAxis("");
yAxis.setAutoRangeIncludesZero(false);

Expand Down Expand Up @@ -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;
}
Expand Down
46 changes: 27 additions & 19 deletions src/main/studio/ui/chart/ChartConfigPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,27 @@

public class ChartConfigPanel extends Box implements ActionListener {

private Chart chart;
private List<String> names;
private List<Integer> xIndex;
private List<Integer> yIndex;
private final Chart chart;
private final List<Integer> xIndex;
private final List<Integer> yIndex;

private JComboBox<ChartType> comboCharType;
private JComboBox<String> comboX;
private JCheckBox chkAll;
private JCheckBox[] chkY;
private LegendIcon[] icons;
private JPanel pnlLagend;
private final JComboBox<ChartType> comboCharType;
private final JComboBox<String> 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
Expand Down Expand Up @@ -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),
Expand All @@ -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<String> names, List<Integer> xIndex, List<Integer> yIndex) {
super(BoxLayout.Y_AXIS);
this.chart = chart;
this.names = names;
this.xIndex = xIndex;
this.yIndex = yIndex;
int count = yIndex.size();
Expand Down Expand Up @@ -109,6 +108,15 @@ public ChartConfigPanel(Chart chart, List<String> names, List<Integer> 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());
Expand Down Expand Up @@ -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() {
Expand Down
6 changes: 3 additions & 3 deletions src/main/studio/ui/chart/ChartType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/studio/ui/chart/SquareIcon.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 3a18cb6

Please sign in to comment.