import gui.jfc.ano.AxisAnnotationEntity;
import gui.jfc.ano.XYDomainValueAnnotation;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import javax.swing.JPanel;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.CombinedDomainXYPlot;
import org.jfree.chart.plot.PlotRenderingInfo;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.*;
import org.jfree.data.time.*;
import org.jfree.data.xy.IntervalXYDataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.*;
public final class CombinedXYPlotDemo1 extends ApplicationFrame {
public CombinedXYPlotDemo1(String s) {
super(s);
JPanel jpanel = createDemoPanel();
((ChartPanel) jpanel).setDomainZoomable(false);
((ChartPanel) jpanel).setRangeZoomable(false);
jpanel.setPreferredSize(new Dimension(500, 270));
jpanel.addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
Point p = e.getPoint();
ChartPanel chartPanel = (ChartPanel) jpanel;
if (chartPanel.getEntityForPoint(p.x, p.y) instanceof AxisAnnotationEntity) {
AxisAnnotationEntity entity = (AxisAnnotationEntity) chartPanel.getEntityForPoint(p.x, p.y);
XYDomainValueAnnotation annotation = (XYDomainValueAnnotation) entity.getAnnotation();
Point2D p2D = chartPanel.translateScreenToJava2D(p);
Rectangle2D dataArea = chartPanel.getScreenDataArea((int) p.getX(), (int) p.getY());
JFreeChart chart = chartPanel.getChart();
ChartRenderingInfo cInfo = chartPanel.getChartRenderingInfo();
PlotRenderingInfo pInfo = null;
if (cInfo != null) {
pInfo = cInfo.getPlotInfo();
}
XYPlot plot = (XYPlot) chart.getPlot();
if (plot instanceof CombinedDomainXYPlot) {
CombinedDomainXYPlot combinedPlot = (CombinedDomainXYPlot) plot;
plot = combinedPlot.findSubplot(pInfo, p2D);
}
ValueAxis domainAxis = plot.getDomainAxis();
ValueAxis rangeAxis = plot.getRangeAxis();
RectangleEdge xAxisLocation = plot.getDomainAxisEdge();
RectangleEdge yAxisLocation = plot.getRangeAxisEdge();
double java2dX = domainAxis.java2DToValue(e.getX(), dataArea, xAxisLocation);
double java2dY = rangeAxis.java2DToValue(e.getY(), dataArea, yAxisLocation);
annotation.setValue(java2dX);
repaint();
}
}
});
jpanel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
ChartPanel chartPanel = (ChartPanel) jpanel;
Point p = e.getPoint();
Point2D p2D = chartPanel.translateScreenToJava2D(p);
Rectangle2D dataArea = chartPanel.getScreenDataArea((int) p.getX(), (int) p.getY());
JFreeChart chart = chartPanel.getChart();
ChartRenderingInfo cInfo = chartPanel.getChartRenderingInfo();
PlotRenderingInfo pInfo = null;
if (cInfo != null) {
pInfo = cInfo.getPlotInfo();
}
XYPlot plot = (XYPlot) chart.getPlot();
if (plot instanceof CombinedDomainXYPlot) {
CombinedDomainXYPlot combinedPlot = (CombinedDomainXYPlot) plot;
plot = combinedPlot.findSubplot(pInfo, p2D);
}
ValueAxis domainAxis = plot.getDomainAxis();
ValueAxis rangeAxis = plot.getRangeAxis();
RectangleEdge xAxisLocation = plot.getDomainAxisEdge();
RectangleEdge yAxisLocation = plot.getRangeAxisEdge();
double java2dX = domainAxis.java2DToValue(e.getX(), dataArea, xAxisLocation);
double java2dY = rangeAxis.java2DToValue(e.getY(), dataArea, yAxisLocation);
XYDomainValueAnnotation annotation = new XYDomainValueAnnotation();
annotation.setPaint(Color.RED);
annotation.setStroke(new BasicStroke(0.5f));
annotation.setValue(java2dX);
plot.addAnnotation(annotation);
}
});
setContentPane(jpanel);
}
private JFreeChart createCombinedChart() {
IntervalXYDataset intervalDataset = createDataset1();
XYLineAndShapeRenderer lineAndShapeRenderer = new XYLineAndShapeRenderer(true, false);
lineAndShapeRenderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator("{0}: ({1}, {2})", new SimpleDateFormat("d-MMM-yyyy"), new DecimalFormat("0.00")));
lineAndShapeRenderer.setSeriesStroke(0, new BasicStroke(4F, 1, 2));
lineAndShapeRenderer.setSeriesPaint(0, Color.blue);
DateAxis dateAxis = new DateAxis("Year");
dateAxis.setLowerMargin(0.0D);
dateAxis.setUpperMargin(0.02D);
NumberAxis numberAxis = new NumberAxis("$billion");
XYPlot plot = new XYPlot(intervalDataset, null, numberAxis, lineAndShapeRenderer);
plot.setBackgroundPaint(Color.lightGray);
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
IntervalXYDataset intervalDataset1 = createDataset2();
XYBarRenderer barRenderer = new XYBarRenderer() {
@Override
public Paint getItemPaint(int i, int j) {
XYDataset dataset = getPlot().getDataset();
if (dataset.getYValue(i, j) >= 0.0D) {
return Color.red;
} else {
return Color.green;
}
}
};
barRenderer.setSeriesPaint(0, Color.red);
barRenderer.setDrawBarOutline(false);
barRenderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator("{0}: ({1}, {2})", new SimpleDateFormat("d-MMM-yyyy"), new DecimalFormat("0.00")));
XYPlot plot1 = new XYPlot(intervalDataset1, null, new NumberAxis("$billion"), barRenderer);
plot1.setBackgroundPaint(Color.lightGray);
plot1.setDomainGridlinePaint(Color.white);
plot1.setRangeGridlinePaint(Color.white);
CombinedDomainXYPlot combinedDomainPlot = new CombinedDomainXYPlot(dateAxis);
combinedDomainPlot.add(plot, 3);
combinedDomainPlot.add(plot1, 2);
combinedDomainPlot.setGap(8D);
combinedDomainPlot.setDomainGridlinePaint(Color.white);
combinedDomainPlot.setDomainGridlinesVisible(true);
JFreeChart freeChart = new JFreeChart("United States Public Debt", JFreeChart.DEFAULT_TITLE_FONT, combinedDomainPlot, false);
freeChart.setBackgroundPaint(Color.white);
return freeChart;
}
private static IntervalXYDataset createDataset1(){
TimeSeries timeseries = new TimeSeries("Public Debt Outstanding", org.jfree.data.time.Month.class);
timeseries.add(new Month(1, 1990), 2974.5839999999998D);
timeseries.add(new Month(1, 2007), 8707.5609999999997D);
return new TimeSeriesCollection(timeseries);
}
private static IntervalXYDataset createDataset2() {
TimeSeriesCollection timeseriescollection = new TimeSeriesCollection();
TimeSeries timeseries = new TimeSeries("Change from previous year", org.jfree.data.time.Month.class);
timeseries.add(new Month(1, 1990), 276.62700000000001D);
timeseries.add(new Month(1, 2007), 511.49099999999999D);
timeseriescollection.addSeries(timeseries);
return timeseriescollection;
}
public JPanel createDemoPanel() {
JFreeChart freeChart = createCombinedChart();
return new ChartPanel(freeChart);
}
public static void main(String args[]) {
CombinedXYPlotDemo1 combinedxyplotdemo1 = new CombinedXYPlotDemo1("JFreeChart : CombinedXYPlotDemo1");
combinedxyplotdemo1.pack();
RefineryUtilities.centerFrameOnScreen(combinedxyplotdemo1);
combinedxyplotdemo1.setVisible(true);
}
}