private int addDataset(PlotState plotState, int actualAutoDatasetNr) { String datasetId = plotState.getValue(PlotConstants.DATA, null); if (datasetId == null) { if (actualAutoDatasetNr >= datasetList.size()) { actualAutoDatasetNr = 0; } plotState.setDataSet(datasetList.get(actualAutoDatasetNr++)); } else { DataSet dataset = null; if (datasetId.startsWith("#")) { String datasetNr = datasetId.substring(1); for (DataSet tempDataset : datasetList) { if (datasetNr.equals(String.valueOf(tempDataset.getNr()))) { dataset = tempDataset; } } } else { for (DataSet tempDataset : datasetList) { if (datasetId.equals(tempDataset.getId())) { dataset = tempDataset; } } } if (dataset != null) { plotState.setDataSet(dataset); } else { throw new ParserException( PlotConstants.DATA, datasetId, plotState.getLine(PlotConstants.DATA)); } } return actualAutoDatasetNr; }
/** Is called after parsing everything to fill datasets in each plotState Object */ private void addDatasetsToPlotStates() { if (datasetList.isEmpty()) { throw new ParserException("You must specify at least one dataset."); } int actualAutoDatasetNr = 0; for (PlotState plotState : parserResult.getPlotStateList()) { actualAutoDatasetNr = addDataset(plotState, actualAutoDatasetNr); // also add datasets to subplots for (PlotState subPlotState : plotState.getSubplots()) { log.info("Add dataset for subplot"); actualAutoDatasetNr = addDataset(subPlotState, actualAutoDatasetNr); } } }
public ParserResult parse(String source) { List<String> inputList = Arrays.asList(source.split("\n", -1)); ListIterator<String> inputIterator = inputList.listIterator(); while (inputIterator.hasNext()) { String line = inputIterator.next(); if (line.isEmpty() || line.matches(PlotConstants.REGEX_COMMENT)) { /* ignore empty lines and comments */ } else if (line.matches(PlotConstants.REGEX_PLOT)) { parserResult.addPlotState(createPlotStateObject(line.split(" "), inputIterator)); } else if (line.matches(PlotConstants.REGEX_PLOT_ADD)) { List<PlotState> plotStates = parserResult.getPlotStateList(); if (plotStates.isEmpty()) { // if no plotStates, create a new one parserResult.addPlotState(createPlotStateObject(line.split(" "), inputIterator)); } else { // if plots exist, add new plotState to last plotState PlotState last = plotStates.get(plotStates.size() - 1); last.addSubPlot(createPlotStateObject(line.split(" "), inputIterator)); } } else if (line.matches(PlotConstants.REGEX_DATA)) { createDatasetObject(line.split(" "), inputIterator); } else if (line.matches(PlotConstants.REGEX_DATA_GUESS)) { inputIterator .previous(); // Must go 1 step back to avoid skipping the first line in // createDatasetObject createDatasetObject(new String[] {PlotConstants.DATA}, inputIterator); } else if (line.matches(PlotConstants.REGEX_VALUE_ASSIGNMENT)) { createKeyValueAssignment(line, inputIterator.nextIndex()); } else { throw new ParserException( "Invalid line: " + line + "(line: " + inputIterator.nextIndex() + ")"); } } analyseDatasets(); addDatasetsToPlotStates(); return parserResult; }
/** * Draws the plot on a Java 2D graphics device (such as the screen or a * printer). Will perform all the placement calculations for each * sub-plots and then tell these to draw themselves. * * @param g2 the graphics device. * @param area the area within which the plot (including axis labels) * should be drawn. * @param anchor the anchor point (<code>null</code> permitted). * @param parentState the parent state. * @param info collects information about the drawing (<code>null</code> * permitted). */ public void draw(Graphics2D g2, Rectangle2D area, Point2D anchor, PlotState parentState, PlotRenderingInfo info) { // set up info collection... if (info != null) { info.setPlotArea(area); } // adjust the drawing area for plot insets (if any)... RectangleInsets insets = getInsets(); insets.trim(area); // calculate the data area... AxisSpace space = calculateAxisSpace(g2, area); Rectangle2D dataArea = space.shrink(area, null); // set the width and height of non-shared axis of all sub-plots setFixedDomainAxisSpaceForSubplots(space); // draw the shared axis ValueAxis axis = getRangeAxis(); RectangleEdge rangeEdge = getRangeAxisEdge(); double cursor = RectangleEdge.coordinate(dataArea, rangeEdge); AxisState state = axis.draw(g2, cursor, area, dataArea, rangeEdge, info); if (parentState == null) { parentState = new PlotState(); } parentState.getSharedAxisStates().put(axis, state); // draw all the charts for (int i = 0; i < this.subplots.size(); i++) { CategoryPlot plot = (CategoryPlot) this.subplots.get(i); PlotRenderingInfo subplotInfo = null; if (info != null) { subplotInfo = new PlotRenderingInfo(info.getOwner()); info.addSubplotInfo(subplotInfo); } Point2D subAnchor = null; if (anchor != null && this.subplotArea[i].contains(anchor)) { subAnchor = anchor; } plot.draw(g2, this.subplotArea[i], subAnchor, parentState, subplotInfo); } if (info != null) { info.setDataArea(dataArea); } }