來源自 這裡
UI 效果: (jfreechart-1.0.14)
範例代碼:
- ScatterPlotDemo1.java
UI 效果: (jfreechart-1.0.14)
範例代碼:
- ScatterPlotDemo1.java
- package charts.xy;
- import java.awt.Color;
- import javax.swing.JPanel;
- import org.jfree.chart.ChartFactory;
- import org.jfree.chart.ChartPanel;
- import org.jfree.chart.JFreeChart;
- import org.jfree.chart.axis.NumberAxis;
- import org.jfree.chart.plot.PlotOrientation;
- import org.jfree.chart.plot.XYPlot;
- import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
- import org.jfree.data.xy.XYDataset;
- import org.jfree.data.xy.XYSeries;
- import org.jfree.data.xy.XYSeriesCollection;
- import org.jfree.ui.ApplicationFrame;
- import org.jfree.ui.RefineryUtilities;
- public class ScatterPlotDemo1 extends ApplicationFrame {
- /**
- * A demonstration application showing a scatter plot.
- *
- * @param title the frame title.
- */
- public ScatterPlotDemo1(String title) {
- super(title);
- JPanel chartPanel = createDemoPanel();
- chartPanel.setPreferredSize(new java.awt.Dimension(550, 350));
- setContentPane(chartPanel);
- }
- private static XYDataset createData()
- {
- XYSeriesCollection my_data_series= new XYSeriesCollection();
- XYSeries s = new XYSeries("Cluster1");
- s.add(1,1);
- s.add(2,2);
- s.add(3,1);
- s.add(2,4);
- my_data_series.addSeries(s);
- s = new XYSeries("Cluster2");
- s.add(2,14);
- s.add(3,13);
- s.add(3,14);
- my_data_series.addSeries(s);
- s = new XYSeries("Cluster3");
- s.add(11,4);
- s.add(12,4);
- s.add(13,4);
- s.add(14,4);
- my_data_series.addSeries(s);
- s = new XYSeries("Cluster4");
- s.add(12,13);
- s.add(13,12);
- s.add(13,13);
- s.add(14,14);
- my_data_series.addSeries(s);
- return my_data_series;
- }
- private static JFreeChart createChart(XYDataset dataset) {
- JFreeChart chart = ChartFactory.createScatterPlot("Scatter Plot Demo 1",
- "X", "Y", dataset, PlotOrientation.VERTICAL, true, false, false);
- XYPlot plot = (XYPlot) chart.getPlot();
- plot.setNoDataMessage("NO DATA");
- plot.setDomainZeroBaselineVisible(true);
- plot.setRangeZeroBaselineVisible(true);
- XYLineAndShapeRenderer renderer
- = (XYLineAndShapeRenderer) plot.getRenderer();
- renderer.setSeriesOutlinePaint(0, Color.black);
- renderer.setUseOutlinePaint(true);
- NumberAxis domainAxis = (NumberAxis) plot.getDomainAxis();
- domainAxis.setAutoRangeIncludesZero(false);
- domainAxis.setTickMarkInsideLength(2.0f);
- domainAxis.setTickMarkOutsideLength(0.0f);
- NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
- rangeAxis.setTickMarkInsideLength(2.0f);
- rangeAxis.setTickMarkOutsideLength(0.0f);
- return chart;
- }
- /**
- * Creates a panel for the demo (used by SuperDemo.java).
- *
- * @return A panel.
- */
- public static JPanel createDemoPanel() {
- JFreeChart chart = createChart(createData());
- ChartPanel chartPanel = new ChartPanel(chart);
- //chartPanel.setVerticalAxisTrace(true);
- //chartPanel.setHorizontalAxisTrace(true);
- // popup menu conflicts with axis trace
- chartPanel.setPopupMenu(null);
- chartPanel.setDomainZoomable(true);
- chartPanel.setRangeZoomable(true);
- return chartPanel;
- }
- /**
- * Starting point for the demonstration application.
- *
- * @param args ignored.
- */
- public static void main(String[] args) {
- ScatterPlotDemo1 demo = new ScatterPlotDemo1("Scatter Plot Demo 1");
- demo.pack();
- RefineryUtilities.centerFrameOnScreen(demo);
- demo.setVisible(true);
- }
- }
沒有留言:
張貼留言