0% found this document useful (0 votes)
58 views8 pages

Lunes 11 Oct 2010

This document contains code for creating a Gantt chart using JFreeChart. It defines a class called GanttDemo2 that extends ApplicationFrame. The createSampleDataset() method generates sample task data to display in the chart, including tasks with subtasks. This data is used to construct a JFreeChart Gantt chart, which is displayed in a ChartPanel and added to the application frame. The main() method runs the demo application.

Uploaded by

alfiljass
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views8 pages

Lunes 11 Oct 2010

This document contains code for creating a Gantt chart using JFreeChart. It defines a class called GanttDemo2 that extends ApplicationFrame. The createSampleDataset() method generates sample task data to display in the chart, including tasks with subtasks. This data is used to construct a JFreeChart Gantt chart, which is displayed in a ChartPanel and added to the application frame. The main() method runs the demo application.

Uploaded by

alfiljass
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

import import import import import import import import import import import

org.jfree.chart.ChartFactory; org.jfree.chart.ChartPanel; org.jfree.chart.JFreeChart; org.jfree.chart.axis.AxisLocation; org.jfree.chart.axis.NumberAxis; org.jfree.chart.plot.CategoryPlot; org.jfree.chart.plot.PlotOrientation; org.jfree.data.category.CategoryDataset; org.jfree.data.general.DatasetUtilities; org.jfree.ui.ApplicationFrame; org.jfree.ui.RefineryUtilities;

public class BarChartDemo2 extends ApplicationFrame { public BarChartDemo2(final String title) { super(title); final CategoryDataset dataset = createDataset(); final JFreeChart chart = createChart(dataset);

// add the chart to a panel... final ChartPanel chartPanel = new ChartPanel(chart); chartPanel.setPreferredSize(new java.awt.Dimension(500, 270)); setContentPane(chartPanel); } private CategoryDataset createDataset() { final double[][] data = new double[][] { {1.0, 43.0, 35.0, 58.0, 54.0, 77.0, 71.0, 89.0}, {54.0, 75.0, 63.0, 83.0, 43.0, 46.0, 27.0, 13.0}, {41.0, 33.0, 22.0, 34.0, 62.0, 32.0, 42.0, 34.0} }; return DatasetUtilities.createCategoryDataset("Series ", "Factor ", data); } private JFreeChart createChart(final CategoryDataset dataset) { final JFreeChart chart = ChartFactory.createBarChart( "Bar Chart Demo 2", // chart title "Category", // domain axis label "Score (%)", // range axis label dataset, // data PlotOrientation.HORIZONTAL, // orientation true, // include legend true, false ); // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART... // set the background color for the chart... chart.setBackgroundPaint(Color.lightGray); // get a reference to the plot for further customisation... final CategoryPlot plot = chart.getCategoryPlot(); plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT); // change the auto tick unit selection to integer units only...

final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); rangeAxis.setRange(0.0, 100.0); rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); // OPTIONAL CUSTOMISATION COMPLETED. return chart; } public static void main(final String[] args) { final BarChartDemo2 demo = new BarChartDemo2("Bar Chart Demo 2"); demo.pack(); RefineryUtilities.centerFrameOnScreen(demo); demo.setVisible(true); } }

import java.awt.Dimension; import javax.swing.JDesktopPane; import javax.swing.JInternalFrame; import import import import import import import import import import import import org.jfree.chart.ChartFactory; org.jfree.chart.ChartPanel; org.jfree.chart.JFreeChart; org.jfree.chart.plot.PlotOrientation; org.jfree.data.category.DefaultCategoryDataset; org.jfree.data.time.Minute; org.jfree.data.time.RegularTimePeriod; org.jfree.data.time.TimeSeries; org.jfree.data.time.TimeSeriesCollection; org.jfree.data.xy.XYDataset; org.jfree.ui.ApplicationFrame; org.jfree.ui.RefineryUtilities;

public class InternalFrameDemo extends ApplicationFrame { public InternalFrameDemo(final String title) { super(title); final JDesktopPane desktopPane = new JDesktopPane(); desktopPane.setPreferredSize(new Dimension(600, 400)); final JInternalFrame frame1 = createFrame1(); desktopPane.add(frame1); frame1.pack(); frame1.setVisible(true); final JInternalFrame frame2 = createFrame2(); desktopPane.add(frame2); frame2.pack(); frame2.setLocation(100, 200); frame2.setVisible(true); getContentPane().add(desktopPane); } private JInternalFrame createFrame1() { final DefaultCategoryDataset dataset = new DefaultCategoryDataset(); dataset.addValue(34.0, "Series 1", "Category 1"); dataset.addValue(23.0, "Series 1", "Category 2"); dataset.addValue(54.0, "Series 1", "Category 3"); final JFreeChart chart = ChartFactory.createBarChart( "Bar Chart", "Category", "Series", dataset, PlotOrientation.VERTICAL, true, true, false ); final ChartPanel chartPanel = new ChartPanel(chart); chartPanel.setPreferredSize(new Dimension(200, 100)); final JInternalFrame frame = new JInternalFrame("Frame 1", true); frame.getContentPane().add(chartPanel); return frame; }

/** * Creates an internal frame. * * @return An internal frame. */ private JInternalFrame createFrame2() { final XYDataset dataset1 = createDataset("Series 1", 100.0, new Minute(), 200); final JFreeChart chart = ChartFactory.createTimeSeriesChart( "Time Series Chart", "Time of Day", "Value", dataset1, true, true, false ); final ChartPanel chartPanel = new ChartPanel(chart); chartPanel.setPreferredSize(new Dimension(200, 100)); final JInternalFrame frame = new JInternalFrame("Frame 2", true); frame.getContentPane().add(chartPanel); return frame; } private XYDataset createDataset(final String name, final double base, final RegularTimePeriod start, final int count) { final TimeSeries series = new TimeSeries(name, start.getClass()); RegularTimePeriod period = start; double value = base; for (int i = 0; i < count; i++) { series.add(period, value); period = period.next(); value = value * (1 + (Math.random() - 0.495) / 10.0); } final TimeSeriesCollection dataset = new TimeSeriesCollection(); dataset.addSeries(series); return dataset; } public static void main(final String[] args) { final InternalFrameDemo demo = new InternalFrameDemo("Internal Frame Demo"); demo.pack(); RefineryUtilities.centerFrameOnScreen(demo); demo.setVisible(true); } }

import java.awt.Color; import java.util.Calendar; import java.util.Date; import import import import import import import import import import import org.jfree.chart.ChartFactory; org.jfree.chart.ChartPanel; org.jfree.chart.JFreeChart; org.jfree.chart.plot.CategoryPlot; org.jfree.chart.renderer.category.CategoryItemRenderer; org.jfree.data.category.IntervalCategoryDataset; org.jfree.data.gantt.Task; org.jfree.data.gantt.TaskSeries; org.jfree.data.gantt.TaskSeriesCollection; org.jfree.ui.ApplicationFrame; org.jfree.ui.RefineryUtilities;

