List of usage examples for org.jfree.chart.axis NumberAxis setNumberFormatOverride
public void setNumberFormatOverride(NumberFormat formatter)
From source file:tmn.dev.project.Player.java
/** * Generate a plot of batting average over the span of a season and write * the plot to a PNG file./*w w w . jav a2s . c o m*/ * * @return The String representation of the name of the PNG file containing * the plot. * @throws IOException */ private String createBAPlot() throws IOException { // Create a scatter plot for batting average over the season // Game number on the x axis, Batting average on the y axis JFreeChart chart = ChartFactory.createScatterPlot("Batting Average Throughout the Season", "Game Number", "Batting Average", new XYSeriesCollection(baGame), PlotOrientation.VERTICAL, false, false, false); XYPlot plot = chart.getXYPlot(); // Set the axis ranges to provide an appropriate zoom NumberAxis xAxis = (NumberAxis) plot.getDomainAxis(); xAxis.setRange(baGame.getMinX(), baGame.getMaxX()); NumberAxis yAxis = (NumberAxis) plot.getRangeAxis(); yAxis.setRange(baGame.getMinY() - .02, baGame.getMaxY() + .02); // Set the tick labels on the y axis to the typical BA format yAxis.setNumberFormatOverride(new DecimalFormat(".000")); // Create the String representation of the /images directory String imgDirStr = System.getProperty("user.dir") + System.getProperty("file.separator") + "images"; // If the directory doesn't already exist, create it File imgDir = new File(imgDirStr); if (!imgDir.exists() && !imgDir.isDirectory()) imgDir.mkdir(); // Create the batting average PNG files String fileName = imgDirStr + System.getProperty("file.separator") + this.getId() + ".png"; File imgFile = new File(fileName); // If the file already exists, append a number in parentheses int index = 0; while (!imgFile.createNewFile()) { // Append (<index>).html after the Player ID fileName = imgDirStr + System.getProperty("file.separator") + this.getId() + "(" + ++index + ")" + ".png"; // Try to create a file with this name imgFile = new File(fileName); } // Write the plot to the PNG file that was created ChartUtilities.saveChartAsPNG(imgFile, chart, 800, 450); return imgFile.getName(); }
From source file:org.drools.planner.benchmark.core.report.BenchmarkReport.java
private XYPlot createScalabilityPlot(List<XYSeries> seriesList, String yAxisLabel, NumberFormat numberFormat) { NumberAxis xAxis = new NumberAxis("Problem scale"); xAxis.setNumberFormatOverride(NumberFormat.getInstance(locale)); NumberAxis yAxis = new NumberAxis(yAxisLabel); yAxis.setNumberFormatOverride(numberFormat); XYPlot plot = new XYPlot(null, xAxis, yAxis, null); int seriesIndex = 0; for (XYSeries series : seriesList) { XYSeriesCollection seriesCollection = new XYSeriesCollection(); seriesCollection.addSeries(series); plot.setDataset(seriesIndex, seriesCollection); XYItemRenderer renderer = createScalabilityPlotRenderer(numberFormat); plot.setRenderer(seriesIndex, renderer); seriesIndex++;//from www. j a v a2s .c o m } plot.setOrientation(PlotOrientation.VERTICAL); return plot; }
From source file:desmoj.extensions.grafic.util.Plotter.java
/** * Build a JPanel with a histogram plot of a desmoJ histogramAccumulate dataset * In the case histogram.getShowTimeSpansInReport() the data values are interpreted as * a timespan in a appropriate time unit. * @param histogram desmoJ histogramAccumulate dataset * @return/*from www. j a v a 2 s .c o m*/ */ private JPanel getHistogramAccumulatePlot(HistogramAccumulate histogram) { JFreeChart chart; NumberFormat formatter = NumberFormat.getInstance(locale); HistogramDataSetAdapter dataSet = new HistogramDataSetAdapter(histogram, locale); String title = histogram.getName(); if (histogram.getDescription() != null) title = histogram.getDescription(); String xLabel = dataSet.getCategoryAxisLabel(); String yLabel = dataSet.getObservationAxisLabel(); chart = ChartFactory.createBarChart(title, xLabel, yLabel, dataSet, PlotOrientation.VERTICAL, false, true, false); chart.setBackgroundPaint(Color.white); CategoryPlot categoryplot = (CategoryPlot) chart.getPlot(); categoryplot.setBackgroundPaint(Color.lightGray); categoryplot.setRangeGridlinePaint(Color.white); categoryplot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT); BarRenderer barrenderer = (BarRenderer) categoryplot.getRenderer(); barrenderer.setBaseItemLabelsVisible(true); StandardCategoryItemLabelGenerator generator = new StandardCategoryItemLabelGenerator("{2}", formatter); barrenderer.setBaseItemLabelGenerator(generator); CategoryAxis categoryaxis = categoryplot.getDomainAxis(); categoryaxis.setMaximumCategoryLabelLines(4); NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis(); numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); numberaxis.setNumberFormatOverride(formatter); return new ChartPanel(chart); }
From source file:mil.tatrc.physiology.utilities.csv.plots.RespiratoryPFTPlotter.java
public void formatRPFTPlot(PlotJob job, JFreeChart chart) { XYPlot plot = (XYPlot) chart.getPlot(); //For Scientific notation NumberFormat formatter = new DecimalFormat("0.######E0"); for (int i = 0; i < plot.getDomainAxisCount(); i++) { plot.getDomainAxis(i).setLabelFont(new Font("SansSerif", Font.PLAIN, job.fontSize)); plot.getDomainAxis(i).setTickLabelFont(new Font("SansSerif", Font.PLAIN, 15)); plot.getDomainAxis(i).setLabelPaint(job.bgColor == Color.red ? Color.white : Color.black); plot.getDomainAxis(i).setTickLabelPaint(job.bgColor == Color.red ? Color.white : Color.black); }// w ww.j a v a 2 s. c o m for (int i = 0; i < plot.getRangeAxisCount(); i++) { plot.getRangeAxis(i).setLabelFont(new Font("SansSerif", Font.PLAIN, job.fontSize)); plot.getRangeAxis(i).setTickLabelFont(new Font("SansSerif", Font.PLAIN, 15)); plot.getRangeAxis(i).setLabelPaint(job.bgColor == Color.red ? Color.white : Color.black); plot.getRangeAxis(i).setTickLabelPaint(job.bgColor == Color.red ? Color.white : Color.black); NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(i); rangeAxis.setNumberFormatOverride(formatter); } //White background outside of plottable area chart.setBackgroundPaint(job.bgColor); plot.setBackgroundPaint(Color.white); plot.setDomainGridlinePaint(Color.black); plot.setRangeGridlinePaint(Color.black); plot.setDomainCrosshairVisible(true); plot.setRangeCrosshairVisible(true); chart.getLegend().setItemFont(new Font("SansSerif", Font.PLAIN, 15)); chart.getTitle().setFont(new Font("SansSerif", Font.PLAIN, job.fontSize)); chart.getTitle().setPaint(job.bgColor == Color.red ? Color.white : Color.black); }
From source file:com.wattzap.view.graphs.SCHRGraph.java
public SCHRGraph(ArrayList<Telemetry> telemetry[]) { super();/*from www .ja v a 2 s. co m*/ this.telemetry = telemetry; final NumberAxis domainAxis = new NumberAxis("Time (h:m:s)"); domainAxis.setVerticalTickLabels(true); domainAxis.setTickLabelPaint(Color.black); domainAxis.setAutoRange(true); domainAxis.setNumberFormatOverride(new NumberFormat() { @Override public StringBuffer format(double millis, StringBuffer toAppendTo, FieldPosition pos) { if (millis >= 3600000) { // hours, minutes and seconds return new StringBuffer( String.format("%d:%d:%d", TimeUnit.MILLISECONDS.toHours((long) millis), TimeUnit.MILLISECONDS.toMinutes( (long) millis - TimeUnit.MILLISECONDS.toHours((long) millis) * 3600000), TimeUnit.MILLISECONDS.toSeconds((long) millis) - TimeUnit.MINUTES .toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) millis)))); } else if (millis >= 60000) { // minutes and seconds return new StringBuffer(String.format("%d:%d", TimeUnit.MILLISECONDS.toMinutes((long) millis), TimeUnit.MILLISECONDS.toSeconds((long) millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) millis)))); } else { return new StringBuffer(String.format("%d", TimeUnit.MILLISECONDS.toSeconds((long) millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) millis)))); } } @Override public StringBuffer format(long number, StringBuffer toAppendTo, FieldPosition pos) { return new StringBuffer(String.format("%s", number)); } @Override public Number parse(String source, ParsePosition parsePosition) { return null; } }); // create plot ... final XYItemRenderer powerRenderer = new StandardXYItemRenderer() { Stroke regularStroke = new BasicStroke(0.7f); Color color; public void setColor(Color color) { this.color = color; } @Override public Stroke getItemStroke(int row, int column) { return regularStroke; } @Override public Paint getItemPaint(int row, int column) { return orange; } }; powerRenderer.setSeriesPaint(0, orange); plot = new XYPlot(); plot.setRenderer(0, powerRenderer); plot.setRangeAxis(0, powerAxis); plot.setDomainAxis(domainAxis); // add a second dataset and renderer... final XYItemRenderer cadenceRenderer = new StandardXYItemRenderer() { Stroke regularStroke = new BasicStroke(0.7f); Color color; public void setColor(Color color) { this.color = color; } @Override public Stroke getItemStroke(int row, int column) { return regularStroke; } @Override public Paint getItemPaint(int row, int column) { return cornflower; } public Shape lookupLegendShape(int series) { return new Rectangle(15, 15); } }; final ValueAxis cadenceAxis = new NumberAxis(userPrefs.messages.getString("cDrpm")); cadenceAxis.setRange(0, 200); // arguments of new XYLineAndShapeRenderer are to activate or deactivate // the display of points or line. Set first argument to true if you want // to draw lines between the points for e.g. plot.setRenderer(1, cadenceRenderer); plot.setRangeAxis(1, cadenceAxis); plot.mapDatasetToRangeAxis(1, 1); cadenceRenderer.setSeriesPaint(0, cornflower); // add a third dataset and renderer... final XYItemRenderer hrRenderer = new StandardXYItemRenderer() { Stroke regularStroke = new BasicStroke(0.7f); Color color; public void setColor(Color color) { this.color = color; } @Override public Stroke getItemStroke(int row, int column) { return regularStroke; } @Override public Paint getItemPaint(int row, int column) { return straw; } }; // arguments of new XYLineAndShapeRenderer are to activate or deactivate // the display of points or line. Set first argument to true if you want // to draw lines between the points for e.g. final ValueAxis heartRateAxis = new NumberAxis(userPrefs.messages.getString("hrBpm")); heartRateAxis.setRange(0, 200); plot.setRenderer(2, hrRenderer); hrRenderer.setSeriesPaint(0, straw); plot.setRangeAxis(2, heartRateAxis); plot.mapDatasetToRangeAxis(2, 2); plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD); plot.setBackgroundPaint(Color.DARK_GRAY); // return a new chart containing the overlaid plot... JFreeChart chart = new JFreeChart("", JFreeChart.DEFAULT_TITLE_FONT, plot, true); chart.getLegend().setBackgroundPaint(Color.gray); chartPanel = new ChartPanel(chart); // TODO: maybe remember sizes set by user? this.setPreferredSize(new Dimension(1200, 500)); chartPanel.setFillZoomRectangle(true); chartPanel.setMouseWheelEnabled(true); chartPanel.setBackground(Color.gray); setLayout(new BorderLayout()); add(chartPanel, BorderLayout.CENTER); SmoothingPanel smoothingPanel = new SmoothingPanel(this); add(smoothingPanel, BorderLayout.SOUTH); //infoPanel = new InfoPanel(); //add(infoPanel, BorderLayout.NORTH); setVisible(true); }
From source file:org.owasp.benchmark.score.report.Scatter.java
private JFreeChart display(String title, int height, int width, OverallResults or) { JFrame f = new JFrame(title); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Note: this is a little weird, since each point is a separate series XYSeriesCollection dataset = new XYSeriesCollection(); XYSeries series = new XYSeries("Scores"); for (OverallResult r : or.getResults()) { series.add(r.fpr * 100, r.tpr * 100); }//from w w w.ja v a2 s .co m dataset.addSeries(series); 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(-9.99, 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 letter = "" + ((String)dataset.getSeries(i).getKey()).charAt(0); // StandardXYItemLabelGenerator generator = new StandardXYItemLabelGenerator(letter); // renderer.setSeriesItemLabelGenerator(i, generator); // renderer.setSeriesItemLabelsVisible(i, true); // ItemLabelPosition position = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER ); // renderer.setSeriesPositiveItemLabelPosition(i, position); // } makeDataLabels(or, xyplot); makeLegend(or, 57, 48, dataset, xyplot); // 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 Stroke dashed = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[] { 6, 3 }, 0); XYLineAnnotation guessing = new XYLineAnnotation(-5, -5, 105, 105, dashed, Color.red); xyplot.addAnnotation(guessing); XYPointerAnnotation worse = makePointer(75, 0, "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 time = new XYTextAnnotation("Tool run time: " + or.getTime(), 12, -5.6); time.setTextAnchor(TextAnchor.TOP_LEFT); time.setFont(theme.getRegularFont()); time.setPaint(Color.red); xyplot.addAnnotation(time); 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; }
From source file:desmoj.extensions.grafic.util.Plotter.java
/** * Build a JPanel with a histogram plot of a desmoJ histogram dataset * In the case histogram.getShowTimeSpansInReport() the data values are interpreted as * a timespan in a appropriate time unit. * @param histogram desmoJ histogram dataset * @return/*from ww w . jav a2 s .com*/ */ private JPanel getHistogramPlot(Histogram histogram) { JFreeChart chart; NumberFormat formatter = NumberFormat.getInstance(locale); HistogramDataSetAdapter dataSet = new HistogramDataSetAdapter(histogram, locale); String title = histogram.getName(); if (histogram.getDescription() != null) title = histogram.getDescription(); String xLabel = dataSet.getCategoryAxisLabel(); String yLabel = dataSet.getObservationAxisLabel(); chart = ChartFactory.createBarChart(title, xLabel, yLabel, dataSet, PlotOrientation.VERTICAL, false, true, false); chart.setBackgroundPaint(Color.white); CategoryPlot categoryplot = (CategoryPlot) chart.getPlot(); categoryplot.setBackgroundPaint(Color.lightGray); categoryplot.setRangeGridlinePaint(Color.white); categoryplot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT); BarRenderer barrenderer = (BarRenderer) categoryplot.getRenderer(); barrenderer.setBaseItemLabelsVisible(true); StandardCategoryItemLabelGenerator generator = new StandardCategoryItemLabelGenerator("{2}", formatter); barrenderer.setBaseItemLabelGenerator(generator); CategoryAxis categoryaxis = categoryplot.getDomainAxis(); //categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_45); categoryaxis.setMaximumCategoryLabelLines(4); NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis(); numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); //numberaxis.setUpperMargin(0.1D); numberaxis.setNumberFormatOverride(formatter); return new ChartPanel(chart); }
From source file:ch.agent.crnickl.demo.stox.Chart.java
private XYPlot getBarPlot() throws KeyedException { // use a number axis on the right side with a special formatter for millions NumberAxis axis = new NumberAxis(); axis.setAutoRangeIncludesZero(false); axis.setNumberFormatOverride(new NumberFormatForMillions()); XYPlot plot = new XYPlot(null, null, axis, null); plot.setRangeAxisLocation(AxisLocation.TOP_OR_RIGHT); return plot;//from w w w .ja v a 2s . c o m }
From source file:org.optaplanner.benchmark.impl.report.BenchmarkReport.java
private CategoryPlot createBarChartPlot(DefaultCategoryDataset dataset, String yAxisLabel, NumberFormat yAxisNumberFormat) { CategoryAxis xAxis = new CategoryAxis("Data"); xAxis.setCategoryMargin(0.40);/*from w ww. j a v a 2 s .co m*/ NumberAxis yAxis = new NumberAxis(yAxisLabel); yAxis.setNumberFormatOverride(yAxisNumberFormat); BarRenderer renderer = createBarChartRenderer(yAxisNumberFormat); CategoryPlot plot = new CategoryPlot(dataset, xAxis, yAxis, renderer); plot.setOrientation(PlotOrientation.VERTICAL); return plot; }
From source file:org.optaplanner.benchmark.impl.report.BenchmarkReport.java
private XYPlot createScalabilityPlot(List<XYSeries> seriesList, String xAxisLabel, NumberFormat xAxisNumberFormat, String yAxisLabel, NumberFormat yAxisNumberFormat) { NumberAxis xAxis = new NumberAxis(xAxisLabel); xAxis.setNumberFormatOverride(xAxisNumberFormat); NumberAxis yAxis = new NumberAxis(yAxisLabel); yAxis.setNumberFormatOverride(yAxisNumberFormat); XYPlot plot = new XYPlot(null, xAxis, yAxis, null); int seriesIndex = 0; for (XYSeries series : seriesList) { XYSeriesCollection seriesCollection = new XYSeriesCollection(); seriesCollection.addSeries(series); plot.setDataset(seriesIndex, seriesCollection); XYItemRenderer renderer = createScalabilityPlotRenderer(yAxisNumberFormat); plot.setRenderer(seriesIndex, renderer); seriesIndex++;/* ww w. j a v a 2 s .c o m*/ } plot.setOrientation(PlotOrientation.VERTICAL); return plot; }