?? drawgraphpro.java
字號:
package com.shfe.mail;
import java.awt.*;
import javax.swing.JPanel;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import java.util.*;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.*;
import org.jfree.chart.renderer.xy.*;
import org.jfree.chart.axis.*;
import org.jfree.data.Range;
import org.jfree.data.xy.*;
import com.shfe.mail.*;
import java.text.*;
import java.io.*;
import org.jfree.chart.*;
import org.jfree.chart.renderer.xy.XYBarRenderer.*;
import org.jfree.chart.title.*;
import org.jfree.ui.*;
public class DrawGraphPro extends ApplicationFrame {
public DrawGraphPro(String s) {
super(s);
JFreeChart jfreechart = createChart(s);
ChartPanel chartpanel = new ChartPanel(jfreechart);
chartpanel.setPreferredSize(new Dimension(550, 340));
setContentPane(chartpanel);
}
public static JFreeChart createChart(String title) {
Map resultMap = (FuturesMap.getInstance()).getFuturesData();
ScaleBean scale = (ScaleBean) resultMap.get("scale");
double maxPrice = scale.getMaxPrice();
double minPrice = scale.getMinPrice();
double plinePriceLength = maxPrice - minPrice;
double maxAmount = scale.getMaxAmount();
double closePrice = scale.getClosePrice();
//Pline
XYDataset xyPlinedataset = createPLineDataset();
StandardXYItemRenderer sxyrender = new StandardXYItemRenderer();
sxyrender.setPaint(Color.BLUE);
NumberAxis plineAxisLeft = new NumberAxis();
plineAxisLeft.setRange(new Range(minPrice, maxPrice));
NumberTickUnit plineTick=new NumberTickUnit(plinePriceLength / 10);
plineAxisLeft.setTickUnit(plineTick);
DecimalFormat decimalformat = new DecimalFormat("#0.00");
plineAxisLeft.setNumberFormatOverride(decimalformat);
plineAxisLeft.setTickLabelPaint(Color.BLUE);
plineAxisLeft.setTickMarksVisible(false);
// pline percent
NumberAxis plineAxisRight = new NumberAxis();
plineAxisRight.setRange((minPrice-closePrice)/closePrice,(maxPrice-closePrice)/closePrice);
NumberTickUnit plinePercentTick=new NumberTickUnit(plinePriceLength/(closePrice*10));
plineAxisRight.setTickUnit(plinePercentTick);
DecimalFormat numberformatRight = new DecimalFormat("#0.00%");
plineAxisRight.setNumberFormatOverride(numberformatRight);
plineAxisRight.setTickLabelPaint(Color.BLUE);
plineAxisRight.setTickMarksVisible(false);
XYPlot plinePlot = new XYPlot();//xyPlinedataset, null, plineAxis,sxyrender);
plinePlot.setRenderer(sxyrender);
plinePlot.setDataset(xyPlinedataset);
plinePlot.setRangeAxis(0, plineAxisLeft);
plinePlot.setRangeAxis(1, plineAxisRight);
//Amount
IntervalXYDataset xyAmountdataset = createAmountDataset();
XYBarRenderer xybarRender = new XYBarRenderer();
xybarRender.setPaint(Color.BLUE);
NumberAxis amountYAxis = new NumberAxis();
amountYAxis.setRange(0, maxAmount);
amountYAxis.setTickUnit(new NumberTickUnit(maxAmount / 5));
amountYAxis.setTickMarksVisible(false);
DecimalFormat numberformat = new DecimalFormat("#0");
amountYAxis.setNumberFormatOverride(numberformat);
XYPlot amountPlot = new XYPlot(xyAmountdataset, null, amountYAxis, xybarRender);
//combine
NumberAxis domainAxis = new NumberAxis();
domainAxis.setTickUnit(new NumberTickUnit(3600));
domainAxis.setTickLabelsVisible(false);
domainAxis.setLowerMargin(0D);
domainAxis.setUpperMargin(0D);
CombinedDomainXYPlot combinePlot = new CombinedDomainXYPlot(domainAxis);
combinePlot.setOrientation(PlotOrientation.VERTICAL);
combinePlot.add(plinePlot,2);
combinePlot.add(amountPlot,1);
JFreeChart jfreechart=new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, combinePlot, false);
jfreechart.setBackgroundPaint(Color.white);
TextTitle domainTick = new TextTitle("9:15 11:30/13:00 15:15");
domainTick.setFont(new Font("SansSerif", 0, 10));
domainTick.setPosition(RectangleEdge.BOTTOM);
domainTick.setHorizontalAlignment(HorizontalAlignment.CENTER);
jfreechart.addSubtitle(domainTick);
return jfreechart;
}
private static XYDataset createPLineDataset() {
//x,y
Map resultMap = (FuturesMap.getInstance()).getFuturesData();
java.util.List points = (java.util.List) resultMap.get("points");
XYSeries xyseries = new XYSeries("Series 1");
for (int i = 0; i < points.size(); i++) {
PointBean point = (PointBean) points.get(i);
double xPoint = point.getPointTime();
double yPoint = point.getPointPrice();
//if(i<10)System.out.println("xPoint0="+xPoint+"yPoint0="+yPoint);
xyseries.add(xPoint, yPoint);
}
return new XYSeriesCollection(xyseries);
}
private static IntervalXYDataset createAmountDataset() {
//x,y
Map resultMap = (FuturesMap.getInstance()).getFuturesData();
java.util.List points = (java.util.List) resultMap.get("points");
XYSeries xyseries = new XYSeries("Series 2");
for (int i = 0; i < points.size(); i++) {
PointBean point = (PointBean) points.get(i);
double xPoint = point.getPointTime();
double yPoint = point.getPointAmount();
//if(i<10)System.out.println("xPoint1="+xPoint+"yPoint1="+yPoint);
xyseries.add(xPoint, yPoint);
}
XYSeriesCollection xyseriescollection = new XYSeriesCollection();
xyseriescollection.addSeries(xyseries);
return new XYBarDataset(xyseriescollection, 0.00000000000000002D);
//return new XYSeriesCollection(xyseries);
}
public static void main(String args[]) {
DrawGraphPro xyseriesdemo1 = new DrawGraphPro("IF0508");
xyseriesdemo1.pack();
RefineryUtilities.centerFrameOnScreen(xyseriesdemo1);
xyseriesdemo1.setVisible(true);
FileOutputStream fos_jpg = null;
try {
fos_jpg = new FileOutputStream("C:\\Furtures.jpg");
ChartUtilities.writeChartAsJPEG(fos_jpg, 100,xyseriesdemo1.createChart("IF0508"), 550,340, null);
} catch (Exception ex) {
} finally {
try {
fos_jpg.close();
} catch (Exception e) {}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -