📄 jfreechartdemobase.java
字号:
* * @return a chart. */ public JFreeChart createNullXYPlot() { // create a default chart based on some sample data... final String title = this.resources.getString("test.null.title"); final String domain = this.resources.getString("test.null.domain"); final String range = this.resources.getString("test.null.range"); final XYDataset data = null; final JFreeChart chart = ChartFactory.createXYLineChart( title, domain, range, data, PlotOrientation.VERTICAL, true, true, false ); // then customise it a little... chart.setBackgroundPaint(new GradientPaint(0, 0, Color.white, 1000, 0, Color.red)); return chart; } /** * Creates a sample XY plot with an empty dataset. * * @return a sample XY plot with an empty dataset. */ public JFreeChart createXYPlotZeroData() { // create a default chart based on some sample data... final String title = this.resources.getString("test.zero.title"); final String domain = this.resources.getString("test.zero.domain"); final String range = this.resources.getString("test.zero.range"); final XYDataset data = new EmptyXYDataset(); final JFreeChart chart = ChartFactory.createXYLineChart( title, domain, range, data, PlotOrientation.VERTICAL, true, true, false ); // then customise it a little... chart.setBackgroundPaint(new GradientPaint(0, 0, Color.white, 1000, 0, Color.red)); return chart; } /** * Creates and returns a sample time series chart that will be displayed in a scroll pane. * * @return a sample time series chart. */ public JFreeChart createTimeSeriesChartInScrollPane() { // create a default chart based on some sample data... final String title = this.resources.getString("test.scroll.title"); final String domain = this.resources.getString("test.scroll.domain"); final String range = this.resources.getString("test.scroll.range"); final String subtitleStr = this.resources.getString("test.scroll.subtitle"); final XYDataset data = DemoDatasetFactory.createTimeSeriesCollection2(); final JFreeChart chart = ChartFactory.createTimeSeriesChart(title, domain, range, data, true, true, false); // then customise it a little... final TextTitle subtitle = new TextTitle(subtitleStr, new Font("SansSerif", Font.BOLD, 12)); chart.addSubtitle(subtitle); chart.setBackgroundPaint(new GradientPaint(0, 0, Color.white, 0, 1000, Color.gray)); return chart; } /** * Creates and returns a sample bar chart with just one series. * * @return a sample bar chart. */ public JFreeChart createSingleSeriesBarChart() { // create a default chart based on some sample data... final String title = this.resources.getString("test.single.title"); final String domain = this.resources.getString("test.single.domain"); final String range = this.resources.getString("test.single.range"); final String subtitle1Str = this.resources.getString("test.single.subtitle1"); final String subtitle2Str = this.resources.getString("test.single.subtitle2"); final CategoryDataset data = DemoDatasetFactory.createSingleSeriesCategoryDataset(); final JFreeChart chart = ChartFactory.createBarChart(title, domain, range, data, PlotOrientation.HORIZONTAL, true, true, false); chart.addSubtitle(new TextTitle(subtitle1Str)); chart.addSubtitle(new TextTitle(subtitle2Str)); chart.setBackgroundPaint(new GradientPaint(0, 0, Color.white, 1000, 0, Color.red)); return chart; } /** * Displays an XY chart that is periodically updated by a background thread. This is to * demonstrate the event notification system that automatically updates charts as required. * * @return a chart. */ public JFreeChart createDynamicXYChart() { final String title = this.resources.getString("test.dynamic.title"); final String domain = this.resources.getString("test.dynamic.domain"); final String range = this.resources.getString("test.dynamic.range"); final SampleXYDataset data = new SampleXYDataset(); final JFreeChart chart = ChartFactory.createXYLineChart( title, domain, range, data, PlotOrientation.VERTICAL, true, true, false ); final SampleXYDatasetThread update = new SampleXYDatasetThread(data); final Thread thread = new Thread(update); thread.start(); return chart; } /** * Creates and returns a sample overlaid chart. * <P> * Note: with the introduction of multiple secondary datasets in JFreeChart version 0.9.10, * the overlaid chart facility has been removed. You can achieve the same results using * a regular XYPlot with multiple datasets. * * @return an overlaid chart. */ public JFreeChart createOverlaidChart() { // create a default chart based on some sample data... final String title = this.resources.getString("combined.overlaid.title"); final String subtitleStr = this.resources.getString("combined.overlaid.subtitle"); final String domainAxisLabel = this.resources.getString("combined.overlaid.domain"); final String rangeAxisLabel = this.resources.getString("combined.overlaid.range"); // create high-low and moving average dataset final DefaultHighLowDataset highLowData = DemoDatasetFactory.createHighLowDataset(); // make an overlaid plot final ValueAxis domainAxis = new DateAxis(domainAxisLabel); final NumberAxis rangeAxis = new NumberAxis(rangeAxisLabel); rangeAxis.setAutoRangeIncludesZero(false); final XYItemRenderer renderer1 = new HighLowRenderer(); renderer1.setToolTipGenerator(new HighLowItemLabelGenerator()); final XYPlot plot = new XYPlot(highLowData, domainAxis, rangeAxis, renderer1); // overlay a moving average dataset final XYDataset maData = MovingAverage.createMovingAverage( highLowData, " (Moving Average)", 5 * 24 * 60 * 60 * 1000L, 5 * 24 * 60 * 60 * 1000L ); plot.setDataset(1, maData); final XYItemRenderer renderer2 = new StandardXYItemRenderer(); renderer2.setToolTipGenerator( new StandardXYToolTipGenerator( StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT, new SimpleDateFormat("d-MMM-yyyy"), new DecimalFormat("0,000.0") ) ); plot.setRenderer(1, renderer2); // make the top level JFreeChart object final JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, true); // then customise it a little... final TextTitle subtitle = new TextTitle(subtitleStr, new Font("SansSerif", Font.BOLD, 12)); chart.addSubtitle(subtitle); chart.setBackgroundPaint(new GradientPaint(0, 0, Color.white, 0, 1000, Color.blue)); return chart; } /** * Creates a horizontally combined chart. * * @return a horizontally combined chart. */ public JFreeChart createHorizontallyCombinedChart() { // create a default chart based on some sample data... final String title = this.resources.getString("combined.horizontal.title"); final String subtitleStr = this.resources.getString("combined.horizontal.subtitle"); final String[] domains = this.resources.getStringArray("combined.horizontal.domains"); final String rangeAxisLabel = this.resources.getString("combined.horizontal.range"); final TimeSeriesCollection dataset0 = new TimeSeriesCollection(); final TimeSeries eur = DemoDatasetFactory.createEURTimeSeries(); dataset0.addSeries(eur); final TimeSeriesCollection dataset1 = new TimeSeriesCollection(); final TimeSeries mav = MovingAverage.createMovingAverage( eur, "EUR/GBP (30 Day MA)", 30, 30 ); dataset1.addSeries(eur); dataset1.addSeries(mav); final TimeSeriesCollection dataset2 = new TimeSeriesCollection(); dataset2.addSeries(eur); // make a combined range plot final NumberAxis valueAxis = new NumberAxis(rangeAxisLabel); valueAxis.setAutoRangeIncludesZero(false); // override default final CombinedRangeXYPlot parent = new CombinedRangeXYPlot(valueAxis); parent.setRenderer(new StandardXYItemRenderer()); // add subplots final int[] weight = {1, 1, 1}; // controls space assigned to each subplot // add subplot 1... final XYPlot subplot1 = new XYPlot(dataset0, new DateAxis(domains[0]), null, new StandardXYItemRenderer()); parent.add(subplot1, weight[0]); // add subplot 2... final XYPlot subplot2 = new XYPlot(dataset1, new DateAxis(domains[1]), null, new StandardXYItemRenderer()); parent.add(subplot2, weight[1]); // add subplot 3... final XYPlot subplot3 = new XYPlot(dataset2, new DateAxis(domains[2]), null, new XYBarRenderer(0.20)); parent.add(subplot3, weight[2]); // now make the top level JFreeChart final JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, parent, true); // then customise it a little... final TextTitle subtitle = new TextTitle(subtitleStr, new Font("SansSerif", Font.BOLD, 12)); chart.addSubtitle(subtitle); chart.setBackgroundPaint(new GradientPaint(0, 0, Color.white, 0, 1000, Color.blue)); return chart; } /** * Creates and returns a sample vertically combined chart. * * @return a sample vertically combined chart. */ public JFreeChart createVerticallyCombinedChart() { // create a default chart based on some sample data... final String title = this.resources.getString("combined.vertical.title"); final String subtitleStr = this.resources.getString("combined.vertical.subtitle"); final String domain = this.resources.getString("combined.vertical.domain"); final String[] ranges = this.resources.getStringArray("combined.vertical.ranges"); final TimeSeriesCollection dataset0 = new TimeSeriesCollection(); final TimeSeries eur = DemoDatasetFactory.createEURTimeSeries(); dataset0.addSeries(eur); final TimeSeriesCollection dataset1 = new TimeSeriesCollection(); final TimeSeries jpy = DemoDatasetFactory.createJPYTimeSeries(); final TimeSeries mav = MovingAverage.createMovingAverage( jpy, "JPY/GBP (30 Day MA)", 30, 30 ); dataset1.addSeries(jpy); dataset1.addSeries(mav); final XYDataset dataset2 = DemoDatasetFactory.createHighLowDataset(); final TimeSeriesCollection dataset3 = new TimeSeriesCollection(); dataset3.addSeries(eur); // make one shared horizontal axis final ValueAxis timeAxis = new DateAxis(domain); // make a vertically CombinedPlot that will contain the sub-plots final CombinedDomainXYPlot multiPlot = new CombinedDomainXYPlot(timeAxis); final int[] weight = {1, 1, 1, 1}; // control vertical space allocated to each sub-plot // add subplot1... final XYPlot subplot1 = new XYPlot(dataset0, null, new NumberAxis(ranges[0]), new StandardXYItemRenderer()); final NumberAxis range1 = (NumberAxis) subplot1.getRangeAxis(); range1.setTickLabelFont(new Font("Monospaced", Font.PLAIN, 7)); range1.setLabelFont(new Font("SansSerif", Font.PLAIN, 8)); range1.setAutoRangeIncludesZero(false); multiPlot.add(subplot1, weight[0]); // add subplot2... final XYPlot subplot2 = new XYPlot(dataset1, null, new NumberAxis(ranges[1]), new StandardXYItemRenderer()); final NumberAxis range2 = (NumberAxis) subplot2.getRangeAxis(); range2.setTickLabelFont(new Font("Monospaced", Font.PLAIN, 7)); range2.setLabelFont(new Font("SansSerif", Font.PLAIN, 8)); range2.setAutoRangeIncludesZero(false); multiPlot.add(subplot2, weight[1]);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -