Search in sources :

Example 96 with JFreeChart

use of org.jfree.chart.JFreeChart in project jgnash by ccavanaugh.

the class PayeePieChart method createPanel.

private JPanel createPanel() {
    JButton refreshButton = new JButton(rb.getString("Button.Refresh"));
    JButton addFilterButton = new JButton(rb.getString("Button.AddFilter"));
    final JButton saveButton = new JButton(rb.getString("Button.SaveFilters"));
    JButton deleteFilterButton = new JButton(rb.getString("Button.DeleteFilter"));
    JButton clearPrefButton = new JButton(rb.getString("Button.MasterDelete"));
    filterCombo = new JComboBox<>();
    startField = new DatePanel();
    endField = new DatePanel();
    txtAddFilter = new TextField();
    filterList = new ArrayList<>();
    filtersChanged = false;
    useFilters = new JCheckBox(rb.getString("Label.UseFilters"));
    useFilters.setSelected(true);
    showPercentCheck = new JCheckBox(rb.getString("Button.ShowPercentValues"));
    combo = AccountListComboBox.getFullInstance();
    final LocalDate dStart = DateUtils.getFirstDayOfTheMonth(LocalDate.now().minusYears(DEBIT));
    long start = pref.getLong(START_DATE, DateUtils.asEpochMilli(dStart));
    startField.setDate(DateUtils.asLocalDate(start));
    currentAccount = combo.getSelectedAccount();
    PieDataset[] data = createPieDataSet(currentAccount);
    JFreeChart chartCredit = createPieChart(currentAccount, data, CREDIT);
    chartPanelCredit = new ChartPanel(chartCredit, true, true, true, false, true);
    //                         (chart, properties, save, print, zoom, tooltips)
    JFreeChart chartDebit = createPieChart(currentAccount, data, DEBIT);
    chartPanelDebit = new ChartPanel(chartDebit, true, true, true, false, true);
    FormLayout layout = new FormLayout("p, $lcgap, 70dlu, 8dlu, p, $lcgap, 70dlu, $ugap, p, $lcgap:g, left:p", "d, $rgap, d, $ugap, f:p:g, $rgap, d");
    DefaultFormBuilder builder = new DefaultFormBuilder(layout);
    layout.setRowGroups(new int[][] { { DEBIT, 3 } });
    // row 1
    builder.append(combo, 9);
    builder.append(useFilters);
    builder.nextLine();
    builder.nextLine();
    // row 3
    builder.append(rb.getString("Label.StartDate"), startField);
    builder.append(rb.getString("Label.EndDate"), endField);
    builder.append(refreshButton);
    builder.append(showPercentCheck);
    builder.nextLine();
    builder.nextLine();
    // row 5
    FormLayout subLayout = new FormLayout("180dlu:g, 1dlu, 180dlu:g", "f:180dlu:g");
    DefaultFormBuilder subBuilder = new DefaultFormBuilder(subLayout);
    subBuilder.append(chartPanelCredit);
    subBuilder.append(chartPanelDebit);
    builder.append(subBuilder.getPanel(), 11);
    builder.nextLine();
    builder.nextLine();
    // row 7
    builder.append(txtAddFilter, 3);
    builder.append(addFilterButton, 3);
    builder.append(saveButton);
    builder.nextLine();
    // row
    builder.append(filterCombo, 3);
    builder.append(deleteFilterButton, 3);
    builder.append(clearPrefButton);
    builder.nextLine();
    JPanel panel = builder.getPanel();
    combo.addActionListener(e -> {
        Account newAccount = combo.getSelectedAccount();
        if (filtersChanged) {
            int result = JOptionPane.showConfirmDialog(null, rb.getString("Message.SaveFilters"), "Warning", JOptionPane.YES_NO_OPTION);
            if (result == JOptionPane.YES_OPTION) {
                saveButton.doClick();
            }
        }
        filtersChanged = false;
        String[] list = POUND_DELIMITER_PATTERN.split(pref.get(FILTER_TAG + newAccount.hashCode(), ""));
        filterList.clear();
        for (String filter : list) {
            if (!filter.isEmpty()) {
                filterList.add(filter);
            }
        }
        refreshFilters();
        setCurrentAccount(newAccount);
        pref.putLong(START_DATE, DateUtils.asEpochMilli(startField.getLocalDate()));
    });
    refreshButton.addActionListener(e -> {
        setCurrentAccount(currentAccount);
        pref.putLong(START_DATE, DateUtils.asEpochMilli(startField.getLocalDate()));
    });
    clearPrefButton.addActionListener(e -> {
        int result = JOptionPane.showConfirmDialog(null, rb.getString("Message.MasterDelete"), "Warning", JOptionPane.YES_NO_OPTION);
        if (result == JOptionPane.YES_OPTION) {
            try {
                pref.clear();
            } catch (Exception ex) {
                System.out.println("Exception clearing preferences" + ex);
            }
        }
    });
    saveButton.addActionListener(e -> {
        final StringBuilder sb = new StringBuilder();
        for (String filter : filterList) {
            sb.append(filter);
            sb.append('#');
        }
        pref.put(FILTER_TAG + currentAccount.hashCode(), sb.toString());
        filtersChanged = false;
    });
    addFilterButton.addActionListener(e -> {
        String newFilter = txtAddFilter.getText();
        filterList.remove(newFilter);
        if (!newFilter.isEmpty()) {
            filterList.add(newFilter);
            filtersChanged = true;
            txtAddFilter.setText("");
        }
        refreshFilters();
    });
    deleteFilterButton.addActionListener(e -> {
        if (!filterList.isEmpty()) {
            String filter = (String) filterCombo.getSelectedItem();
            filterList.remove(filter);
            filtersChanged = true;
        }
        refreshFilters();
    });
    useFilters.addActionListener(e -> setCurrentAccount(currentAccount));
    showPercentCheck.addActionListener(e -> {
        ((PiePlot) chartPanelCredit.getChart().getPlot()).setLabelGenerator(showPercentCheck.isSelected() ? percentLabels : defaultLabels);
        ((PiePlot) chartPanelDebit.getChart().getPlot()).setLabelGenerator(showPercentCheck.isSelected() ? percentLabels : defaultLabels);
    });
    return panel;
}
Also used : FormLayout(com.jgoodies.forms.layout.FormLayout) JPanel(javax.swing.JPanel) Account(jgnash.engine.Account) ChartPanel(org.jfree.chart.ChartPanel) JButton(javax.swing.JButton) LocalDate(java.time.LocalDate) JFreeChart(org.jfree.chart.JFreeChart) JCheckBox(javax.swing.JCheckBox) PieDataset(org.jfree.data.general.PieDataset) DefaultPieDataset(org.jfree.data.general.DefaultPieDataset) DatePanel(jgnash.ui.components.DatePanel) DefaultFormBuilder(com.jgoodies.forms.builder.DefaultFormBuilder) TextField(java.awt.TextField) PiePlot(org.jfree.chart.plot.PiePlot)

Example 97 with JFreeChart

use of org.jfree.chart.JFreeChart in project jgnash by ccavanaugh.

the class RunningAccountBalanceChart method createPanel.

private JPanel createPanel() {
    LocalDate end = DateUtils.getLastDayOfTheMonth(endDateField.getLocalDate());
    LocalDate start = end.minusYears(1);
    startDateField.setDate(start);
    JButton refreshButton = new JButton(rb.getString("Button.Refresh"));
    refreshButton.setIcon(IconUtils.getIcon("/jgnash/resource/view-refresh.png"));
    subAccountCheckBox = new JCheckBox(rb.getString("Button.IncludeSubAccounts"));
    subAccountCheckBox.setSelected(true);
    hideLockedAccountCheckBox = new JCheckBox(rb.getString("Button.HideLockedAccount"));
    hidePlaceholderAccountCheckBox = new JCheckBox(rb.getString("Button.HidePlaceholderAccount"));
    JFreeChart chart = createVerticalXYBarChart(combo.getSelectedAccount());
    final ChartPanel chartPanel = new ChartPanel(chart);
    FormLayout layout = new FormLayout("p, 4dlu, p:g", "");
    DefaultFormBuilder builder = new DefaultFormBuilder(layout);
    FormLayout dLayout = new FormLayout("p, 4dlu, p, 8dlu, p, 4dlu, p, 8dlu, p", "");
    DefaultFormBuilder dBuilder = new DefaultFormBuilder(dLayout);
    dBuilder.append(rb.getString("Label.StartDate"), startDateField);
    dBuilder.append(rb.getString("Label.EndDate"), endDateField);
    dBuilder.append(refreshButton);
    FormLayout cbLayout = new FormLayout("p, 4dlu, p, 4dlu, p, 4dlu", "");
    DefaultFormBuilder cbBuilder = new DefaultFormBuilder(cbLayout);
    cbBuilder.append(subAccountCheckBox);
    cbBuilder.append(hideLockedAccountCheckBox);
    cbBuilder.append(hidePlaceholderAccountCheckBox);
    builder.append(rb.getString("Label.Account"), combo);
    builder.nextLine();
    builder.append(" ");
    builder.append(cbBuilder.getPanel());
    builder.nextLine();
    builder.appendRelatedComponentsGapRow();
    builder.nextLine();
    builder.append(dBuilder.getPanel(), 3);
    builder.nextLine();
    builder.appendUnrelatedComponentsGapRow();
    builder.nextLine();
    builder.appendRow(RowSpec.decode("fill:p:g"));
    builder.append(chartPanel, 3);
    final JPanel panel = builder.getPanel();
    ActionListener listener = e -> {
        updateSubAccountBox();
        Account a = combo.getSelectedAccount();
        if (a == null) {
            return;
        }
        chartPanel.setChart(createVerticalXYBarChart(a));
        panel.validate();
    };
    hideLockedAccountCheckBox.addActionListener(e -> {
        combo.setHideLocked(hideLockedAccountCheckBox.isSelected());
        updateSubAccountBox();
        Account a = combo.getSelectedAccount();
        if (a == null) {
            return;
        }
        chartPanel.setChart(createVerticalXYBarChart(a));
        panel.validate();
    });
    hidePlaceholderAccountCheckBox.addActionListener(e -> {
        combo.setHidePlaceholder(hidePlaceholderAccountCheckBox.isSelected());
        updateSubAccountBox();
        Account a = combo.getSelectedAccount();
        if (a == null) {
            return;
        }
        chartPanel.setChart(createVerticalXYBarChart(a));
        panel.validate();
    });
    updateSubAccountBox();
    combo.addActionListener(listener);
    refreshButton.addActionListener(listener);
    return panel;
}
Also used : JCheckBox(javax.swing.JCheckBox) FormLayout(com.jgoodies.forms.layout.FormLayout) ActionListener(java.awt.event.ActionListener) AccountBalanceDisplayManager(jgnash.ui.register.AccountBalanceDisplayManager) TimeSeriesCollection(org.jfree.data.time.TimeSeriesCollection) DefaultFormBuilder(com.jgoodies.forms.builder.DefaultFormBuilder) IconUtils(jgnash.ui.util.IconUtils) ResourceUtils(jgnash.util.ResourceUtils) SimpleDateFormat(java.text.SimpleDateFormat) NumberAxis(org.jfree.chart.axis.NumberAxis) NumberFormat(java.text.NumberFormat) BigDecimal(java.math.BigDecimal) TimeSeries(org.jfree.data.time.TimeSeries) ResourceBundle(java.util.ResourceBundle) JFreeChart(org.jfree.chart.JFreeChart) AccountType(jgnash.engine.AccountType) DateTickMarkPosition(org.jfree.chart.axis.DateTickMarkPosition) XYBarRenderer(org.jfree.chart.renderer.xy.XYBarRenderer) DateFormat(java.text.DateFormat) CurrencyNode(jgnash.engine.CurrencyNode) EventQueue(java.awt.EventQueue) FilteredAccountListComboBox(jgnash.ui.components.FilteredAccountListComboBox) DateUtils(jgnash.time.DateUtils) DateTickUnit(org.jfree.chart.axis.DateTickUnit) JButton(javax.swing.JButton) Comparators(jgnash.engine.Comparators) RowSpec(com.jgoodies.forms.layout.RowSpec) XYPlot(org.jfree.chart.plot.XYPlot) GenericCloseDialog(jgnash.ui.components.GenericCloseDialog) List(java.util.List) DateAxis(org.jfree.chart.axis.DateAxis) DatePanel(jgnash.ui.components.DatePanel) FormLayout(com.jgoodies.forms.layout.FormLayout) LocalDate(java.time.LocalDate) JCheckBox(javax.swing.JCheckBox) Account(jgnash.engine.Account) ChartPanel(org.jfree.chart.ChartPanel) Collections(java.util.Collections) StandardXYToolTipGenerator(org.jfree.chart.labels.StandardXYToolTipGenerator) Month(org.jfree.data.time.Month) JPanel(javax.swing.JPanel) DateTickUnitType(org.jfree.chart.axis.DateTickUnitType) JPanel(javax.swing.JPanel) Account(jgnash.engine.Account) ChartPanel(org.jfree.chart.ChartPanel) ActionListener(java.awt.event.ActionListener) DefaultFormBuilder(com.jgoodies.forms.builder.DefaultFormBuilder) JButton(javax.swing.JButton) LocalDate(java.time.LocalDate) JFreeChart(org.jfree.chart.JFreeChart)

Example 98 with JFreeChart

use of org.jfree.chart.JFreeChart in project jgnash by ccavanaugh.

the class SecurityHighLowChart method createPanel.

private JPanel createPanel() {
    combo = new SecurityComboBox();
    // create an empty chart for panel construction
    chartPanel = new ChartPanel(new JFreeChart(new XYPlot()));
    FormLayout layout = new FormLayout("p, 4dlu:g", "");
    DefaultFormBuilder builder = new DefaultFormBuilder(layout);
    builder.append(combo);
    builder.nextLine();
    builder.appendRelatedComponentsGapRow();
    builder.nextLine();
    builder.appendRow(RowSpec.decode("fill:p:g"));
    builder.append(chartPanel, 2);
    combo.addActionListener(e -> updateChart());
    return builder.getPanel();
}
Also used : FormLayout(com.jgoodies.forms.layout.FormLayout) ChartPanel(org.jfree.chart.ChartPanel) XYPlot(org.jfree.chart.plot.XYPlot) DefaultFormBuilder(com.jgoodies.forms.builder.DefaultFormBuilder) SecurityComboBox(jgnash.ui.components.SecurityComboBox) JFreeChart(org.jfree.chart.JFreeChart)

Example 99 with JFreeChart

use of org.jfree.chart.JFreeChart in project opennms by OpenNMS.

