private static JFreeChart createDistributionViewChart() { XYPlot plot = new XYPlot(); XYLineAndShapeRenderer dRenderer = new XYSplineRenderer(); dRenderer.setBaseShapesVisible(false); dRenderer.setAutoPopulateSeriesPaint(false); dRenderer.setAutoPopulateSeriesStroke(false); dRenderer.setBaseStroke(TsCharts.getStrongStroke(LinesThickness.Thin)); dRenderer.setDrawSeriesLineAsPath(true); // not sure if useful plot.setDataset(DISTRIBUTION_INDEX, Charts.emptyXYDataset()); plot.setRenderer(DISTRIBUTION_INDEX, dRenderer); XYBarRenderer hRenderer = new XYBarRenderer(); hRenderer.setShadowVisible(false); hRenderer.setDrawBarOutline(true); hRenderer.setAutoPopulateSeriesPaint(false); hRenderer.setAutoPopulateSeriesOutlinePaint(false); hRenderer.setBaseSeriesVisibleInLegend(false); plot.setDataset(HISTOGRAM_INDEX, Charts.emptyXYDataset()); plot.setRenderer(HISTOGRAM_INDEX, hRenderer); NumberAxis domainAxis = new NumberAxis(); domainAxis.setTickLabelPaint(TsCharts.CHART_TICK_LABEL_COLOR); plot.setDomainAxis(domainAxis); plot.setDomainGridlinesVisible(false); NumberAxis rangeAxis = new NumberAxis(); rangeAxis.setTickLabelPaint(TsCharts.CHART_TICK_LABEL_COLOR); rangeAxis.setTickUnit(new NumberTickUnit(0.05)); rangeAxis.setNumberFormatOverride(new DecimalFormat("0.###")); plot.setRangeAxis(rangeAxis); plot.mapDatasetToDomainAxis(0, 0); plot.mapDatasetToRangeAxis(0, 0); plot.mapDatasetToDomainAxis(1, 0); plot.mapDatasetToRangeAxis(1, 0); JFreeChart result = new JFreeChart("", JFreeChart.DEFAULT_TITLE_FONT, plot, true); result.setPadding(TsCharts.CHART_PADDING); result.getTitle().setFont(TsCharts.CHART_TITLE_FONT); result.getLegend().setFrame(BlockBorder.NONE); result.getLegend().setBackgroundPaint(null); return result; }
/** * Update background, tick and gridline colors * * @param cfg cfg[0] Background, cfg[1] Chart background, cfg[2] y cfg[3] gridline */ public void updateChartColors(String[] cfg) { strBackgroundColor = cfg[0]; for (Node le : legendFrame.getChildren()) { if (le instanceof LegendAxis) { le.setStyle("-fx-background-color:" + strBackgroundColor); ((LegendAxis) le).selected = false; } } chart.setBackgroundPaint(scene2awtColor(javafx.scene.paint.Color.web(strBackgroundColor))); chartPanel.setBorder( BorderFactory.createCompoundBorder( BorderFactory.createEmptyBorder(4, 4, 4, 4), BorderFactory.createLineBorder( scene2awtColor(javafx.scene.paint.Color.web(strBackgroundColor))))); chartPanel.setBackground(scene2awtColor(javafx.scene.paint.Color.web(strBackgroundColor))); legendFrame.setStyle("marco: " + strBackgroundColor + ";-fx-background-color: marco;"); strChartBackgroundColor = cfg[1]; ; plot.setBackgroundPaint(scene2awtColor(javafx.scene.paint.Color.web(strChartBackgroundColor))); for (Node le : legendFrame.getChildren()) { if (le instanceof LegendAxis) { le.setStyle("-fx-background-color:" + strBackgroundColor); ((LegendAxis) le).selected = false; for (Node nn : ((LegendAxis) le).getChildren()) { if (nn instanceof Label) { ((Label) nn) .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"); } } } } strGridlineColor = cfg[2]; ; plot.setDomainGridlinePaint(scene2awtColor(javafx.scene.paint.Color.web(strGridlineColor))); plot.setRangeGridlinePaint(scene2awtColor(javafx.scene.paint.Color.web(strGridlineColor))); strTickColor = cfg[3]; ; abcissaAxis.setLabelPaint(scene2awtColor(javafx.scene.paint.Color.web(strTickColor))); abcissaAxis.setTickLabelPaint(scene2awtColor(javafx.scene.paint.Color.web(strTickColor))); for (NumberAxis ejeOrdenada : AxesList) { ejeOrdenada.setLabelPaint(scene2awtColor(javafx.scene.paint.Color.web(strTickColor))); ejeOrdenada.setTickLabelPaint(scene2awtColor(javafx.scene.paint.Color.web(strTickColor))); } }
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; }
/** * Add axis * * @param name new axis name */ @Override public final void addAxis(String name) { boolean encontrado = false; for (AxisChart categoria : axes) { if (categoria.getName().equals(name)) { encontrado = true; break; } } if (!encontrado) { axes.add(new AxisChart((name))); } XYSeriesCollection dataset = new XYSeriesCollection(); NumberAxis ejeOrdenada = new NumberAxis(name); ejeOrdenada.setLabelPaint(scene2awtColor(javafx.scene.paint.Color.web(strTickColor))); ejeOrdenada.setTickLabelPaint(scene2awtColor(javafx.scene.paint.Color.web(strTickColor))); ejeOrdenada.setLabelFont(ejeOrdenada.getLabelFont().deriveFont(fontSize)); ejeOrdenada.setTickLabelFont(ejeOrdenada.getLabelFont().deriveFont(fontSize)); int i = datasetList.size(); datasetList.add(dataset); AxesList.add(ejeOrdenada); plot.setDataset(i, dataset); plot.setRangeAxis(i, ejeOrdenada); plot.setRangeAxisLocation(i, AxisLocation.BOTTOM_OR_LEFT); plot.mapDatasetToRangeAxis(i, i); XYItemRenderer renderer = new XYLineAndShapeRenderer(true, true); if (i == 0) { plot.setRenderer(renderer); } else { plot.setRenderer(i, renderer); } final LegendAxis le; final int indiceLeyenda = legendFrame.getChildren().size(); legendFrame.getChildren().add(le = new LegendAxis(name)); le.setOnMouseClicked( (MouseEvent t) -> { if (le.selected && t.getClickCount() == 2) { setOrdinateRange(AxesList.get(indiceLeyenda)); } }); le.setOnMouseEntered( (MouseEvent t) -> { le.setStyle("-fx-background-color:blue"); le.selected = true; AxesList.get(indiceLeyenda) .setLabelPaint(scene2awtColor(javafx.scene.paint.Color.web("blue"))); AxesList.get(indiceLeyenda) .setTickLabelPaint(scene2awtColor(javafx.scene.paint.Color.web("blue"))); }); le.setOnMouseExited( (MouseEvent t) -> { le.setStyle("-fx-background-color:" + strBackgroundColor); le.selected = false; AxesList.get(indiceLeyenda) .setLabelPaint(scene2awtColor(javafx.scene.paint.Color.web(strTickColor))); AxesList.get(indiceLeyenda) .setTickLabelPaint(scene2awtColor(javafx.scene.paint.Color.web(strTickColor))); }); }
package com.xuyuan.chart.jfreechart.helloworld;
/** * Generates a chart in PNG format. * * @param outputFile the output file, it should exist. * @param pxWidth the image width in pixels. * @param pxHeight the image height in pixels. * @param chartTitle the chart title, may be null. * @param xAxisTitle the x axis title * @param yDataSeriesRange the axis range (null for auto) * @param yDataSeriesTitles the Y axis titles. * @param timeValuesInSeconds the time values in seconds * @param yDataSeries the Y axis value series. * @param yDataSeriesColors the Y axis value series drawing colors. * @param yDataSeriesTickSuffix TODO explain argument yDataSeriesTickSuffix * @param drawBorder draw, or not, the border. * @param backgroundColor the chart background color. */ final void generatePngChart( File outputFile, int pxWidth, int pxHeight, String chartTitle, String xAxisTitle, String[] yDataSeriesTitles, double[] timeValuesInSeconds, double[][] yDataSeriesRange, double[][] yDataSeries, Color[] yDataSeriesColors, String[] yDataSeriesTickSuffix, boolean drawBorder, Color backgroundColor) { // Domain axis NumberAxis xAxis = new NumberAxis(xAxisTitle); xAxis.setFixedDimension(CHART_AXIS_DIMENSION); xAxis.setLabelPaint(Color.black); xAxis.setTickLabelPaint(Color.black); double maxSeconds = getMaxValue(timeValuesInSeconds); TimeAxisResolution xAxisRes = TimeAxisResolution.findTimeUnit(maxSeconds); xAxis.setTickUnit(new NumberTickUnit(xAxisRes.tickStep)); double[] scaledTimeValues = xAxisRes.scale(timeValuesInSeconds); String tickSymbol = I18N.getString(locale, "running.job.details.chart.timeunit.symbol." + xAxisRes.name()); xAxis.setNumberFormatOverride(new DecimalFormat("###.##'" + tickSymbol + "'")); // First dataset String firstDataSetTitle = yDataSeriesTitles[0]; XYDataset firstDataSet = createXYDataSet(firstDataSetTitle, scaledTimeValues, yDataSeries[0]); Color firstDataSetColor = yDataSeriesColors[0]; // First range axis NumberAxis firstYAxis = new NumberAxis(firstDataSetTitle); firstYAxis.setFixedDimension(CHART_AXIS_DIMENSION); setAxisRange(firstYAxis, yDataSeriesRange[0]); firstYAxis.setLabelPaint(firstDataSetColor); firstYAxis.setTickLabelPaint(firstDataSetColor); String firstAxisTickSuffix = yDataSeriesTickSuffix[0]; if (firstAxisTickSuffix != null && !firstAxisTickSuffix.isEmpty()) { firstYAxis.setNumberFormatOverride(new DecimalFormat("###.##'" + firstAxisTickSuffix + "'")); } // Create the plot with domain axis and first range axis XYPlot plot = new XYPlot(firstDataSet, xAxis, firstYAxis, null); XYLineAndShapeRenderer firstRenderer = new XYLineAndShapeRenderer(true, false); plot.setRenderer(firstRenderer); plot.setOrientation(PlotOrientation.VERTICAL); plot.setBackgroundPaint(Color.lightGray); plot.setDomainGridlinePaint(Color.white); plot.setRangeGridlinePaint(Color.white); plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0)); firstRenderer.setSeriesPaint(0, firstDataSetColor); // Now iterate on next axes for (int i = 1; i < yDataSeries.length; i++) { // Create axis String seriesTitle = yDataSeriesTitles[i]; Color seriesColor = yDataSeriesColors[i]; NumberAxis yAxis = new NumberAxis(seriesTitle); yAxis.setFixedDimension(CHART_AXIS_DIMENSION); setAxisRange(yAxis, yDataSeriesRange[i]); yAxis.setLabelPaint(seriesColor); yAxis.setTickLabelPaint(seriesColor); String yAxisTickSuffix = yDataSeriesTickSuffix[i]; if (yAxisTickSuffix != null && !yAxisTickSuffix.isEmpty()) { yAxis.setNumberFormatOverride(new DecimalFormat("###.##'" + yAxisTickSuffix + "'")); } // Create dataset and add axis to plot plot.setRangeAxis(i, yAxis); plot.setRangeAxisLocation(i, AxisLocation.BOTTOM_OR_LEFT); plot.setDataset(i, createXYDataSet(seriesTitle, scaledTimeValues, yDataSeries[i])); plot.mapDatasetToRangeAxis(i, i); XYItemRenderer renderer = new StandardXYItemRenderer(); renderer.setSeriesPaint(0, seriesColor); plot.setRenderer(i, renderer); } // Create the chart JFreeChart chart = new JFreeChart(chartTitle, JFreeChart.DEFAULT_TITLE_FONT, plot, false); // Customize rendering chart.setBackgroundPaint(Color.white); chart.setBorderVisible(true); chart.setBorderPaint(Color.BLACK); // Render image try { ChartUtilities.saveChartAsPNG(outputFile, chart, pxWidth, pxHeight); } catch (IOException e) { LOG.error("Chart export failed", e); } }
private JFreeChart display( String title, int height, int width, String category, List<Report> toolResults) { JFrame f = new JFrame(title); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // averages ArrayList<Double> averageFalseRates = new ArrayList<Double>(); ArrayList<Double> averageTrueRates = new ArrayList<Double>(); double averageFalseRate = 0; double averageTrueRate = 0; XYSeriesCollection dataset = new XYSeriesCollection(); XYSeries series = new XYSeries("Scores"); for (int i = 0; i < toolResults.size(); i++) { Report toolReport = toolResults.get(i); OverallResult overallResults = toolReport.getOverallResults().getResults(category); series.add( overallResults.getFalsePositiveRate() * 100, overallResults.getTruePositiveRate() * 100); if (toolReport.isCommercial()) { averageFalseRates.add(overallResults.getFalsePositiveRate()); averageTrueRates.add(overallResults.getTruePositiveRate()); } } for (double d : averageFalseRates) { averageFalseRate += d; } averageFalseRate = averageFalseRate / averageFalseRates.size(); for (double d : averageTrueRates) { averageTrueRate += d; } averageTrueRate = averageTrueRate / averageTrueRates.size(); series.add(averageFalseRate * 100, averageTrueRate * 100); dataset.addSeries(series); afr = averageFalseRate; atr = averageTrueRate; chart = ChartFactory.createScatterPlot( title, "False Positive Rate", "True Positive Rate", dataset, PlotOrientation.VERTICAL, true, true, false); String fontName = "Arial"; DecimalFormat pctFormat = new DecimalFormat("0'%'"); theme = (StandardChartTheme) org.jfree.chart.StandardChartTheme.createJFreeTheme(); theme.setExtraLargeFont(new Font(fontName, Font.PLAIN, 24)); // title theme.setLargeFont(new Font(fontName, Font.PLAIN, 20)); // axis-title theme.setRegularFont(new Font(fontName, Font.PLAIN, 16)); theme.setSmallFont(new Font(fontName, Font.PLAIN, 12)); theme.setRangeGridlinePaint(Color.decode("#C0C0C0")); theme.setPlotBackgroundPaint(Color.white); theme.setChartBackgroundPaint(Color.white); theme.setGridBandPaint(Color.red); theme.setAxisOffset(new RectangleInsets(0, 0, 0, 0)); theme.setBarPainter(new StandardBarPainter()); theme.setAxisLabelPaint(Color.decode("#666666")); theme.apply(chart); XYPlot xyplot = chart.getXYPlot(); NumberAxis rangeAxis = (NumberAxis) xyplot.getRangeAxis(); NumberAxis domainAxis = (NumberAxis) xyplot.getDomainAxis(); xyplot.setOutlineVisible(true); rangeAxis.setRange(-5, 109.99); rangeAxis.setNumberFormatOverride(pctFormat); rangeAxis.setTickLabelPaint(Color.decode("#666666")); rangeAxis.setMinorTickCount(5); rangeAxis.setTickUnit(new NumberTickUnit(10)); rangeAxis.setAxisLineVisible(true); rangeAxis.setMinorTickMarksVisible(true); rangeAxis.setTickMarksVisible(true); rangeAxis.setLowerMargin(10); rangeAxis.setUpperMargin(10); xyplot.setRangeGridlineStroke(new BasicStroke()); xyplot.setRangeGridlinePaint(Color.lightGray); xyplot.setRangeMinorGridlinePaint(Color.decode("#DDDDDD")); xyplot.setRangeMinorGridlinesVisible(true); domainAxis.setRange(-5, 105); domainAxis.setNumberFormatOverride(pctFormat); domainAxis.setTickLabelPaint(Color.decode("#666666")); domainAxis.setMinorTickCount(5); domainAxis.setTickUnit(new NumberTickUnit(10)); domainAxis.setAxisLineVisible(true); domainAxis.setTickMarksVisible(true); domainAxis.setMinorTickMarksVisible(true); domainAxis.setLowerMargin(10); domainAxis.setUpperMargin(10); xyplot.setDomainGridlineStroke(new BasicStroke()); xyplot.setDomainGridlinePaint(Color.lightGray); xyplot.setDomainMinorGridlinePaint(Color.decode("#DDDDDD")); xyplot.setDomainMinorGridlinesVisible(true); chart.setTextAntiAlias(true); chart.setAntiAlias(true); chart.removeLegend(); chart.setPadding(new RectangleInsets(20, 20, 20, 20)); xyplot.getRenderer().setSeriesPaint(0, Color.decode("#4572a7")); // // setup item labels // XYItemRenderer renderer = xyplot.getRenderer(); // Shape circle = new Ellipse2D.Float(-2.0f, -2.0f, 7.0f, 7.0f); // for ( int i = 0; i < dataset.getSeriesCount(); i++ ) { // renderer.setSeriesShape(i, circle); // renderer.setSeriesPaint(i, Color.blue); // String label = ""+((String)dataset.getSeries(i).getKey()); // int idx = label.indexOf( ':'); // label = label.substring( 0, idx ); // StandardXYItemLabelGenerator generator = new StandardXYItemLabelGenerator(label); // renderer.setSeriesItemLabelGenerator(i, generator); // renderer.setSeriesItemLabelsVisible(i, true); // ItemLabelPosition position = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, // TextAnchor.BASELINE_CENTER ); // renderer.setSeriesPositiveItemLabelPosition(i, position); // } makeDataLabels(category, toolResults, xyplot); makeLegend(category, toolResults, 57, 48, dataset, xyplot); Stroke dashed = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[] {6, 3}, 0); for (XYDataItem item : (List<XYDataItem>) series.getItems()) { double x = item.getX().doubleValue(); double y = item.getY().doubleValue(); double z = (x + y) / 2; XYLineAnnotation score = new XYLineAnnotation(x, y, z, z, dashed, Color.blue); xyplot.addAnnotation(score); } // // put legend inside plot // LegendTitle lt = new LegendTitle(xyplot); // lt.setItemFont(theme.getSmallFont()); // lt.setPosition(RectangleEdge.RIGHT); // lt.setItemFont(theme.getSmallFont()); // XYTitleAnnotation ta = new XYTitleAnnotation(.7, .55, lt, RectangleAnchor.TOP_LEFT); // ta.setMaxWidth(0.48); // xyplot.addAnnotation(ta); // draw guessing line XYLineAnnotation guessing = new XYLineAnnotation(-5, -5, 105, 105, dashed, Color.red); xyplot.addAnnotation(guessing); XYPointerAnnotation worse = makePointer(75, 5, "Worse than guessing", TextAnchor.TOP_CENTER, 90); xyplot.addAnnotation(worse); XYPointerAnnotation better = makePointer(25, 100, "Better than guessing", TextAnchor.BOTTOM_CENTER, 270); xyplot.addAnnotation(better); XYTextAnnotation stroketext = new XYTextAnnotation(" Random Guess", 88, 107); stroketext.setTextAnchor(TextAnchor.CENTER_RIGHT); stroketext.setBackgroundPaint(Color.white); stroketext.setPaint(Color.red); stroketext.setFont(theme.getRegularFont()); xyplot.addAnnotation(stroketext); XYLineAnnotation strokekey = new XYLineAnnotation(58, 107, 68, 107, dashed, Color.red); xyplot.setBackgroundPaint(Color.white); xyplot.addAnnotation(strokekey); ChartPanel cp = new ChartPanel( chart, height, width, 400, 400, 1200, 1200, false, false, false, false, false, false); f.add(cp); f.pack(); f.setLocationRelativeTo(null); // f.setVisible(true); return chart; }
private JFreeChart createChart() { // create the chart... JFreeChart chart = ChartFactory.createXYLineChart( null, // chart title null, // x axis label null, // y axis label null, // data PlotOrientation.VERTICAL, false, // include legend true, // tooltips false // urls ); chart.setBackgroundPaint(Color.white); // get a reference to the plot for further customization... XYPlot plot = (XYPlot) chart.getPlot(); plot.setBackgroundPaint(Color.WHITE); plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0)); plot.setDomainGridlinePaint(Color.LIGHT_GRAY); plot.setRangeGridlinePaint(Color.LIGHT_GRAY); // domain axis if ((indexAxis >= 0) && (!dataTable.isNominal(indexAxis))) { if ((dataTable.isDate(indexAxis)) || (dataTable.isDateTime(indexAxis))) { DateAxis domainAxis = new DateAxis(dataTable.getColumnName(indexAxis)); domainAxis.setTimeZone(Tools.getPreferredTimeZone()); chart.getXYPlot().setDomainAxis(domainAxis); } } else { plot.getDomainAxis().setStandardTickUnits(NumberAxis.createIntegerTickUnits(Locale.US)); ((NumberAxis) plot.getDomainAxis()).setAutoRangeStickyZero(false); ((NumberAxis) plot.getDomainAxis()).setAutoRangeIncludesZero(false); } ValueAxis xAxis = plot.getDomainAxis(); if (indexAxis > -1) xAxis.setLabel(getDataTable().getColumnName(indexAxis)); else xAxis.setLabel(SERIESINDEX_LABEL); xAxis.setAutoRange(true); xAxis.setLabelFont(LABEL_FONT_BOLD); xAxis.setTickLabelFont(LABEL_FONT); xAxis.setVerticalTickLabels(isLabelRotating()); if (indexAxis > 0) { if (getRangeForDimension(indexAxis) != null) { xAxis.setRange(getRangeForDimension(indexAxis)); } } else { if (getRangeForName(SERIESINDEX_LABEL) != null) { xAxis.setRange(getRangeForName(SERIESINDEX_LABEL)); } } // renderer and range axis synchronized (dataTable) { int numberOfSelectedColumns = 0; for (int c = 0; c < dataTable.getNumberOfColumns(); c++) { if (getPlotColumn(c)) { if (dataTable.isNumerical(c)) { numberOfSelectedColumns++; } } } int columnCount = 0; for (int c = 0; c < dataTable.getNumberOfColumns(); c++) { if (getPlotColumn(c)) { if (dataTable.isNumerical(c)) { // YIntervalSeries series = new YIntervalSeries(this.dataTable.getColumnName(c)); XYSeriesCollection dataset = new XYSeriesCollection(); XYSeries series = new XYSeries(dataTable.getColumnName(c)); Iterator<DataTableRow> i = dataTable.iterator(); int index = 1; while (i.hasNext()) { DataTableRow row = i.next(); double value = row.getValue(c); if ((indexAxis >= 0) && (!dataTable.isNominal(indexAxis))) { double indexValue = row.getValue(indexAxis); series.add(indexValue, value); } else { series.add(index++, value); } } dataset.addSeries(series); XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(); Color color = getColorProvider().getPointColor(1.0d); if (numberOfSelectedColumns > 1) { color = getColorProvider() .getPointColor(columnCount / (double) (numberOfSelectedColumns - 1)); } renderer.setSeriesPaint(0, color); renderer.setSeriesStroke( 0, new BasicStroke(1.5f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); renderer.setSeriesShapesVisible(0, false); NumberAxis yAxis = new NumberAxis(dataTable.getColumnName(c)); if (getRangeForDimension(c) != null) { yAxis.setRange(getRangeForDimension(c)); } else { yAxis.setAutoRange(true); yAxis.setAutoRangeStickyZero(false); yAxis.setAutoRangeIncludesZero(false); } yAxis.setLabelFont(LABEL_FONT_BOLD); yAxis.setTickLabelFont(LABEL_FONT); if (numberOfSelectedColumns > 1) { yAxis.setAxisLinePaint(color); yAxis.setTickMarkPaint(color); yAxis.setLabelPaint(color); yAxis.setTickLabelPaint(color); } plot.setRangeAxis(columnCount, yAxis); plot.setRangeAxisLocation(columnCount, AxisLocation.TOP_OR_LEFT); plot.setDataset(columnCount, dataset); plot.setRenderer(columnCount, renderer); plot.mapDatasetToRangeAxis(columnCount, columnCount); columnCount++; } } } } chart.setBackgroundPaint(Color.white); return chart; }