public class GanttDemo2 extends ApplicationFrame { public GanttDemo2(final String title) { super(title); final IntervalCategoryDataset dataset = createSampleDataset(); // create the chart... final JFreeChart chart = ChartFactory.createGanttChart( "Gantt Chart Demo", // chart title "Task", // domain axis label "Date", // range axis label dataset, // data true, // include legend true, // tooltips false // urls ); final CategoryPlot plot = (CategoryPlot) chart.getPlot(); plot.getDomainAxis().setMaxCategoryLabelWidthRatio(10.0f); final CategoryItemRenderer renderer = plot.getRenderer(); renderer.setSeriesPaint(0, Color.blue); // add the chart to a panel... final ChartPanel chartPanel = new ChartPanel(chart); chartPanel.setPreferredSize(new java.awt.Dimension(500, 270)); setContentPane(chartPanel); }

//

private IntervalCategoryDataset createSampleDataset() { final TaskSeries s1 = new TaskSeries("Scheduled"); final Task t1 = new Task( "Write Proposal", date(1, Calendar.APRIL, 2001), date(5, Calendar.APRIL, 20 01) ); t1.setPercentComplete(1.00); s1.add(t1);

final Task t2 = new Task( "Obtain Approval", date(9, Calendar.APRIL, 2001), date(9, Calendar.APRIL, 2 001) ); t2.setPercentComplete(1.00); s1.add(t2); // here is a task split into two subtasks... final Task t3 = new Task( "Requirements Analysis", date(10, Calendar.APRIL, 2001), date(5, Calendar.MAY, 2001) ); final Task st31 = new Task( "Requirements 1", date(10, Calendar.APRIL, 2001), date(25, Calendar.APRIL, 2001) ); st31.setPercentComplete(1.0); final Task st32 = new Task( "Requirements 2", date(1, Calendar.MAY, 2001), date(5, Calendar.MAY, 2001) ); st32.setPercentComplete(1.0); t3.addSubtask(st31); t3.addSubtask(st32); s1.add(t3); // and another... final Task t4 = new Task( "Design Phase", date(6, Calendar.MAY, 2001), date(30, Calendar.MAY, 2001) ); final Task st41 = new Task( "Design 1", date(6, Calendar.MAY, 2001), date(10, Calendar.MAY, 2001) ); st41.setPercentComplete(1.0); final Task st42 = new Task( "Design 2", date(15, Calendar.MAY, 2001), date(20, Calendar.MAY, 2001) ); st42.setPercentComplete(1.0); final Task st43 = new Task( "Design 3", date(23, Calendar.MAY, 2001), date(30, Calendar.MAY, 2001) ); st43.setPercentComplete(0.50); t4.addSubtask(st41); t4.addSubtask(st42); t4.addSubtask(st43); s1.add(t4); final Task t5 = new Task( "Design Signoff", date(2, Calendar.JUNE, 2001), date(2, Calendar.JUNE, 2001 ) ); s1.add(t5); final Task t6 = new Task(

"Alpha Implementation", date(3, Calendar.JUNE, 2001), date(31, Calendar.JUL Y, 2001) ); t6.setPercentComplete(0.60); s1.add(t6); final Task t7 = new Task( "Design Review", date(1, Calendar.AUGUST, 2001), date(8, Calendar.AUGUST, 2 001) ); t7.setPercentComplete(0.0); s1.add(t7); final Task t8 = new Task( "Revised Design Signoff", date(10, Calendar.AUGUST, 2001), date(10, Calendar.AUGUST, 2001) ); t8.setPercentComplete(0.0); s1.add(t8); final Task t9 = new Task( "Beta Implementation", date(12, Calendar.AUGUST, 2001), date(12, Calendar.SEPTEMBER, 2001) ); t9.setPercentComplete(0.0); s1.add(t9); final Task t10 = new Task( "Testing", date(13, Calendar.SEPTEMBER, 2001), date(31, Calendar.OCTOBER, 2 001) ); t10.setPercentComplete(0.0); s1.add(t10); final Task t11 = new Task( "Final Implementation", date(1, Calendar.NOVEMBER, 2001), date(15, Calendar.NOVEMBER, 2001) ); t11.setPercentComplete(0.0); s1.add(t11); final Task t12 = new Task( "Signoff", date(28, Calendar.NOVEMBER, 2001), date(30, Calendar.NOVEMBER, 2 001) ); t12.setPercentComplete(0.0); s1.add(t12); final TaskSeriesCollection collection = new TaskSeriesCollection(); collection.add(s1); return collection; } private static Date date(final int day, final int month, final int year) { final Calendar calendar = Calendar.getInstance(); calendar.set(year, month, day);

final Date result = calendar.getTime(); return result; } public static void main(final String[] args) { final GanttDemo2 demo = new GanttDemo2("Gantt Chart Demo 2"); demo.pack(); RefineryUtilities.centerFrameOnScreen(demo); demo.setVisible(true); } }

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy