📄 jfreechartdemobase.java
字号:
// add subplot3... final XYPlot subplot3 = new XYPlot(dataset2, null, new NumberAxis(ranges[2]), null); final XYItemRenderer renderer3 = new HighLowRenderer(); subplot3.setRenderer(renderer3); final NumberAxis range3 = (NumberAxis) subplot3.getRangeAxis(); range3.setTickLabelFont(new Font("Monospaced", Font.PLAIN, 7)); range3.setLabelFont(new Font("SansSerif", Font.PLAIN, 8)); range3.setAutoRangeIncludesZero(false); multiPlot.add(subplot3, weight[2]); // add subplot4... final XYPlot subplot4 = new XYPlot(dataset3, null, new NumberAxis(ranges[3]), null); final XYItemRenderer renderer4 = new XYBarRenderer(); subplot4.setRenderer(renderer4); final NumberAxis range4 = (NumberAxis) subplot4.getRangeAxis(); range4.setTickLabelFont(new Font("Monospaced", Font.PLAIN, 7)); range4.setLabelFont(new Font("SansSerif", Font.PLAIN, 8)); range4.setAutoRangeIncludesZero(false); multiPlot.add(subplot4, weight[3]); // now make the top level JFreeChart that contains the CombinedPlot final JFreeChart chart = new JFreeChart( title, JFreeChart.DEFAULT_TITLE_FONT, multiPlot, 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 combined and overlaid chart. * <p> * Note: from version 0.9.10, the overlaid chart is no longer supported (you can achieve * the same result using a regular XYPlot with multiple datasets and renderers). * * @return a combined and overlaid chart. */ public JFreeChart createCombinedAndOverlaidChart1() { // create a default chart based on some sample data... final String title = this.resources.getString("combined.combined-overlaid.title"); final String subtitleStr = this.resources.getString("combined.combined-overlaid.subtitle"); final String domain = this.resources.getString("combined.combined-overlaid.domain"); final String[] ranges = this.resources.getStringArray("combined.combined-overlaid.ranges"); final TimeSeries jpy = DemoDatasetFactory.createJPYTimeSeries(); final TimeSeries mav = MovingAverage.createMovingAverage( jpy, "30 Day Moving Average", 30, 30 ); final TimeSeriesCollection dataset0 = new TimeSeriesCollection(); dataset0.addSeries(jpy); final TimeSeriesCollection dataset1 = new TimeSeriesCollection(); dataset1.addSeries(jpy); dataset1.addSeries(mav); final DefaultHighLowDataset highLowDataset = DemoDatasetFactory.createHighLowDataset(); final XYDataset highLowDatasetMA = MovingAverage.createMovingAverage( highLowDataset, " (MA)", 5 * 24 * 60 * 60 * 1000L, 5 * 24 * 60 * 60 * 1000L ); // make one vertical axis for each (vertical) chart final NumberAxis[] valueAxis = new NumberAxis[3]; for (int i = 0; i < valueAxis.length; i++) { valueAxis[i] = new NumberAxis(ranges[i]); if (i <= 1) { valueAxis[i].setAutoRangeIncludesZero(false); // override default } } // create CombinedPlot... final CombinedDomainXYPlot parent = new CombinedDomainXYPlot(new DateAxis(domain)); final int[] weight = {1, 2, 2}; // add subplot1... final XYItemRenderer renderer1 = new StandardXYItemRenderer(); final XYPlot subplot1 = new XYPlot(dataset0, null, new NumberAxis(ranges[0]), renderer1); final NumberAxis axis1 = (NumberAxis) subplot1.getRangeAxis(); axis1.setTickLabelFont(new Font("Monospaced", Font.PLAIN, 7)); axis1.setLabelFont(new Font("SansSerif", Font.PLAIN, 8)); axis1.setAutoRangeIncludesZero(false); parent.add(subplot1, weight[0]); // add subplot2 (an overlaid plot)... final XYPlot subplot2 = new XYPlot(dataset0, null, new NumberAxis(ranges[1]), new StandardXYItemRenderer()); final NumberAxis axis2 = (NumberAxis) subplot2.getRangeAxis(); axis2.setTickLabelFont(new Font("Monospaced", Font.PLAIN, 7)); axis2.setLabelFont(new Font("SansSerif", Font.PLAIN, 8)); axis2.setAutoRangeIncludesZero(false); subplot2.setDataset(1, dataset1); subplot2.setRenderer(1, new StandardXYItemRenderer()); parent.add(subplot2, weight[1]); // add subplot3 (an overlaid plot)... final XYItemRenderer renderer3 = new HighLowRenderer(); final XYPlot subplot3 = new XYPlot( highLowDataset, null, new NumberAxis(ranges[2]), renderer3 ); final NumberAxis axis3 = (NumberAxis) subplot3.getRangeAxis(); axis3.setTickLabelFont(new Font("Monospaced", Font.PLAIN, 7)); axis3.setLabelFont(new Font("SansSerif", Font.PLAIN, 8)); axis3.setAutoRangeIncludesZero(false); subplot3.setDataset(1, highLowDatasetMA); subplot3.setRenderer(1, new StandardXYItemRenderer()); parent.add(subplot3, weight[2]); // now create the master JFreeChart object final JFreeChart chart = new JFreeChart( title, new Font("SansSerif", Font.BOLD, 12), parent, true ); // then customise it a little... final TextTitle subtitle = new TextTitle(subtitleStr, new Font("SansSerif", Font.BOLD, 10)); chart.addSubtitle(subtitle); chart.setBackgroundPaint(new GradientPaint(0, 0, Color.white, 0, 1000, Color.blue)); 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 createCombinedAndOverlaidDynamicXYChart() { // chart title and axis labels... final String title = this.resources.getString("combined.dynamic.title"); final String subtitleStr = this.resources.getString("combined.dynamic.subtitle"); final String domainAxisLabel = this.resources.getString("combined.dynamic.domain"); final String[] ranges = this.resources.getStringArray("combined.dynamic.ranges"); // setup sample base 2-series dataset final SampleXYDataset data = new SampleXYDataset(); // create some SubSeriesDatasets and CombinedDatasets to test events final XYDataset series0 = new SubSeriesDataset(data, 0); final XYDataset series1 = new SubSeriesDataset(data, 1); final CombinedDataset combinedData = new CombinedDataset(); combinedData.add(series0); combinedData.add(series1); // create common time axis final NumberAxis timeAxis = new NumberAxis(domainAxisLabel); timeAxis.setTickMarksVisible(true); timeAxis.setAutoRangeIncludesZero(false); // make one vertical axis for each (vertical) chart final NumberAxis[] valueAxis = new NumberAxis[4]; for (int i = 0; i < valueAxis.length; i++) { valueAxis[i] = new NumberAxis(ranges[i]); valueAxis[i].setAutoRangeIncludesZero(false); } final CombinedDomainXYPlot plot = new CombinedDomainXYPlot(timeAxis); // add subplot1... final XYItemRenderer renderer0 = new StandardXYItemRenderer(); final XYPlot subplot0 = new XYPlot(series0, null, valueAxis[0], renderer0); plot.add(subplot0, 1); // add subplot2... final XYItemRenderer renderer1 = new StandardXYItemRenderer(); final XYPlot subplot1 = new XYPlot(series1, null, valueAxis[1], renderer1); plot.add(subplot1, 1); // add subplot3... final XYPlot subplot2 = new XYPlot( series0, null, valueAxis[2], new StandardXYItemRenderer() ); subplot2.setDataset(1, series1); subplot2.setRenderer(1, new StandardXYItemRenderer()); plot.add(subplot2, 1); // add subplot4... final XYItemRenderer renderer3 = new StandardXYItemRenderer(); final XYPlot subplot3 = new XYPlot(data, null, valueAxis[3], renderer3); plot.add(subplot3, 1); 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.cyan)); // setup thread to update base Dataset final SampleXYDatasetThread update = new SampleXYDatasetThread(data); final Thread thread = new Thread(update); thread.start(); return chart; } /** * Creates a gantt chart. * * @return a gantt chart. */ public JFreeChart createGanttChart() { final String title = this.resources.getString("gantt.task.title"); final String domain = this.resources.getString("gantt.task.domain"); final String range = this.resources.getString("gantt.task.range"); final IntervalCategoryDataset data = createGanttDataset1(); final JFreeChart chart = ChartFactory.createGanttChart( title, domain, range, data, true, true, false ); // then customise it a little... chart.setBackgroundPaint(new GradientPaint(0, 0, Color.white, 1000, 0, Color.blue)); return chart; } /** * Creates a sample dataset for a Gantt chart. * * @return The dataset. * * @deprecated Moved to the demo applications that require it. */ private static IntervalCategoryDataset createGanttDataset1() { final TaskSeries s1 = new TaskSeries("Scheduled"); s1.add(new Task("Write Proposal", new SimpleTimePeriod(date(1, Calendar.APRIL, 2001), date(5, Calendar.APRIL, 2001)))); s1.add(new Task("Obtain Approval", new SimpleTimePeriod(date(9, Calendar.APRIL, 2001), date(9, Calendar.APRIL, 2001)))); s1.add(new Task("Requirements Analysis", new SimpleTimePeriod(date(10, Calendar.APRIL, 2001), date(5, Calendar.MAY, 2001)))); s1.add(new Task("Design Phase", new SimpleTimePeriod(date(6, Calendar.MAY, 2001), date(30, Calendar.MAY, 2001)))); s1.add(new Task("Design Signoff", new SimpleTimePeriod(date(2, Calendar.JUNE, 2001), date(2, Calendar.JUNE, 2001)))); s1.add(new Task("Alpha Implementation", new SimpleTimePeriod(date(3, Calendar.JUNE, 2001), date(31, Calendar.JULY, 2001)))); s1.add(new Task("Design Review", new SimpleTimePeriod(date(1, Calendar.AUGUST, 2001), date(8, Calendar.AUGUST, 2001)))); s1.add(new Task("Revised Design Signoff", new SimpleTimePeriod(date(10, Calendar.AUGUST, 2001), date(10, Calendar.AUGUST, 2001)))); s1.add(new Task("Beta Implementation", new SimpleTimePeriod(date(12, Calendar.AUGUST, 2001), date(12, Calendar.SEPTEMBER, 2001)))); s1.add(new Task("Testing", new SimpleTimePeriod(date(13, Calendar.SEPTEMBER, 2001), date(31, Calendar.OCTOBER, 2001)))); s1.add(new Task("Final Implementation", new SimpleTimePeriod(date(1, Calendar.NOVEMBER, 2001), date(15, Calendar.NOVEMBER, 2001)))); s1.add(new Task("Signoff", new SimpleTimePeriod(date(28, Calendar.NOVEMBER, 2001), date(30, Calendar.NOVEMBER, 2001)))); final TaskSeries s2 = new TaskSeries("Actual"); s2.add(new Task("Write Proposal", new SimpleTimePeriod(date(1, Calendar.APRIL, 2001), date(5, Calendar.APRIL, 2001)))); s2.add(new Task("Obtain Approval", new SimpleTimePeriod(date(9, Calendar.APRIL, 2001), date(9, Calendar.APRIL, 2001)))); s2.add(new Task("Requirements Analysis", new SimpleTimePeriod(date(10, Calendar.APRIL, 2001), date(15, Calendar.MAY, 2001)))); s2.add(new Task("Design Phase", new SimpleTimePeriod(date(15, Calendar.MAY, 2001), date(17, Calendar.JUNE, 2001)))); s2.add(new Task("Design Signoff", new SimpleTimePeriod(date(30, Calendar.JUNE, 2001), date(30, Calendar.JUNE, 2001)))); s2.add(new Task("Alpha Implementation", new SimpleTimePeriod(date(1, Calendar.JULY, 2001), date(12, Calendar.SEPTEMBER, 2001)))); s2.add(new Task("Design Review",
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -