Search in sources :

Example 56 with JFreeChart

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

the class MonthlyAccountBalanceChartCompare 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"));
    jcb_compare = new JCheckBox(rb.getString("Button.Compare"));
    jcb_compare.setSelected(true);
    Account a = combo1.getSelectedAccount();
    Account a2 = combo2.getSelectedAccount();
    JFreeChart chart = createVerticalXYBarChart(a, a2);
    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"), combo1);
    builder.nextLine();
    builder.append(rb.getString("Label.Compare"), combo2);
    builder.nextLine();
    builder.append(jcb_compare);
    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 -> {
        try {
            if (e.getSource() == jcb_compare) {
                combo2.setEnabled(jcb_compare.isSelected());
            }
            Account account = combo1.getSelectedAccount();
            if (account == null) {
                return;
            }
            Account account2 = combo2.getSelectedAccount();
            if (jcb_compare.isSelected() && account2 == null) {
                return;
            }
            updateSubAccountBox();
            chartPanel.setChart(createVerticalXYBarChart(account, account2));
            panel.validate();
        } catch (final Exception ex) {
            Logger.getLogger(MonthlyAccountBalanceChartCompare.class.getName()).log(Level.SEVERE, ex.getLocalizedMessage(), ex);
        }
    };
    combo1.addActionListener(listener);
    combo2.addActionListener(listener);
    jcb_compare.addActionListener(listener);
    subAccountCheckBox.addActionListener(listener);
    hideLockedAccountCheckBox.addActionListener(e -> {
        combo1.setHideLocked(hideLockedAccountCheckBox.isSelected());
        combo2.setHideLocked(hideLockedAccountCheckBox.isSelected());
        try {
            Account account = combo1.getSelectedAccount();
            if (account == null) {
                return;
            }
            Account account2 = combo2.getSelectedAccount();
            if (jcb_compare.isSelected() && account2 == null) {
                return;
            }
            updateSubAccountBox();
            chartPanel.setChart(createVerticalXYBarChart(account, account2));
            panel.validate();
        } catch (final Exception ex) {
            Logger.getLogger(MonthlyAccountBalanceChartCompare.class.getName()).log(Level.SEVERE, ex.getLocalizedMessage(), ex);
        }
    });
    hidePlaceholderAccountCheckBox.addActionListener(e -> {
        combo1.setHidePlaceholder(hidePlaceholderAccountCheckBox.isSelected());
        combo2.setHidePlaceholder(hidePlaceholderAccountCheckBox.isSelected());
        try {
            Account account = combo1.getSelectedAccount();
            if (account == null) {
                return;
            }
            Account account2 = combo2.getSelectedAccount();
            if (jcb_compare.isSelected() && account2 == null) {
                return;
            }
            updateSubAccountBox();
            chartPanel.setChart(createVerticalXYBarChart(account, account2));
            panel.validate();
        } catch (final Exception ex) {
            Logger.getLogger(MonthlyAccountBalanceChartCompare.class.getName()).log(Level.SEVERE, ex.getLocalizedMessage(), ex);
        }
    });
    refreshButton.addActionListener(listener);
    updateSubAccountBox();
    return panel;
}
Also used : FormLayout(com.jgoodies.forms.layout.FormLayout) ActionListener(java.awt.event.ActionListener) AccountBalanceDisplayManager(jgnash.ui.register.AccountBalanceDisplayManager) TimeSeriesCollection(org.jfree.data.time.TimeSeriesCollection) Date(java.util.Date) 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) Level(java.util.logging.Level) 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) 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) Logger(java.util.logging.Logger) 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) ClusteredXYBarRenderer(org.jfree.chart.renderer.xy.ClusteredXYBarRenderer) TemporalAdjusters(java.time.temporal.TemporalAdjusters) ChartPanel(org.jfree.chart.ChartPanel) StandardXYToolTipGenerator(org.jfree.chart.labels.StandardXYToolTipGenerator) Month(org.jfree.data.time.Month) JPanel(javax.swing.JPanel) DateTickUnitType(org.jfree.chart.axis.DateTickUnitType) Account(jgnash.engine.Account) JPanel(javax.swing.JPanel) ChartPanel(org.jfree.chart.ChartPanel) JButton(javax.swing.JButton) LocalDate(java.time.LocalDate) JFreeChart(org.jfree.chart.JFreeChart) JCheckBox(javax.swing.JCheckBox) ActionListener(java.awt.event.ActionListener) DefaultFormBuilder(com.jgoodies.forms.builder.DefaultFormBuilder)

Example 57 with JFreeChart

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

the class PayeePieChart method createPieChart.

private JFreeChart createPieChart(Account a, PieDataset[] data, int index) {
    PiePlot plot = new PiePlot(data[index]);
    // rebuilt each time because they're based on the account's commodity
    NumberFormat valueFormat = CommodityFormat.getFullNumberFormat(a.getCurrencyNode());
    NumberFormat percentFormat = new DecimalFormat("0.0#%");
    defaultLabels = new StandardPieSectionLabelGenerator("{0} = {1}", valueFormat, percentFormat);
    percentLabels = new StandardPieSectionLabelGenerator("{0} = {1}\n{2}", valueFormat, percentFormat);
    plot.setLabelGenerator(showPercentCheck.isSelected() ? percentLabels : defaultLabels);
    plot.setLabelGap(.02);
    plot.setInteriorGap(.1);
    BigDecimal thisTotal = BigDecimal.ZERO;
    for (int i = 0; i < data[index].getItemCount(); i++) {
        thisTotal = thisTotal.add((BigDecimal) (data[index].getValue(i)));
    }
    BigDecimal acTotal = a.getTreeBalance(startField.getLocalDate(), endField.getLocalDate()).abs();
    String title = "";
    String subtitle = "";
    // pick an appropriate title(s)
    if (index == CREDIT) {
        title = a.getPathName();
        subtitle = rb.getString("Column.Credit") + " : " + valueFormat.format(thisTotal);
    } else if (index == DEBIT) {
        title = rb.getString("Column.Balance") + " : " + valueFormat.format(acTotal);
        subtitle = rb.getString("Column.Debit") + " : " + valueFormat.format(thisTotal);
    }
    JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, false);
    chart.addSubtitle(new TextTitle(subtitle));
    chart.setBackgroundPaint(null);
    return chart;
}
Also used : TextTitle(org.jfree.chart.title.TextTitle) DecimalFormat(java.text.DecimalFormat) PiePlot(org.jfree.chart.plot.PiePlot) StandardPieSectionLabelGenerator(org.jfree.chart.labels.StandardPieSectionLabelGenerator) BigDecimal(java.math.BigDecimal) JFreeChart(org.jfree.chart.JFreeChart) NumberFormat(java.text.NumberFormat)

Example 58 with JFreeChart

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

the class IncomeExpensePieChart method createPieChart.

private JFreeChart createPieChart(final Account a) {
    final Engine engine = EngineFactory.getEngine(EngineFactory.DEFAULT);
    Objects.requireNonNull(engine);
    Objects.requireNonNull(a);
    PieDataset data = createPieDataSet(a);
    PiePlot plot = new PiePlot(data);
    // rebuilt each time because they're based on the account's commodity
    CurrencyNode defaultCurrency = engine.getDefaultCurrency();
    NumberFormat valueFormat = CommodityFormat.getFullNumberFormat(a.getCurrencyNode());
    NumberFormat percentFormat = new DecimalFormat("0.0#%");
    defaultLabels = new StandardPieSectionLabelGenerator("{0} = {1}", valueFormat, percentFormat);
    percentLabels = new StandardPieSectionLabelGenerator("{0} = {1}\n{2}", valueFormat, percentFormat);
    plot.setLabelGenerator(showPercentCheck.isSelected() ? percentLabels : defaultLabels);
    plot.setLabelGap(.02);
    plot.setInteriorGap(.1);
    // own transactions, not just child accounts), separate it from children.
    if (data.getIndex(a) != -1) {
        plot.setExplodePercent(a, .10);
    }
    String title;
    // pick an appropriate title
    if (a.getAccountType() == AccountType.EXPENSE) {
        title = rb.getString("Title.PercentExpense");
    } else if (a.getAccountType() == AccountType.INCOME) {
        title = rb.getString("Title.PercentIncome");
    } else {
        title = rb.getString("Title.PercentDist");
    }
    title = title + " - " + a.getPathName();
    JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, false);
    BigDecimal total = a.getTreeBalance(startField.getLocalDate(), endField.getLocalDate()).abs();
    String subtitle = valueFormat.format(total);
    if (!defaultCurrency.equals(a.getCurrencyNode())) {
        BigDecimal totalDefaultCurrency = total.multiply(a.getCurrencyNode().getExchangeRate(defaultCurrency));
        NumberFormat defaultValueFormat = CommodityFormat.getFullNumberFormat(defaultCurrency);
        subtitle += "  -  " + defaultValueFormat.format(totalDefaultCurrency);
    }
    chart.addSubtitle(new TextTitle(subtitle));
    chart.setBackgroundPaint(null);
    return chart;
}
Also used : CurrencyNode(jgnash.engine.CurrencyNode) TextTitle(org.jfree.chart.title.TextTitle) PieDataset(org.jfree.data.general.PieDataset) DefaultPieDataset(org.jfree.data.general.DefaultPieDataset) DecimalFormat(java.text.DecimalFormat) PiePlot(org.jfree.chart.plot.PiePlot) StandardPieSectionLabelGenerator(org.jfree.chart.labels.StandardPieSectionLabelGenerator) Engine(jgnash.engine.Engine) JFreeChart(org.jfree.chart.JFreeChart) BigDecimal(java.math.BigDecimal) NumberFormat(java.text.NumberFormat)

Example 59 with JFreeChart

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

the class IncomeExpensePieChart method createPanel.

private JPanel createPanel() {
    EnumSet<AccountType> set = EnumSet.of(AccountType.INCOME, AccountType.EXPENSE);
    JButton refreshButton = new JButton(rb.getString("Button.Refresh"));
    startField = new DatePanel();
    endField = new DatePanel();
    showEmptyCheck = new JCheckBox(rb.getString("Label.ShowEmptyAccounts"));
    showPercentCheck = new JCheckBox(rb.getString("Button.ShowPercentValues"));
    final Engine engine = EngineFactory.getEngine(EngineFactory.DEFAULT);
    Objects.requireNonNull(engine);
    combo = AccountListComboBox.getParentTypeInstance(engine.getRootAccount(), set);
    final LocalDate dStart = DateUtils.getFirstDayOfTheMonth(LocalDate.now()).minusYears(1);
    long start = pref.getLong(START_DATE, DateUtils.asEpochMilli(dStart));
    startField.setDate(DateUtils.asLocalDate(start));
    currentAccount = combo.getSelectedAccount();
    JFreeChart chart = createPieChart(currentAccount);
    chartPanel = new ChartPanel(chart, true, true, true, false, true);
    //                         (chart, properties, save, print, zoom, tooltips)
    FormLayout layout = new FormLayout("p, 4dlu, 70dlu, 8dlu, p, 4dlu, 70dlu, 8dlu, p, 4dlu:g, left:p", "f:d, 3dlu, f:d, 6dlu, f:p:g");
    DefaultFormBuilder builder = new DefaultFormBuilder(layout);
    layout.setRowGroups(new int[][] { { 1, 3 } });
    builder.append(combo, 9);
    builder.append(showEmptyCheck);
    builder.nextLine();
    builder.nextLine();
    builder.append(rb.getString("Label.StartDate"), startField);
    builder.append(rb.getString("Label.EndDate"), endField);
    builder.append(refreshButton);
    builder.append(showPercentCheck);
    builder.nextLine();
    builder.nextLine();
    builder.append(chartPanel, 11);
    JPanel panel = builder.getPanel();
    combo.addActionListener(e -> {
        setCurrentAccount(combo.getSelectedAccount());
        pref.putLong(START_DATE, DateUtils.asEpochMilli(startField.getLocalDate()));
    });
    refreshButton.addActionListener(e -> {
        setCurrentAccount(currentAccount);
        pref.putLong(START_DATE, DateUtils.asEpochMilli(startField.getLocalDate()));
    });
    showEmptyCheck.addActionListener(e -> setCurrentAccount(currentAccount));
    showPercentCheck.addActionListener(e -> ((PiePlot) chartPanel.getChart().getPlot()).setLabelGenerator(showPercentCheck.isSelected() ? percentLabels : defaultLabels));
    ChartMouseListener mouseListener = new ChartMouseListener() {

        @Override
        public void chartMouseClicked(final ChartMouseEvent event) {
            MouseEvent me = event.getTrigger();
            if (me.getID() == MouseEvent.MOUSE_CLICKED && me.getClickCount() == 1) {
                try {
                    ChartEntity entity = event.getEntity();
                    // expand sections if interesting, back out if in nothing
                    if (entity instanceof PieSectionEntity) {
                        Account a = (Account) ((PieSectionEntity) entity).getSectionKey();
                        if (a.getChildCount() > 0) {
                            setCurrentAccount(a);
                        }
                    } else if (entity == null) {
                        Account parent = currentAccount;
                        if (parent == null) {
                            return;
                        }
                        parent = parent.getParent();
                        if (parent == null || parent instanceof RootAccount) {
                            return;
                        }
                        setCurrentAccount(parent);
                    }
                } catch (final Exception e) {
                    Logger.getLogger(IncomeExpensePieChart.class.getName()).log(Level.SEVERE, e.getLocalizedMessage(), e);
                }
            }
        }

        @Override
        public void chartMouseMoved(ChartMouseEvent event) {
            setChartCursor(chartPanel, event.getEntity(), event.getTrigger().getPoint());
        }
    };
    chartPanel.addChartMouseListener(mouseListener);
    return panel;
}
Also used : FormLayout(com.jgoodies.forms.layout.FormLayout) JPanel(javax.swing.JPanel) RootAccount(jgnash.engine.RootAccount) Account(jgnash.engine.Account) ChartPanel(org.jfree.chart.ChartPanel) ChartMouseEvent(org.jfree.chart.ChartMouseEvent) MouseEvent(java.awt.event.MouseEvent) JButton(javax.swing.JButton) ChartMouseEvent(org.jfree.chart.ChartMouseEvent) AccountType(jgnash.engine.AccountType) LocalDate(java.time.LocalDate) JFreeChart(org.jfree.chart.JFreeChart) ChartMouseListener(org.jfree.chart.ChartMouseListener) JCheckBox(javax.swing.JCheckBox) RootAccount(jgnash.engine.RootAccount) DatePanel(jgnash.ui.components.DatePanel) DefaultFormBuilder(com.jgoodies.forms.builder.DefaultFormBuilder) ChartEntity(org.jfree.chart.entity.ChartEntity) Engine(jgnash.engine.Engine) PieSectionEntity(org.jfree.chart.entity.PieSectionEntity)

