Example usage for org.jfree.chart JFreeChart getTitle

List of usage examples for org.jfree.chart JFreeChart getTitle

Introduction

In this page you can find the example usage for org.jfree.chart JFreeChart getTitle.

Prototype

public TextTitle getTitle() 

Source Link

Document

Returns the main chart title.

Usage

From source file:view.statistics.IssueChart.java

private void showChartBtn1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showChartBtn1ActionPerformed
    String option = "" + optionCombo.getSelectedItem();
    String chartType = "" + chartCombo.getSelectedItem();
    if (option.equals("Blood Components")) {
        try {/* w  w w.j  a va2s . co  m*/
            int cryoCount = 0;
            int ffpCount = 0;
            int freshBloodCount = 0;
            int plasmaCount = 0;
            int plateletsCount = 0;

            ResultSet rst = null;
            String year = "" + yearCombo.getSelectedItem();
            String month = "" + monthCombo.getSelectedItem();
            rst = IssueController.getIssueInfo(year, month);

            while (rst.next()) {

                String type = rst.getString("BloodType");
                if (type.equalsIgnoreCase("CRYO")) {
                    cryoCount++;
                } else if (type.equalsIgnoreCase("FFP")) {
                    ffpCount++;
                } else if (type.equalsIgnoreCase("Fresh Blood")) {
                    freshBloodCount++;
                } else if (type.equalsIgnoreCase("Plasma/CSP")) {
                    plasmaCount++;
                } else if (type.equalsIgnoreCase("Platelets")) {
                    plateletsCount++;
                }
            }

            if (chartType.equals("Pie Chart")) {
                DefaultPieDataset piedataset = new DefaultPieDataset();
                piedataset.setValue("CRYO", cryoCount);
                piedataset.setValue("FFP", ffpCount);
                piedataset.setValue("Fresh Blood", freshBloodCount);
                piedataset.setValue("Plasma/CSP", plasmaCount);
                piedataset.setValue("Platelets", plateletsCount);
                JFreeChart chart = ChartFactory.createPieChart3D("Issued Blood Components", piedataset, true,
                        true, true);
                ChartPanel panel = new ChartPanel(chart);
                chart.setBackgroundPaint(Color.PINK);
                chart.getTitle().setPaint(Color.RED);
                chartArea.add(panel);
                panel.setSize(chartArea.getSize());
                panel.setVisible(true);
            } else {
                DefaultCategoryDataset dataset = new DefaultCategoryDataset();
                dataset.setValue(cryoCount, "Issued Values", "CRYO");
                dataset.setValue(ffpCount, "Issued Values", "FFP");
                dataset.setValue(freshBloodCount, "Issued Values", "Fresh Blood");
                dataset.setValue(plasmaCount, "Issued Values", "Plasma/CSP");
                dataset.setValue(plateletsCount, "Issued Values", "Platelets");
                if (chartType.equals("Bar Chart")) {
                    JFreeChart chart = ChartFactory.createBarChart3D("Issued Bloood Components",
                            "Blood Component", "Issued Values", dataset, PlotOrientation.VERTICAL, false, true,
                            false);

                    chart.setBackgroundPaint(Color.PINK);
                    chart.getTitle().setPaint(Color.RED);

                    CategoryPlot p = chart.getCategoryPlot();
                    p.setRangeGridlinePaint(Color.BLUE);
                    ChartPanel panel = new ChartPanel(chart);
                    chartArea.add(panel);
                    panel.setSize(chartArea.getSize());
                    panel.setVisible(true);
                } else if (chartType.equals("Line Chart")) {
                    JFreeChart chart = ChartFactory.createLineChart3D("Issued Blood Components",
                            "Blood Component", "Issued Values", dataset, PlotOrientation.VERTICAL, false, true,
                            false);

                    chart.setBackgroundPaint(Color.PINK);
                    chart.getTitle().setPaint(Color.RED);

                    CategoryPlot p = chart.getCategoryPlot();
                    p.setRangeGridlinePaint(Color.BLUE);
                    ChartPanel panel = new ChartPanel(chart);
                    chartArea.add(panel);
                    panel.setSize(chartArea.getSize());
                    panel.setVisible(true);
                }
            }

        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "-1Data Error!", "Warning!", JOptionPane.OK_OPTION);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(IssueChart.class.getName()).log(Level.SEVERE, null, ex);
        }
    } else if (option.equals("Blood Groups")) {
        try {
            int Apos = 0;
            int Bpos = 0;
            int Aneg = 0;
            int Bneg = 0;
            int ABpos = 0;
            int Opos = 0;
            int ABneg = 0;
            int Oneg = 0;

            ResultSet rst = null;
            String year = "" + yearCombo.getSelectedItem();
            String month = "" + monthCombo.getSelectedItem();

            rst = IssueController.getIssueInfo(year, month);

            while (rst.next()) {

                String type = rst.getString("BloodGroup");
                if (type.equalsIgnoreCase("A+")) {
                    Apos++;
                } else if (type.equalsIgnoreCase("B+")) {
                    Bpos++;
                } else if (type.equalsIgnoreCase("A-")) {
                    Aneg++;
                } else if (type.equalsIgnoreCase("B-")) {
                    Bneg++;
                } else if (type.equalsIgnoreCase("AB+")) {
                    ABpos++;
                } else if (type.equalsIgnoreCase("AB-")) {
                    ABneg++;
                } else if (type.equalsIgnoreCase("O+")) {
                    Opos++;
                } else if (type.equalsIgnoreCase("O-")) {
                    Oneg++;
                }
            }

            if (chartType.equals("Pie Chart")) {
                DefaultPieDataset piedataset = new DefaultPieDataset();
                piedataset.setValue("A+", Apos);
                piedataset.setValue("A-", Aneg);
                piedataset.setValue("B+", Bpos);
                piedataset.setValue("B-", Bneg);
                piedataset.setValue("AB+", ABpos);
                piedataset.setValue("AB-", ABneg);
                piedataset.setValue("O+", Opos);
                piedataset.setValue("O-", Oneg);

                JFreeChart chart = ChartFactory.createPieChart3D("Issued Blood Groups", piedataset, true, true,
                        true);
                ChartPanel panel = new ChartPanel(chart);
                chart.setBackgroundPaint(Color.PINK);
                chart.getTitle().setPaint(Color.RED);
                chartArea.add(panel);
                panel.setSize(chartArea.getSize());
                panel.setVisible(true);
            } else {
                DefaultCategoryDataset dataset = new DefaultCategoryDataset();
                dataset.setValue(Apos, "Issued Values", "A+");
                dataset.setValue(Aneg, "Issued Values", "A-");
                dataset.setValue(Bpos, "Issued Values", "B+");
                dataset.setValue(Bneg, "Issued Values", "B-");
                dataset.setValue(ABpos, "Issued Values", "AB+");
                dataset.setValue(ABneg, "Issued Values", "AB-");
                dataset.setValue(Opos, "Issued Values", "O+");
                dataset.setValue(Oneg, "Issued Values", "O-");
                if (chartType.equals("Bar Chart")) {
                    JFreeChart chart = ChartFactory.createBarChart3D("Issued Bloood Groups", "Blood Group",
                            "Issued Values", dataset, PlotOrientation.VERTICAL, false, true, false);

                    chart.setBackgroundPaint(Color.PINK);
                    chart.getTitle().setPaint(Color.RED);

                    CategoryPlot p = chart.getCategoryPlot();
                    p.setRangeGridlinePaint(Color.BLUE);
                    ChartPanel panel = new ChartPanel(chart);
                    chartArea.add(panel);
                    panel.setSize(chartArea.getSize());
                    panel.setVisible(true);
                } else if (chartType.equals("Line Chart")) {
                    JFreeChart chart = ChartFactory.createLineChart3D("Issued Blood Groups", "Blood Group",
                            "Issued Values", dataset, PlotOrientation.VERTICAL, false, true, false);
                    chart.setBackgroundPaint(Color.PINK);
                    chart.getTitle().setPaint(Color.RED);

                    CategoryPlot p = chart.getCategoryPlot();
                    p.setRangeGridlinePaint(Color.BLUE);
                    ChartPanel panel = new ChartPanel(chart);
                    chartArea.add(panel);
                    panel.setSize(chartArea.getSize());
                    panel.setVisible(true);
                }

            }
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "0Data Error!", "Warning!", JOptionPane.OK_OPTION);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(IssueChart.class.getName()).log(Level.SEVERE, null, ex);
        }
    } else if (option.equals("Gender")) {
        try {
            int male = 0;
            int female = 0;

            ResultSet rst = null;
            String year = "" + yearCombo.getSelectedItem();
            String month = "" + monthCombo.getSelectedItem();
            rst = IssueController.getRequesteeInfo(year, month);

            while (rst.next()) {

                String type = rst.getString("Gender");
                if (type.equalsIgnoreCase("Male")) {
                    male++;
                } else if (type.equalsIgnoreCase("Female")) {
                    female++;
                }
            }

            if (chartType.equals("Pie Chart")) {
                DefaultPieDataset piedataset = new DefaultPieDataset();
                piedataset.setValue("Male", male);
                piedataset.setValue("Female", female);

                JFreeChart chart = ChartFactory.createPieChart3D("Blood Requestees", piedataset, true, true,
                        true);
                ChartPanel panel = new ChartPanel(chart);
                chart.setBackgroundPaint(Color.PINK);
                chart.getTitle().setPaint(Color.RED);
                chartArea.add(panel);
                panel.setSize(chartArea.getSize());
                panel.setVisible(true);
            } else {
                DefaultCategoryDataset dataset = new DefaultCategoryDataset();
                dataset.setValue(male, "", "Male");
                dataset.setValue(female, "", "Female");

                if (chartType.equals("Bar Chart")) {
                    JFreeChart chart = ChartFactory.createBarChart3D("Blood Requestees", "Gender", "", dataset,
                            PlotOrientation.VERTICAL, false, true, false);

                    chart.setBackgroundPaint(Color.PINK);
                    chart.getTitle().setPaint(Color.RED);

                    CategoryPlot p = chart.getCategoryPlot();
                    p.setRangeGridlinePaint(Color.BLUE);
                    ChartPanel panel = new ChartPanel(chart);
                    chartArea.add(panel);
                    panel.setSize(chartArea.getSize());
                    panel.setVisible(true);
                } else if (chartType.equals("Line Chart")) {
                    JFreeChart chart = ChartFactory.createLineChart3D("Blood Requestees", "Gender", "", dataset,
                            PlotOrientation.VERTICAL, false, true, false);
                    chart.setBackgroundPaint(Color.PINK);
                    chart.getTitle().setPaint(Color.RED);

                    CategoryPlot p = chart.getCategoryPlot();
                    p.setRangeGridlinePaint(Color.BLUE);
                    ChartPanel panel = new ChartPanel(chart);
                    chartArea.add(panel);
                    panel.setSize(chartArea.getSize());
                    panel.setVisible(true);
                }
            }
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "1Data Error!", "Warning!", JOptionPane.OK_OPTION);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(IssueChart.class.getName()).log(Level.SEVERE, null, ex);
        }
    } else if (option.equals("Hospitals")) {
        try {

            String[] hospitals = new String[10];
            int[] hospitalCount = new int[10];
            int noOfHospitals = 0;
            ResultSet rst = null;
            String year = "" + yearCombo.getSelectedItem();
            String month = "" + monthCombo.getSelectedItem();
            rst = SampleDetailsController.getAllHospitals();
            while (rst.next()) {
                hospitals[noOfHospitals] = rst.getString("Name");
                hospitalCount[noOfHospitals] = 0;
                noOfHospitals++;
            }

            rst = IssueController.getRequesteeInfo(year, month);

            while (rst.next()) {
                String type = rst.getString("Hospital");
                for (int i = 0; i < noOfHospitals; i++) {
                    if (type.equalsIgnoreCase(hospitals[i])) {
                        hospitalCount[i]++;
                    }
                }
            }

            if (chartType.equals("Pie Chart")) {
                DefaultPieDataset piedataset = new DefaultPieDataset();
                for (int i = 0; i < noOfHospitals; i++) {
                    piedataset.setValue(hospitals[i], hospitalCount[i]);
                }

                JFreeChart chart = ChartFactory.createPieChart3D("Issued Hospitals", piedataset, true, true,
                        true);
                ChartPanel panel = new ChartPanel(chart);
                chart.setBackgroundPaint(Color.PINK);
                chart.getTitle().setPaint(Color.RED);
                chartArea.add(panel);
                panel.setSize(chartArea.getSize());
                panel.setVisible(true);
            } else {
                DefaultCategoryDataset dataset = new DefaultCategoryDataset();
                for (int i = 0; i < noOfHospitals; i++) {
                    dataset.setValue(hospitalCount[i], "Issued Values", hospitals[i]);
                }

                if (chartType.equals("Bar Chart")) {
                    JFreeChart chart = ChartFactory.createBarChart3D("Issued Hospitals", "Hospital",
                            "Issued Values", dataset, PlotOrientation.VERTICAL, false, true, false);

                    chart.setBackgroundPaint(Color.PINK);
                    chart.getTitle().setPaint(Color.RED);

                    CategoryPlot p = chart.getCategoryPlot();
                    p.setRangeGridlinePaint(Color.BLUE);
                    ChartPanel panel = new ChartPanel(chart);
                    chartArea.add(panel);
                    panel.setSize(chartArea.getSize());
                    panel.setVisible(true);
                } else if (chartType.equals("Line Chart")) {
                    JFreeChart chart = ChartFactory.createLineChart3D("Issued Hospitals", "Hospital",
                            "Issued Values", dataset, PlotOrientation.VERTICAL, false, true, false);
                    chart.setBackgroundPaint(Color.PINK);
                    chart.getTitle().setPaint(Color.RED);

                    CategoryPlot p = chart.getCategoryPlot();
                    p.setRangeGridlinePaint(Color.BLUE);
                    ChartPanel panel = new ChartPanel(chart);
                    chartArea.add(panel);
                    panel.setSize(chartArea.getSize());
                    panel.setVisible(true);
                }
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(null, "2Data Error!", "Warning!", JOptionPane.OK_OPTION);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(IssueChart.class.getName()).log(Level.SEVERE, null, ex);
        }
    } else if (option.equals("Age Groups")) {
        try {
            int[] ages = new int[10];
            for (int i = 0; i < 10; i++) {
                ages[i] = 0;
            }
            ResultSet rst = null;
            String year = "" + yearCombo.getSelectedItem();
            String month = "" + monthCombo.getSelectedItem();
            rst = IssueController.getRequesteeInfo(year, month);

            while (rst.next()) {
                int age = Integer.parseInt(rst.getString("Age"));
                if (age <= 10 && age > 0) {
                    ages[0]++;
                } else if (age <= 20 && age > 10) {
                    ages[1]++;
                } else if (age <= 30 && age > 20) {
                    ages[2]++;
                } else if (age <= 40 && age > 30) {
                    ages[3]++;
                } else if (age <= 50 && age > 40) {
                    ages[4]++;
                } else if (age <= 60 && age > 50) {
                    ages[5]++;
                } else if (age <= 70 && age > 60) {
                    ages[6]++;
                } else if (age <= 80 && age > 70) {
                    ages[7]++;
                } else if (age <= 90 && age > 80) {
                    ages[8]++;
                } else if (age <= 100 && age > 90) {
                    ages[9]++;
                }
            }

            rst = IssueController.getRequesteeInfo(year, month);

            if (chartType.equals("Pie Chart")) {
                DefaultPieDataset piedataset = new DefaultPieDataset();
                for (int i = 0; i < 10; i++) {
                    piedataset.setValue(i * 10 + "-" + (i * 10 + 10), ages[i]);
                }

                JFreeChart chart = ChartFactory.createPieChart3D("Issued Age Groups", piedataset, true, true,
                        true);
                ChartPanel panel = new ChartPanel(chart);
                chart.setBackgroundPaint(Color.PINK);
                chart.getTitle().setPaint(Color.RED);
                chartArea.add(panel);
                panel.setSize(chartArea.getSize());
                panel.setVisible(true);
            } else {
                DefaultCategoryDataset dataset = new DefaultCategoryDataset();
                for (int i = 0; i < 10; i++) {
                    dataset.setValue(ages[i], "Issued Values", i * 10 + "-" + (i * 10 + 10));
                }

                if (chartType.equals("Bar Chart")) {
                    JFreeChart chart = ChartFactory.createBarChart3D("Issued Age Groups", "Age Groups",
                            "Issued Values", dataset, PlotOrientation.VERTICAL, false, true, false);

                    chart.setBackgroundPaint(Color.PINK);
                    chart.getTitle().setPaint(Color.RED);

                    CategoryPlot p = chart.getCategoryPlot();
                    p.setRangeGridlinePaint(Color.BLUE);
                    ChartPanel panel = new ChartPanel(chart);
                    chartArea.add(panel);
                    panel.setSize(chartArea.getSize());
                    panel.setVisible(true);
                } else if (chartType.equals("Line Chart")) {
                    JFreeChart chart = ChartFactory.createLineChart3D("Issued Age Groups", "Age Groups",
                            "Issued Values", dataset, PlotOrientation.VERTICAL, false, true, false);
                    chart.setBackgroundPaint(Color.PINK);
                    chart.getTitle().setPaint(Color.RED);

                    CategoryPlot p = chart.getCategoryPlot();
                    p.setRangeGridlinePaint(Color.BLUE);
                    ChartPanel panel = new ChartPanel(chart);
                    chartArea.add(panel);
                    panel.setSize(chartArea.getSize());
                    panel.setVisible(true);
                }
            }
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "3Data Error!", "Warning!", JOptionPane.OK_OPTION);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(IssueChart.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

From source file:net.sf.dynamicreports.test.jasper.chart.SpiderChartTest.java

@Override
public void test() {
    super.test();

    numberOfPagesTest(1);//from w  w w  .java2  s.com

    JRPrintFrame printFrame = (JRPrintFrame) getElementAt("summary.list1", 0);

    JRPrintImage image = (JRPrintImage) printFrame.getElements().get(0);
    JFreeChart chart = getChart(image);
    SpiderWebPlot plot = (SpiderWebPlot) chart.getPlot();
    Assert.assertEquals("max value", 10d, plot.getMaxValue());
    Assert.assertEquals("rotation", Rotation.ANTICLOCKWISE, plot.getDirection());
    Assert.assertEquals("table order", org.jfree.util.TableOrder.BY_COLUMN, plot.getDataExtractOrder());
    Assert.assertFalse("web filled", plot.isWebFilled());
    Assert.assertEquals("start angle", 20d, plot.getStartAngle());
    Assert.assertEquals("head percent", 30d, plot.getHeadPercent());
    Assert.assertEquals("interior gap", 0.15d, plot.getInteriorGap());
    Assert.assertEquals("axis line color", Color.RED, plot.getAxisLinePaint());
    Assert.assertEquals("interior gap", 2f, ((BasicStroke) plot.getAxisLineStroke()).getLineWidth());
    Assert.assertEquals("label color", Color.BLUE, plot.getLabelPaint());
    Assert.assertEquals("label gap", 2d, plot.getAxisLabelGap());
    Assert.assertTrue("label font", plot.getLabelFont().isBold());

    image = (JRPrintImage) printFrame.getElements().get(1);
    chart = getChart(image);
    plot = (SpiderWebPlot) chart.getPlot();
    Assert.assertEquals("title", "title", chart.getTitle().getText());
    Assert.assertEquals("subtitle", "subtitle", ((TextTitle) chart.getSubtitle(1)).getText());
}

From source file:cs.cirg.cida.CIDAView.java

@Action
public void savePlotPNG() {
    JFreeChart chartToSave = ((ChartPanel) chartPanel).getChart();
    String name = chartToSave.getTitle().getText();
    name = name.trim().replaceAll(" ", "");
    try {/*from   w ww. j a v  a 2 s  .c  o  m*/
        ChartUtilities.saveChartAsPNG(new File(name + CIDAConstants.EXT_PNG), chartToSave,
                CIDAConstants.DEFAULT_CHART_HORIZONTAL_RES, CIDAConstants.DEFAULT_CHART_VERTICAL_RES);
    } catch (IOException ex) {
        CIDAPromptDialog dialog = exceptionController.handleException(this.getFrame(), ex,
                CIDAConstants.DIALOG_NEW_NAME_MSG);
        dialog.displayPrompt();
    }
}

From source file:cs.cirg.cida.CIDAView.java

@Action
public void savePlotEPS() {
    JFreeChart chartToSave = ((ChartPanel) chartPanel).getChart();
    String name = chartToSave.getTitle().getText();
    name = name.trim().replaceAll(" ", "");
    try {/*from w w  w.  ja va2 s.  c o  m*/
        OutputStream out = new FileOutputStream(name + CIDAConstants.EXT_EPS);
        EPSDocumentGraphics2D g2d = new EPSDocumentGraphics2D(false);
        g2d.setGraphicContext(new GraphicContext());

        g2d.setupDocument(out, CIDAConstants.DEFAULT_CHART_HORIZONTAL_RES,
                CIDAConstants.DEFAULT_CHART_VERTICAL_RES);
        chartToSave.draw(g2d, new Rectangle2D.Double(0, 0, CIDAConstants.DEFAULT_CHART_HORIZONTAL_RES,
                CIDAConstants.DEFAULT_CHART_VERTICAL_RES));

        g2d.finish();
    } catch (IOException ex) {
        CIDAPromptDialog dialog = exceptionController.handleException(this.getFrame(), ex,
                CIDAConstants.DIALOG_NEW_NAME_MSG);
        dialog.displayPrompt();
    }
}

From source file:com.bdaum.zoom.report.internal.wizards.ReportComponent.java

public void saveChartProperties(Report report) {
    JFreeChart chart = chartComposite.getChart();
    if (chart != null) {
        Map<String, Object> properties = new HashMap<>();
        TextTitle title = chart.getTitle();
        if (title != null) {
            properties.put(TITLE, title.getText());
            properties.put(TITLEFONT, title.getFont());
            properties.put(TITLECOLOR, title.getPaint());
        }//from  w  w w  . jav a  2s.  c  om
        Plot plot = chart.getPlot();
        properties.put(BGCOLOR, plot.getBackgroundPaint());
        properties.put(OUTLINEPAINT, plot.getOutlinePaint());
        properties.put(OUTLINESTROKE, plot.getOutlineStroke());
        Axis domainAxis = null;
        Axis rangeAxis = null;
        if (plot instanceof CategoryPlot) {
            CategoryPlot p = (CategoryPlot) plot;
            domainAxis = p.getDomainAxis();
            rangeAxis = p.getRangeAxis();
            properties.put(ORIENTATION, p.getOrientation());
        } else if (plot instanceof XYPlot) {
            XYPlot p = (XYPlot) plot;
            domainAxis = p.getDomainAxis();
            rangeAxis = p.getRangeAxis();
            properties.put(ORIENTATION, p.getOrientation());
        }
        if (domainAxis != null)
            saveAxisProperties(domainAxis, "x", properties); //$NON-NLS-1$
        if (rangeAxis != null)
            saveAxisProperties(rangeAxis, "y", properties); //$NON-NLS-1$
        properties.put(ANTIALIAS, chart.getAntiAlias());
        properties.put(CANVASPAINT, chart.getBackgroundPaint());
        report.setProperties(properties);
    }
}

From source file:nu.nethome.home.items.web.GraphServlet.java

/**
* This is the main enterence point of the class. This is called when a http request is
* routed to this servlet./*from  ww w .j a  v a 2 s .  co m*/
*/
public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    ServletOutputStream p = res.getOutputStream();
    Date startTime = null;
    Date stopTime = null;

    // Analyse arguments
    String fileName = req.getParameter("file");
    if (fileName != null)
        fileName = getFullFileName(fromURL(fileName));
    String startTimeString = req.getParameter("start");
    String stopTimeString = req.getParameter("stop");
    try {
        if (startTimeString != null) {
            startTime = m_Format.parse(startTimeString);
        }
        if (stopTimeString != null) {
            stopTime = m_Format.parse(stopTimeString);
        }
    } catch (ParseException e1) {
        e1.printStackTrace();
    }
    String look = req.getParameter("look");
    if (look == null)
        look = "";

    TimeSeries timeSeries = new TimeSeries("Data", Minute.class);

    // Calculate time window
    Calendar cal = Calendar.getInstance();
    Date currentTime = cal.getTime();
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    Date startOfDay = cal.getTime();
    cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
    Date startOfWeek = cal.getTime();
    cal.set(Calendar.DAY_OF_MONTH, 1);
    Date startOfMonth = cal.getTime();
    cal.set(Calendar.MONTH, Calendar.JANUARY);
    Date startOfYear = cal.getTime();

    // if (startTime == null) startTime = startOfWeek;
    if (stopTime == null)
        stopTime = currentTime;
    if (startTime == null)
        startTime = new Date(stopTime.getTime() - 1000L * 60L * 60L * 24L * 2L);

    try {
        // Open the data file
        File logFile = new File(fileName);
        Scanner fileScanner = new Scanner(logFile);
        Long startTimeMs = startTime.getTime();
        Long month = 1000L * 60L * 60L * 24L * 30L;
        boolean doOptimize = true;
        boolean justOptimized = false;
        try {
            while (fileScanner.hasNext()) {
                try {
                    // Get next log entry
                    String line = fileScanner.nextLine();
                    if (line.length() > 21) {
                        // Adapt the time format
                        String minuteTime = line.substring(0, 16).replace('.', '-');
                        // Parse the time stamp
                        Minute min = Minute.parseMinute(minuteTime);

                        // Ok, this is an ugly optimization. If the current time position in the file
                        // is more than a month (30 days) ahead of the start of the time window, we
                        // quick read two weeks worth of data, assuming that there is 4 samples per hour.
                        // This may lead to scanning past start of window if there are holes in the data
                        // series.
                        if (doOptimize && ((startTimeMs - min.getFirstMillisecond()) > month)) {
                            for (int i = 0; (i < (24 * 4 * 14)) && fileScanner.hasNext(); i++) {
                                fileScanner.nextLine();
                            }
                            justOptimized = true;
                            continue;
                        }
                        // Detect if we have scanned past the window start position just after an optimization scan.
                        // If this is the case it may be because of the optimization. In that case we have to switch 
                        // optimization off and start over.
                        if ((min.getFirstMillisecond() > startTimeMs) && doOptimize && justOptimized) {
                            logFile = new File(fileName);
                            fileScanner = new Scanner(logFile);
                            doOptimize = false;
                            continue;
                        }
                        justOptimized = false;
                        // Check if value is within time window
                        if ((min.getFirstMillisecond() > startTimeMs)
                                && (min.getFirstMillisecond() < stopTime.getTime())) {
                            // Parse the value
                            double value = Double.parseDouble((line.substring(20)).replace(',', '.'));
                            // Add the entry
                            timeSeries.add(min, value);
                            doOptimize = false;
                        }
                    }
                } catch (SeriesException se) {
                    // Bad entry, for example due to duplicates at daylight saving time switch
                } catch (NumberFormatException nfe) {
                    // Bad number format in a line, try to continue
                }
            }
        } catch (Exception e) {
            System.out.println(e.toString());
        } finally {
            fileScanner.close();
        }
    } catch (FileNotFoundException f) {
        System.out.println(f.toString());
    }

    // Create a collection for plotting
    TimeSeriesCollection data = new TimeSeriesCollection();
    data.addSeries(timeSeries);

    JFreeChart chart;

    int xSize = 750;
    int ySize = 450;
    // Customize colors and look of the Graph.
    if (look.equals("mobtemp")) {
        // Look for the mobile GUI
        chart = ChartFactory.createTimeSeriesChart(null, null, null, data, false, false, false);
        XYPlot plot = chart.getXYPlot();
        ValueAxis timeAxis = plot.getDomainAxis();
        timeAxis.setAxisLineVisible(false);
        ValueAxis valueAxis = plot.getRangeAxis(0);
        valueAxis.setAxisLineVisible(false);
        xSize = 175;
        ySize = 180;
    } else {
        // Create a Chart with time range as heading
        SimpleDateFormat localFormat = new SimpleDateFormat();
        String heading = localFormat.format(startTime) + " - " + localFormat.format(stopTime);
        chart = ChartFactory.createTimeSeriesChart(heading, null, null, data, false, false, false);

        Paint background = new Color(0x9D8140);
        chart.setBackgroundPaint(background);
        TextTitle title = chart.getTitle(); // fix title
        Font titleFont = title.getFont();
        titleFont = titleFont.deriveFont(Font.PLAIN, (float) 14.0);
        title.setFont(titleFont);
        title.setPaint(Color.darkGray);
        XYPlot plot = chart.getXYPlot();
        plot.setBackgroundPaint(background);
        plot.setDomainGridlinePaint(Color.darkGray);
        ValueAxis timeAxis = plot.getDomainAxis();
        timeAxis.setAxisLineVisible(false);
        ValueAxis valueAxis = plot.getRangeAxis(0);
        valueAxis.setAxisLineVisible(false);
        plot.setRangeGridlinePaint(Color.darkGray);
        XYItemRenderer renderer = plot.getRenderer(0);
        renderer.setSeriesPaint(0, Color.darkGray);
        xSize = 750;
        ySize = 450;
    }

    try {
        res.setContentType("image/png");
        res.setHeader("Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
        res.setHeader("Pragma", "no-cache");
        res.setStatus(HttpServletResponse.SC_OK);
        ChartUtilities.writeChartAsPNG(p, chart, xSize, ySize);
    } catch (IOException e) {
        System.err.println("Problem occurred creating chart.");
    }

    p.flush();
    p.close();
    return;
}

From source file:net.sourceforge.processdash.ui.web.CGIChartBase.java

/** Generate CGI chart output. */
@Override/*ww  w . ja va2  s.  c  o m*/
protected void writeContents() throws IOException {
    buildData(); // get the data for display

    chromeless = (parameters.get("chromeless") != null);
    JFreeChart chart = createChart();

    int width = getIntSetting("width");
    int height = getIntSetting("height");
    Color initGradColor = getColorSetting("initGradColor");
    Color finalGradColor = getColorSetting("finalGradColor");
    chart.setBackgroundPaint(new GradientPaint(0, 0, initGradColor, width, height, finalGradColor));
    if (parameters.get("hideOutline") != null)
        chart.getPlot().setOutlinePaint(INVISIBLE);

    String title = getSetting("title");
    if (chromeless || title == null || title.length() == 0)
        chart.setTitle((TextTitle) null);
    else {
        chart.setTitle(Translator.translate(title));
        String titleFontSize = getSetting("titleFontSize");
        if (titleFontSize != null)
            try {
                float fontSize = Float.parseFloat(titleFontSize);
                TextTitle t = chart.getTitle();
                t.setFont(t.getFont().deriveFont(fontSize));
            } catch (Exception tfe) {
            }
    }

    if (chromeless || parameters.get("hideLegend") != null)
        chart.removeLegend();
    else {
        LegendTitle l = chart.getLegend();
        String legendFontSize = getSetting("legendFontSize");
        if (l != null && legendFontSize != null)
            try {
                float fontSize = Float.parseFloat(legendFontSize);
                l.setItemFont(l.getItemFont().deriveFont(fontSize));
            } catch (Exception lfe) {
            }
    }

    chart.getPlot().setNoDataMessage(resources.getString("No_Data_Message"));

    Axis xAxis = getHorizontalAxis(chart);
    if (xAxis != null) {
        if (parameters.get("hideTickLabels") != null || parameters.get("hideXTickLabels") != null) {
            xAxis.setTickLabelsVisible(false);
        } else if (parameters.get("tickLabelFontSize") != null
                || parameters.get("xTickLabelFontSize") != null) {
            String tfs = getParameter("xTickLabelFontSize");
            if (tfs == null)
                tfs = getParameter("tickLabelFontSize");
            float fontSize = Float.parseFloat(tfs);
            xAxis.setTickLabelFont(xAxis.getTickLabelFont().deriveFont(fontSize));
        }
    }

    Axis yAxis = getVerticalAxis(chart);
    if (yAxis != null) {
        if (parameters.get("hideTickLabels") != null || parameters.get("hideYTickLabels") != null) {
            yAxis.setTickLabelsVisible(false);
        } else if (parameters.get("tickLabelFontSize") != null
                || parameters.get("yTickLabelFontSize") != null) {
            String tfs = getParameter("yTickLabelFontSize");
            if (tfs == null)
                tfs = getParameter("tickLabelFontSize");
            float fontSize = Float.parseFloat(tfs);
            yAxis.setTickLabelFont(yAxis.getTickLabelFont().deriveFont(fontSize));
        }
    }

    String axisFontSize = getSetting("axisLabelFontSize");
    if (axisFontSize != null)
        try {
            float fontSize = Float.parseFloat(axisFontSize);
            if (xAxis != null)
                xAxis.setLabelFont(xAxis.getLabelFont().deriveFont(fontSize));
            if (yAxis != null)
                yAxis.setLabelFont(yAxis.getLabelFont().deriveFont(fontSize));
        } catch (Exception afs) {
        }

    ChartRenderingInfo info = (isHtmlMode() ? new ChartRenderingInfo() : null);
    BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = img.createGraphics();
    if ("auto".equals(getSetting("titleFontSize")))
        maybeAdjustTitleFontSize(chart, g2, width);
    chart.draw(g2, new Rectangle2D.Double(0, 0, width, height), info);
    g2.dispose();

    String outputFormat = getSetting("outputFormat");
    OutputStream imgOut;
    if (isHtmlMode()) {
        imgOut = PngCache.getOutputStream();
    } else {
        imgOut = outStream;
    }
    ImageIO.write(img, outputFormat, imgOut);
    imgOut.flush();
    imgOut.close();
    if (isHtmlMode())
        writeImageHtml(width, height, imgOut.hashCode(), info);
}

From source file:org.knime.knip.core.ui.imgviewer.panels.HistogramBC.java

private final void setBackgroundDefault(final JFreeChart chart) {
    final BasicStroke gridStroke = new BasicStroke(1.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 1.0f,
            new float[] { 2.0f, 1.0f }, 0.0f);
    final XYPlot plot = (XYPlot) chart.getPlot();
    plot.setRangeGridlineStroke(gridStroke);
    plot.setDomainGridlineStroke(gridStroke);
    // Background of Histogram inside border
    //plot.setBackgroundPaint(new Color(235,235,235));
    plot.setBackgroundPaint(Color.white);
    // change from white to gray
    plot.setRangeGridlinePaint(Color.gray);
    plot.setDomainGridlinePaint(Color.gray);
    // set lines invisible
    plot.setDomainGridlinesVisible(false);
    plot.setRangeGridlinesVisible(false);
    plot.setOutlineVisible(true);/*from www.  j a  v  a  2  s.  co m*/
    plot.getDomainAxis().setAxisLineVisible(false);
    plot.getRangeAxis().setAxisLineVisible(false);
    plot.getDomainAxis().setLabelPaint(Color.gray);
    plot.getRangeAxis().setLabelPaint(Color.gray);
    plot.getDomainAxis().setTickLabelPaint(Color.gray);
    plot.getRangeAxis().setTickLabelPaint(Color.gray);
    final TextTitle title = chart.getTitle();
    if (title != null) {
        title.setPaint(Color.black);
    }
}

From source file:it.unifi.rcl.chess.traceanalysis.gui.TracePanel.java

private void plotTrace() {
    XYSeriesCollection dataset = new XYSeriesCollection();

    XYSeries xyBoundsPositive = null;/*from   w w  w.ja v a2 s.  com*/
    XYSeries xyBoundsNegative = null;
    Trace[] posNeg = null;

    dataset.addSeries(Plotter.traceToSeries(trace));
    dataset.addSeries(Plotter.valueToSeries(trace.getMax(), "Max", trace.getSampleSize()));
    dataset.addSeries(Plotter.valueToSeries(trace.getAverage(), "Average", trace.getSampleSize()));
    dataset.addSeries(Plotter.valueToSeries(trace.getMin(), "Min", trace.getSampleSize()));

    int rows = tableWindowSize.getRowCount();
    double coverage;
    int wsize;
    for (int i = 0; i < rows; i++) {
        try {
            wsize = (Integer) tableWindowSize.getValueAt(i, 0);
            coverage = (Double) tableWindowSize.getValueAt(i, 1);

            if (trace.hasNegativeValues()) {
                posNeg = trace.splitPositiveNegative();
                xyBoundsPositive = Plotter.arrayToSeries(posNeg[0].getDynamicBound(coverage, wsize),
                        "c=" + coverage + ",w=" + wsize, wsize - 1);
                xyBoundsNegative = Plotter.arrayToSeriesInvert(posNeg[1].getDynamicBound(coverage, wsize),
                        "c=" + coverage + ",w=" + wsize + "(neg)", wsize - 1);
                dataset.addSeries(xyBoundsPositive);
                dataset.addSeries(xyBoundsNegative);
            } else {
                dataset.addSeries(Plotter.arrayToSeries(trace.getDynamicBound(coverage, wsize),
                        "c=" + coverage + ",w=" + wsize, wsize - 1));
            }
        } catch (NullPointerException e) {
            //Ignore: cell value is null
            ;
        }
    }

    // Generate the graph
    JFreeChart chart = ChartFactory.createXYLineChart(trace.getName(),
            // Title
            "Time Point",
            // x-axis Labels
            "Value",
            // y-axis Label
            dataset,
            // Dataset
            PlotOrientation.VERTICAL, // Plot Orientation
            true,
            // Show Legend
            true,
            // Use tooltips
            false
    // Configure chart to generate URLs?
    );

    chart.setBackgroundPaint(Color.WHITE);
    chart.getXYPlot().setBackgroundPaint(ChartColor.VERY_LIGHT_YELLOW);
    chart.getXYPlot().setBackgroundAlpha(0.05f);
    chart.getXYPlot().setRangeGridlinePaint(Color.LIGHT_GRAY);
    chart.getXYPlot().setDomainGridlinePaint(Color.LIGHT_GRAY);

    chart.getTitle().setFont(new Font("Dialog", Font.BOLD, 14));

    XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
    renderer.setDrawSeriesLineAsPath(true);
    chart.getXYPlot().setRenderer(renderer);

    renderer.setSeriesPaint(0, Color.BLACK);
    renderer.setSeriesPaint(1, ChartColor.DARK_BLUE);
    renderer.setSeriesPaint(2, ChartColor.DARK_GRAY);
    renderer.setSeriesPaint(3, ChartColor.DARK_BLUE);
    renderer.setSeriesPaint(4, ChartColor.RED);

    int nSeries = chart.getXYPlot().getSeriesCount();
    for (int i = 0; i < nSeries; i++) {
        renderer.setSeriesShapesVisible(i, false);
    }
    if (posNeg != null) {
        renderer.setSeriesVisibleInLegend(5, false);
        renderer.setSeriesPaint(5, renderer.getSeriesPaint(4));
    }

    Stroke plainStroke = new BasicStroke(1.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 1.0f);

    renderer.setSeriesStroke(0, plainStroke);
    renderer.setSeriesStroke(1, new BasicStroke(1.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 1.0f,
            new float[] { 6.0f, 3.0f }, 0.0f));
    renderer.setSeriesStroke(2, new BasicStroke(1.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 1.0f,
            new float[] { 3.0f, 0.5f, 3.0f }, 0.0f));
    renderer.setSeriesStroke(3, new BasicStroke(1.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 1.0f,
            new float[] { 6.0f, 3.0f }, 0.0f));

    JPanel plotPanel = new ChartPanel(chart);
    JFrame plotFrame = new JFrame();
    plotFrame.add(plotPanel);
    plotFrame.setVisible(true);
    plotFrame.pack();
    plotFrame.setTitle(TracePanel.this.trace.getName());
    plotFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}

From source file:lucee.runtime.tag.Chart.java

private void setFont(JFreeChart chart, Font font) {
    // title/*  www. j a v a2s  .  co  m*/
    TextTitle title = chart.getTitle();
    if (title != null) {
        title.setFont(font);
        title.setPaint(foregroundcolor);
        chart.setTitle(title);
    }

    // axis fonts
    Plot plot = chart.getPlot();
    if (plot instanceof CategoryPlot) {
        CategoryPlot cp = (CategoryPlot) plot;
        setAxis(cp.getRangeAxis(), font);
        setAxis(cp.getDomainAxis(), font);
    }
    if (plot instanceof XYPlot) {
        XYPlot cp = (XYPlot) plot;
        setAxis(cp.getRangeAxis(), font);
        setAxis(cp.getDomainAxis(), font);
    }

}