⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chartjdialog.java

📁 JStock是一个免费股市软件
💻 JAVA
字号:
/*
 * ChartJDialog.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) 2007 Cheok YanCheng <yccheok@yahoo.com>
 */

package org.yccheok.jstock.gui;

import org.yccheok.jstock.engine.*;

import java.util.*;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYBarRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.*;
import org.jfree.date.*;

/**
 *
 * @author  yccheok
 */
public class ChartJDialog extends javax.swing.JDialog {
    
    /** Creates new form ChartJDialog */
    public ChartJDialog(java.awt.Frame parent, String title, boolean modal, StockHistoryServer stockHistoryServer) {
        super(parent, title, modal);
                
        initComponents();
        
        this.stockHistoryServer = stockHistoryServer;
        
        updateLabels(stockHistoryServer);        
        
        final JFreeChart freeChart = createChart(stockHistoryServer);
        
        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() {

        jPanel4 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        jComboBox1 = new javax.swing.JComboBox();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel4.setLayout(new java.awt.BorderLayout());

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Price Volume", "Candlestick" }));
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });
        jPanel5.add(jComboBox1);

        jPanel4.add(jPanel5, java.awt.BorderLayout.EAST);

        getContentPane().add(jPanel4, java.awt.BorderLayout.SOUTH);

        jPanel1.setLayout(new java.awt.BorderLayout());

        jLabel1.setFont(new java.awt.Font("Dialog", 0, 12));
        jLabel1.setText("Market Capital (RM) : ");
        jPanel2.add(jLabel1);
        jPanel2.add(jLabel2);

        jLabel3.setFont(new java.awt.Font("Dialog", 0, 12));
        jLabel3.setText("Shares Issued : ");
        jPanel2.add(jLabel3);
        jPanel2.add(jLabel4);

        jPanel1.add(jPanel2, java.awt.BorderLayout.WEST);

        jLabel5.setFont(new java.awt.Font("Dialog", 0, 12));
        jLabel5.setText("Board : ");
        jPanel3.add(jLabel5);
        jPanel3.add(jLabel6);

        jLabel7.setFont(new java.awt.Font("Dialog", 0, 12));
        jLabel7.setText("Sector : ");
        jPanel3.add(jLabel7);
        jPanel3.add(jLabel8);

        jPanel1.add(jPanel3, java.awt.BorderLayout.EAST);

        getContentPane().add(jPanel1, java.awt.BorderLayout.NORTH);

        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 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();
        
        if(selected.equals("Price Volume")) {            
            final JFreeChart freeChart = this.createChart(stockHistoryServer);
            chartPanel.setChart(freeChart);
        }
        else if(selected.equals("Candlestick")) {
            final JFreeChart freeChart = this.createCandlestickChart(stockHistoryServer);
            chartPanel.setChart(freeChart);
        }
    }//GEN-LAST:event_jComboBox1ActionPerformed
    
    private void updateLabels(StockHistoryServer stockHistoryServer) {
        java.text.NumberFormat numberFormat = java.text.NumberFormat.getInstance();
        this.jLabel4.setText(numberFormat.format(stockHistoryServer.getSharesIssued()));
        
        numberFormat.setMaximumFractionDigits(2);
        numberFormat.setMinimumFractionDigits(2);
        this.jLabel2.setText(numberFormat.format(stockHistoryServer.getMarketCapital()));
        
        final int num = stockHistoryServer.getNumOfCalendar();
        final Stock stock = stockHistoryServer.getStock(stockHistoryServer.getCalendar(num - 1));
        
        this.jLabel6.setText(stock.getBoard().toString());
        this.jLabel8.setText(stock.getIndustry().toString());
    }
    
    /**
     * Creates a chart.
     *
     * @return a chart.
     */
    private static JFreeChart createChart(StockHistoryServer stockHistoryServer) {

        XYDataset priceData = createPriceDataset(stockHistoryServer);
        
        final int num = stockHistoryServer.getNumOfCalendar();
        final Stock stock = stockHistoryServer.getStock(stockHistoryServer.getCalendar(num - 1));
        
        final String title = stock.getName();
        JFreeChart chart = ChartFactory.createTimeSeriesChart(
            title,
            "Date",
            "Price",
            priceData,
            true,
            true,
            false
        );
        XYPlot plot = chart.getXYPlot();
        NumberAxis rangeAxis1 = (NumberAxis) plot.getRangeAxis();
        rangeAxis1.setLowerMargin(0.40);  // to leave room for volume bars
        DecimalFormat format = new DecimalFormat("00.00");
        rangeAxis1.setNumberFormatOverride(format);

        XYItemRenderer renderer1 = plot.getRenderer();
        renderer1.setToolTipGenerator(
            new StandardXYToolTipGenerator(
                StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT,
                new SimpleDateFormat("d-MMM-yyyy"), new DecimalFormat("0.00")
            )
        );

        NumberAxis rangeAxis2 = new NumberAxis("Volume");
        rangeAxis2.setUpperMargin(1.00);  // to leave room for price line
        plot.setRangeAxis(1, rangeAxis2);
        plot.setDataset(1, createVolumeDataset(stockHistoryServer));
        plot.setRangeAxis(1, rangeAxis2);
        plot.mapDatasetToRangeAxis(1, 1);
        XYBarRenderer renderer2 = new XYBarRenderer(0.20);
        renderer2.setToolTipGenerator(
            new StandardXYToolTipGenerator(
                StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT,
                new SimpleDateFormat("d-MMM-yyyy"), new DecimalFormat("0,000.00")
            )
        );
        plot.setRenderer(1, renderer2);
        return chart;
    }
    
    /**
     * Creates a sample dataset.
     *
     * @return A sample dataset.
     */
    private static XYDataset createPriceDataset(StockHistoryServer stockHistoryServer) {

        // create dataset 1...
        TimeSeries series1 = new TimeSeries("Price", Day.class);
        
        final int num = stockHistoryServer.getNumOfCalendar();
        
        for(int i = 0; i < num; i++) {
            final Calendar c = stockHistoryServer.getCalendar(i);
            final Stock s = stockHistoryServer.getStock(c);
            series1.add(new Day(c.getTime()), s.getLastPrice());
        }
        
        return new TimeSeriesCollection(series1);

    }

    /**
     * Creates a sample dataset.
     *
     * @return A sample dataset.
     */
    private static IntervalXYDataset createVolumeDataset(StockHistoryServer stockHistoryServer) {

        // create dataset 2...
        TimeSeries series1 = new TimeSeries("Volume", Day.class);

        final int num = stockHistoryServer.getNumOfCalendar();
        
        for(int i = 0; i < num; i++) {
            final Calendar c = stockHistoryServer.getCalendar(i);
            final Stock s = stockHistoryServer.getStock(c);
            series1.add(new Day(c.getTime()), s.getVolume());
        }

        return new TimeSeriesCollection(series1);
    }
    
    
    /**
     * Creates a chart.
     * 
     * @param dataset  the dataset.
     * 
     * @return The dataset.
     */
    private static JFreeChart createCandlestickChart(StockHistoryServer stockHistoryServer) {
        OHLCDataset dataset = createDataset(stockHistoryServer);
    
        final int num = stockHistoryServer.getNumOfCalendar();
        final Stock stock = stockHistoryServer.getStock(stockHistoryServer.getCalendar(num - 1));
        
        final String title = stock.getName();
        
        JFreeChart chart = ChartFactory.createCandlestickChart(
            title,
            "Time", 
            "Value",
            dataset, 
            true
        );
        XYPlot plot = (XYPlot) chart.getPlot();        
        NumberAxis axis = (NumberAxis) plot.getRangeAxis();
        axis.setAutoRangeIncludesZero(false);
        axis.setUpperMargin(0.0);
        axis.setLowerMargin(0.0);
        return chart;        
    }
    
    /**
     * Creates a sample high low dataset.
     *
     * @return a sample high low dataset.
     */
    public static OHLCDataset createDataset(StockHistoryServer stockHistoryServer) {

        final int num = stockHistoryServer.getNumOfCalendar();
        
        Date[] date = new Date[num];
        double[] high = new double[num];
        double[] low = new double[num];
        double[] open = new double[num];
        double[] close = new double[num];
        double[] volume = new double[num];
        
        for(int i = 0; i < num; i++) {
            final Calendar c = stockHistoryServer.getCalendar(i);
            final Stock s = stockHistoryServer.getStock(c);
            
            date[i] = s.getCalendar().getTime();
            high[i] = s.getHighPrice();
            low[i] = s.getLowPrice();
            open[i] = s.getOpenPrice();
            close[i] = s.getLastPrice();
            volume[i] = s.getVolume();
        }
        
        return new DefaultHighLowDataset("Series 1", date, high, low, open, 
                close, volume);
    }
    
    private final StockHistoryServer stockHistoryServer;
    private final ChartPanel chartPanel;
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    // End of variables declaration//GEN-END:variables
    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -