Java tutorial
/* * JStock - Free Stock Market Software * Copyright (C) 2012 Yan Cheng CHEOK <yccheok@yahoo.com> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ package org.yccheok.jstock.gui; import java.awt.Color; import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.ValueMarker; import org.jfree.chart.plot.XYPlot; import org.jfree.data.time.Day; import org.jfree.data.time.TimeSeries; import org.jfree.data.time.TimeSeriesCollection; import org.yccheok.jstock.engine.Code; import org.yccheok.jstock.gui.treetable.SellPortfolioTreeTableModelEx; import org.yccheok.jstock.internationalization.GUIBundle; import org.yccheok.jstock.portfolio.DividendSummary; import org.yccheok.jstock.portfolio.Portfolio; import org.yccheok.jstock.portfolio.Transaction; import org.yccheok.jstock.portfolio.TransactionSummary; /** * * @author yccheok * @author oboudet */ public class SellPortfolioTimeChartJDialog extends javax.swing.JDialog { private static final String[] cNames = { GUIBundle.getString("SellPortfolioTreeTableModel_GainValue"), GUIBundle.getString("SellPortfolioTreeTableModel_LossValue"), GUIBundle.getString("SellPortfolioTreeTableModel_CumulativeGainValue"), GUIBundle.getString("SellPortfolioTreeTableModel_CumulativeLossValue"), }; /** * Creates new form SellPortfolioChartJDialog */ public SellPortfolioTimeChartJDialog(java.awt.Frame parent, boolean modal, SellPortfolioTreeTableModelEx portfolioTreeTableModel, DividendSummary dividendSummary) { super(parent, GUIBundle.getString("SellPortfolioChartJDialog_SellSummary"), modal); initComponents(); this.portfolioTreeTableModel = portfolioTreeTableModel; final JFreeChart freeChart; freeChart = createChart(cNames[0]); org.yccheok.jstock.charting.Utils.applyChartTheme(freeChart); chartPanel = new ChartPanel(freeChart, true, true, true, true, true); getContentPane().add(chartPanel, java.awt.BorderLayout.CENTER); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jPanel1 = new javax.swing.JPanel(); jPanel2 = new javax.swing.JPanel(); jComboBox1 = new javax.swing.JComboBox(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); jPanel1.setLayout(new java.awt.BorderLayout()); for (String cName : this.cNames) { this.jComboBox1.addItem(cName); } jComboBox1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jComboBox1ActionPerformed(evt); } }); jPanel2.add(jComboBox1); jPanel1.add(jPanel2, java.awt.BorderLayout.EAST); getContentPane().add(jPanel1, java.awt.BorderLayout.SOUTH); setSize(new java.awt.Dimension(750, 600)); setLocationRelativeTo(null); }// </editor-fold>//GEN-END:initComponents private static class DataEx implements Comparable<DataEx> { public final Date date; public final double value; private DataEx(Date date, double value) { this.date = date; this.value = value; } public static DataEx newInstance(Date date, double value) { return new DataEx(date, value); } @Override public int compareTo(DataEx o) { if (o.date.after(this.date)) { return -1; } else if (o.date.before(this.date)) { return 1; } else { return 0; } } } private JFreeChart createChart(String name) { final JStockOptions jStockOptions = MainFrame.getInstance().getJStockOptions(); final boolean isFeeCalculationEnabled = jStockOptions.isFeeCalculationEnabled(); final boolean isPenceToPoundConversionEnabled = jStockOptions.isPenceToPoundConversionEnabled(); final Portfolio portfolio = (Portfolio) portfolioTreeTableModel.getRoot(); final int count = portfolio.getChildCount(); final List<DataEx> dataExs = new ArrayList<DataEx>(); for (int i = 0; i < count; i++) { TransactionSummary transactionSummary = (TransactionSummary) portfolio.getChildAt(i); if (transactionSummary.getChildCount() <= 0) { continue; } Transaction transaction = (Transaction) transactionSummary.getChildAt(0); final Date date = transaction.getDate().getTime(); if (name.equals(cNames[2]) || name.equals(cNames[3])) { this.isCumulativeChart = true; } else { this.isCumulativeChart = false; } /* Should use reflection technology. */ if (name.equals(cNames[0]) || name.equals(cNames[2])) { if (isPenceToPoundConversionEnabled == false) { if (isFeeCalculationEnabled) { dataExs.add(DataEx.newInstance(date, portfolioTreeTableModel.getNetGainLossValue(transactionSummary))); } else { dataExs.add(DataEx.newInstance(date, portfolioTreeTableModel.getGainLossValue(transactionSummary))); } } else { if (isFeeCalculationEnabled) { dataExs.add(DataEx.newInstance(date, portfolioTreeTableModel.getNetGainLossValue(transactionSummary) / 100.0)); } else { dataExs.add(DataEx.newInstance(date, portfolioTreeTableModel.getGainLossValue(transactionSummary) / 100.0)); } } } else if (name.equals(cNames[1]) || name.equals(cNames[3])) { if (isPenceToPoundConversionEnabled == false) { if (isFeeCalculationEnabled) { dataExs.add(DataEx.newInstance(date, -portfolioTreeTableModel.getNetGainLossValue(transactionSummary))); } else { dataExs.add(DataEx.newInstance(date, -portfolioTreeTableModel.getGainLossValue(transactionSummary))); } } else { if (isFeeCalculationEnabled) { dataExs.add(DataEx.newInstance(date, -portfolioTreeTableModel.getNetGainLossValue(transactionSummary) / 100.0)); } else { dataExs.add(DataEx.newInstance(date, -portfolioTreeTableModel.getGainLossValue(transactionSummary) / 100.0)); } } } } Collections.sort(dataExs); TimeSeries series = new TimeSeries(name); double totalValue = 0; for (DataEx dataEx : dataExs) { double value = dataEx.value; if (!this.isCumulativeChart && series.getValue(new Day(dataEx.date)) != null) { value += series.getValue(new Day(dataEx.date)).doubleValue(); } if (this.isCumulativeChart) { totalValue += value; series.addOrUpdate(new Day(dataEx.date), totalValue); } else { series.addOrUpdate(new Day(dataEx.date), value); } } TimeSeriesCollection dataset = new TimeSeriesCollection(); dataset.addSeries(series); JFreeChart chart = ChartFactory.createTimeSeriesChart(name, "date", null, dataset, false, true, false); ValueMarker marker = new ValueMarker(0); marker.setPaint(Color.black); XYPlot plot = (XYPlot) chart.getXYPlot(); plot.addRangeMarker(marker); return chart; } private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed String selected = ((javax.swing.JComboBox) evt.getSource()).getSelectedItem().toString(); final int selectedIndex = ((javax.swing.JComboBox) evt.getSource()).getSelectedIndex(); MainFrame.getInstance().getJStockOptions().setLastSelectedSellPortfolioChartIndex(selectedIndex); final JFreeChart freeChart = this.createChart(selected); org.yccheok.jstock.charting.Utils.applyChartTheme(freeChart); chartPanel.setChart(freeChart); }//GEN-LAST:event_jComboBox1ActionPerformed private SellPortfolioTreeTableModelEx portfolioTreeTableModel; private ChartPanel chartPanel; private Map<Code, Double> codeToTotalDividend = new HashMap<Code, Double>(); private boolean isCumulativeChart = false; // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JComboBox jComboBox1; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; // End of variables declaration//GEN-END:variables }