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教程
- 用JFreeChart 來分析Cassandra/Oracle插入海量Oracle
- 大資料路線圖大資料
- 資料視覺化圖表之折線圖視覺化
- ECHARTS-折線圖不顯示資料 控制折線圖顏色Echarts
- 大資料學習路線圖大資料
- 資料庫學習線路圖資料庫
- Tableau——資料前處理、折線圖、餅圖(環形圖)
- Echarts資料視覺化:圖表篇(2)—— 折線圖、堆疊面積折線圖Echarts視覺化
- 解析csv資料繪製曲線圖
- 解讀圖資料庫技術路線資料庫
- 2019最新大資料學習路線圖大資料
- [資料庫][SQL]圖解各種連線join資料庫SQL圖解
- 2019版大資料學習路線圖大資料
- 微信小程式折線圖表折線圖加區域圖微信小程式
- Vue 高德地圖 API Loca 如何使用 連線線圖層、脈衝連線圖層Vue地圖API
- delphi 畫圖表,曲線圖
- Android K線圖圖表庫Android
- R : 折線圖
- echarts - 折線圖Echarts
- java路線圖Java
- echarts折線圖Echarts
- Python路線圖Python
- DAMA資料管理知識體系指南 路線圖
- 生物資訊-McScan(Python-jcvi)共線性畫圖Python
- 大資料Hadoop系統性學習路線圖大資料Hadoop
- 計算機小白大資料學習線路圖計算機大資料
- Python畫圖——matplotlib(普通折線圖)Python
- python 資料視覺化:直方圖、核密度估計圖、箱線圖、累積分佈函式圖Python視覺化直方圖函式
- 一線網際網路企業資深架構師成長路線圖架構
- pandas讀取csv檔案資料並使用matplotlib畫折線圖和餅圖
- Android 折線圖之hellocharts (餅狀圖)餅圖Android
- 演算法與資料結構學習路線圖演算法資料結構
- 圖資料庫 Nebula Graph v.1.0.0-beta 已上線資料庫
- echarts 折線圖拼接Echarts
- AUTOCAD——圖層線型