Example 60 with JFreeChart

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

the class RunningAccountBalanceChart method createVerticalXYBarChart.

private JFreeChart createVerticalXYBarChart(Account a) {
    DateFormat df = new SimpleDateFormat("MM/yy");
    TimeSeriesCollection data = createTimeSeriesCollection(a);
    DateAxis dateAxis = new DateAxis(rb.getString("Column.Date"));
    dateAxis.setTickUnit(new DateTickUnit(DateTickUnitType.MONTH, 1, df));
    dateAxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);
    LocalDate start = DateUtils.getFirstDayOfTheMonth(startDateField.getLocalDate());
    LocalDate end = DateUtils.getLastDayOfTheMonth(endDateField.getLocalDate());
    dateAxis.setRange(DateUtils.asDate(start), DateUtils.asDate(end));
    NumberAxis valueAxis = new NumberAxis(rb.getString("Column.Balance"));
    StandardXYToolTipGenerator tooltipGenerator = new StandardXYToolTipGenerator("{1}, {2}", df, NumberFormat.getNumberInstance());
    XYBarRenderer renderer = new XYBarRenderer(0.2);
    renderer.setBaseToolTipGenerator(tooltipGenerator);
    XYPlot plot = new XYPlot(data, dateAxis, valueAxis, renderer);
    String title = rb.getString("Title.EndMonthBalance") + " - " + a.getPathName();
    JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, false);
    chart.setBackgroundPaint(null);
    return chart;
}
Also used : DateAxis(org.jfree.chart.axis.DateAxis) StandardXYToolTipGenerator(org.jfree.chart.labels.StandardXYToolTipGenerator) NumberAxis(org.jfree.chart.axis.NumberAxis) DateTickUnit(org.jfree.chart.axis.DateTickUnit) XYPlot(org.jfree.chart.plot.XYPlot) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) TimeSeriesCollection(org.jfree.data.time.TimeSeriesCollection) SimpleDateFormat(java.text.SimpleDateFormat) LocalDate(java.time.LocalDate) XYBarRenderer(org.jfree.chart.renderer.xy.XYBarRenderer) JFreeChart(org.jfree.chart.JFreeChart)

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