List of usage examples for org.jfree.chart.plot XYPlot getDomainAxis
public ValueAxis getDomainAxis()
From source file:udpserver.UDPui.java
/** * Creates new form UDPui/*from ww w . jav a 2 s. c o m*/ */ public UDPui() { // <editor-fold defaultstate="collapsed" desc="Graph"> series = new XYSeries("ECG Reading"); series.setMaximumItemCount(50); XYSeriesCollection dataset = new XYSeriesCollection(series); JFreeChart chart = ChartFactory.createXYLineChart("ECG Reading", "Time (seconds)", "Voltage (volt)", dataset); final XYPlot plot = chart.getXYPlot(); NumberAxis domain = (NumberAxis) plot.getDomainAxis(); JPanel jPanel1 = new JPanel(); jPanel1.setLayout(new java.awt.BorderLayout()); jPanel1.setVisible(true); jPanel1.setSize(600, 500); jPanel1.add(new ChartPanel(chart), BorderLayout.CENTER); jPanel1.validate(); add(jPanel1); // </editor-fold> initComponents(); receiveUDP(); // tempReceiveUDP(); // new UDPServer(valuePane); }
From source file:net.sf.mzmine.chartbasics.ChartLogics.java
/** * Returns dimensions for limiting factor width or height * /*from w w w . ja v a 2 s .c o m*/ * @param myChart * @return */ public static Dimension calcMaxSize(ChartPanel myChart, double chartWidth, double chartHeight) { makeChartResizable(myChart); // paint on a ghost panel JPanel parent = (JPanel) myChart.getParent(); JPanel p = new JPanel(); p.removeAll(); p.add(myChart, BorderLayout.CENTER); p.setBounds(myChart.getBounds()); myChart.paintImmediately(myChart.getBounds()); p.removeAll(); parent.add(myChart); XYPlot plot = (XYPlot) myChart.getChart().getPlot(); ChartRenderingInfo info = myChart.getChartRenderingInfo(); Rectangle2D dataArea = info.getPlotInfo().getDataArea(); Rectangle2D chartArea = info.getChartArea(); // calc title space: will be added later to the right plot size double titleWidth = chartArea.getWidth() - dataArea.getWidth(); double titleHeight = chartArea.getHeight() - dataArea.getHeight(); // calculatig width for max height // calc right plot size with axis dim. // real plot width is given by factor; double realPH = chartHeight - titleHeight; // ranges ValueAxis domainAxis = plot.getDomainAxis(); org.jfree.data.Range x = domainAxis.getRange(); ValueAxis rangeAxis = plot.getRangeAxis(); org.jfree.data.Range y = rangeAxis.getRange(); // real plot height can be calculated by double realPW = realPH / y.getLength() * x.getLength(); double width = realPW + titleWidth; // if width is higher than given chartWidth then calc height for chartWidth if (width > chartWidth) { // calc right plot size with axis dim. // real plot width is given by factor; realPW = chartWidth - titleWidth; // real plot height can be calculated by realPH = realPW / x.getLength() * y.getLength(); double height = realPH + titleHeight; // Return size return new Dimension((int) chartWidth, (int) height); } else { // Return size return new Dimension((int) width, (int) chartHeight); } }
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 w ww . j a v a2 s . c om */ 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:org.jfree.chart.demo.DynamicDataDemo2.java
/** * Constructs a new demonstration application. * * @param title the frame title./*from www.j a v a2 s . co m*/ */ public DynamicDataDemo2(final String title) { super(title); this.series1 = new TimeSeries("Random 1", Millisecond.class); this.series2 = new TimeSeries("Random 2", Millisecond.class); final TimeSeriesCollection dataset1 = new TimeSeriesCollection(this.series1); final TimeSeriesCollection dataset2 = new TimeSeriesCollection(this.series2); final JFreeChart chart = ChartFactory.createTimeSeriesChart("Dynamic Data Demo 2", "Time", "Value", dataset1, true, true, false); chart.setBackgroundPaint(Color.white); final XYPlot plot = chart.getXYPlot(); plot.setBackgroundPaint(Color.lightGray); plot.setDomainGridlinePaint(Color.white); plot.setRangeGridlinePaint(Color.white); // plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 4, 4, 4, 4)); final ValueAxis axis = plot.getDomainAxis(); axis.setAutoRange(true); axis.setFixedAutoRange(60000.0); // 60 seconds plot.setDataset(1, dataset2); final NumberAxis rangeAxis2 = new NumberAxis("Range Axis 2"); rangeAxis2.setAutoRangeIncludesZero(false); plot.setRenderer(1, new DefaultXYItemRenderer()); plot.setRangeAxis(1, rangeAxis2); plot.mapDatasetToRangeAxis(1, 1); final JPanel content = new JPanel(new BorderLayout()); final ChartPanel chartPanel = new ChartPanel(chart); content.add(chartPanel); final JButton button1 = new JButton("Add To Series 1"); button1.setActionCommand("ADD_DATA_1"); button1.addActionListener(this); final JButton button2 = new JButton("Add To Series 2"); button2.setActionCommand("ADD_DATA_2"); button2.addActionListener(this); final JButton button3 = new JButton("Add To Both"); button3.setActionCommand("ADD_BOTH"); button3.addActionListener(this); final JPanel buttonPanel = new JPanel(new FlowLayout()); buttonPanel.add(button1); buttonPanel.add(button2); buttonPanel.add(button3); content.add(buttonPanel, BorderLayout.SOUTH); chartPanel.setPreferredSize(new java.awt.Dimension(500, 270)); setContentPane(content); }
From source file:dbseer.gui.panel.DBSeerSelectableChartPanel.java
@Override public void mousePressed(MouseEvent e) { super.mousePressed(e); Rectangle2D origArea = this.getScreenDataArea(); Plot plot = chart.getPlot();// w w w .jav a 2s . co m if (!(plot instanceof XYPlot)) { return; } XYPlot xyPlot = chart.getXYPlot(); String origDomainAxisLabel = xyPlot.getDomainAxis().getLabel(); if (SwingUtilities.isRightMouseButton(e)) { return; } for (DBSeerSelectableChartPanel panel : DBSeerPlotPresetFrame.chartPanels) { if (panel != this) { Plot otherPlot = panel.getChart().getPlot(); if (!(otherPlot instanceof XYPlot)) { continue; } Rectangle2D otherArea = panel.getScreenDataArea(); XYPlot otherXYPlot = panel.getChart().getXYPlot(); String otherDomainAxisLabel = otherXYPlot.getDomainAxis().getLabel(); if (origDomainAxisLabel.equalsIgnoreCase(otherDomainAxisLabel)) { double origRangeX = origArea.getMaxX() - origArea.getMinX(); double origRangeY = origArea.getMaxY() - origArea.getMinY(); double otherRangeX = otherArea.getMaxX() - otherArea.getMinX(); double otherRangeY = otherArea.getMaxY() - otherArea.getMinY(); double syncX = otherArea.getMinX() + (e.getX() - origArea.getMinX()) / origRangeX * otherRangeX; double syncY = otherArea.getMinY() + (e.getY() - origArea.getMinY()) / origRangeY * otherRangeY; MouseEvent syncEvent = new MouseEvent(this, 0, 0, 0, (int) syncX, (int) syncY, 1, false); panel.syncMousePressed(syncEvent); } } } }
From source file:dbseer.gui.panel.DBSeerSelectableChartPanel.java
@Override public void mouseDragged(MouseEvent e) { super.mouseDragged(e); Rectangle2D origArea = this.getScreenDataArea(); Plot plot = chart.getPlot();//from w ww. j av a 2 s .co m if (!(plot instanceof XYPlot)) { return; } XYPlot xyPlot = chart.getXYPlot(); String origDomainAxisLabel = xyPlot.getDomainAxis().getLabel(); if (SwingUtilities.isRightMouseButton(e)) { return; } for (DBSeerSelectableChartPanel panel : DBSeerPlotPresetFrame.chartPanels) { if (panel != this) { Plot otherPlot = panel.getChart().getPlot(); if (!(otherPlot instanceof XYPlot)) { continue; } Rectangle2D otherArea = panel.getScreenDataArea(); XYPlot otherXYPlot = panel.getChart().getXYPlot(); String otherDomainAxisLabel = otherXYPlot.getDomainAxis().getLabel(); if (origDomainAxisLabel.equalsIgnoreCase(otherDomainAxisLabel)) { double origRangeX = origArea.getMaxX() - origArea.getMinX(); double origRangeY = origArea.getMaxY() - origArea.getMinY(); double otherRangeX = otherArea.getMaxX() - otherArea.getMinX(); double otherRangeY = otherArea.getMaxY() - otherArea.getMinY(); double syncX = otherArea.getMinX() + (e.getX() - origArea.getMinX()) / origRangeX * otherRangeX; double syncY = otherArea.getMinY() + (e.getY() - origArea.getMinY()) / origRangeY * otherRangeY; MouseEvent syncEvent = new MouseEvent(this, 0, 0, 0, (int) syncX, (int) syncY, 1, false); panel.syncMouseDragged(syncEvent); } } } }
From source file:dbseer.gui.panel.DBSeerSelectableChartPanel.java
@Override public void mouseReleased(MouseEvent e) { super.mouseReleased(e); Rectangle2D origArea = this.getScreenDataArea(); Plot plot = chart.getPlot();//from w ww . j a va 2s. co m if (!(plot instanceof XYPlot)) { return; } XYPlot xyPlot = chart.getXYPlot(); String origDomainAxisLabel = xyPlot.getDomainAxis().getLabel(); if (SwingUtilities.isRightMouseButton(e)) { return; } for (DBSeerSelectableChartPanel panel : DBSeerPlotPresetFrame.chartPanels) { if (panel != this) { Plot otherPlot = panel.getChart().getPlot(); if (!(otherPlot instanceof XYPlot)) { continue; } Rectangle2D otherArea = panel.getScreenDataArea(); XYPlot otherXYPlot = panel.getChart().getXYPlot(); String otherDomainAxisLabel = otherXYPlot.getDomainAxis().getLabel(); if (origDomainAxisLabel.equalsIgnoreCase(otherDomainAxisLabel)) { double origRangeX = origArea.getMaxX() - origArea.getMinX(); double origRangeY = origArea.getMaxY() - origArea.getMinY(); double otherRangeX = otherArea.getMaxX() - otherArea.getMinX(); double otherRangeY = otherArea.getMaxY() - otherArea.getMinY(); double syncX = otherArea.getMinX() + (e.getX() - origArea.getMinX()) / origRangeX * otherRangeX; double syncY = otherArea.getMinY() + (e.getY() - origArea.getMinY()) / origRangeY * otherRangeY; MouseEvent syncEvent = new MouseEvent(this, 0, 0, 0, (int) syncX, (int) syncY, 1, false); panel.syncMouseReleased(syncEvent); } } } }
From source file:ws.moor.bt.gui.charts.PiecesStats.java
private JFreeChart createChart(XYDataset dataset) { JFreeChart chart = ChartFactory.createTimeSeriesChart("Pieces", "Time", "Pieces", dataset, true, false, false);/*from www .j a va 2 s. com*/ chart.setBackgroundPaint(Color.white); XYPlot plot = (XYPlot) chart.getPlot(); plot.setBackgroundPaint(Color.lightGray); plot.setDomainGridlinePaint(Color.white); plot.setRangeGridlinePaint(Color.white); plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0)); plot.setDomainCrosshairVisible(true); plot.setRangeCrosshairVisible(true); DateAxis axis = (DateAxis) plot.getDomainAxis(); axis.setDateFormatOverride(new SimpleDateFormat("HH:mm:ss")); return chart; }
From source file:ws.moor.bt.gui.charts.OpenConnections.java
private JFreeChart createChart(XYDataset dataset) { JFreeChart chart = ChartFactory.createTimeSeriesChart("Open Connections", "Time", "Connections", dataset, true, false, false);//from w ww . j a va 2 s . c om chart.setBackgroundPaint(Color.white); XYPlot plot = (XYPlot) chart.getPlot(); plot.setBackgroundPaint(Color.lightGray); plot.setDomainGridlinePaint(Color.white); plot.setRangeGridlinePaint(Color.white); plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0)); plot.setDomainCrosshairVisible(true); plot.setRangeCrosshairVisible(true); DateAxis axis = (DateAxis) plot.getDomainAxis(); axis.setDateFormatOverride(new SimpleDateFormat("HH:mm:ss")); return chart; }
From source file:org.jfree.chart.demo.XYAreaChartTest.java
/** * Creates a chart.//w ww . j a v a2 s . co m * * @param dataset the dataset. * * @return A chart. */ private JFreeChart createChart(final XYDataset dataset) { final JFreeChart chart = ChartFactory.createXYAreaChart("XY Area Chart Test", "Domain (X)", "Range (Y)", dataset, PlotOrientation.VERTICAL, true, // legend true, // tool tips false // URLs ); chart.setBackgroundPaint(Color.white); final XYPlot plot = chart.getXYPlot(); //plot.setOutlinePaint(Color.black); plot.setBackgroundPaint(Color.lightGray); plot.setForegroundAlpha(0.65f); plot.setDomainGridlinePaint(Color.white); plot.setRangeGridlinePaint(Color.white); plot.setRenderer(new XYAreaRenderer2()); final ValueAxis domainAxis = plot.getDomainAxis(); domainAxis.setTickMarkPaint(Color.black); domainAxis.setLowerMargin(0.0); domainAxis.setUpperMargin(0.0); final ValueAxis rangeAxis = plot.getRangeAxis(); rangeAxis.setTickMarkPaint(Color.black); return chart; }