jfreechat 一些杂记

本文介绍了jFreeChart在实际项目中的高级应用技巧,包括图形平移、通过鼠标操作Legend、实现多坐标轴等功能,并提供了具体实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近做个项目要用到jFreechart,所以在用的过程中的一些东西记录下来,方便自己以后查询

1.图形平移

private void moveRightActionPerformed(java.awt.event.ActionEvent evt) {

Rectangle2D screenDataArea = chartPanel.getScreenDataArea();

screenDataArea.setRect(screenDataArea.getX() + MOVE_LEFT_STEP, screenDataArea.getY(), screenDataArea.getWidth(), screenDataArea.getHeight());
chartPanel.zoom(screenDataArea);
chartPanel.repaint();
}



2.通过鼠标对legend进行操作

……

chartPanel.addChartMouseListener(new MouseListener());//添加鼠标监听响应

……


private class MouseListener implements ChartMouseListener {

public void chartMouseClicked(ChartMouseEvent event) {
if (event.getTrigger().getClickCount() == 2) { //双击相应
ChartEntity localChartEntity = event.getEntity();
if (localChartEntity instanceof LegendItemEntity) { // 判别是否是legend
LegendItemEntity legendItemEntity = (LegendItemEntity) localChartEntity;
XYPlot xyPlot = (XYPlot) chart.getPlot();
TimeSeriesCollection historyDataset = (TimeSeriesCollection) xyPlot.getDataset(HISTROY_LINE); // 数据集

XYLineAndShapeRenderer historyXyLineAndShapeRenderer = (XYLineAndShapeRenderer) xyPlot.getRenderer(HISTROY_LINE); // 画线
// 其他操作……
}
}

public void chartMouseMoved(ChartMouseEvent event) {
}
}




3.多坐标轴操作

private JFreeChart createChart() {
JFreeChart chart = ChartFactory.createTimeSeriesChart("综合曲线面板", "历史曲线时间", "历史曲线值", historyDataset, true, true, false);
chart.getTitle().setFont(titleFont);
XYPlot xyPlot = (XYPlot) chart.getPlot();
DateAxis histroyDataAxis = new DateAxis("历史曲线时间");
histroyDataAxis.setAutoRange(true);
histroyDataAxis.setLabelFont(axisFont);
histroyDataAxis.setTickLabelFont(axisFont);
xyPlot.setDomainAxis(HISTROY_LINE, histroyDataAxis);

ValueAxis historyRangeAxis = new NumberAxis("历史数据值");
historyRangeAxis.setLabelFont(axisFont);
historyRangeAxis.setTickLabelFont(axisFont);
xyPlot.setRangeAxis(HISTROY_LINE,historyRangeAxis);
XYLineAndShapeRenderer historyXyLineAndShapeRenderer = new XYLineAndShapeRenderer();
historyXyLineAndShapeRenderer.setBaseShapesVisible(true);
int i = 0;

xyPlot.setRenderer(HISTROY_LINE, historyXyLineAndShapeRenderer);


realTimeDataset = new TimeSeriesCollection();
xyPlot.setDataset(REAL_TIME_LINE, realTimeDataset);
ValueAxis realTimeValueaxis = new DateAxis("实时时间");
realTimeValueaxis.setLabelFont(axisFont);
//自动设置数据轴数据范围
realTimeValueaxis.setAutoRange(true);
//数据轴固定数据范围 30s
realTimeValueaxis.setFixedAutoRange(30000D);

xyPlot.setDomainAxis(REAL_TIME_LINE, realTimeValueaxis);
ValueAxis realTimeRangeAxis = new NumberAxis("实时数据值");
realTimeRangeAxis.setLabelFont(axisFont);
xyPlot.setRangeAxis(REAL_TIME_LINE, realTimeRangeAxis);
XYLineAndShapeRenderer realTimeXyLineAndShapeRenderer = new XYLineAndShapeRenderer();
//historyXyLineAndShapeRenderer.setBaseShapesVisible(true);
realTimeXyLineAndShapeRenderer.setUseFillPaint(true);
realTimeXyLineAndShapeRenderer.setLegendItemToolTipGenerator(new StandardXYSeriesLabelGenerator("history {0}"));
xyPlot.setRenderer(REAL_TIME_LINE, realTimeXyLineAndShapeRenderer);

// 数据映射
xyPlot.setDomainAxisLocation(REAL_TIME_LINE, AxisLocation.TOP_OR_RIGHT);
xyPlot.setRangeAxisLocation(REAL_TIME_LINE,AxisLocation.TOP_OR_RIGHT);
xyPlot.setDomainAxisLocation(HISTROY_LINE, AxisLocation.BOTTOM_OR_LEFT);
xyPlot.setRangeAxisLocation(HISTROY_LINE, AxisLocation.BOTTOM_OR_LEFT);
xyPlot.mapDatasetToDomainAxis(REAL_TIME_LINE, REAL_TIME_LINE);
xyPlot.mapDatasetToRangeAxis(REAL_TIME_LINE, REAL_TIME_LINE);
xyPlot.mapDatasetToDomainAxis(HISTROY_LINE, HISTROY_LINE);
xyPlot.mapDatasetToRangeAxis(HISTROY_LINE, HISTROY_LINE);
return chart;



4.
jfreechart中,series是表示每个线,他有个自己的key,另外series还有自己在render中的一个index,两者转化靠Dataset中indexOf()函数
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值