the class ChartUtils method createBarChart.

/**
 * @param chartConfig
 * @param baseDataSet
 * @return
 */
private static JFreeChart createBarChart(BarChart chartConfig, DefaultCategoryDataset baseDataSet) {
    PlotOrientation po = (chartConfig.getPlotOrientation().orElse(null) == "horizontal" ? PlotOrientation.HORIZONTAL : PlotOrientation.VERTICAL);
    JFreeChart barChart = null;
    if ("3d".equalsIgnoreCase(chartConfig.getVariation().orElse(null))) {
        barChart = ChartFactory.createBarChart3D(chartConfig.getTitle().getValue(), chartConfig.getDomainAxisLabel(), chartConfig.getRangeAxisLabel(), baseDataSet, po, chartConfig.getShowLegend(), chartConfig.getShowToolTips(), chartConfig.getShowUrls());
    } else {
        barChart = ChartFactory.createBarChart(chartConfig.getTitle().getValue(), chartConfig.getDomainAxisLabel(), chartConfig.getRangeAxisLabel(), baseDataSet, po, chartConfig.getShowLegend(), chartConfig.getShowToolTips(), chartConfig.getShowUrls());
    }
    // Create a bit more headroom for value labels than is allowed for by the default 0.05 upper margin
    ValueAxis rangeAxis = barChart.getCategoryPlot().getRangeAxis();
    if (rangeAxis.getUpperMargin() < 0.1) {
        rangeAxis.setUpperMargin(0.1);
    }
    return barChart;
}
Also used : PlotOrientation(org.jfree.chart.plot.PlotOrientation) ValueAxis(org.jfree.chart.axis.ValueAxis) JFreeChart(org.jfree.chart.JFreeChart)

