📄 buyportfoliochartjdialog.java
字号:
/*
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Copyright (C) 2008 Cheok YanCheng <yccheok@yahoo.com>
*/
package org.yccheok.jstock.gui;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;
import org.yccheok.jstock.engine.Symbol;
import org.yccheok.jstock.portfolio.Portfolio;
import org.yccheok.jstock.portfolio.Transaction;
import org.yccheok.jstock.portfolio.TransactionSummary;
/**
*
* @author yccheok
*/
public class BuyPortfolioChartJDialog extends javax.swing.JDialog {
private static final String[] cNames = {
"Net Gain %",
"Net Loss %",
"Gain %",
"Loss %",
"Net Gain Value",
"Net Loss Value",
"Gain Value",
"Loss Value",
"Net Purchase Value",
"Purchase Value",
"Current Value",
"Gain Price",
"Loss Price",
"Purchase Price",
"Current Price",
"Units",
"Broker",
"Stamp Duty",
"Clearing Fee",
};
/** Creates new form BuyPortfolioChartJDialog */
public BuyPortfolioChartJDialog(java.awt.Frame parent, boolean modal, BuyPortfolioTreeTableModel portfolioTreeTableModel) {
super(parent, "Buy Summary", modal);
initComponents();
this.portfolioTreeTableModel = portfolioTreeTableModel;
final JFreeChart freeChart = createChart(cNames[0]);
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);
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-750)/2, (screenSize.height-600)/2, 750, 600);
}// </editor-fold>//GEN-END:initComponents
private JFreeChart createChart(String name) {
final Portfolio portfolio = (Portfolio)portfolioTreeTableModel.getRoot();
final int count = portfolio.getChildCount();
DefaultPieDataset data = new DefaultPieDataset();
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 Symbol symbol = transaction.getContract().getStock().getSymbol();
/* Should use reflection technology. */
if(name.equals(cNames[0])) {
data.setValue(symbol.toString(), portfolioTreeTableModel.getNetGainLossPercentage(transactionSummary));
}
if(name.equals(cNames[1])) {
data.setValue(symbol.toString(), -portfolioTreeTableModel.getNetGainLossPercentage(transactionSummary));
}
else if(name.equals(cNames[2])) {
data.setValue(symbol.toString(), portfolioTreeTableModel.getGainLossPercentage(transactionSummary));
}
else if(name.equals(cNames[3])) {
data.setValue(symbol.toString(), -portfolioTreeTableModel.getGainLossPercentage(transactionSummary));
}
else if(name.equals(cNames[4])) {
data.setValue(symbol.toString(), portfolioTreeTableModel.getNetGainLossValue(transactionSummary));
}
else if(name.equals(cNames[5])) {
data.setValue(symbol.toString(), -portfolioTreeTableModel.getNetGainLossValue(transactionSummary));
}
else if(name.equals(cNames[6])) {
data.setValue(symbol.toString(), portfolioTreeTableModel.getGainLossValue(transactionSummary));
}
else if(name.equals(cNames[7])) {
data.setValue(symbol.toString(), -portfolioTreeTableModel.getGainLossValue(transactionSummary));
}
else if(name.equals(cNames[8])) {
data.setValue(symbol.toString(), transactionSummary.getNetTotal());
}
else if(name.equals(cNames[9])) {
data.setValue(symbol.toString(), transactionSummary.getTotal());
}
else if(name.equals(cNames[10])) {
data.setValue(symbol.toString(), portfolioTreeTableModel.getCurrentValue(transactionSummary));
}
else if(name.equals(cNames[11])) {
data.setValue(symbol.toString(), portfolioTreeTableModel.getGainLossPrice(transactionSummary));
}
else if(name.equals(cNames[12])) {
data.setValue(symbol.toString(), -portfolioTreeTableModel.getGainLossPrice(transactionSummary));
}
else if(name.equals(cNames[13])) {
data.setValue(symbol.toString(), portfolioTreeTableModel.getPurchasePrice(transactionSummary));
}
else if(name.equals(cNames[14])) {
data.setValue(symbol.toString(), portfolioTreeTableModel.getCurrentPrice(transactionSummary));
}
else if(name.equals(cNames[15])) {
data.setValue(symbol.toString(), transactionSummary.getQuantity());
}
else if(name.equals(cNames[16])) {
data.setValue(symbol.toString(), transactionSummary.getCalculatedBroker());
}
else if(name.equals(cNames[17])) {
data.setValue(symbol.toString(), transactionSummary.getCalculatedStampDuty());
}
else if(name.equals(cNames[18])) {
data.setValue(symbol.toString(), transactionSummary.getCalculatdClearingFee());
}
}
// create a chart...
return ChartFactory.createPieChart(name, data, true, true, true);
}
private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
// TODO add your handling code here:
String selected = ((javax.swing.JComboBox)evt.getSource()).getSelectedItem().toString();
final JFreeChart freeChart = this.createChart(selected);
chartPanel.setChart(freeChart);
}//GEN-LAST:event_jComboBox1ActionPerformed
private BuyPortfolioTreeTableModel portfolioTreeTableModel;
private ChartPanel chartPanel;
// 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
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -