ChartPanelShowStatistics( String title, String timeAxisLabel, String valueAxisLabel, XYDataset dataset /*, boolean legend, boolean tooltips, boolean urls*/) { this(); // chart = ChartFactory.createTimeSeriesChart(title, timeAxisLabel, valueAxisLabel, dataset); chart = ChartFactory.createScatterPlot(title, timeAxisLabel, valueAxisLabel, dataset); ChartPanel p = new ChartPanel(chart); add(p, BorderLayout.CENTER); XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) chart.getXYPlot().getRenderer(); renderer.setBaseShapesVisible(false); // renderer.setBaseShape(itemShape); // 好像不管用,必须用setSeriesShape renderer.setBaseLinesVisible(true); // renderer.setBasePaint(new Color(0)); // 好像不管用,必须用setSeriesPaint itemShape = ShapeUtilities.createDiamond((float) 3); renderer.setSeriesShape(0, itemShape); renderer.setSeriesPaint(0, new Color(255, 0, 0)); renderer.setSeriesShape(1, itemShape); renderer.setSeriesPaint(1, new Color(0, 255, 0)); renderer.setBaseToolTipGenerator( new StandardXYToolTipGenerator( "{0}:({1} , {2})", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), new DecimalFormat("#.00"))); }
private static void configureXYLineAndShapeRenderer( XYLineAndShapeRenderer renderer, ValueSource valueSource, PlotInstance plotInstance) { renderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator()); SeriesFormat seriesFormat = valueSource.getSeriesFormat(); DimensionConfig domainConfig = valueSource.getDomainConfig(); DimensionConfig colorDimensionConfig = plotInstance.getCurrentPlotConfigurationClone().getDimensionConfig(PlotDimension.COLOR); DimensionConfig shapeDimensionConfig = plotInstance.getCurrentPlotConfigurationClone().getDimensionConfig(PlotDimension.SHAPE); ValueSourceData valueSourceData = plotInstance.getPlotData().getValueSourceData(valueSource); int seriesCount = valueSourceData.getSeriesDataForAllGroupCells().groupCellCount(); // Loop all series and set series format. // Format based on dimension configs will be set later on in initFormatDelegate(). for (int seriesIdx = 0; seriesIdx < seriesCount; ++seriesIdx) { // configure linestyle if (seriesFormat.getLineStyle() == LineStyle.NONE) { renderer.setSeriesLinesVisible(seriesIdx, false); } else { renderer.setSeriesLinesVisible(seriesIdx, true); renderer.setSeriesStroke(seriesIdx, seriesFormat.getStroke(), false); } // configure series shape if necessary if (!SeriesFormat.calculateIndividualFormatForEachItem(domainConfig, shapeDimensionConfig)) { if (seriesFormat.getItemShape() != ItemShape.NONE) { renderer.setSeriesShapesVisible(seriesIdx, true); renderer.setSeriesShape(seriesIdx, seriesFormat.getItemShape().getShape()); } else { renderer.setSeriesShapesVisible(seriesIdx, false); } } // configure series color if necessary if (!SeriesFormat.calculateIndividualFormatForEachItem(domainConfig, colorDimensionConfig)) { Color itemColor = seriesFormat.getItemColor(); renderer.setSeriesPaint(seriesIdx, itemColor); renderer.setSeriesFillPaint(seriesIdx, itemColor); } renderer.setSeriesOutlinePaint(seriesIdx, PlotConfiguration.DEFAULT_SERIES_OUTLINE_PAINT); renderer.setUseOutlinePaint(true); } }
public static XYLineAndShapeRenderer getLineAndShapeRenderer(TimeSeriesCollection dataset) { XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(true, false); renderer.setSeriesPaint(dataset.indexOf(dataset.getSeries("ADX")), Color.BLACK); renderer.setSeriesPaint(dataset.indexOf(dataset.getSeries("PlusDI")), Color.BLUE); renderer.setSeriesPaint(dataset.indexOf(dataset.getSeries("MinusDI")), Color.RED); renderer.setSeriesStroke(0, new BasicStroke(1.5f)); renderer.setSeriesStroke(1, new BasicStroke(1.5f)); renderer.setSeriesStroke(2, new BasicStroke(1.5f)); // renderer.setBaseShape(new Rectangle(1, 1)); // renderer.setShapesVisible(true); renderer.setBaseToolTipGenerator(ChartToolTipGenerators.DatedToolTipGenerator()); // XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(); // renderer.setBaseShapesVisible(true); // renderer.setDrawOutlines(true); // renderer.setUseFillPaint(true); // renderer.setBaseFillPaint(Color.white); // renderer.setSeriesStroke(0, new BasicStroke(1.0f)); // renderer.setSeriesOutlineStroke(0, new BasicStroke(1.0f)); // renderer.setSeriesShape(0, new Ellipse2D.Double(-1.0, -1.0, 1.0, 1.0)); // renderer.setBaseToolTipGenerator(getToolTipGenerator()); return renderer; }
protected void createChart(JPanel plotPanel, String xAxisName, String yAxisName) { JFreeChart chart = ChartFactory.createScatterPlot( null, null, null, null, PlotOrientation.VERTICAL, false, true, false); chart.setBorderVisible(true); chartPanel = new ChartPanel(chart, true, true, true, true, true); chartPanel.setAutoscrolls(true); chartPanel.setMouseZoomable(false); GridBagConstraints gbl_chartPanel = new GridBagConstraints(); gbl_chartPanel.anchor = GridBagConstraints.CENTER; gbl_chartPanel.insets = new Insets(3, 3, 3, 3); gbl_chartPanel.weightx = 1.0; gbl_chartPanel.weighty = 1.0; gbl_chartPanel.fill = GridBagConstraints.BOTH; gbl_chartPanel.gridx = 0; gbl_chartPanel.gridy = 1; plotPanel.add(chartPanel, gbl_chartPanel); XYLineAndShapeRenderer lineRenderer = new XYLineAndShapeRenderer(); lineRenderer.setUseFillPaint(true); lineRenderer.setBaseToolTipGenerator( new StandardXYToolTipGenerator( StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT, new DecimalFormat("0.00"), new DecimalFormat("0.00"))); Stroke stroke = new BasicStroke(2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 1.0f, null, 0.0f); lineRenderer.setSeriesStroke(0, stroke); lineRenderer.setSeriesPaint(0, Color.RED); lineRenderer.setSeriesShape(0, ShapeUtilities.createDiamond((float) 2.5)); lineRenderer.setLegendItemLabelGenerator( new StandardXYSeriesLabelGenerator() { private static final long serialVersionUID = 7593430826693873496L; public String generateLabel(XYDataset dataset, int series) { XYSeries xys = ((XYSeriesCollection) dataset).getSeries(series); return xys.getDescription(); } }); NumberAxis xAxis = new NumberAxis(xAxisName); xAxis.setAutoRangeIncludesZero(false); NumberAxis yAxis = new NumberAxis(yAxisName); yAxis.setAutoRangeIncludesZero(false); XYSeriesCollection lineDataset = new XYSeriesCollection(); XYPlot plot = chart.getXYPlot(); plot.setRangePannable(true); plot.setDomainPannable(true); plot.setDomainGridlinePaint(Color.DARK_GRAY); plot.setRangeGridlinePaint(Color.DARK_GRAY); plot.setBackgroundPaint(new Color(224, 224, 224)); plot.setDataset(0, lineDataset); plot.setRenderer(0, lineRenderer); plot.setDomainAxis(0, xAxis); plot.setRangeAxis(0, yAxis); plot.mapDatasetToDomainAxis(0, 0); plot.mapDatasetToRangeAxis(0, 0); LegendTitle legend = new LegendTitle(plot.getRenderer()); legend.setItemFont(new Font("Arial", 0, 10)); legend.setPosition(RectangleEdge.TOP); chart.addLegend(legend); }
/** * Plot the results produced by the testing methods in this class. * * @param results The results of one or more tests. They must all have the same type {@link TYPE}. * If more than one result is to be plotted then only {@link TYPE#STDP} or {@link * TYPE#STDP_1D} are supported, and it is assumed that they all have the same pre- and * post-synaptic spike patterns. * @param singlePlot If plotting more than one TestResult then whether to show the results on a * single plot (true) or separate plots (false). * @param timeResolution The time resolution which the simulation was run at. * @param logSpikesAndStateVariables Whether to include pre- and post-synaptic spikes and any * state variables exposed by the synapse model in the plot. This is ignored if more than one * result is to be plotted. * @param showInFrame Whether to display the result in a frame. */ public static JFreeChart createChart( TestResults[] results, boolean singlePlot, int timeResolution, boolean logSpikesAndStateVariables, boolean showInFrame, String title) { JFreeChart resultsPlot = null; int resultsCount = results.length; // Make sure they're all the same type. for (int ri = 0; ri < results.length; ri++) { if (ri < resultsCount - 1 && results[ri].getProperty("type") != results[ri + 1].getProperty("type")) { throw new IllegalArgumentException("All results must have the same type."); } if (resultsCount > 1 && results[ri].getProperty("type") != TYPE.STDP && results[ri].getProperty("type") != TYPE.STDP_1D) { throw new IllegalArgumentException( "Multiple results can only be plotted for types STDP or STDP_1D."); } } TYPE type = (TYPE) results[0].getProperty("type"); XYLineAndShapeRenderer xyRenderer; XYToolTipGenerator tooltipGen = new StandardXYToolTipGenerator(); if (type == TYPE.STDP) { CombinedDomainXYPlot combinedPlot = new CombinedDomainXYPlot(new NumberAxis("t (s)")); if (singlePlot) { // Plot all result sets together. DefaultXYDataset efficacyData = new DefaultXYDataset(); for (TestResults result : results) { String efficacyLabel = (resultsCount == 1) ? "Efficacy" : "" + result.getProperty("label"); efficacyData.addSeries(efficacyLabel, result.getResult("Time", "Efficacy")); } xyRenderer = new XYLineAndShapeRenderer(true, false); xyRenderer.setBaseToolTipGenerator(tooltipGen); combinedPlot.add(new XYPlot(efficacyData, null, new NumberAxis("Efficacy"), xyRenderer), 4); } else { // Plot each result set separately. for (TestResults result : results) { DefaultXYDataset efficacyData = new DefaultXYDataset(); String efficacyLabel = (resultsCount == 1) ? "Efficacy" : "" + result.getProperty("label"); efficacyData.addSeries(efficacyLabel, result.getResult("Time", "Efficacy")); xyRenderer = new XYLineAndShapeRenderer(true, false); xyRenderer.setBaseToolTipGenerator(tooltipGen); combinedPlot.add( new XYPlot(efficacyData, null, new NumberAxis("Efficacy"), xyRenderer), 4); } } // Don't plot trace data for multiple tests. if (resultsCount == 1 && logSpikesAndStateVariables) { DefaultXYDataset traceData = new DefaultXYDataset(); for (String label : results[0].getResultLabels()) { if (!label.startsWith("Time") && !label.startsWith("Efficacy") && !label.equals("Pre-synaptic spikes") && !label.equals("Post-synaptic spikes")) { traceData.addSeries(label, results[0].getResult("Time", label)); } } xyRenderer = new XYLineAndShapeRenderer(true, false); xyRenderer.setBaseToolTipGenerator(tooltipGen); combinedPlot.add(new XYPlot(traceData, null, new NumberAxis("State"), xyRenderer), 3); DefaultXYDataset spikeData = new DefaultXYDataset(); spikeData.addSeries( "Pre-synaptic spikes", results[0].getResult("Time", "Pre-synaptic spikes")); spikeData.addSeries( "Post-synaptic spikes", results[0].getResult("Time", "Post-synaptic spikes")); xyRenderer = new XYLineAndShapeRenderer(true, false); xyRenderer.setBaseToolTipGenerator(tooltipGen); combinedPlot.add( new XYPlot(spikeData, null, new NumberAxis("Pre/post potential"), xyRenderer), 3); } resultsPlot = new JFreeChart(title, null, combinedPlot, true); resultsPlot.setBackgroundPaint(Color.WHITE); resultsPlot.getPlot().setBackgroundPaint(Color.WHITE); ((XYPlot) resultsPlot.getPlot()).setRangeGridlinePaint(Color.LIGHT_GRAY); ((XYPlot) resultsPlot.getPlot()).setDomainGridlinePaint(Color.LIGHT_GRAY); } else if (type == TYPE.STDP_1D) { DecimalFormat timeFormatter = new DecimalFormat(); timeFormatter.setMultiplier(1000); NumberAxis domainAxis = new NumberAxis("\u0394t (ms)"); domainAxis.setNumberFormatOverride(timeFormatter); CombinedDomainXYPlot combinedPlot = new CombinedDomainXYPlot(domainAxis); if (singlePlot) { // Plot all result sets together. DefaultXYDataset efficacyData = new DefaultXYDataset(); for (TestResults result : results) { String efficacyLabel = (resultsCount == 1) ? "Efficacy" : "" + result.getProperty("label"); efficacyData.addSeries(efficacyLabel, result.getResult("Time delta", "Efficacy")); } xyRenderer = new XYLineAndShapeRenderer(true, false); xyRenderer.setBaseToolTipGenerator(tooltipGen); combinedPlot.add(new XYPlot(efficacyData, null, new NumberAxis("Efficacy"), xyRenderer), 4); } else { // Plot each result set separately. for (TestResults result : results) { DefaultXYDataset efficacyData = new DefaultXYDataset(); String efficacyLabel = (resultsCount == 1) ? "Efficacy" : "" + result.getProperty("label"); efficacyData.addSeries(efficacyLabel, result.getResult("Time delta", "Efficacy")); xyRenderer = new XYLineAndShapeRenderer(true, false); xyRenderer.setBaseToolTipGenerator(tooltipGen); combinedPlot.add( new XYPlot(efficacyData, null, new NumberAxis("Efficacy"), xyRenderer), 4); } } resultsPlot = new JFreeChart(title, null, combinedPlot, true); resultsPlot.setBackgroundPaint(Color.WHITE); resultsPlot.getPlot().setBackgroundPaint(Color.WHITE); ((XYPlot) resultsPlot.getPlot()).setRangeGridlinePaint(Color.LIGHT_GRAY); ((XYPlot) resultsPlot.getPlot()).setDomainGridlinePaint(Color.LIGHT_GRAY); } else if (type == TYPE.STDP_2D) { double[][] data = results[0].getResult("Time delta 1", "Time delta 2", "Efficacy"); DefaultXYZDataset plotData = new DefaultXYZDataset(); plotData.addSeries("Efficacy", data); // Set up paint scale, and convert domain axes from seconds to // milliseconds (XYBlockRenderer won't deal with fractional values // in the domain axes) double min = Double.MAX_VALUE, max = -min; double[] efficacy = data[2]; for (int i = 0; i < data[0].length; i++) { if (efficacy[i] < min) min = efficacy[i]; if (efficacy[i] > max) max = efficacy[i]; data[0][i] = Math.round(data[0][i] * 1000); data[1][i] = Math.round(data[1][i] * 1000); } XYBlockRenderer renderer = new XYBlockRenderer(); double range = Math.max(Math.abs(min), Math.abs(max)); double rangeBase = 0; if (min < 0) min = -range; if (max > 0) max = range; // If the value range does not cross the zero point, don't use a zero-based range. if ((min > 0) || (max < 0)) { range = Math.abs(max - min); rangeBase = Math.min(Math.abs(min), Math.abs(max)); } if (min >= max) { max = min + Double.MIN_VALUE * 10; } LookupPaintScale scale = new LookupPaintScale(min, max, Color.WHITE); if (min < 0) { for (int ci = 0; ci <= 255; ci++) { double v = -(ci / 255.0) * range - rangeBase; scale.add(v, new Color(0, ci, ci)); } } if (max > 0) { for (int ci = 0; ci <= 255; ci++) { double v = (ci / 255.0) * range + rangeBase; scale.add(v, new Color(ci, ci, 0)); } } renderer.setPaintScale(scale); renderer.setSeriesToolTipGenerator(0, new StandardXYZToolTipGenerator()); int displayResolution = ((Integer) results[0].getProperty("display time resolution")).intValue(); renderer.setBlockWidth(1000.0 / displayResolution); renderer.setBlockHeight(1000.0 / displayResolution); NumberAxis xAxis = new NumberAxis("\u0394t1 (ms)"); NumberAxis yAxis = new NumberAxis("\u0394t2 (ms)"); XYPlot plot = new XYPlot(plotData, xAxis, yAxis, renderer); plot.setDomainGridlinesVisible(false); resultsPlot = new JFreeChart(title, plot); resultsPlot.removeLegend(); NumberAxis valueAxis = new NumberAxis(); valueAxis.setLowerBound(scale.getLowerBound()); valueAxis.setUpperBound(scale.getUpperBound()); PaintScaleLegend legend = new PaintScaleLegend(scale, valueAxis); legend.setPosition(RectangleEdge.RIGHT); legend.setMargin(5, 5, 5, 5); resultsPlot.addSubtitle(legend); } if (showInFrame) { JFrame plotFrame = new JFrame(title); plotFrame.add(new ChartPanel(resultsPlot)); plotFrame.setExtendedState(plotFrame.getExtendedState() | JFrame.MAXIMIZED_BOTH); plotFrame.pack(); plotFrame.setVisible(true); } return resultsPlot; }