求JFreechart绘制折线图!!高手请进!!急!急!急!

来源:互联网 发布:淘宝上有特色的礼物店 编辑:程序博客网 时间:2024/05/01 20:51

package com;

import java.awt.Color;//颜色系统
import java.text.DateFormat;
import java.text.SimpleDateFormat;//时间格式
import java.util.Calendar;
import java.awt.Paint;//画笔系统
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.time.*;
import org.jfree.data.time.ohlc.OHLCSeries;
import org.jfree.data.time.ohlc.OHLCSeriesCollection;
import org.jfree.chart.renderer.xy.*;
import org.jfree.chart.axis.*;
import org.jfree.chart.plot.*;
import org.jfree.chart.*;

public class KLineCombineChart {

public static void main(String[] args) {

     SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
     double highValue = Double.MIN_VALUE;//设置K线数据当中的最大值
     double minValue = Double.MAX_VALUE;//设置K线数据当中的最小值
     double high1Value = Double.MIN_VALUE;//设置收盘价当中的最大值
     double min1Value = Double.MAX_VALUE;//设置收盘价当中的最小值
     double high2Value = Double.MIN_VALUE;//设置成交量的最大值
     double min2Value = Double.MAX_VALUE;//设置成交量的最低值
     OHLCSeries series = new OHLCSeries("");//高开低收数据序列,股票K线图的四个数据,依次是开,高,低,收
     TimeSeries series2=new TimeSeries("");//对应时间成交量 的数据
     TimeSeries series1=new TimeSeries("");//对应时间收盘价的数据
     try {
      String value;
      BufferedReader br = new BufferedReader(new FileReader("c://SZ000001.TXT"));
   while((value=br.readLine())!=null){
    String[] a = value.split(" ");
            int year = Integer.parseInt(a[0]);
    int month = Integer.parseInt(a[1]);
    int day = Integer.parseInt(a[2]);
    float open = Float.parseFloat(a[3]);
    float high = Float.parseFloat(a[4]);
    float low = Float.parseFloat(a[5]);
    float close = Float.parseFloat(a[6]);
    int volume = Integer.parseInt(a[7]);
    series.add(new Day(day, month, year), open,high,low,close );
    series2.add(new Day(day, month,year ), volume);
    series1.add(new Day(day, month,year ), close);
    DefaultCategoryDataset dataset=new DefaultCategoryDataset();
    dataset.addValue(close,"MA5","yy-mm-dd");
     
   
   }
  
 } catch (FileNotFoundException e1) {
  // TODO Auto-generated catch block
  e1.printStackTrace();
 } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }   
     final OHLCSeriesCollection seriesCollection = new OHLCSeriesCollection();//保留K线数据的数据集,必须申明为final,后面要在匿名内部类里面用到
     seriesCollection.addSeries(series);
 
     TimeSeriesCollection timeSeriesCollection1=new TimeSeriesCollection();//保留收盘价的集合
     timeSeriesCollection1.addSeries(series1);
    
     TimeSeriesCollection timeSeriesCollection=new TimeSeriesCollection();//保留成交量数据的集合
     timeSeriesCollection.addSeries(series2);
 
     //获取K线数据的最高值和最低值
     int seriesCount = seriesCollection.getSeriesCount();//一共有多少个序列,目前为一个
     for (int i = 0; i < seriesCount; i++) {
      int itemCount = seriesCollection.getItemCount(i);//每一个序列有多少个数据项
      for (int j = 0; j < itemCount; j++) {
       if (highValue < seriesCollection.getHighValue(i, j)) {//取第i个序列中的第j个数据项的最大值
        highValue = seriesCollection.getHighValue(i, j);
       }
       if (minValue > seriesCollection.getLowValue(i, j)) {//取第i个序列中的第j个数据项的最小值
        minValue = seriesCollection.getLowValue(i, j);
       }
      }

     }
  //获取最高值和最低值
     int seriesCount2 = timeSeriesCollection.getSeriesCount();//一共有多少个序列,目前为一个
     for (int i = 0; i < seriesCount2; i++) {
      int itemCount = timeSeriesCollection.getItemCount(i);//每一个序列有多少个数据项
      for (int j = 0; j < itemCount; j++) {
       if (high2Value < timeSeriesCollection.getYValue(i,j)) {//取第i个序列中的第j个数据项的值
        high2Value = timeSeriesCollection.getYValue(i,j);
       }
       if (min2Value > timeSeriesCollection.getYValue(i, j)) {//取第i个序列中的第j个数据项的值
        min2Value = timeSeriesCollection.getYValue(i, j);
       }
      }

     }
     //获取收盘价
     int seriesCount1 = timeSeriesCollection.getSeriesCount();//一共有多少个序列,目前为一个
     for (int i = 0; i < seriesCount1; i++) {
         int itemCount = timeSeriesCollection.getItemCount(i);//每一个序列有多少个数据项
         double close=timeSeriesCollection.getYValue(i,4);
     }

     final CandlestickRenderer candlestickRender=new CandlestickRenderer();//设置K线图的画图器,必须申明为final,后面要在匿名内部类里面用到
     candlestickRender.setUseOutlinePaint(true); //设置是否使用自定义的边框线,程序自带的边框线的颜色不符合中国股票市场的习惯
     candlestickRender.setAutoWidthMethod(CandlestickRenderer.WIDTHMETHOD_AVERAGE);//设置如何对K线图的宽度进行设定
     candlestickRender.setAutoWidthGap(0.001);//设置各个K线图之间的间隔
     candlestickRender.setUpPaint(Color.RED.brighter());//设置股票上涨的K线图颜色
     candlestickRender.setDownPaint(Color.green.brighter());//设置股票下跌的K线图颜色
 /* 宝塔线*/  // candlestickRender.setUpPaint(Color.RED.brighter());//设置股票上涨的K线图颜色
 /* 宝塔线*/    // candlestickRender.setDownPaint(Color.green.brighter());//设置股票下跌的K线图颜色
   DateAxis x1Axis=new DateAxis();//设置x轴,也就是时间轴
     x1Axis.setAutoRange(true);//设置不采用自动设置时间范围
     x1Axis.setTimeline(SegmentedTimeline.newMondayThroughFridayTimeline());

     x1Axis.setAutoTickUnitSelection(false);//设置不采用自动选择刻度值 (false为以下的主要前提条件)
     x1Axis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);//设置标记的位置
     x1Axis.setStandardTickUnits(DateAxis.createStandardDateTickUnits());//设置标准的时间刻度单位
     x1Axis.setTickUnit(new DateTickUnit(DateTickUnitType.DAY,7));//设置时间刻度的间隔,一般以周为单位
     x1Axis.setDateFormatOverride(new SimpleDateFormat("yyyy-MM-dd"));//设置显示时间的格式
    x1Axis.setLowerMargin(0.02D);//下跌范围
   x1Axis.setUpperMargin(0.02D);//上涨范围
     
     NumberAxis y1Axis=new NumberAxis();//设定y轴,就是数字轴
     y1Axis.setAutoRange(false);//不不使用自动设定范围
     y1Axis.setRange(minValue*0.9, highValue*1.1);//设定y轴值的范围,比最低值要低一些,比最大值要大一些,这样图形看起来会美观些
     y1Axis.setTickUnit(new NumberTickUnit((highValue*1.1-minValue*0.9)/10));//设置刻度显示的密度
  
     XYPlot plot1=new XYPlot(seriesCollection,x1Axis,y1Axis,candlestickRender);//设置画图区域对象
     XYBarRenderer xyBarRender=new XYBarRenderer(){
     private static final long serialVersionUID = 1L;//为了避免出现警告消息,特设定此值
    public Paint getItemPaint(int i, int j){//

       if(seriesCollection.getCloseValue(i,j)>seriesCollection.getOpenValue(i,j)){//

        return candlestickRender.getUpPaint();
       }else{
        return candlestickRender.getDownPaint();
       }
     }};   
     xyBarRender.setMargin(0.1);//设置柱形图之间的间隔
     NumberAxis y2Axis=new NumberAxis();//设置Y轴,为数值,后面的设置,参考上面的y轴设置
     y2Axis.setAutoRange(false);
     y2Axis.setRange(min2Value*0.9, high2Value*1.1);
     y2Axis.setTickUnit(new NumberTickUnit((high2Value*1.1-min2Value*0.9)/5));
     XYPlot plot2=new XYPlot(timeSeriesCollection,null,y2Axis,xyBarRender);//建立第二个画图区域对象,主要此时的x轴设为了null值,因为要与第一个画图区域对象共享x轴
     CombinedDomainXYPlot combineddomainxyplot = new CombinedDomainXYPlot(x1Axis);//建立一个恰当的联合图形区域对象,以x轴为共享轴
     combineddomainxyplot.add(plot1, 3);//添加图形区域对象,后面的数字是计算这个区域对象应该占据多大的区域2/3
        combineddomainxyplot.add(plot2, 1);//添加图形区域对象,后面的数字是计算这个区域对象应该占据多大的区域1/3
        combineddomainxyplot.setGap(5);//设置两个图形区域对象之间的间隔空间
        JFreeChart chart = new JFreeChart("SZ000001   深发展A", JFreeChart.DEFAULT_TITLE_FONT, combineddomainxyplot, false);    
//        JFreeChart chart1=ChartFactory.createLineChart("SZ000001   深发展A",x1Axis,y1Axis,dataset,PlotOrientation.VERTICAL,true,false,false); 
     ChartFrame frame = new ChartFrame("股票行情分析图", chart);
  frame.setJMenuBar(mnb);
     frame.pack();
     frame.setVisible(true);
    
    
}


}

编译出的结果  没有折线图 求高手帮忙写个折线图的代码

00001.TXT的文件的数据是:

2009 11 05 25.38 25.46 24.85 25.05 28931192 727022400.000
2009 11 06 25.39 25.69 24.81 25.10 30297501 762726336.000
2009 11 09 24.91 25.20 24.28 24.86 32796651 810528320.000

 

 

求均线的绘制。。。5日、10日等的均线  求宝塔线的绘制