Jfreechart资金曲线图
Jfreechart版本1.0.19,实现效果 — 鼠标滑动显示横纵坐标线,标题显示鼠标当前点对应的数据。
import java.awt.Font;
import java.text.DecimalFormat;
import java.util.Date;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.RefineryUtilities;

import com.fda.fc.chart.GraphPanel;
import com.fda.fc.utils.DateUtils;

public class GraphChartDemo extends ApplicationFrame {

	private static final long serialVersionUID = 6034161131043097089L;

	private static final String FRAME_NAME = "资金曲线分析图";

	private String varietyCode = "RB";

	public GraphChartDemo() {
		super(FRAME_NAME);
		try {
			JFreeChart chart = createChart();
			ChartPanel panel = new GraphPanel(chart, varietyCode);
			// panel = new ChartPanel(chart);
			// panel.setHorizontalAxisTrace(true);
			// panel.setVerticalAxisTrace(true);
			// panel.setDomainZoomable(true);
			setContentPane(panel);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private JFreeChart createChart() throws Exception {
		StandardChartTheme standardChartTheme = new StandardChartTheme("CN");
		// 处理中文乱码
		Font font = new Font("宋书", Font.BOLD, 16);
		standardChartTheme.setExtraLargeFont(font);
		standardChartTheme.setRegularFont(font);
		standardChartTheme.setLargeFont(font);
		ChartFactory.setChartTheme(standardChartTheme);

		XYDataset priceData = getCloseXYDataset();
		JFreeChart chart = ChartFactory.createTimeSeriesChart(varietyCode, "日期", "价格", priceData, true, false, false);

		XYPlot plot = (XYPlot) chart.getPlot();
		NumberAxis rangeAxis1 = (NumberAxis) plot.getRangeAxis();
		// rangeAxis1.setLowerMargin(0.1);
		DecimalFormat format = new DecimalFormat("00.00");
		rangeAxis1.setNumberFormatOverride(format);

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

		NumberAxis rangeAxis2 = new NumberAxis("资金");
		// rangeAxis2.setUpperMargin(1.00);
		rangeAxis2.setLabelFont(font);
		rangeAxis2.setAutoTickUnitSelection(true);

		// 初始金额
		int initMoney = 100000;
		// 最终盈利金额
		int finalMoney = 102000;
		if (finalMoney > initMoney) {
			// 设置纵坐标值的间距为1000
			rangeAxis2.setTickUnit(new NumberTickUnit((finalMoney - initMoney) / 10));
			// 纵坐标值只能是arg1-arg2之间的值
			rangeAxis2.setRangeWithMargins(initMoney, finalMoney);
		}

		plot.setDataset(1, getFundDataset(initMoney));
		plot.setRangeAxis(1, rangeAxis2);
		plot.mapDatasetToRangeAxis(1, 1);
		XYLineAndShapeRenderer renderer2 = new XYLineAndShapeRenderer(true, true);
		renderer2.setBaseShapesVisible(false);
		renderer2.setBasePaint(null);
		// renderer2.setToolTipGenerator(new
		// StandardXYToolTipGenerator(StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT,
		// new SimpleDateFormat("yyyy-MM-dd"), new DecimalFormat("0,000.00")));
		plot.setRenderer(1, renderer2);

		// 设置图例位置
		chart.getLegend().setPosition(RectangleEdge.BOTTOM);
		return chart;
	}

	private XYDataset getCloseXYDataset() throws Exception {
		TimeSeries t1 = new TimeSeries("收盘曲线");
		Date date = DateUtils.getDate("2017/02/26", DateUtils.S_YYYYMMDD);
		t1.add(new Day(date), 1250);
		date = DateUtils.getDate("2017/02/27", DateUtils.S_YYYYMMDD);
		t1.add(new Day(date), 1276);
		date = DateUtils.getDate("2017/02/28", DateUtils.S_YYYYMMDD);
		t1.add(new Day(date), 1280);

		return new TimeSeriesCollection(t1);
	}

	private XYDataset getFundDataset(int initMoney) throws Exception {
		TimeSeries t1 = new TimeSeries("资金曲线");
		Date date = DateUtils.getDate("2017/02/26", DateUtils.S_YYYYMMDD);
		t1.add(new Day(date), 100000);
		date = DateUtils.getDate("2017/02/27", DateUtils.S_YYYYMMDD);
		t1.add(new Day(date), 101250);
		date = DateUtils.getDate("2017/02/28", DateUtils.S_YYYYMMDD);
		t1.add(new Day(date), 100150);

		return new TimeSeriesCollection(t1);
	}

	public static void main(String[] args) {
		// 创建曲线图
		GraphChartDemo chartFrame = new GraphChartDemo();
		chartFrame.setSize(600, 400);
		RefineryUtilities.centerFrameOnScreen(chartFrame);
		// chartFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		// chartFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
		// 窗口显示
		chartFrame.setVisible(true);
	}

}

import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.math.BigDecimal;
import java.util.Calendar;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.ui.RectangleEdge;

import com.fda.fc.utils.DateUtils;

public class GraphPanel extends ChartPanel {

	private static final long serialVersionUID = -2590814745466972923L;

	private String varietyCode;

	public GraphPanel(JFreeChart chart, String varietyCode) {
		super(chart);
		this.varietyCode = varietyCode;
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		Graphics2D g2 = (Graphics2D) getGraphics();
		drawHorizontalAxisTrace(g2, e.getX());
		drawVerticalAxisTrace(g2, e.getY());

		XYPlot xyPlot = getChart().getXYPlot();
		int xPos = (int) e.getPoint().getX();
		int yPos = (int) e.getPoint().getY();
		Point2D point2D = this.translateScreenToJava2D(new Point(xPos, yPos));

		ChartRenderingInfo info = this.getChartRenderingInfo();
		Rectangle2D rectangle2D = info.getPlotInfo().getDataArea();
		ValueAxis valueAxis1 = xyPlot.getDomainAxis();
		RectangleEdge edge1 = xyPlot.getDomainAxisEdge();

		ValueAxis valueAxis2 = xyPlot.getRangeAxis();
		RectangleEdge edge2 = xyPlot.getRangeAxisEdge();

		ValueAxis valueAxis3 = xyPlot.getRangeAxis(1);
		RectangleEdge edge3 = xyPlot.getRangeAxisEdge(1);

		double d1 = valueAxis1.java2DToValue(point2D.getX(), rectangle2D, edge1);
		double d2 = valueAxis2.java2DToValue(point2D.getY(), rectangle2D, edge2);
		double d3 = valueAxis3.java2DToValue(point2D.getY(), rectangle2D, edge3);

		BigDecimal dateBD = new BigDecimal(d1);
		long value = dateBD.longValue();
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(value);
		String date = DateUtils.getYYYYMMDD(c);

		BigDecimal priceBD = new BigDecimal(d2);
		int price = priceBD.setScale(0, BigDecimal.ROUND_HALF_UP).intValue();

		BigDecimal fundBD = new BigDecimal(d3);
		int fund = fundBD.setScale(0, BigDecimal.ROUND_HALF_UP).intValue();

		String title = varietyCode + "[日期=" + date + ",价格=" + price + ",资金=" + fund + "]";
		getChart().getTitle().setText(title);
		g2.dispose();
	}

}

阅读更多
文章标签: Jfreechart
个人分类: Jfreechart
想对作者说点什么? 我来说一句

jfreechart各类曲线图JSP代码

2009年03月02日 187KB 下载

没有更多推荐了,返回首页

不良信息举报

Jfreechart资金曲线图

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