List of usage examples for org.jfree.data.xy XYSeries getMaxY
public double getMaxY()
From source file:org.pf.midea.MainUI.java
private void showConstellationWindow(ConstellationPoint[] _map, String _name) { JFrame constellation = new JFrame(" ?? " + _name); constellation.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); XYSeriesCollection dots = new XYSeriesCollection(); XYSeries series = new XYSeries(_name); JFreeChart chart = ChartFactory.createScatterPlot("", "I", "Q", dots, PlotOrientation.VERTICAL, false, false, false);//from ww w . j a va 2 s . c om XYPlot xyPlot = chart.getXYPlot(); CustomXYToolTipGenerator tooltipsGenerator = new CustomXYToolTipGenerator(); ArrayList<String> tooltips = new ArrayList<>(); for (ConstellationPoint ccp : _map) { double I = ccp.getI(); double Q = ccp.getQ(); series.add(I, Q); tooltips.add(ccp.getCode().getStringSequence()); } tooltipsGenerator.addToolTipSeries(tooltips); xyPlot.getRenderer().setBaseToolTipGenerator(tooltipsGenerator); double maxX = StatisticsTools.round(Math.abs(series.getMaxX()), 3); double maxY = StatisticsTools.round(Math.abs(series.getMaxY()), 3); double minX = StatisticsTools.round(Math.abs(series.getMinX()), 3); double minY = StatisticsTools.round(Math.abs(series.getMinY()), 3); if (maxX != 0 || minX != 0) { double X = Math.max(minX, maxX); xyPlot.getDomainAxis().setRange(-1.1 * X, 1.1 * X); } else xyPlot.getDomainAxis().setRange(-1, 1); if (maxY != 0 || minY != 0) { double Y = Math.max(minY, maxY); xyPlot.getRangeAxis().setRange(-1.1 * Y, 1.1 * Y); } else xyPlot.getRangeAxis().setRange(-1, 1); dots.addSeries(series); xyPlot.setBackgroundPaint(Color.WHITE); xyPlot.setDomainGridlinePaint(Color.GRAY); xyPlot.setRangeGridlinePaint(Color.GRAY); xyPlot.getRenderer().setSeriesPaint(0, Color.BLACK); xyPlot.setDomainZeroBaselineVisible(true); xyPlot.setRangeZeroBaselineVisible(true); ChartPanel chartPanel = new ChartPanel(chart); JPanel nestedPanel = new JPanel(); nestedPanel.add(chartPanel, new CellConstraints()); constellation.add(nestedPanel); constellation.pack(); constellation.setLocationRelativeTo(null); constellation.setResizable(false); constellation.setVisible(true); }
From source file:mil.tatrc.physiology.utilities.csv.plots.ActionEventPlotter.java
public void createGraph(PlotJob job, List<List<Double>> timeData, List<List<Double>> data, List<LogEvent> events, List<SEAction> actions) { CSVPlotTool plotTool = new CSVPlotTool(); //to leverage existing functions String title = job.name + "_"; XYSeriesCollection dataSet = new XYSeriesCollection(); double maxY = 0; double minY = Double.MAX_VALUE; for (int i = 0; i < timeData.size(); i++) { if (timeData.get(i) == null || data.get(i) == null) { job.bgColor = Color.white; //This hits when we have Expected data but NOT computed data continue; }/*from w w w .ja va 2 s .c om*/ title = title + job.headers.get(i) + "_"; XYSeries dataSeries; if (job.isComparePlot) { if (timeData.size() > 1) dataSeries = plotTool.createXYSeries(i == 0 ? "Expected" : "Computed", timeData.get(i), data.get(i)); else //If we're comparing but only have one data list, expected is missing, so rename to computed { dataSeries = plotTool.createXYSeries("Computed", timeData.get(i), data.get(i)); } } else dataSeries = plotTool.createXYSeries(job.headers.get(i), timeData.get(i), data.get(i)); dataSet.addSeries(dataSeries); maxY = maxY < dataSeries.getMaxY() ? dataSeries.getMaxY() : maxY; minY = minY > dataSeries.getMinY() ? dataSeries.getMinY() : minY; } title = title + "vs_Time_Action_Event_Plot"; //Override the constructed title if desired (usually for compare plots) if (job.titleOverride != null && !job.titleOverride.isEmpty() && !job.titleOverride.equalsIgnoreCase("None")) title = job.titleOverride; double rangeLength = maxY - minY; if (Math.abs(rangeLength) < 1e-6) { rangeLength = .01; } class AEEntry implements Comparable<AEEntry> { public String name; public List<Double> times = new ArrayList<Double>(); public List<Double> YVals = new ArrayList<Double>(); public String type = ""; public int compareTo(AEEntry entry) { return times.get(0) < entry.times.get(0) ? -1 : times.get(0) > entry.times.get(0) ? 1 : 0; } } List<AEEntry> allActionsAndEvents = new ArrayList<AEEntry>(); if (!job.skipAllEvents) { //Make points for each event //Treat each event like two points on the same vertical line for (LogEvent event : events) { boolean skip = false; for (String eventToSkip : job.eventOmissions) { if (event.text.contains(eventToSkip)) skip = true; } if (skip) continue; AEEntry entry = new AEEntry(); entry.times.add(event.time.getValue()); if (job.logAxis) entry.YVals.add(maxY); else if (job.forceZeroYAxisBound && maxY < 0) entry.YVals.add(-.01); else entry.YVals.add(maxY + 0.15 * rangeLength); entry.times.add(event.time.getValue()); if (job.logAxis) entry.YVals.add(minY); else if (job.forceZeroYAxisBound && minY > 0) entry.YVals.add(-.01); else entry.YVals.add(minY - 0.15 * rangeLength); entry.name = event.text + "\r\nt=" + event.time.getValue(); entry.type = "EVENT:"; allActionsAndEvents.add(entry); } } if (!job.skipAllActions) { //Make similar entries for actions for (SEAction action : actions) { boolean skip = false; for (String actionToSkip : job.actionOmissions) { if (action.toString().contains(actionToSkip)) skip = true; } if (skip) continue; if (action.toString().contains("Advance Time")) continue; AEEntry entry = new AEEntry(); entry.times.add(action.getScenarioTime().getValue()); if (job.logAxis) entry.YVals.add(maxY); else if (job.forceZeroYAxisBound && maxY < 0) entry.YVals.add(-.01); else entry.YVals.add(maxY + 0.15 * rangeLength); entry.times.add(action.getScenarioTime().getValue()); if (job.logAxis) entry.YVals.add(minY); else if (job.forceZeroYAxisBound && minY > 0) entry.YVals.add(-.01); else entry.YVals.add(minY - 0.15 * rangeLength); entry.name = action.toString() + "\r\nt=" + action.getScenarioTime().getValue(); entry.type = "ACTION:"; allActionsAndEvents.add(entry); } } //Sort the list Collections.sort(allActionsAndEvents); //Add a series for each entry for (AEEntry entry : allActionsAndEvents) { dataSet.addSeries(plotTool.createXYSeries(entry.type + entry.name, entry.times, entry.YVals)); } //If we have experimental data, try to load it and create a dataset for it XYSeriesCollection expDataSet = new XYSeriesCollection(); if (job.experimentalData != null && !job.experimentalData.isEmpty()) { Map<String, List<Double>> expData = new HashMap<String, List<Double>>(); List<String> expHeaders = new ArrayList<String>(); try { CSVContents csv = new CSVContents(job.experimentalData); csv.abbreviateContents = 0; csv.readAll(expData); expHeaders = csv.getHeaders(); } catch (Exception e) { Log.error("Unable to read experimental data"); } if (!expData.isEmpty() && !expHeaders.isEmpty()) { List<Double> expTimeData = new ArrayList<Double>(); expTimeData = expData.get("Time(s)"); for (String h : expHeaders) //Will assume all headers from exp file will be on same Y axis vs time { if (h.equalsIgnoreCase("Time(s)")) continue; expDataSet.addSeries(plotTool.createXYSeries("Experimental " + h, expTimeData, expData.get(h))); } } } //set labels String XAxisLabel = "Time(s)"; String YAxisLabel = job.headers.get(0); JFreeChart chart = ChartFactory.createXYLineChart( job.titleOverride != null && job.titleOverride.equalsIgnoreCase("None") ? "" : title, // chart title XAxisLabel, // x axis label YAxisLabel, // y axis label dataSet, // data PlotOrientation.VERTICAL, // orientation true, // include legend true, // tooltips false // urls ); Log.info("Creating Graph " + title); XYPlot plot = (XYPlot) chart.getPlot(); if (!job.logAxis) { // Determine Y range double resMax0 = maxY; double resMin0 = minY; if (Double.isNaN(resMax0) || Double.isNaN(resMin0)) plot.getDomainAxis().setLabel("Range is NaN"); if (DoubleUtils.isZero(resMin0)) resMin0 = -0.000001; if (DoubleUtils.isZero(resMax0)) resMax0 = 0.000001; if (job.forceZeroYAxisBound && resMin0 >= 0) resMin0 = -.000001; if (job.forceZeroYAxisBound && resMax0 <= 0) resMax0 = .000001; rangeLength = resMax0 - resMin0; ValueAxis yAxis = plot.getRangeAxis(); if (rangeLength != 0) yAxis.setRange(resMin0 - 0.15 * rangeLength, resMax0 + 0.15 * rangeLength);//15% buffer so we can see top and bottom clearly //Add another Y axis to the right side for easier reading ValueAxis rightYAxis = new NumberAxis(); rightYAxis.setRange(yAxis.getRange()); rightYAxis.setLabel(""); //Override the bounds if desired try { if (job.Y1LowerBound != null) { yAxis.setLowerBound(job.Y1LowerBound); rightYAxis.setLowerBound(job.Y1LowerBound); } if (job.Y1UpperBound != null) { yAxis.setUpperBound(job.Y1UpperBound); rightYAxis.setUpperBound(job.Y1UpperBound); } } catch (Exception e) { Log.error( "Couldn't set Y bounds. You probably tried to set a bound on an axis that doesn't exist."); } plot.setRangeAxis(0, yAxis); plot.setRangeAxis(1, rightYAxis); } else { double resMin = minY; double resMax = maxY; if (resMin <= 0.0) resMin = .00001; LogarithmicAxis yAxis = new LogarithmicAxis("Log(" + YAxisLabel + ")"); LogarithmicAxis rightYAxis = new LogarithmicAxis(""); yAxis.setLowerBound(resMin); rightYAxis.setLowerBound(resMin); yAxis.setUpperBound(resMax); rightYAxis.setUpperBound(resMax); //Override the bounds if desired try { if (job.Y1LowerBound != null) { yAxis.setLowerBound(job.Y1LowerBound); rightYAxis.setLowerBound(job.Y1LowerBound); } if (job.Y1UpperBound != null) { yAxis.setUpperBound(job.Y1UpperBound); rightYAxis.setUpperBound(job.Y1UpperBound); } } catch (Exception e) { Log.error( "Couldn't set Y bounds. You probably tried to set a bound on an axis that doesn't exist."); } plot.setRangeAxis(0, yAxis); plot.setRangeAxis(1, rightYAxis); } //Override X bounds if desired try { if (job.X1LowerBound != null) plot.getDomainAxis(0).setLowerBound(job.X1LowerBound); if (job.X1UpperBound != null) plot.getDomainAxis(0).setUpperBound(job.X1UpperBound); } catch (Exception e) { Log.error("Couldn't set X bounds. You probably tried to set a bound on an axis that doesn't exist."); } //Override labels if desired if (job.X1Label != null && !plot.getDomainAxis(0).getLabel().contains("NaN")) plot.getDomainAxis(0).setLabel(job.X1Label.equalsIgnoreCase("None") ? "" : job.X1Label); if (job.Y1Label != null) plot.getRangeAxis(0).setLabel(job.Y1Label.equalsIgnoreCase("None") ? "" : job.Y1Label); //If we have experimental data, set up the renderer for it and add to plot if (expDataSet.getSeriesCount() != 0) { XYItemRenderer renderer1 = new XYLineAndShapeRenderer(false, true); // Shapes only renderer1.setSeriesShape(0, ShapeUtilities.createDiamond(8)); plot.setDataset(1, expDataSet); plot.setRenderer(1, renderer1); plot.mapDatasetToDomainAxis(1, 0); plot.mapDatasetToRangeAxis(1, 0); } formatAEPlot(job, chart); plot.setDomainGridlinesVisible(job.showGridLines); plot.setRangeGridlinesVisible(job.showGridLines); //Changing line widths and colors XYItemRenderer r = plot.getRenderer(); BasicStroke wideLine = new BasicStroke(2.0f); Color[] AEcolors = { Color.red, Color.green, Color.black, Color.magenta, Color.orange }; Color[] dataColors = { Color.blue, Color.cyan, Color.gray, Color.black, Color.red }; for (int i = 0, cIndex = 0; i < dataSet.getSeriesCount(); i++, cIndex++) { r.setSeriesStroke(i, wideLine); XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer(); renderer.setBaseShapesVisible(false); if (cIndex > 4) cIndex = 0; if (i < job.headers.size()) //Our actual data { renderer.setSeriesFillPaint(i, dataColors[cIndex]); renderer.setSeriesPaint(i, dataColors[cIndex]); } else //actions and events in procession of other colors { renderer.setSeriesFillPaint(i, AEcolors[cIndex]); renderer.setSeriesPaint(i, AEcolors[cIndex]); } } //Special color and format changes for compare plots if (job.isComparePlot) { XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer(); for (int i = 0; i < dataSet.getSeriesCount(); i++) { if (dataSet.getSeries(i).getKey().toString().equalsIgnoreCase("Expected")) { renderer.setSeriesStroke(//makes a dashed line i, //argument below float[]{I,K} -> alternates between solid and opaque (solid for I, opaque for K) new BasicStroke(2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 1.0f, new float[] { 15.0f, 30.0f }, 0.0f)); renderer.setDrawSeriesLineAsPath(true); renderer.setUseFillPaint(true); renderer.setBaseShapesVisible(false); renderer.setSeriesFillPaint(i, Color.black); renderer.setSeriesPaint(i, Color.black); } if (dataSet.getSeries(i).getKey().toString().equalsIgnoreCase("Computed")) { renderer.setSeriesFillPaint(i, Color.red); renderer.setSeriesPaint(i, Color.red); } if (dataSet.getSeries(i).getKey().toString().startsWith("ACTION")) { renderer.setSeriesFillPaint(i, Color.green); renderer.setSeriesPaint(i, Color.green); } if (dataSet.getSeries(i).getKey().toString().startsWith("EVENT")) { renderer.setSeriesFillPaint(i, Color.blue); renderer.setSeriesPaint(i, Color.blue); } } } //Split the auto-generated legend into two legends, one for data and one for actions and events LegendItemCollection originalLegendCollection = plot.getLegendItems(); final LegendItemCollection dataLegendCollection = new LegendItemCollection(); int i; for (i = 0; i < job.headers.size() && i < originalLegendCollection.getItemCount(); i++) { if (originalLegendCollection.get(i).getLabel().startsWith("ACTION") || originalLegendCollection.get(i).getLabel().startsWith("EVENT")) break; dataLegendCollection.add(originalLegendCollection.get(i)); } final LegendItemCollection remainingLegendCollection = new LegendItemCollection(); for (; i < originalLegendCollection.getItemCount(); i++) { remainingLegendCollection.add(originalLegendCollection.get(i)); } chart.removeLegend(); LegendItemSource source = new LegendItemSource() { LegendItemCollection lic = new LegendItemCollection(); { lic.addAll(dataLegendCollection); } public LegendItemCollection getLegendItems() { return lic; } }; LegendTitle dataLegend = new LegendTitle(source); dataLegend.setMargin(new RectangleInsets(1.0, 1.0, 1.0, 1.0)); dataLegend.setBorder(2, 2, 2, 2); dataLegend.setBackgroundPaint(Color.white); dataLegend.setPosition(RectangleEdge.TOP); dataLegend.setItemFont(new Font("SansSerif", Font.PLAIN, 22)); chart.addLegend(dataLegend); source = new LegendItemSource() { LegendItemCollection lic = new LegendItemCollection(); { lic.addAll(remainingLegendCollection); } public LegendItemCollection getLegendItems() { return lic; } }; LegendTitle actionEventsLegend = new LegendTitle(source); actionEventsLegend.setMargin(new RectangleInsets(1.0, 1.0, 1.0, 1.0)); actionEventsLegend.setBorder(2, 2, 2, 2); actionEventsLegend.setBackgroundPaint(Color.white); actionEventsLegend.setPosition(RectangleEdge.BOTTOM); actionEventsLegend.setItemFont(new Font("SansSerif", Font.PLAIN, 22)); if (!job.hideAELegend && !job.removeAllLegends) chart.addLegend(actionEventsLegend); if (job.removeAllLegends) chart.removeLegend(); int verticalPixels = 800 + 170 * (allActionsAndEvents.size() / 5); //This is a little hacky, but if we want only the legend, just extend Plot() and remove the draw functionality so it makes a blank plot class legendPlot extends Plot { public void draw(Graphics2D arg0, Rectangle2D arg1, Point2D arg2, PlotState arg3, PlotRenderingInfo arg4) { } public String getPlotType() { return null; } } //Then add the legend to that and throw away the original plot if (job.legendOnly) { chart = new JFreeChart("", null, new legendPlot(), false); chart.addLegend(actionEventsLegend); } try { FileUtils.createDirectory(job.outputDir); String filename = job.outputFilename == null ? job.outputDir + "/" + plotTool.MakeFileName(title) + ".jpg" : job.outputDir + "/" + job.outputFilename; if (!filename.endsWith(".jpg")) filename = filename + ".jpg"; File JPGFile = new File(filename); if (job.imageHeight != null && job.imageWidth != null) ChartUtilities.saveChartAsJPEG(JPGFile, chart, job.imageWidth, job.imageHeight); else if (!job.hideAELegend && !job.removeAllLegends) ChartUtilities.saveChartAsJPEG(JPGFile, chart, 1600, verticalPixels); else ChartUtilities.saveChartAsJPEG(JPGFile, chart, 1600, 800); } catch (IOException e) { Log.error(e.getMessage()); } }
From source file:be.ugent.maf.cellmissy.gui.controller.analysis.doseresponse.DoseResponseController.java
/** * Create a dose-response chart containing experimental data, simulated data * and an annotated R value./*w w w .java 2 s .c om*/ * * @param dataToPlot The experimental data on which the fit was performed * @param analysisGroup The analysis group * @param normalized Whether the responses are normalized * @return */ public JFreeChart createDoseResponseChart(List<DoseResponsePair> dataToPlot, DoseResponseAnalysisGroup analysisGroup, boolean normalized) { // setup scatter data of experimental concentrations/slopes, renderer and axis XYSeriesCollection experimentalData = new XYSeriesCollection(); XYSeries scatterXYSeries = JFreeChartUtils.generateXYSeries(AnalysisUtils.generateXValues(dataToPlot), AnalysisUtils.generateYValues(dataToPlot)); scatterXYSeries.setKey("Experimental data"); experimentalData.addSeries(scatterXYSeries); // compute how far the simulated data and axes should range: from the lowest and highest dose continue half of the range between these two List<Double> extremes = new ArrayList<>(); Double range = Math.abs(scatterXYSeries.getMaxX() - scatterXYSeries.getMinX()); extremes.add(scatterXYSeries.getMinX() - (range / 2)); extremes.add(scatterXYSeries.getMaxX() + (range / 2)); // Create the simulated line data, renderer, and axis XYSeriesCollection fitting = new XYSeriesCollection(); // create xy series of simulated data from the parameters from the fitting SigmoidFittingResultsHolder resultsHolder = analysisGroup.getDoseResponseAnalysisResults() .getFittingResults(normalized); XYSeries fittingData = simulateData(resultsHolder, extremes); fittingData.setKey("Fitting"); fitting.addSeries(fittingData); XYPlot plot = JFreeChartUtils.setupDoseResponseDatasets(experimentalData, fitting, getPlotAxesNames(normalized), extremes); // show the r squared value, put the value at a certain place between the min and max dose double xPlace = extremes.get(1) - range; double yPlace = scatterXYSeries.getMinY() + ((scatterXYSeries.getMaxY() - scatterXYSeries.getMinY()) / 4); plot.addAnnotation(new XYTextAnnotation( "R2=" + AnalysisUtils.roundThreeDecimals(AnalysisUtils.computeRSquared(dataToPlot, resultsHolder)), xPlace, yPlace)); // Create the chart with the plot and no legend JFreeChart chart = new JFreeChart("Title", JFreeChart.DEFAULT_TITLE_FONT, plot, false); String title = ""; if (normalized) { title = "Normalized fitting"; } else { title = "Initial fitting"; } JFreeChartUtils.setupDoseResponseChart(chart, title); return chart; }
From source file:mil.tatrc.physiology.utilities.csv.plots.ConvexHullPlotter.java
public void plot(LogListener listener, SESubstanceManager subMgr) { //fill PlotJob with needed data if it doesn't exist PlotJob job = (PlotJob) listener;/*from ww w . j a v a2 s .c om*/ if (job.dataPath == null || job.dataPath.isEmpty()) { job.dataPath = "../verification/Scenarios/" + job.verificationDirectory + "/Current Baseline/"; } if (job.dataFile == null || job.dataFile.isEmpty()) { job.dataFile = job.name + "Results.zip"; } //Get data contents for all headers if (data.isEmpty() || data == null) { try { CSVContents csv = new CSVContents(job.dataPath + job.dataFile); csv.abbreviateContents = job.resultsSkipNum; for (int i = 0; i < job.headers.size(); i++) { List<Double> headerData = new ArrayList<Double>(); csv.readHeader(csv.unitUnderscoreToSpace(job.headers.get(i)), headerData); data.put(job.headers.get(i), headerData); } } catch (IOException e) { Log.error("Could not analyze file " + job.dataPath + job.dataFile); } } //Catch some errors if (job.Y2headers.size() > 0 && job.X2header == null) { Log.error("No X2 header specified for job " + job.name + ". Each Y axis must have a corresponding X axis."); return; } //Make a dataSeries for desired headers and add to collection(s) CSVPlotTool plotTool = new CSVPlotTool(); //to leverage existing functions String title = job.name + "_"; XYSeriesCollection dataSet1 = new XYSeriesCollection(); XYSeriesCollection dataSet2 = new XYSeriesCollection(); double maxY1 = 0; double minY1 = Double.MAX_VALUE; double maxY2 = 0; double minY2 = Double.MAX_VALUE; for (int i = 0; i < job.Y1headers.size(); i++) { XYSeries dataSeriesTop; XYSeries dataSeriesBottom; XYSeries dataSeriesLeft; XYSeries dataSeriesRight; //For convex hulls, we have to reorder points before inserting into the dataset ConvexHullMaker maker = new ConvexHullMaker(); List<List<Double>> newVals = new ArrayList<List<Double>>(); List<List<Double>> splitVals = new ArrayList<List<Double>>(); newVals = maker.make(data.get(job.X1header), data.get(job.Y1headers.get(i))); splitVals = splitHull(newVals); dataSeriesTop = plotTool.createXYSeries(job.Y1headers.get(i), splitVals.get(0), splitVals.get(1)); dataSeriesBottom = plotTool.createXYSeries("", splitVals.get(2), splitVals.get(3)); dataSeriesLeft = plotTool.createXYSeries("", splitVals.get(4), splitVals.get(5)); dataSeriesRight = plotTool.createXYSeries("", splitVals.get(6), splitVals.get(7)); dataSeriesBottom.setKey("REMOVE"); dataSeriesLeft.setKey("REMOVE"); dataSeriesRight.setKey("REMOVE"); dataSet1.addSeries(dataSeriesTop); dataSet1.addSeries(dataSeriesBottom); dataSet1.addSeries(dataSeriesLeft); dataSet1.addSeries(dataSeriesRight); title = title + job.Y1headers.get(i) + "_"; maxY1 = maxY1 < dataSeriesTop.getMaxY() ? dataSeriesTop.getMaxY() : maxY1; minY1 = minY1 > dataSeriesBottom.getMinY() ? dataSeriesBottom.getMinY() : minY1; } for (int i = 0; i < job.Y2headers.size(); i++) { XYSeries dataSeriesTop; XYSeries dataSeriesBottom; XYSeries dataSeriesLeft; XYSeries dataSeriesRight; ConvexHullMaker maker = new ConvexHullMaker(); List<List<Double>> newVals = new ArrayList<List<Double>>(); List<List<Double>> splitVals = new ArrayList<List<Double>>(); newVals = maker.make(data.get(job.X2header), data.get(job.Y2headers.get(i))); splitVals = splitHull(newVals); dataSeriesTop = plotTool.createXYSeries(job.Y2headers.get(i), splitVals.get(0), splitVals.get(1)); dataSeriesBottom = plotTool.createXYSeries("", splitVals.get(2), splitVals.get(3)); dataSeriesLeft = plotTool.createXYSeries("", splitVals.get(4), splitVals.get(5)); dataSeriesRight = plotTool.createXYSeries("", splitVals.get(6), splitVals.get(7)); dataSeriesBottom.setKey("REMOVE"); dataSeriesLeft.setKey("REMOVE"); dataSeriesRight.setKey("REMOVE"); dataSet2.addSeries(dataSeriesTop); dataSet2.addSeries(dataSeriesBottom); dataSet2.addSeries(dataSeriesLeft); dataSet2.addSeries(dataSeriesRight); title = title + job.Y2headers.get(i) + "_"; maxY2 = maxY2 < dataSeriesTop.getMaxY() ? dataSeriesTop.getMaxY() : maxY2; minY2 = minY2 > dataSeriesBottom.getMinY() ? dataSeriesBottom.getMinY() : minY2; } title = title + "vs_" + job.X1header; if (job.X2header != null && !job.X1header.equalsIgnoreCase(job.X2header)) title = title + "_" + job.X2header; //Override the constructed title if desired if (job.titleOverride != null && !job.titleOverride.isEmpty() && !job.titleOverride.equalsIgnoreCase("None")) title = job.titleOverride; //set labels String XAxisLabel = job.X1header; String YAxisLabel = job.Y1headers.get(0); JFreeChart chart = ChartFactory.createXYLineChart( job.titleOverride != null && job.titleOverride.equalsIgnoreCase("None") ? "" : title, // chart title XAxisLabel, // x axis label YAxisLabel, // y axis label dataSet1, // data PlotOrientation.VERTICAL, // orientation true, // include legend true, // tooltips false // urls ); Log.info("Creating Graph " + title); XYPlot plot = (XYPlot) chart.getPlot(); if (!job.logAxis) { // Determine Y1 range double resMax0 = maxY1; double resMin0 = minY1; if (Double.isNaN(resMax0) || Double.isNaN(resMin0)) plot.getDomainAxis(0).setLabel("Range is NaN"); if (DoubleUtils.isZero(resMin0)) resMin0 = -0.001; if (DoubleUtils.isZero(resMax0)) resMax0 = 0.001; double rangeLength = resMax0 - resMin0; ValueAxis yAxis = plot.getRangeAxis(0); yAxis.setRange(resMin0 - 0.15 * rangeLength, resMax0 + 0.15 * rangeLength);//15% buffer so we can see top and bottom clearly //Override the bounds if desired try { if (job.Y1LowerBound != null) yAxis.setLowerBound(job.Y1LowerBound); if (job.Y1UpperBound != null) yAxis.setUpperBound(job.Y1UpperBound); } catch (Exception e) { Log.error( "Couldn't set Y bounds. You probably tried to set a bound on an axis that doesn't exist."); } plot.setRangeAxis(0, yAxis); //Add the second Y axis to the right side if (!job.Y2headers.isEmpty()) { ValueAxis rightYAxis = new NumberAxis(); // Determine Y2 range double resMax1 = maxY2; double resMin1 = minY2; if (Double.isNaN(resMax1) || Double.isNaN(resMin1)) plot.getDomainAxis(1).setLabel("Range is NaN"); if (DoubleUtils.isZero(resMin1)) resMin1 = -0.001; if (DoubleUtils.isZero(resMax1)) resMax1 = 0.001; rangeLength = resMax1 - resMin1; rightYAxis.setRange(resMin1 - 0.15 * rangeLength, resMax1 + 0.15 * rangeLength); rightYAxis.setLabel(job.Y2headers.get(0)); //Override the bounds if desired try { if (job.Y2LowerBound != null) rightYAxis.setLowerBound(job.Y2LowerBound); if (job.Y2UpperBound != null) rightYAxis.setUpperBound(job.Y2UpperBound); } catch (Exception e) { Log.error( "Couldn't set Y bounds. You probably tried to set a bound on an axis that doesn't exist."); } plot.setRangeAxis(1, rightYAxis); } } else { double resMin = minY1 < minY2 ? minY1 : minY2; if (resMin <= 0.0) resMin = .00001; LogarithmicAxis yAxis = new LogarithmicAxis("Log(" + YAxisLabel + ")"); yAxis.setLowerBound(resMin); //Override the bounds if desired try { if (job.Y1LowerBound != null) yAxis.setLowerBound(job.Y1LowerBound); if (job.Y1UpperBound != null) yAxis.setUpperBound(job.Y1UpperBound); } catch (Exception e) { Log.error( "Couldn't set Y bounds. You probably tried to set a bound on an axis that doesn't exist."); } plot.setRangeAxis(0, yAxis); if (!job.Y2headers.isEmpty()) { LogarithmicAxis rightYAxis = new LogarithmicAxis("Log(" + job.Y2headers.get(0) + ")"); rightYAxis.setLowerBound(resMin); //Override the bounds if desired try { if (job.Y2LowerBound != null) rightYAxis.setLowerBound(job.Y2LowerBound); if (job.Y2UpperBound != null) rightYAxis.setUpperBound(job.Y2UpperBound); } catch (Exception e) { Log.error( "Couldn't set Y bounds. You probably tried to set a bound on an axis that doesn't exist."); } plot.setRangeAxis(1, rightYAxis); } } //Override X bounds if desired try { if (job.X1LowerBound != null) plot.getDomainAxis(0).setLowerBound(job.X1LowerBound); if (job.X1UpperBound != null) plot.getDomainAxis(0).setUpperBound(job.X1UpperBound); if (job.X2LowerBound != null) plot.getDomainAxis(1).setLowerBound(job.X2LowerBound); if (job.X2UpperBound != null) plot.getDomainAxis(1).setUpperBound(job.X2UpperBound); } catch (Exception e) { Log.error("Couldn't set X bounds. You probably tried to set a bound on an axis that doesn't exist."); } //Add the second dataset if necessary if (!job.Y2headers.isEmpty()) { plot.setDataset(1, dataSet2); plot.mapDatasetToRangeAxis(1, 1); } //Override labels if desired if (job.X1Label != null && !plot.getDomainAxis(0).getLabel().contains("NaN")) plot.getDomainAxis(0).setLabel(job.X1Label.equalsIgnoreCase("None") ? "" : job.X1Label); if (job.X2Label != null && plot.getDomainAxis(1) != null) plot.getDomainAxis(1).setLabel(job.X2Label.equalsIgnoreCase("None") ? "" : job.X2Label); if (job.Y1Label != null) plot.getRangeAxis(0).setLabel(job.Y1Label.equalsIgnoreCase("None") ? "" : job.Y1Label); if (job.Y2Label != null && plot.getRangeAxis(1) != null) plot.getRangeAxis(1).setLabel(job.Y2Label.equalsIgnoreCase("None") ? "" : job.Y2Label); //Format lines and colors plotTool.formatXYPlot(chart, job.bgColor); plot.setDomainGridlinesVisible(job.showGridLines); plot.setRangeGridlinesVisible(job.showGridLines); formatConvexHullPlot(job, chart, dataSet1, dataSet2); //Handle legends if (job.removeAllLegends) chart.removeLegend(); //Make the file try { FileUtils.createDirectory(job.outputDir); String filename = job.outputFilename == null ? job.outputDir + "/" + plotTool.MakeFileName(title) + ".jpg" : job.outputDir + "/" + job.outputFilename; if (!filename.endsWith(".jpg")) filename = filename + ".jpg"; File JPGFile = new File(filename); if (job.imageHeight != null && job.imageWidth != null) ChartUtilities.saveChartAsJPEG(JPGFile, chart, job.imageWidth, job.imageHeight); else ChartUtilities.saveChartAsJPEG(JPGFile, chart, 1600, 800); } catch (IOException e) { Log.error(e.getMessage()); } }
From source file:org.lmn.fc.frameworks.starbase.plugins.observatory.ui.tabs.charts.ChartHelper.java
/*********************************************************************************************** * Dump the (partial) contents of each Series in an XYdatset. * * @param dump//w w w .ja va2s.com * @param calendar * @param dataset * @param dumprowcount * @param title */ public static void dumpXYDataset(final boolean dump, final Calendar calendar, final XYDataset dataset, final int dumprowcount, final String title) { final String SOURCE = "ChartHelper.dumpXYDataset() "; if (dump) { LOGGER.log(title); if ((dataset != null) && (dataset instanceof XYSeriesCollection)) { final XYSeriesCollection seriesCollection; seriesCollection = (XYSeriesCollection) dataset; LOGGER.log("XYSeriesCollection"); LOGGER.log(" [series.count=" + seriesCollection.getSeriesCount() + "]"); LOGGER.log(" [domain.lowerbound.interval.true=" + (long) seriesCollection.getDomainLowerBound(true) + "]"); LOGGER.log(" [domain.lowerbound.interval.false=" + (long) seriesCollection.getDomainLowerBound(false) + "]"); LOGGER.log(" [domain.upperbound.interval.true=" + (long) seriesCollection.getDomainUpperBound(true) + "]"); LOGGER.log(" [domain.upperbound.interval.false=" + (long) seriesCollection.getDomainUpperBound(false) + "]"); LOGGER.log(" [domain.order=" + seriesCollection.getDomainOrder() + "]"); for (int intSeriesIndex = 0; intSeriesIndex < seriesCollection.getSeriesCount(); intSeriesIndex++) { final XYSeries xySeries; LOGGER.log(""); LOGGER.log(" [xyseries.index=" + intSeriesIndex + "]"); xySeries = seriesCollection.getSeries(intSeriesIndex); LOGGER.log(" [xyseries.itemcount=" + xySeries.getItemCount() + "]"); LOGGER.log(" [xyseries.key=" + xySeries.getKey() + "]"); LOGGER.log(" [xyseries.xmin=" + xySeries.getMinX() + "]"); LOGGER.log(" [xyseries.xmax=" + xySeries.getMaxX() + "]"); LOGGER.log(" [xyseries.ymin=" + xySeries.getMinY() + "]"); LOGGER.log(" [xyseries.ymax=" + xySeries.getMaxY() + "]"); LOGGER.log(" [xyseries.description=" + xySeries.getDescription() + "]"); LOGGER.log(" [xyseries.autosort=" + xySeries.getAutoSort() + "]"); LOGGER.log(" [xyseries.allowduplicatex=" + xySeries.getAllowDuplicateXValues() + "]"); // Dump the first chunk for (int intItemIndex = 0; intItemIndex < (Math.min(dumprowcount, xySeries.getItemCount())); intItemIndex++) { final XYDataItem item; item = xySeries.getDataItem(intItemIndex); LOGGER.log(" [item.index=" + intItemIndex + "] [item.x=" + item.getXValue() + "] [item.y=" + item.getYValue() + "]"); } LOGGER.log(" ..."); // Dump the last chunk for (int intItemIndex = 0; intItemIndex < (Math.min(dumprowcount, xySeries.getItemCount())); intItemIndex++) { final XYDataItem item; final int intIndex; intIndex = Math.max(0, xySeries.getItemCount() - dumprowcount) + intItemIndex; item = xySeries.getDataItem(intIndex); LOGGER.log(" [item.index=" + intIndex + "] [item.x=" + item.getXValue() + "] [item.y=" + item.getYValue() + "]"); } } } else if ((dataset != null) && (dataset instanceof TimeSeriesCollection)) { final TimeSeriesCollection seriesCollection; seriesCollection = (TimeSeriesCollection) dataset; LOGGER.log("TimeSeriesCollection"); LOGGER.log(" [series.count=" + seriesCollection.getSeriesCount() + "]"); LOGGER.log(" [domain.lowerbound.interval.true=" + (long) seriesCollection.getDomainLowerBound(true) + "]"); LOGGER.log(" [domain.lowerbound.interval.false=" + (long) seriesCollection.getDomainLowerBound(false) + "]"); LOGGER.log(" [domain.upperbound.interval.true=" + (long) seriesCollection.getDomainUpperBound(true) + "]"); LOGGER.log(" [domain.upperbound.interval.false=" + (long) seriesCollection.getDomainUpperBound(false) + "]"); LOGGER.log(" [domain.order=" + seriesCollection.getDomainOrder() + "]"); for (int intSeriesIndex = 0; intSeriesIndex < seriesCollection.getSeriesCount(); intSeriesIndex++) { final TimeSeries timeSeries; LOGGER.log(""); LOGGER.log(" [timeseries.index=" + intSeriesIndex + "]"); timeSeries = seriesCollection.getSeries(intSeriesIndex); LOGGER.log(" [timeseries.itemcount=" + timeSeries.getItemCount() + "]"); LOGGER.log(" [timeseries.key=" + timeSeries.getKey() + "]"); LOGGER.log(" [timeseries.ymin=" + timeSeries.getMinY() + "]"); LOGGER.log(" [timeseries.ymax=" + timeSeries.getMaxY() + "]"); LOGGER.log(" [timeseries.domain=" + timeSeries.getDomainDescription() + "]"); LOGGER.log(" [timeseries.range=" + timeSeries.getRangeDescription() + "]"); LOGGER.log( " [timeseries.timeperiodclass=" + timeSeries.getTimePeriodClass().getName() + "]"); for (int intItemIndex = 0; intItemIndex < (Math.min(dumprowcount, timeSeries.getItemCount())); intItemIndex++) { final TimeSeriesDataItem item; item = timeSeries.getDataItem(intItemIndex); LOGGER.log(" [item.index=" + intItemIndex + "] [item.period.serialindex=" + item.getPeriod().getSerialIndex() + "] [item.period.firstmillis=" + item.getPeriod().getFirstMillisecond(calendar) + "] [item.value=" + item.getValue() + "]"); } LOGGER.log(" ..."); for (int intItemIndex = 0; intItemIndex < (Math.min(dumprowcount, timeSeries.getItemCount())); intItemIndex++) { final TimeSeriesDataItem item; final int intIndex; intIndex = Math.max(0, timeSeries.getItemCount() - dumprowcount) + intItemIndex; item = timeSeries.getDataItem(intIndex); LOGGER.log(" [item.index=" + intIndex + "] [item.period.serialindex=" + item.getPeriod().getSerialIndex() + "] [item.period.firstmillis=" + item.getPeriod().getFirstMillisecond(calendar) + "] [item.value=" + item.getValue() + "]"); } } } else { LOGGER.error(SOURCE + "Unsupported XYDataset type"); } } }
From source file:ubic.gemma.web.controller.expression.experiment.ExpressionExperimentQCController.java
/** * @param os response output stream/*from w ww .ja v a 2s. c om*/ * @param mvr MeanVarianceRelation object to plot * @return true if mvr data points were plotted */ private boolean writeMeanVariance(OutputStream os, MeanVarianceRelation mvr, Double size) throws Exception { // if number of datapoints > THRESHOLD then alpha = TRANSLUCENT, else alpha = OPAQUE final int THRESHOLD = 1000; final int TRANSLUCENT = 50; final int OPAQUE = 255; // Set maximum plot range to Y_MAX + YRANGE * OFFSET to leave some extra white space final double OFFSET_FACTOR = 0.05f; // set the final image size to be the minimum of MAX_IMAGE_SIZE_PX or size final int MAX_IMAGE_SIZE_PX = 5; if (mvr == null) { return false; } // get data points XYSeriesCollection collection = this.getMeanVariance(mvr); if (collection.getSeries().size() == 0) { return false; } ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme()); JFreeChart chart = ChartFactory.createScatterPlot("", "mean (log2)", "variance (log2)", collection, PlotOrientation.VERTICAL, false, false, false); // adjust colors and shapes XYRegressionRenderer renderer = new XYRegressionRenderer(); renderer.setBasePaint(Color.white); XYSeries series = collection.getSeries(0); int alpha = series.getItemCount() > THRESHOLD ? TRANSLUCENT : OPAQUE; renderer.setSeriesPaint(0, new Color(0, 0, 0, alpha)); renderer.setSeriesPaint(1, Color.red); renderer.setSeriesStroke(1, new BasicStroke(1)); renderer.setSeriesShape(0, new Ellipse2D.Double(4, 4, 4, 4)); renderer.setSeriesShapesFilled(0, false); renderer.setSeriesLinesVisible(0, false); renderer.setSeriesLinesVisible(1, true); renderer.setSeriesShapesVisible(1, false); XYPlot plot = chart.getXYPlot(); plot.setRenderer(renderer); plot.setRangeGridlinesVisible(false); plot.setDomainGridlinesVisible(false); // adjust the chart domain and ranges double yRange = series.getMaxY() - series.getMinY(); double xRange = series.getMaxX() - series.getMinX(); if (xRange < 0) { log.warn("Min X was greater than Max X: Max=" + series.getMaxY() + " Min= " + series.getMinY()); return false; } double ybuffer = (yRange) * OFFSET_FACTOR; double xbuffer = (xRange) * OFFSET_FACTOR; double newYMin = series.getMinY() - ybuffer; double newYMax = series.getMaxY() + ybuffer; double newXMin = series.getMinX() - xbuffer; double newXMax = series.getMaxX() + xbuffer; ValueAxis yAxis = new NumberAxis("Variance"); yAxis.setRange(newYMin, newYMax); ValueAxis xAxis = new NumberAxis("Mean"); xAxis.setRange(newXMin, newXMax); chart.getXYPlot().setRangeAxis(yAxis); chart.getXYPlot().setDomainAxis(xAxis); int finalSize = (int) Math.min( MAX_IMAGE_SIZE_PX * ExpressionExperimentQCController.DEFAULT_QC_IMAGE_SIZE_PX, size * ExpressionExperimentQCController.DEFAULT_QC_IMAGE_SIZE_PX); ChartUtilities.writeChartAsPNG(os, chart, finalSize, finalSize); return true; }
From source file:org.micromanager.saim.fit.Fitter.java
/** * Utility to facilitate fitting data plotted in JFreeChart * Provide data in JFReeChart format (XYSeries), and retrieve univariate * function parameters that best fit (using least squares) the data. All data * points will be weighted equally./*from w ww. j a va 2s. co m*/ * * Various weightmethods are implemented and can be selected using the * weightMethods parameter. * * @param data xy series in JFReeChart format * @param type one of the Fitter.FunctionType predefined functions * @param guess initial guess for the fit. The number and meaning of these parameters depends on the FunctionType. Implemented: Gaussian: 0: Normalization, 1: Mean 2: Sigma * @param weightMethod One of the methods in the WeightMethod enum * @return array with parameters, whose meaning depends on the FunctionType. * Use the function getXYSeries to retrieve the XYDataset predicted * by this fit */ public static double[] fit(XYSeries data, FunctionType type, double[] guess, WeightMethod weightMethod) { if (type == FunctionType.NoFit) { return null; } // create the commons math data object from the JFreeChart data object final WeightedObservedPoints obs = new WeightedObservedPoints(); // range is used in weigt calculations double range = data.getMaxY() - data.getMinY(); for (int i = 0; i < data.getItemCount(); i++) { // add weight based on y intensity and selected weight method double weight = 1.0; // used in Equal method if (weightMethod != WeightMethod.Equal) { double valueMinusMin = data.getY(i).doubleValue() - data.getMinY(); weight = valueMinusMin / range; switch (weightMethod) { case Equal: break; // weight is already linear case Quadratic: weight *= weight; break; case Top50Linear: if (valueMinusMin < (0.5 * range)) weight = 0.0; break; case Top80Linear: if (valueMinusMin < (0.8 * range)) weight = 0.0; break; } } obs.add(weight, data.getX(i).doubleValue(), data.getY(i).doubleValue()); } // Carry out the actual fit double[] result = null; switch (type) { case Pol1: final PolynomialCurveFitter fitter1 = PolynomialCurveFitter.create(1); result = fitter1.fit(obs.toList()); break; case Pol2: final PolynomialCurveFitter fitter2 = PolynomialCurveFitter.create(2); result = fitter2.fit(obs.toList()); break; case Pol3: final PolynomialCurveFitter fitter3 = PolynomialCurveFitter.create(3); result = fitter3.fit(obs.toList()); break; case Gaussian: GaussianWithOffsetCurveFitter gf = GaussianWithOffsetCurveFitter.create(); gf = gf.withMaxIterations(50); if (guess != null) { gf.withStartPoint(guess); } result = gf.fit(obs.toList()); } return result; }