Example 100 with JFreeChart

use of org.jfree.chart.JFreeChart in project opennms by OpenNMS.

the class ChartUtils method getBarChartPNG.

/**
 * Helper method that returns the JFreeChart to an output stream written in JPEG format.
 *
 * @param chartName a {@link java.lang.String} object.
 * @param out a {@link java.io.OutputStream} object.
 * @throws java.io.IOException if any.
 * @throws java.sql.SQLException if any.
 */
public static void getBarChartPNG(String chartName, OutputStream out) throws IOException, SQLException {
    ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme());
    BarChart chartConfig = getBarChartConfigByName(chartName);
    JFreeChart chart = getBarChart(chartName);
    if (chartConfig.getChartBackgroundColor().isPresent()) {
        setChartBackgroundColor(chartConfig, chart);
    }
    if (chartConfig.getPlotBackgroundColor().isPresent()) {
        setPlotBackgroundColor(chartConfig, chart);
    }
    ImageSize imageSize = chartConfig.getImageSize();
    int hzPixels;
    int vtPixels;
    if (imageSize == null) {
        hzPixels = 400;
        vtPixels = 400;
    } else {
        hzPixels = imageSize.getHzSize().getPixels();
        vtPixels = imageSize.getVtSize().getPixels();
    }
    ChartUtilities.writeChartAsPNG(out, chart, hzPixels, vtPixels, false, 6);
}
Also used : ImageSize(org.opennms.netmgt.config.charts.ImageSize) BarChart(org.opennms.netmgt.config.charts.BarChart) JFreeChart(org.jfree.chart.JFreeChart) Paint(java.awt.Paint)

Aggregations

JFreeChart (org.jfree.chart.JFreeChart)178 XYPlot (org.jfree.chart.plot.XYPlot)40 NumberAxis (org.jfree.chart.axis.NumberAxis)26 Color (java.awt.Color)22 DateAxis (org.jfree.chart.axis.DateAxis)21 DecimalFormat (java.text.DecimalFormat)18 ChartPanel (org.jfree.chart.ChartPanel)17 XYSeries (org.jfree.data.xy.XYSeries)17 XYSeriesCollection (org.jfree.data.xy.XYSeriesCollection)17 RectangleInsets (org.jfree.ui.RectangleInsets)17 NumberFormat (java.text.NumberFormat)16 CategoryAxis (org.jfree.chart.axis.CategoryAxis)16 CategoryPlot (org.jfree.chart.plot.CategoryPlot)16 DefaultCategoryDataset (org.jfree.data.category.DefaultCategoryDataset)13 TimeSeriesCollection (org.jfree.data.time.TimeSeriesCollection)13 HashMap (java.util.HashMap)12 ValueAxis (org.jfree.chart.axis.ValueAxis)12 XYLineAndShapeRenderer (org.jfree.chart.renderer.xy.XYLineAndShapeRenderer)11 CategoryDataset (org.jfree.data.category.CategoryDataset)11 Paint (java.awt.Paint)10