List of usage examples for org.jfree.chart.title TextTitle getFont
public Font getFont()
From source file:org.jfree.experimental.chart.swt.editor.SWTTitleEditor.java
/** * Standard constructor: builds a panel for displaying/editing the * properties of the specified title./*w w w . j ava 2 s .co m*/ * * @param title the title, which should be changed. */ SWTTitleEditor(Composite parent, int style, Title title) { super(parent, style); FillLayout layout = new FillLayout(); layout.marginHeight = layout.marginWidth = 4; setLayout(layout); TextTitle t = (title != null ? (TextTitle) title : new TextTitle(localizationResources.getString("Title"))); this.showTitle = (title != null); this.titleFont = SWTUtils.toSwtFontData(getDisplay(), t.getFont(), true); this.titleColor = SWTUtils.toSwtColor(getDisplay(), t.getPaint()); Group general = new Group(this, SWT.NONE); general.setLayout(new GridLayout(3, false)); general.setText(localizationResources.getString("General")); // row 1 Label label = new Label(general, SWT.NONE); label.setText(localizationResources.getString("Show_Title")); GridData gridData = new GridData(); gridData.horizontalSpan = 2; label.setLayoutData(gridData); this.showTitleCheckBox = new Button(general, SWT.CHECK); this.showTitleCheckBox.setSelection(this.showTitle); this.showTitleCheckBox.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false)); this.showTitleCheckBox.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { SWTTitleEditor.this.showTitle = SWTTitleEditor.this.showTitleCheckBox.getSelection(); } }); // row 2 new Label(general, SWT.NONE).setText(localizationResources.getString("Text")); this.titleField = new Text(general, SWT.BORDER); this.titleField.setText(t.getText()); this.titleField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); new Label(general, SWT.NONE).setText(""); // row 3 new Label(general, SWT.NONE).setText(localizationResources.getString("Font")); this.fontField = new Text(general, SWT.BORDER); this.fontField.setText(this.titleFont.toString()); this.fontField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); this.selectFontButton = new Button(general, SWT.PUSH); this.selectFontButton.setText(localizationResources.getString("Select...")); this.selectFontButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { // Create the font-change dialog FontDialog dlg = new FontDialog(getShell()); dlg.setText(localizationResources.getString("Font_Selection")); dlg.setFontList(new FontData[] { SWTTitleEditor.this.titleFont }); if (dlg.open() != null) { // Dispose of any fonts we have created if (SWTTitleEditor.this.font != null) { SWTTitleEditor.this.font.dispose(); } // Create the new font and set it into the title // label SWTTitleEditor.this.font = new Font(getShell().getDisplay(), dlg.getFontList()); //titleField.setFont(font); SWTTitleEditor.this.fontField.setText(SWTTitleEditor.this.font.getFontData()[0].toString()); SWTTitleEditor.this.titleFont = SWTTitleEditor.this.font.getFontData()[0]; } } }); // row 4 new Label(general, SWT.NONE).setText(localizationResources.getString("Color")); // Use a SwtPaintCanvas to show the color, note that we must set the // heightHint. final SWTPaintCanvas colorCanvas = new SWTPaintCanvas(general, SWT.NONE, this.titleColor); GridData canvasGridData = new GridData(SWT.FILL, SWT.CENTER, true, false); canvasGridData.heightHint = 20; colorCanvas.setLayoutData(canvasGridData); this.selectColorButton = new Button(general, SWT.PUSH); this.selectColorButton.setText(localizationResources.getString("Select...")); this.selectColorButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { // Create the color-change dialog ColorDialog dlg = new ColorDialog(getShell()); dlg.setText(localizationResources.getString("Title_Color")); dlg.setRGB(SWTTitleEditor.this.titleColor.getRGB()); RGB rgb = dlg.open(); if (rgb != null) { // create the new color and set it to the // SwtPaintCanvas SWTTitleEditor.this.titleColor = new Color(getDisplay(), rgb); colorCanvas.setColor(SWTTitleEditor.this.titleColor); } } }); }
From source file:com.rcp.wbw.demo.editor.SWTTitleEditor.java
/** * Standard constructor: builds a panel for displaying/editing the * properties of the specified title.// w ww . j ava2s.com * * @param parent * the parent. * @param style * the style. * @param title * the title, which should be changed. * */ SWTTitleEditor(Composite parent, int style, Title title) { super(parent, style); FillLayout layout = new FillLayout(); layout.marginHeight = layout.marginWidth = 4; setLayout(layout); TextTitle t = (title != null ? (TextTitle) title : new TextTitle(localizationResources.getString("Title"))); this.showTitle = (title != null); this.titleFont = SWTUtils.toSwtFontData(getDisplay(), t.getFont(), true); this.titleColor = SWTUtils.toSwtColor(getDisplay(), t.getPaint()); Group general = new Group(this, SWT.NONE); general.setLayout(new GridLayout(3, false)); general.setText(localizationResources.getString("General")); // row 1 Label label = new Label(general, SWT.NONE); label.setText(localizationResources.getString("Show_Title")); GridData gridData = new GridData(); gridData.horizontalSpan = 2; label.setLayoutData(gridData); this.showTitleCheckBox = new Button(general, SWT.CHECK); this.showTitleCheckBox.setSelection(this.showTitle); this.showTitleCheckBox.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false)); this.showTitleCheckBox.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { SWTTitleEditor.this.showTitle = SWTTitleEditor.this.showTitleCheckBox.getSelection(); } }); // row 2 new Label(general, SWT.NONE).setText(localizationResources.getString("Text")); this.titleField = new Text(general, SWT.BORDER); this.titleField.setText(t.getText()); this.titleField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); new Label(general, SWT.NONE).setText(""); // row 3 new Label(general, SWT.NONE).setText(localizationResources.getString("Font")); this.fontField = new Text(general, SWT.BORDER); this.fontField.setText(this.titleFont.toString()); this.fontField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); this.selectFontButton = new Button(general, SWT.PUSH); this.selectFontButton.setText(localizationResources.getString("Select...")); this.selectFontButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { // Create the font-change dialog FontDialog dlg = new FontDialog(getShell()); dlg.setText(localizationResources.getString("Font_Selection")); dlg.setFontList(new FontData[] { SWTTitleEditor.this.titleFont }); if (dlg.open() != null) { // Dispose of any fonts we have created if (SWTTitleEditor.this.font != null) { SWTTitleEditor.this.font.dispose(); } // Create the new font and set it into the title // label SWTTitleEditor.this.font = new Font(getShell().getDisplay(), dlg.getFontList()); // titleField.setFont(font); SWTTitleEditor.this.fontField.setText(SWTTitleEditor.this.font.getFontData()[0].toString()); SWTTitleEditor.this.titleFont = SWTTitleEditor.this.font.getFontData()[0]; } } }); // row 4 new Label(general, SWT.NONE).setText(localizationResources.getString("Color")); // Use a SwtPaintCanvas to show the color, note that we must set the // heightHint. final SWTPaintCanvas colorCanvas = new SWTPaintCanvas(general, SWT.NONE, this.titleColor); GridData canvasGridData = new GridData(SWT.FILL, SWT.CENTER, true, false); canvasGridData.heightHint = 20; colorCanvas.setLayoutData(canvasGridData); this.selectColorButton = new Button(general, SWT.PUSH); this.selectColorButton.setText(localizationResources.getString("Select...")); this.selectColorButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { // Create the color-change dialog ColorDialog dlg = new ColorDialog(getShell()); dlg.setText(localizationResources.getString("Title_Color")); dlg.setRGB(SWTTitleEditor.this.titleColor.getRGB()); RGB rgb = dlg.open(); if (rgb != null) { // create the new color and set it to the // SwtPaintCanvas SWTTitleEditor.this.titleColor = new Color(getDisplay(), rgb); colorCanvas.setColor(SWTTitleEditor.this.titleColor); } } }); }
From source file:nu.nethome.tools.protocol_analyzer.RawMessageDistributionWindow.java
private void configurePanelLooks(JFreeChart chart, int selectionSeries) { TextTitle title = chart.getTitle(); // fix title Font titleFont = title.getFont(); titleFont = titleFont.deriveFont(Font.PLAIN, (float) 14.0); title.setFont(titleFont);//w ww .j av a 2 s . c o m title.setPaint(Color.darkGray); XYPlot plot = chart.getXYPlot(); plot.setBackgroundPaint(Color.WHITE); plot.setDomainGridlinePaint(Color.lightGray); plot.setRangeGridlinePaint(Color.lightGray); XYLineAndShapeRenderer signalRenderer = (XYLineAndShapeRenderer) chart.getXYPlot().getRenderer(); signalRenderer.setSeriesStroke(selectionSeries, new BasicStroke(5f)); }
From source file:net.sourceforge.processdash.ui.web.CGIChartBase.java
/** Generate CGI chart output. */ @Override/*from ww w .j a v a 2 s . c om*/ protected void writeContents() throws IOException { buildData(); // get the data for display chromeless = (parameters.get("chromeless") != null); JFreeChart chart = createChart(); int width = getIntSetting("width"); int height = getIntSetting("height"); Color initGradColor = getColorSetting("initGradColor"); Color finalGradColor = getColorSetting("finalGradColor"); chart.setBackgroundPaint(new GradientPaint(0, 0, initGradColor, width, height, finalGradColor)); if (parameters.get("hideOutline") != null) chart.getPlot().setOutlinePaint(INVISIBLE); String title = getSetting("title"); if (chromeless || title == null || title.length() == 0) chart.setTitle((TextTitle) null); else { chart.setTitle(Translator.translate(title)); String titleFontSize = getSetting("titleFontSize"); if (titleFontSize != null) try { float fontSize = Float.parseFloat(titleFontSize); TextTitle t = chart.getTitle(); t.setFont(t.getFont().deriveFont(fontSize)); } catch (Exception tfe) { } } if (chromeless || parameters.get("hideLegend") != null) chart.removeLegend(); else { LegendTitle l = chart.getLegend(); String legendFontSize = getSetting("legendFontSize"); if (l != null && legendFontSize != null) try { float fontSize = Float.parseFloat(legendFontSize); l.setItemFont(l.getItemFont().deriveFont(fontSize)); } catch (Exception lfe) { } } chart.getPlot().setNoDataMessage(resources.getString("No_Data_Message")); Axis xAxis = getHorizontalAxis(chart); if (xAxis != null) { if (parameters.get("hideTickLabels") != null || parameters.get("hideXTickLabels") != null) { xAxis.setTickLabelsVisible(false); } else if (parameters.get("tickLabelFontSize") != null || parameters.get("xTickLabelFontSize") != null) { String tfs = getParameter("xTickLabelFontSize"); if (tfs == null) tfs = getParameter("tickLabelFontSize"); float fontSize = Float.parseFloat(tfs); xAxis.setTickLabelFont(xAxis.getTickLabelFont().deriveFont(fontSize)); } } Axis yAxis = getVerticalAxis(chart); if (yAxis != null) { if (parameters.get("hideTickLabels") != null || parameters.get("hideYTickLabels") != null) { yAxis.setTickLabelsVisible(false); } else if (parameters.get("tickLabelFontSize") != null || parameters.get("yTickLabelFontSize") != null) { String tfs = getParameter("yTickLabelFontSize"); if (tfs == null) tfs = getParameter("tickLabelFontSize"); float fontSize = Float.parseFloat(tfs); yAxis.setTickLabelFont(yAxis.getTickLabelFont().deriveFont(fontSize)); } } String axisFontSize = getSetting("axisLabelFontSize"); if (axisFontSize != null) try { float fontSize = Float.parseFloat(axisFontSize); if (xAxis != null) xAxis.setLabelFont(xAxis.getLabelFont().deriveFont(fontSize)); if (yAxis != null) yAxis.setLabelFont(yAxis.getLabelFont().deriveFont(fontSize)); } catch (Exception afs) { } ChartRenderingInfo info = (isHtmlMode() ? new ChartRenderingInfo() : null); BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = img.createGraphics(); if ("auto".equals(getSetting("titleFontSize"))) maybeAdjustTitleFontSize(chart, g2, width); chart.draw(g2, new Rectangle2D.Double(0, 0, width, height), info); g2.dispose(); String outputFormat = getSetting("outputFormat"); OutputStream imgOut; if (isHtmlMode()) { imgOut = PngCache.getOutputStream(); } else { imgOut = outStream; } ImageIO.write(img, outputFormat, imgOut); imgOut.flush(); imgOut.close(); if (isHtmlMode()) writeImageHtml(width, height, imgOut.hashCode(), info); }
From source file:com.netflix.ice.basic.BasicWeeklyCostEmailService.java
private File createImage(ApplicationGroup appgroup) throws IOException { Map<String, Double> costs = Maps.newHashMap(); DateTime end = new DateTime(DateTimeZone.UTC).withDayOfWeek(1).withMillisOfDay(0); Interval interval = new Interval(end.minusWeeks(numWeeks), end); for (Product product : products) { List<ResourceGroup> resourceGroups = getResourceGroups(appgroup, product); if (resourceGroups.size() == 0) { continue; }//from w ww . j a v a 2 s .c om DataManager dataManager = config.managers.getCostManager(product, ConsolidateType.weekly); if (dataManager == null) { continue; } TagLists tagLists = new TagLists(accounts, regions, null, Lists.newArrayList(product), null, null, resourceGroups); Map<Tag, double[]> data = dataManager.getData(interval, tagLists, TagType.Product, AggregateType.none, false); for (Tag tag : data.keySet()) { for (int week = 0; week < numWeeks; week++) { String key = tag + "|" + week; if (costs.containsKey(key)) costs.put(key, data.get(tag)[week] + costs.get(key)); else costs.put(key, data.get(tag)[week]); } } } boolean hasData = false; for (Map.Entry<String, Double> entry : costs.entrySet()) { if (!entry.getKey().contains("monitor") && entry.getValue() != null && entry.getValue() >= 0.1) { hasData = true; break; } } if (!hasData) return null; DefaultCategoryDataset dataset = new DefaultCategoryDataset(); for (Product product : products) { for (int week = 0; week < numWeeks; week++) { String weekStr = String.format("%s - %s week", formatter.print(end.minusWeeks(numWeeks - week)).substring(5), formatter.print(end.minusWeeks(numWeeks - week - 1)).substring(5)); dataset.addValue(costs.get(product + "|" + week), product.name, weekStr); } } JFreeChart chart = ChartFactory.createBarChart3D(appgroup.getDisplayName() + " Weekly AWS Costs", "", "Costs", dataset, PlotOrientation.VERTICAL, true, false, false); CategoryPlot categoryplot = (CategoryPlot) chart.getPlot(); BarRenderer3D renderer = (BarRenderer3D) categoryplot.getRenderer(); renderer.setItemLabelAnchorOffset(10.0); TextTitle title = chart.getTitle(); title.setFont(title.getFont().deriveFont((title.getFont().getSize() - 3))); renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator() { public java.lang.String generateLabel(org.jfree.data.category.CategoryDataset dataset, int row, int column) { return costFormatter.format(dataset.getValue(row, column)); } }); renderer.setBaseItemLabelsVisible(true); renderer.setBasePositiveItemLabelPosition( new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER)); NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis(); numberaxis.setNumberFormatOverride(costFormatter); BufferedImage image = chart.createBufferedImage(1200, 400); File outputfile = File.createTempFile("awscost", "png"); ImageIO.write(image, "png", outputfile); return outputfile; }
From source file:weka.classifiers.timeseries.eval.graph.JFreeChartDriver.java
protected JFreeChart getPredictedTargetsChart(TSForecaster forecaster, ErrorModule preds, List<String> targetNames, int stepNumber, int instanceNumOffset, Instances data) { if (forecaster instanceof TSLagUser && data != null) { TSLagMaker lagMaker = ((TSLagUser) forecaster).getTSLagMaker(); if (lagMaker.getAdjustForTrends() && !lagMaker.isUsingAnArtificialTimeIndex()) { // fill in any missing time stamps only data = new Instances(data); data = weka.classifiers.timeseries.core.Utils.replaceMissing(data, null, lagMaker.getTimeStampField(), true, lagMaker.getPeriodicity(), lagMaker.getSkipEntries()); }//from w ww . j a va 2s . c o m } // set up a collection of predicted and actual series XYIntervalSeriesCollection xyDataset = new XYIntervalSeriesCollection(); for (String target : targetNames) { XYIntervalSeries targetSeries = new XYIntervalSeries(target + "-actual", false, false); xyDataset.addSeries(targetSeries); targetSeries = new XYIntervalSeries(target + "-predicted", false, false); xyDataset.addSeries(targetSeries); } ValueAxis timeAxis = null; NumberAxis valueAxis = new NumberAxis(""); valueAxis.setAutoRangeIncludesZero(false); int timeIndex = -1; boolean timeAxisIsDate = false; if (forecaster instanceof TSLagUser && data != null) { TSLagMaker lagMaker = ((TSLagUser) forecaster).getTSLagMaker(); if (!lagMaker.isUsingAnArtificialTimeIndex() && lagMaker.getAdjustForTrends()) { String timeName = lagMaker.getTimeStampField(); if (data.attribute(timeName).isDate()) { timeAxis = new DateAxis(""); timeAxisIsDate = true; timeIndex = data.attribute(timeName).index(); } } } if (timeAxis == null) { timeAxis = new NumberAxis(""); ((NumberAxis) timeAxis).setAutoRangeIncludesZero(false); } // now populate the series boolean hasConfidenceIntervals = false; for (int i = 0; i < targetNames.size(); i++) { String targetName = targetNames.get(i); List<NumericPrediction> predsForI = preds.getPredictionsForTarget(targetName); int predIndex = xyDataset.indexOf(targetName + "-predicted"); int actualIndex = xyDataset.indexOf(targetName + "-actual"); XYIntervalSeries predSeries = xyDataset.getSeries(predIndex); XYIntervalSeries actualSeries = xyDataset.getSeries(actualIndex); for (int j = 0; j < predsForI.size(); j++) { double x = Utils.missingValue(); if (timeAxisIsDate) { if (instanceNumOffset + j + stepNumber - 1 < data.numInstances()) { x = data.instance(instanceNumOffset + j + stepNumber - 1).value(timeIndex); } } else { x = instanceNumOffset + j + stepNumber; } double yPredicted = predsForI.get(j).predicted(); double yHigh = yPredicted; double yLow = yPredicted; double[][] conf = predsForI.get(j).predictionIntervals(); if (conf.length > 0) { yLow = conf[0][0]; yHigh = conf[0][1]; hasConfidenceIntervals = true; } if (!Utils.isMissingValue(x) && !Utils.isMissingValue(yPredicted)) { if (predSeries != null) { predSeries.add(x, x, x, yPredicted, yLow, yHigh); } // System.err.println("* " + yPredicted + " " + x); } double yActual = predsForI.get(j).actual(); if (!Utils.isMissingValue(x) && !Utils.isMissingValue(yActual)) { if (actualSeries != null) { actualSeries.add(x, x, x, yActual, yActual, yActual); } } } } // set up the chart String title = "" + stepNumber + " step-ahead predictions for: "; for (String s : targetNames) { title += s + ","; } title = title.substring(0, title.lastIndexOf(",")); /* * String algoSpec = forecaster.getAlgorithmName(); title += " (" + algoSpec * + ")"; */ if (forecaster instanceof WekaForecaster && hasConfidenceIntervals) { double confPerc = ((WekaForecaster) forecaster).getConfidenceLevel() * 100.0; title += " [" + Utils.doubleToString(confPerc, 0) + "% conf. intervals]"; } XYErrorRenderer renderer = new XYErrorRenderer(); renderer.setBaseLinesVisible(true); renderer.setDrawXError(false); renderer.setDrawYError(true); // renderer.setShapesFilled(true); XYPlot plot = new XYPlot(xyDataset, timeAxis, valueAxis, renderer); JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, true); chart.setBackgroundPaint(java.awt.Color.white); TextTitle chartTitle = chart.getTitle(); String fontName = chartTitle.getFont().getFontName(); java.awt.Font newFont = new java.awt.Font(fontName, java.awt.Font.PLAIN, 12); chartTitle.setFont(newFont); return chart; }
From source file:weka.classifiers.timeseries.eval.graph.JFreeChartDriver.java
protected JFreeChart getPredictedStepsChart(TSForecaster forecaster, List<ErrorModule> preds, String targetName, List<Integer> stepsToPlot, int instanceNumOffset, Instances data) { if (forecaster instanceof TSLagUser && data != null) { TSLagMaker lagMaker = ((TSLagUser) forecaster).getTSLagMaker(); if (lagMaker.getAdjustForTrends() && !lagMaker.isUsingAnArtificialTimeIndex()) { // fill in any missing time stamps only data = new Instances(data); data = weka.classifiers.timeseries.core.Utils.replaceMissing(data, null, lagMaker.getTimeStampField(), true, lagMaker.getPeriodicity(), lagMaker.getSkipEntries()); }/*w w w .ja v a 2 s.c o m*/ } // set up a collection of predicted series XYIntervalSeriesCollection xyDataset = new XYIntervalSeriesCollection(); XYIntervalSeries targetSeries = new XYIntervalSeries(targetName, false, false); xyDataset.addSeries(targetSeries); // for (int i = 0; i < preds.size(); i++) { for (int z = 0; z < stepsToPlot.size(); z++) { int i = stepsToPlot.get(z); i--; // ignore out of range steps if (i < 0 || i >= preds.size()) { continue; } String step = "-steps"; if (i == 0) { step = "-step"; } targetSeries = new XYIntervalSeries(targetName + "_" + (i + 1) + step + "-ahead", false, false); xyDataset.addSeries(targetSeries); } ValueAxis timeAxis = null; NumberAxis valueAxis = new NumberAxis(""); valueAxis.setAutoRangeIncludesZero(false); int timeIndex = -1; boolean timeAxisIsDate = false; if (forecaster instanceof TSLagUser && data != null) { TSLagMaker lagMaker = ((TSLagUser) forecaster).getTSLagMaker(); if (!lagMaker.isUsingAnArtificialTimeIndex() && lagMaker.getAdjustForTrends()) { String timeName = lagMaker.getTimeStampField(); if (data.attribute(timeName).isDate()) { timeAxis = new DateAxis(""); timeAxisIsDate = true; timeIndex = data.attribute(timeName).index(); } } } if (timeAxis == null) { timeAxis = new NumberAxis(""); ((NumberAxis) timeAxis).setAutoRangeIncludesZero(false); } // now populate the series // for (int i = 0; i < preds.size(); i++) { boolean doneActual = false; boolean hasConfidenceIntervals = false; for (int z = 0; z < stepsToPlot.size(); z++) { int i = stepsToPlot.get(z); i--; // ignore out of range steps if (i < 0 || i >= preds.size()) { continue; } ErrorModule predsForStepI = preds.get(i); List<NumericPrediction> predsForTargetAtI = predsForStepI.getPredictionsForTarget(targetName); String step = "-steps"; if (i == 0) { step = "-step"; } int predIndex = xyDataset.indexOf(targetName + "_" + (i + 1) + step + "-ahead"); XYIntervalSeries predSeries = xyDataset.getSeries(predIndex); XYIntervalSeries actualSeries = null; if (!doneActual) { int actualIndex = xyDataset.indexOf(targetName); actualSeries = xyDataset.getSeries(actualIndex); } for (int j = 0; j < predsForTargetAtI.size(); j++) { double x = Utils.missingValue(); if (timeAxisIsDate) { if (instanceNumOffset + j + i < data.numInstances()) { x = data.instance(instanceNumOffset + j + i).value(timeIndex); } } else { x = instanceNumOffset + j + i; } double yPredicted = predsForTargetAtI.get(j).predicted(); double yHigh = yPredicted; double yLow = yPredicted; double[][] conf = predsForTargetAtI.get(j).predictionIntervals(); if (conf.length > 0) { yLow = conf[0][0]; yHigh = conf[0][1]; hasConfidenceIntervals = true; } if (!Utils.isMissingValue(x) && !Utils.isMissingValue(yPredicted)) { if (predSeries != null) { predSeries.add(x, x, x, yPredicted, yLow, yHigh); } // System.err.println("* " + yPredicted + " " + x); } if (!doneActual && actualSeries != null) { double yActual = predsForTargetAtI.get(j).actual(); if (!Utils.isMissingValue(x) && !Utils.isMissingValue(yActual)) { actualSeries.add(x, x, x, yActual, yActual, yActual); } } } if (actualSeries != null) { doneActual = true; } } // set up the chart String title = ""; for (int i : stepsToPlot) { title += i + ","; } title = title.substring(0, title.lastIndexOf(",")); title += " step-ahead predictions for " + targetName; /* * String algoSpec = forecaster.getAlgorithmName(); title += " (" + algoSpec * + ")"; */ if (forecaster instanceof WekaForecaster && hasConfidenceIntervals) { double confPerc = ((WekaForecaster) forecaster).getConfidenceLevel() * 100.0; title += " [" + Utils.doubleToString(confPerc, 0) + "% conf. intervals]"; } XYErrorRenderer renderer = new XYErrorRenderer(); renderer.setBaseLinesVisible(true); // renderer.setShapesFilled(true); XYPlot plot = new XYPlot(xyDataset, timeAxis, valueAxis, renderer); JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, true); chart.setBackgroundPaint(java.awt.Color.white); TextTitle chartTitle = chart.getTitle(); String fontName = chartTitle.getFont().getFontName(); java.awt.Font newFont = new java.awt.Font(fontName, java.awt.Font.PLAIN, 12); chartTitle.setFont(newFont); return chart; }
From source file:weka.classifiers.timeseries.eval.graph.JFreeChartDriver.java
protected JFreeChart getFutureForecastChart(TSForecaster forecaster, List<List<NumericPrediction>> preds, List<String> targetNames, Instances history) { if (forecaster instanceof TSLagUser && history != null) { TSLagMaker lagMaker = ((TSLagUser) forecaster).getTSLagMaker(); if (lagMaker.getAdjustForTrends() && !lagMaker.isUsingAnArtificialTimeIndex()) { // fill in any missing time stamps only history = new Instances(history); history = weka.classifiers.timeseries.core.Utils.replaceMissing(history, null, lagMaker.getTimeStampField(), true, lagMaker.getPeriodicity(), lagMaker.getSkipEntries()); }/*from w ww . j a v a2s .c om*/ } // set up a collection of series XYIntervalSeriesCollection xyDataset = new XYIntervalSeriesCollection(); if (history != null) { // add actual historical data values for (String targetName : targetNames) { XYIntervalSeries targetSeries = new XYIntervalSeries(targetName, false, false); xyDataset.addSeries(targetSeries); } } // add predicted series for (String targetName : targetNames) { XYIntervalSeries targetSeries = new XYIntervalSeries(targetName + "-predicted", false, false); xyDataset.addSeries(targetSeries); } ValueAxis timeAxis = null; NumberAxis valueAxis = new NumberAxis(""); valueAxis.setAutoRangeIncludesZero(false); int timeIndex = -1; boolean timeAxisIsDate = false; double artificialTimeStart = 0; double lastRealTimeValue = Utils.missingValue(); if (forecaster instanceof TSLagUser && history != null) { TSLagMaker lagMaker = ((TSLagUser) forecaster).getTSLagMaker(); if (!lagMaker.isUsingAnArtificialTimeIndex() && lagMaker.getAdjustForTrends()) { String timeName = lagMaker.getTimeStampField(); if (history.attribute(timeName).isDate()) { timeAxis = new DateAxis(""); timeAxisIsDate = true; timeIndex = history.attribute(timeName).index(); } } else { try { artificialTimeStart = (history != null) ? 1 : lagMaker.getArtificialTimeStartValue() + 1; } catch (Exception ex) { } } } if (timeAxis == null) { timeAxis = new NumberAxis(""); ((NumberAxis) timeAxis).setAutoRangeIncludesZero(false); } boolean hasConfidenceIntervals = false; // now populate the series if (history != null) { // do the actuals first for (int i = 0; i < history.numInstances(); i++) { Instance current = history.instance(i); for (String targetName : targetNames) { int dataIndex = history.attribute(targetName.trim()).index(); if (dataIndex >= 0) { XYIntervalSeries actualSeries = null; int actualIndex = xyDataset.indexOf(targetName); actualSeries = xyDataset.getSeries(actualIndex); double x = Utils.missingValue(); if (timeAxisIsDate) { x = current.value(timeIndex); if (!Utils.isMissingValue(x)) { lastRealTimeValue = x; } } else { x = artificialTimeStart; } double y = Utils.missingValue(); y = current.value(dataIndex); if (!Utils.isMissingValue(x) && !Utils.isMissingValue(y)) { if (actualSeries != null) { actualSeries.add(x, x, x, y, y, y); } } } } if (!timeAxisIsDate) { artificialTimeStart++; } } } // now do the futures List<String> forecasterTargets = AbstractForecaster.stringToList(forecaster.getFieldsToForecast()); // loop over the steps for (int j = 0; j < preds.size(); j++) { List<NumericPrediction> predsForStepJ = preds.get(j); // advance the real time index (if appropriate) if (timeAxisIsDate) { lastRealTimeValue = ((TSLagUser) forecaster).getTSLagMaker() .advanceSuppliedTimeValue(lastRealTimeValue); } for (String targetName : targetNames) { // look up this requested target in the list that the forecaster // has predicted int predIndex = forecasterTargets.indexOf(targetName.trim()); if (predIndex >= 0) { NumericPrediction predsForTargetAtStepJ = predsForStepJ.get(predIndex); XYIntervalSeries predSeries = null; int datasetIndex = xyDataset.indexOf(targetName + "-predicted"); predSeries = xyDataset.getSeries(datasetIndex); if (predSeries != null) { double y = predsForTargetAtStepJ.predicted(); double x = Utils.missingValue(); double yHigh = y; double yLow = y; double[][] conf = predsForTargetAtStepJ.predictionIntervals(); if (conf.length > 0) { yLow = conf[0][0]; yHigh = conf[0][1]; hasConfidenceIntervals = true; } if (!timeAxisIsDate) { x = artificialTimeStart; } else { x = lastRealTimeValue; } if (!Utils.isMissingValue(x) && !Utils.isMissingValue(y)) { predSeries.add(x, x, x, y, yLow, yHigh); } } } } // advance the artificial time index (if appropriate) if (!timeAxisIsDate) { artificialTimeStart++; } } String title = "Future forecast for: "; for (String s : targetNames) { title += s + ","; } title = title.substring(0, title.lastIndexOf(",")); /* * String algoSpec = forecaster.getAlgorithmName(); title += " (" + algoSpec * + ")"; */ if (forecaster instanceof WekaForecaster && hasConfidenceIntervals) { double confPerc = ((WekaForecaster) forecaster).getConfidenceLevel() * 100.0; title += " [" + Utils.doubleToString(confPerc, 0) + "% conf. intervals]"; } XYErrorRenderer renderer = new XYErrorRenderer(); // renderer.setShapesFilled(true); XYPlot plot = new XYPlot(xyDataset, timeAxis, valueAxis, renderer); // renderer = (XYErrorRenderer)plot.getRenderer(); if (history != null) { for (String targetName : targetNames) { XYIntervalSeries predSeries = null; int predIndex = xyDataset.indexOf(targetName + "-predicted"); predSeries = xyDataset.getSeries(predIndex); XYIntervalSeries actualSeries = null; int actualIndex = xyDataset.indexOf(targetName); actualSeries = xyDataset.getSeries(actualIndex); if (actualSeries != null && predSeries != null) { // match the color of the actual series java.awt.Paint actualPaint = renderer.lookupSeriesPaint(actualIndex); renderer.setSeriesPaint(predIndex, actualPaint); // now set the line style to dashed BasicStroke dashed = new BasicStroke(1.5f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, new float[] { 5.0f }, 0.0f); renderer.setSeriesStroke(predIndex, dashed); } } } renderer.setBaseLinesVisible(true); renderer.setDrawXError(false); renderer.setDrawYError(true); JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, true); chart.setBackgroundPaint(java.awt.Color.white); TextTitle chartTitle = chart.getTitle(); String fontName = chartTitle.getFont().getFontName(); java.awt.Font newFont = new java.awt.Font(fontName, java.awt.Font.PLAIN, 12); chartTitle.setFont(newFont); return chart; }
From source file:com.bdaum.zoom.report.internal.wizards.ReportComponent.java
public void saveChartProperties(Report report) { JFreeChart chart = chartComposite.getChart(); if (chart != null) { Map<String, Object> properties = new HashMap<>(); TextTitle title = chart.getTitle(); if (title != null) { properties.put(TITLE, title.getText()); properties.put(TITLEFONT, title.getFont()); properties.put(TITLECOLOR, title.getPaint()); }//w ww. ja v a 2 s .co m Plot plot = chart.getPlot(); properties.put(BGCOLOR, plot.getBackgroundPaint()); properties.put(OUTLINEPAINT, plot.getOutlinePaint()); properties.put(OUTLINESTROKE, plot.getOutlineStroke()); Axis domainAxis = null; Axis rangeAxis = null; if (plot instanceof CategoryPlot) { CategoryPlot p = (CategoryPlot) plot; domainAxis = p.getDomainAxis(); rangeAxis = p.getRangeAxis(); properties.put(ORIENTATION, p.getOrientation()); } else if (plot instanceof XYPlot) { XYPlot p = (XYPlot) plot; domainAxis = p.getDomainAxis(); rangeAxis = p.getRangeAxis(); properties.put(ORIENTATION, p.getOrientation()); } if (domainAxis != null) saveAxisProperties(domainAxis, "x", properties); //$NON-NLS-1$ if (rangeAxis != null) saveAxisProperties(rangeAxis, "y", properties); //$NON-NLS-1$ properties.put(ANTIALIAS, chart.getAntiAlias()); properties.put(CANVASPAINT, chart.getBackgroundPaint()); report.setProperties(properties); } }
From source file:org.gumtree.vis.awt.JChartPanel.java
@Override public void draw(Graphics2D g2, Rectangle2D area, double shiftX, double shiftY) { // g2.setPaint(Color.white); // g2.fill(new Rectangle2D.Double(0, 0, getWidth(), getHeight())); // if (getChart() != null) { //// Image chartImage = getChart().createBufferedImage((int) area.getWidth(), //// (int) area.getHeight()); //// g2.drawImage(chartImage, (int) area.getMinX(), (int) area.getMinY(), //// this); // getChart().draw(g2, area, getAnchor(), null); // ChartMaskingUtilities.drawMasks(g2, getScreenDataArea(), maskList, // null, getChart()); // }// w w w .j a v a2 s .c o m double widthRatio = area.getWidth() / getWidth(); double heightRatio = area.getHeight() / getHeight(); double overallRatio = 1; overallRatio = widthRatio < heightRatio ? widthRatio : heightRatio; XYPlot plot = (XYPlot) getChart().getPlot(); Font domainFont = plot.getDomainAxis().getLabelFont(); int domainSize = domainFont.getSize(); Font rangeFont = plot.getRangeAxis().getLabelFont(); int rangeSize = rangeFont.getSize(); TextTitle titleBlock = getChart().getTitle(); Font titleFont = null; int titleSize = 0; if (titleBlock != null) { titleFont = titleBlock.getFont(); titleSize = titleFont.getSize(); getChart().getTitle().setFont(titleFont.deriveFont((float) (titleSize * overallRatio))); } Font domainScaleFont = plot.getDomainAxis().getTickLabelFont(); int domainScaleSize = domainScaleFont.getSize(); Font rangeScaleFont = plot.getRangeAxis().getTickLabelFont(); int rangeScaleSize = rangeScaleFont.getSize(); plot.getDomainAxis().setLabelFont(domainFont.deriveFont((float) (domainSize * overallRatio))); plot.getRangeAxis().setLabelFont(rangeFont.deriveFont((float) (rangeSize * overallRatio))); plot.getDomainAxis().setTickLabelFont(domainScaleFont.deriveFont((float) (domainScaleSize * overallRatio))); plot.getRangeAxis().setTickLabelFont(rangeScaleFont.deriveFont((float) (rangeScaleSize * overallRatio))); LegendTitle legend = getChart().getLegend(); Font legendFont = null; int legendFontSize = 0; if (legend != null) { legendFont = legend.getItemFont(); legendFontSize = legendFont.getSize(); legend.setItemFont(legendFont.deriveFont((float) (legendFontSize * overallRatio))); } Rectangle2D chartArea = (Rectangle2D) area.clone(); getChart().getPadding().trim(chartArea); if (titleBlock != null) { AxisUtilities.trimTitle(chartArea, g2, titleBlock, titleBlock.getPosition()); } Axis scaleAxis = null; Font scaleAxisFont = null; int scaleAxisFontSize = 0; for (Object object : getChart().getSubtitles()) { Title title = (Title) object; if (title instanceof PaintScaleLegend) { scaleAxis = ((PaintScaleLegend) title).getAxis(); scaleAxisFont = scaleAxis.getTickLabelFont(); scaleAxisFontSize = scaleAxisFont.getSize(); scaleAxis.setTickLabelFont(scaleAxisFont.deriveFont((float) (scaleAxisFontSize * overallRatio))); } AxisUtilities.trimTitle(chartArea, g2, title, title.getPosition()); } AxisSpace axisSpace = AxisUtilities.calculateAxisSpace(getChart().getXYPlot(), g2, chartArea); Rectangle2D dataArea = axisSpace.shrink(chartArea, null); getChart().getXYPlot().getInsets().trim(dataArea); getChart().getXYPlot().getAxisOffset().trim(dataArea); // Rectangle2D screenArea = getScreenDataArea(); // Rectangle2D visibleArea = getVisibleRect(); // Rectangle2D printScreenArea = new Rectangle2D.Double(screenArea.getMinX() * overallRatio + x, // screenArea.getMinY() * overallRatio + y, // printArea.getWidth() - visibleArea.getWidth() + screenArea.getWidth(), // printArea.getHeight() - visibleArea.getHeight() + screenArea.getHeight()); getChart().draw(g2, area, getAnchor(), null); ChartMaskingUtilities.drawMasks(g2, dataArea, maskList, null, getChart(), overallRatio); ChartMaskingUtilities.drawShapes(g2, dataArea, shapeMap, getChart()); ChartMaskingUtilities.drawText(g2, dataArea, textContentMap, getChart()); plot.getDomainAxis().setLabelFont(domainFont); plot.getRangeAxis().setLabelFont(rangeFont); if (titleBlock != null) { titleBlock.setFont(titleFont); } if (legend != null) { legend.setItemFont(legendFont); } plot.getDomainAxis().setTickLabelFont(domainScaleFont); plot.getRangeAxis().setTickLabelFont(rangeScaleFont); if (scaleAxis != null) { scaleAxis.setTickLabelFont(scaleAxisFont); } // System.out.println("print " + titleBlock.getText() + // " at [" + area.getX() + ", " + area.getY() + ", " + // area.getWidth() + ", " + area.getHeight() + "]"); }