Jfreechart資金曲線圖

pridefc發表於2018-05-14
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();
	}

}

相關文章