private XYItemRenderer createScalabilityPlotRenderer(NumberFormat numberFormat) { XYItemRenderer renderer = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES_AND_LINES); // Use dashed line renderer.setSeriesStroke( 0, new BasicStroke( 1.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 1.0f, new float[] {2.0f, 6.0f}, 0.0f)); return renderer; }
public static JFreeChart createXYLinePredictionChart(PredictionCenter center) { StatisticalPackageRunner runner = DBSeerGUI.runner; String title = runner.getVariableString("title"); Object[] legends = (Object[]) runner.getVariableCell("legends"); Object[] xCellArray = (Object[]) runner.getVariableCell("Xdata"); Object[] yCellArray = (Object[]) runner.getVariableCell("Ydata"); String xLabel = runner.getVariableString("Xlabel"); String yLabel = runner.getVariableString("Ylabel"); XYSeriesCollection dataSet = new XYSeriesCollection(); int numLegends = legends.length; int numXCellArray = xCellArray.length; int numYCellArray = yCellArray.length; int dataCount = 0; if (numXCellArray != numYCellArray) { JOptionPane.showMessageDialog( null, "The number of X dataset and Y dataset does not match.", "The number of X dataset and Y dataset does not match.", JOptionPane.ERROR_MESSAGE); System.out.println(numXCellArray + " : " + numYCellArray); return null; } final java.util.List<String> transactionNames = center.getTrainConfig().getDataset(0).getTransactionTypeNames(); for (int i = 0; i < numLegends; ++i) { String legend = (String) legends[i]; for (int j = 0; j < transactionNames.size(); ++j) { if (legend.contains("Type " + (j + 1))) { legends[i] = legend.replace("Type " + (j + 1), transactionNames.get(j)); break; } } } for (int j = 0; j < transactionNames.size(); ++j) { if (xLabel.contains("Type " + (j + 1))) { xLabel = xLabel.replace("Type " + (j + 1), transactionNames.get(j)); break; } } for (int j = 0; j < transactionNames.size(); ++j) { if (yLabel.contains("Type " + (j + 1))) { yLabel = yLabel.replace("Type " + (j + 1), transactionNames.get(j)); break; } } for (int i = 0; i < numYCellArray; ++i) { double[] xArray = (double[]) xCellArray[i]; runner.eval("yArraySize = size(Ydata{" + (i + 1) + "});"); runner.eval("yArray = Ydata{" + (i + 1) + "};"); double[] yArraySize = runner.getVariableDouble("yArraySize"); double[] yArray = runner.getVariableDouble("yArray"); int xLength = xArray.length; int row = (int) yArraySize[0]; int col = (int) yArraySize[1]; for (int c = 0; c < col; ++c) { XYSeries series; int legendIdx = (dataCount >= numLegends) ? numLegends - 1 : dataCount; String legend = (String) legends[legendIdx]; if (numLegends == 0) { series = new XYSeries("Data " + dataCount + 1); } else if (dataCount >= numLegends) { series = new XYSeries(legend + (dataCount + 1)); } else { series = new XYSeries(legend); } for (int r = 0; r < row; ++r) { int xRow = (r >= xLength) ? xLength - 1 : r; double yValue = yArray[r + c * row]; // remove negatives & NaN & infs. if (yValue < 0 || yValue == Double.NaN || yValue == Double.POSITIVE_INFINITY || yValue == Double.NEGATIVE_INFINITY) { yValue = 0.0; } series.add(xArray[xRow], yValue); } dataSet.addSeries(series); ++dataCount; } } JFreeChart chart = ChartFactory.createXYLineChart(title, xLabel, yLabel, dataSet); // change 'predicted' data to have dotted lines. BasicStroke dashStroke = toStroke(STYLE_DASH); BasicStroke dotStroke = toStroke(STYLE_DOT); BasicStroke lineStroke = toStroke(STYLE_LINE); for (int i = 0; i < dataSet.getSeriesCount(); ++i) { String legend = (String) dataSet.getSeriesKey(i); XYPlot plot = chart.getXYPlot(); XYItemRenderer renderer = plot.getRenderer(); if (legend.contains("predicted") || legend.contains("Predicted")) { renderer.setSeriesStroke(i, dotStroke); } else { renderer.setSeriesStroke(i, lineStroke); } } return chart; }
private BufferedImage generarGraficoLineal( String titulo, String ejeHorizontal, String ejeVertical, double[] valores, String[] funciones, boolean porcentaje, boolean leyenda, int anchoImagen, int altoImagen) { XYSeries frecObservadas = new XYSeries("Cantidades de resoluciones por calificación"); // Calculamos las frecuencias por intervalo for (int i = 0; i < valores.length; i++) { frecObservadas.add(i, valores[i]); } XYDataset dataset = new XYSeriesCollection(frecObservadas); // Creamos el gráfico JFreeChart chart = ChartFactory.createXYLineChart( titulo, ejeHorizontal, ejeVertical, null, PlotOrientation.VERTICAL, leyenda, false, true); // Seteamos color de título chart.setBackgroundPaint(null); chart.setBorderVisible(false); chart.getTitle().setPaint(LookAndFeelEntropy.COLOR_FUENTE_TITULO_PANEL); chart.getTitle().setFont(LookAndFeelEntropy.FUENTE_TITULO_GRANDE); if (leyenda) { chart.getLegend().setItemFont(LookAndFeelEntropy.FUENTE_REGULAR); chart.getLegend().setBackgroundPaint(LookAndFeelEntropy.COLOR_TABLA_PRIMARIO); } // Seteamos datasets XYPlot xyplot = chart.getXYPlot(); xyplot.setDataset(1, dataset); xyplot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD); // Seteamos características de la función lineal XYItemRenderer xyLineRendererObs = new XYLineAndShapeRenderer(); xyLineRendererObs.setSeriesShape(0, new Line2D.Double(0.0, 0.0, 0.0, 0.0)); xyLineRendererObs.setSeriesStroke(0, new BasicStroke(2.5f)); xyLineRendererObs.setBasePaint(generarColorAleatorio(Color.orange)); xyplot.setRenderer(1, xyLineRendererObs); // Seteamos color de labels de ejes ValueAxis axisX = new SymbolAxis(ejeHorizontal, funciones); axisX.setVerticalTickLabels(true); xyplot.setDomainAxis(axisX); xyplot.getDomainAxis().setTickLabelPaint(Color.BLACK); xyplot.getDomainAxis().setLabelPaint(Color.BLACK); xyplot.getDomainAxis().setTickLabelFont(LookAndFeelEntropy.FUENTE_REGULAR); xyplot.getRangeAxis().setTickLabelPaint(Color.BLACK); xyplot.getRangeAxis().setLabelPaint(Color.BLACK); xyplot.getRangeAxis().setTickLabelFont(LookAndFeelEntropy.FUENTE_REGULAR); // Seteamos porcentaje final NumberAxis rangeAxis = (NumberAxis) xyplot.getRangeAxis(); if (porcentaje) { rangeAxis.setNumberFormatOverride(NumberFormat.getPercentInstance()); } else { rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); } // Seteamos transparencia de fondo Plot plot = chart.getPlot(); plot.setBackgroundPaint(null); plot.setBackgroundImageAlpha(0.0f); plot.setOutlineVisible(false); // Generamos una imagen this.lastChart = chart; return chart.createBufferedImage(anchoImagen, altoImagen); }
/** * Creates an overlaid chart. * * @return The chart. */ private static JFreeChart createCombinedChart() { // create plot ... IntervalXYDataset data1 = createDataset1(); XYItemRenderer renderer1 = new XYLineAndShapeRenderer(true, false); renderer1.setBaseToolTipGenerator( new StandardXYToolTipGenerator( StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT, new SimpleDateFormat("d-MMM-yyyy"), new DecimalFormat("0.00"))); renderer1.setSeriesStroke( 0, new BasicStroke(4.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL)); renderer1.setSeriesPaint(0, Color.blue); DateAxis domainAxis = new DateAxis("Year"); domainAxis.setLowerMargin(0.0); domainAxis.setUpperMargin(0.02); ValueAxis rangeAxis = new NumberAxis("$billion"); XYPlot plot1 = new XYPlot(data1, null, rangeAxis, renderer1); plot1.setBackgroundPaint(Color.lightGray); plot1.setDomainGridlinePaint(Color.white); plot1.setRangeGridlinePaint(Color.white); // add a second dataset and renderer... IntervalXYDataset data2 = createDataset2(); XYBarRenderer renderer2 = new XYBarRenderer() { public Paint getItemPaint(int series, int item) { XYDataset dataset = getPlot().getDataset(); if (dataset.getYValue(series, item) >= 0.0) { return Color.red; } else { return Color.green; } } }; renderer2.setSeriesPaint(0, Color.red); renderer2.setDrawBarOutline(false); renderer2.setBaseToolTipGenerator( new StandardXYToolTipGenerator( StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT, new SimpleDateFormat("d-MMM-yyyy"), new DecimalFormat("0.00"))); XYPlot plot2 = new XYPlot(data2, null, new NumberAxis("$billion"), renderer2); plot2.setBackgroundPaint(Color.lightGray); plot2.setDomainGridlinePaint(Color.white); plot2.setRangeGridlinePaint(Color.white); CombinedDomainXYPlot cplot = new CombinedDomainXYPlot(domainAxis); cplot.add(plot1, 3); cplot.add(plot2, 2); cplot.setGap(8.0); cplot.setDomainGridlinePaint(Color.white); cplot.setDomainGridlinesVisible(true); cplot.setDomainPannable(true); // return a new chart containing the overlaid plot... JFreeChart chart = new JFreeChart("United States Public Debt", JFreeChart.DEFAULT_TITLE_FONT, cplot, false); TextTitle source = new TextTitle( "Source: http://www.publicdebt.treas.gov/opd/opdhisms.htm", new Font("Dialog", Font.PLAIN, 10)); source.setPosition(RectangleEdge.BOTTOM); source.setHorizontalAlignment(HorizontalAlignment.RIGHT); chart.addSubtitle(source); LegendTitle legend = new LegendTitle(cplot); chart.addSubtitle(legend); ChartUtilities.applyCurrentTheme(chart); renderer2.setBarPainter(new StandardXYBarPainter()); renderer2.setShadowVisible(false); return chart; }
static JFreeChart createChart( NavigableMap<TimeAxisKey, DiffStat> aggregatedDiffstats, DiffStatConfiguration configuration) { boolean legend = false; boolean tooltips = false; boolean urls = false; Font helvetica = new Font("Helvetica", Font.PLAIN, 11 * configuration.multiplierInt()); XYDatasetMinMax datasetMinMax = createDeltaDataset("Additions and Delections", aggregatedDiffstats); XYDataset dataset = datasetMinMax.dataset; JFreeChart chart = ChartFactory.createTimeSeriesChart("", "", "", dataset, legend, tooltips, urls); chart.setBackgroundPaint(WHITE); chart.setBorderVisible(false); float strokeWidth = 1.2f * configuration.multiplierFloat(); XYPlot plot = chart.getXYPlot(); plot.setOrientation(VERTICAL); plot.setBackgroundPaint(WHITE); plot.setDomainGridlinesVisible(true); plot.setDomainGridlinePaint(AXIS_LINE_COLOR); plot.setDomainGridlineStroke(new BasicStroke(1.0f * configuration.multiplierFloat())); plot.setRangeGridlinesVisible(false); plot.setOutlineVisible(false); DateAxis dateAxis = (DateAxis) plot.getDomainAxis(); dateAxis.setDateFormatOverride(new SimpleDateFormat("MM/yy")); dateAxis.setTickLabelFont(helvetica); dateAxis.setAxisLineVisible(false); dateAxis.setTickUnit(computeDateTickUnit(aggregatedDiffstats)); RectangleInsets insets = new RectangleInsets( 8.0d * configuration.multiplierDouble(), 4.0d * configuration.multiplierDouble(), 4.0d * configuration.multiplierDouble(), 4.0d * configuration.multiplierDouble()); dateAxis.setTickLabelInsets(insets); NumberAxis additionDeletionAxis = (NumberAxis) plot.getRangeAxis(0); additionDeletionAxis.setAxisLineVisible(false); additionDeletionAxis.setLabel("Additions and Deletions"); additionDeletionAxis.setLabelFont(helvetica); additionDeletionAxis.setTickLabelFont(helvetica); additionDeletionAxis.setRangeType(RangeType.FULL); int lowerBound = datasetMinMax.min + (int) (datasetMinMax.min * 0.1d); additionDeletionAxis.setLowerBound(lowerBound); int upperBound = datasetMinMax.max + (int) (datasetMinMax.max * 0.1d); additionDeletionAxis.setUpperBound(upperBound); additionDeletionAxis.setNumberFormatOverride(new AbbreviatingNumberFormat()); additionDeletionAxis.setMinorTickMarksVisible(false); additionDeletionAxis.setTickMarkInsideLength(5.0f * configuration.multiplierFloat()); additionDeletionAxis.setTickMarkOutsideLength(0.0f); additionDeletionAxis.setTickMarkStroke(new BasicStroke(2.0f * configuration.multiplierFloat())); additionDeletionAxis.setTickUnit( new NumberTickUnit(computeTickUnitSize(datasetMinMax.max + abs(datasetMinMax.min)))); XYAreaRenderer areaRenderer = new XYAreaRenderer(XYAreaRenderer.AREA); areaRenderer.setOutline(true); areaRenderer.setSeriesOutlinePaint(0, ADDED_STROKE); areaRenderer.setSeriesOutlineStroke(0, new BasicStroke(strokeWidth)); areaRenderer.setSeriesPaint(0, ADDED_FILL); areaRenderer.setSeriesOutlinePaint(1, REMOVED_STROKE); areaRenderer.setSeriesOutlineStroke(1, new BasicStroke(strokeWidth)); areaRenderer.setSeriesPaint(1, REMOVED_FILL); plot.setRenderer(0, areaRenderer); // Total Axis NumberAxis totalAxis = new NumberAxis("Total Lines"); totalAxis.setAxisLineVisible(false); totalAxis.setLabelPaint(VALUE_LABEL); totalAxis.setTickLabelPaint(TOTAL_LABEL); totalAxis.setLabelFont(helvetica); totalAxis.setTickLabelFont(helvetica); totalAxis.setNumberFormatOverride(new AbbreviatingNumberFormat()); totalAxis.setMinorTickMarksVisible(false); totalAxis.setTickMarkInsideLength(5.0f * configuration.multiplierFloat()); totalAxis.setTickMarkOutsideLength(0.0f); totalAxis.setTickMarkStroke(new BasicStroke(2.0f * configuration.multiplierFloat())); totalAxis.setTickMarkPaint(TOTAL_LABEL); plot.setRangeAxis(1, totalAxis); plot.setRangeAxisLocation(1, AxisLocation.BOTTOM_OR_RIGHT); XYDatasetAndTotal datasetAndTotal = createTotalDataset("Total Lines", aggregatedDiffstats); XYDataset totalDataSet = datasetAndTotal.dataset; plot.setDataset(1, totalDataSet); plot.mapDatasetToRangeAxis(1, 1); // XYItemRenderer totalRenderer = new XYSplineRenderer(); XYItemRenderer totalRenderer = new StandardXYItemRenderer(); totalRenderer.setSeriesPaint(0, TOTAL_FILL); totalRenderer.setSeriesStroke( 0, new BasicStroke( strokeWidth, CAP_ROUND, JOIN_ROUND, 10.0f * configuration.multiplierFloat(), new float[] { 6.0f * configuration.multiplierFloat(), 3.0f * configuration.multiplierFloat() }, 0.0f)); plot.setRenderer(1, totalRenderer); totalAxis.setTickUnit(new NumberTickUnit(computeTickUnitSize(datasetAndTotal.total))); return chart; }
/** * @param axis Axis name to wich the new series belongs * @param cs Series Coinfiguration */ @Override public final void addSeries(String axis, SimpleSeriesConfiguration cs) { for (int i = 0; i < axes.size(); i++) { if (axes.get(i).getName().equals(axis)) { String strColor; javafx.scene.paint.Color color; int indice = seriesList.size(); if (cs.getColor() == null) { color = getColor(indice); } else { color = cs.getColor(); } strColor = color.toString(); XYSeriesCollection dataset = datasetList.get(i); Series series = new Series( cs.getName(), "color: " + strColor + ";width: " + String.valueOf(cs.getLineWidth()) + ";shape: " + cs.getShapeName() + ";", i, dataset.getSeriesCount()); dataset.addSeries(series); XYItemRenderer renderer = plot.getRenderer(i); renderer.setSeriesPaint(dataset.getSeriesCount() - 1, scene2awtColor(color)); SeriesShape simb = new SeriesShape( cs.getShapeName(), javafx.scene.paint.Color.web(strColor.replace("#", "0x"))); if (cs.getLineWidth() > 0) { ((XYLineAndShapeRenderer) renderer) .setSeriesLinesVisible(dataset.getSeriesCount() - 1, true); renderer.setSeriesStroke( dataset.getSeriesCount() - 1, new BasicStroke(cs.getLineWidth())); } else { ((XYLineAndShapeRenderer) renderer) .setSeriesLinesVisible(dataset.getSeriesCount() - 1, false); } if (cs.getShapeName().equals("null")) { renderer.setSeriesShape(dataset.getSeriesCount() - 1, null); ((XYLineAndShapeRenderer) renderer) .setSeriesShapesVisible(dataset.getSeriesCount() - 1, false); } else { renderer.setSeriesShape(dataset.getSeriesCount() - 1, simb.getShapeAWT()); ((XYLineAndShapeRenderer) renderer) .setSeriesShapesVisible(dataset.getSeriesCount() - 1, true); if (cs.getShapeName().contains("empty")) { ((XYLineAndShapeRenderer) renderer) .setSeriesShapesFilled(dataset.getSeriesCount() - 1, false); } else { ((XYLineAndShapeRenderer) renderer) .setSeriesShapesFilled(dataset.getSeriesCount() - 1, true); } } if (i == 0) { plot.setRenderer(renderer); } else { plot.setRenderer(i, renderer); } seriesList.add(series); final LegendAxis le = getLegendAxis(axis); final Label label = new Label(cs.toString()); Platform.runLater( () -> { label.setStyle( "fondo: " + strChartBackgroundColor + ";-fx-background-color: fondo;-fx-text-fill: ladder(fondo, white 49%, black 50%);-fx-padding:5px;-fx-background-radius: 5;-fx-font-size: " + String.valueOf(fontSize) + "px"); }); label.setOnMouseClicked( (MouseEvent t) -> { if (t.getClickCount() == 2) { for (int i1 = 0; i1 < seriesList.size(); i1++) { if (seriesList.get(i1).getKey().toString().equals(label.getText())) { editSeries(seriesList.get(i1)); break; } } } }); label.setOnMouseExited( (MouseEvent t) -> { label.setStyle( label .getStyle() .replace("-fx-background-color: blue", "-fx-background-color: fondo")); }); label.setOnMouseEntered( (MouseEvent t) -> { label.setStyle( label .getStyle() .replace("-fx-background-color: fondo", "-fx-background-color: blue")); for (Node le1 : legendFrame.getChildren()) { if (le1 instanceof LegendAxis) { le1.setStyle("-fx-background-color:" + strBackgroundColor); ((LegendAxis) le1).selected = false; } } }); label.setStyle( "fondo: " + strChartBackgroundColor + ";-fx-text-fill: white;-fx-background-color: fondo;-fx-padding:5px;-fx-background-radius: 5;-fx-font-size: " + String.valueOf(fontSize) + "px"); le.getChildren().add(label); label.setGraphic(simb.getShapeGraphic()); break; } } }
@Override public void writeGraphFiles(BenchmarkReport benchmarkReport) { List<XYPlot> plotList = new ArrayList<XYPlot>(BenchmarkReport.CHARTED_SCORE_LEVEL_SIZE); int seriesIndex = 0; for (SingleBenchmarkResult singleBenchmarkResult : problemBenchmarkResult.getSingleBenchmarkResultList()) { List<XYSeries> seriesList = new ArrayList<XYSeries>(BenchmarkReport.CHARTED_SCORE_LEVEL_SIZE); // No direct ascending lines between 2 points, but a stepping line instead XYItemRenderer renderer = new XYStepRenderer(); if (singleBenchmarkResult.isSuccess()) { BestScoreSingleStatistic singleStatistic = (BestScoreSingleStatistic) singleBenchmarkResult.getSingleStatistic(problemStatisticType); for (BestScoreStatisticPoint point : singleStatistic.getPointList()) { long timeMillisSpent = point.getTimeMillisSpent(); double[] levelValues = ScoreUtils.extractLevelDoubles(point.getScore()); for (int i = 0; i < levelValues.length && i < BenchmarkReport.CHARTED_SCORE_LEVEL_SIZE; i++) { if (i >= seriesList.size()) { seriesList.add( new XYSeries( singleBenchmarkResult .getSolverBenchmarkResult() .getNameWithFavoriteSuffix())); } seriesList.get(i).add(timeMillisSpent, levelValues[i]); } } // TODO if startingSolution is initialized and no improvement is made, a horizontal line // should be shown // Draw a horizontal line from the last new best step to how long the solver actually ran long timeMillisSpent = singleBenchmarkResult.getTimeMillisSpent(); double[] bestScoreLevels = ScoreUtils.extractLevelDoubles(singleBenchmarkResult.getScore()); for (int i = 0; i < bestScoreLevels.length && i < BenchmarkReport.CHARTED_SCORE_LEVEL_SIZE; i++) { if (i >= seriesList.size()) { seriesList.add( new XYSeries( singleBenchmarkResult.getSolverBenchmarkResult().getNameWithFavoriteSuffix())); } seriesList.get(i).add(timeMillisSpent, bestScoreLevels[i]); } if (singleStatistic.getPointList().size() <= 1) { // Workaround for // https://sourceforge.net/tracker/?func=detail&aid=3387330&group_id=15494&atid=115494 renderer = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES_AND_LINES); } } if (singleBenchmarkResult.getSolverBenchmarkResult().isFavorite()) { // Make the favorite more obvious renderer.setSeriesStroke(0, new BasicStroke(2.0f)); } for (int i = 0; i < seriesList.size(); i++) { if (i >= plotList.size()) { plotList.add(createPlot(benchmarkReport, i)); } plotList.get(i).setDataset(seriesIndex, new XYSeriesCollection(seriesList.get(i))); plotList.get(i).setRenderer(seriesIndex, renderer); } seriesIndex++; } graphFileList = new ArrayList<File>(plotList.size()); for (int scoreLevelIndex = 0; scoreLevelIndex < plotList.size(); scoreLevelIndex++) { JFreeChart chart = new JFreeChart( problemBenchmarkResult.getName() + " best score level " + scoreLevelIndex + " statistic", JFreeChart.DEFAULT_TITLE_FONT, plotList.get(scoreLevelIndex), true); graphFileList.add( writeChartToImageFile( chart, problemBenchmarkResult.getName() + "BestScoreStatisticLevel" + scoreLevelIndex)); } }