List of usage examples for org.jfree.chart.title TextTitle setPaint
public void setPaint(Paint paint)
From source file:uk.co.moonsit.sockets.GraphClient.java
private void setSubtitles(JFreeChart chart, String[] labelsArray) { for (String label : labelsArray) { int axis = Integer.parseInt(label.substring(label.length() - 1, label.length())); TextTitle tt = new TextTitle(label); tt.setPaint(getAxisColor(axis - 1)); chart.addSubtitle(tt);//w w w .j a va 2 s .c o m } }
From source file:nu.nethome.home.items.web.GraphServlet.java
/** * This is the main enterence point of the class. This is called when a http request is * routed to this servlet./*from ww w .ja v a 2s . c o m*/ */ public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { ServletOutputStream p = res.getOutputStream(); Date startTime = null; Date stopTime = null; // Analyse arguments String fileName = req.getParameter("file"); if (fileName != null) fileName = getFullFileName(fromURL(fileName)); String startTimeString = req.getParameter("start"); String stopTimeString = req.getParameter("stop"); try { if (startTimeString != null) { startTime = m_Format.parse(startTimeString); } if (stopTimeString != null) { stopTime = m_Format.parse(stopTimeString); } } catch (ParseException e1) { e1.printStackTrace(); } String look = req.getParameter("look"); if (look == null) look = ""; TimeSeries timeSeries = new TimeSeries("Data", Minute.class); // Calculate time window Calendar cal = Calendar.getInstance(); Date currentTime = cal.getTime(); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); Date startOfDay = cal.getTime(); cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); Date startOfWeek = cal.getTime(); cal.set(Calendar.DAY_OF_MONTH, 1); Date startOfMonth = cal.getTime(); cal.set(Calendar.MONTH, Calendar.JANUARY); Date startOfYear = cal.getTime(); // if (startTime == null) startTime = startOfWeek; if (stopTime == null) stopTime = currentTime; if (startTime == null) startTime = new Date(stopTime.getTime() - 1000L * 60L * 60L * 24L * 2L); try { // Open the data file File logFile = new File(fileName); Scanner fileScanner = new Scanner(logFile); Long startTimeMs = startTime.getTime(); Long month = 1000L * 60L * 60L * 24L * 30L; boolean doOptimize = true; boolean justOptimized = false; try { while (fileScanner.hasNext()) { try { // Get next log entry String line = fileScanner.nextLine(); if (line.length() > 21) { // Adapt the time format String minuteTime = line.substring(0, 16).replace('.', '-'); // Parse the time stamp Minute min = Minute.parseMinute(minuteTime); // Ok, this is an ugly optimization. If the current time position in the file // is more than a month (30 days) ahead of the start of the time window, we // quick read two weeks worth of data, assuming that there is 4 samples per hour. // This may lead to scanning past start of window if there are holes in the data // series. if (doOptimize && ((startTimeMs - min.getFirstMillisecond()) > month)) { for (int i = 0; (i < (24 * 4 * 14)) && fileScanner.hasNext(); i++) { fileScanner.nextLine(); } justOptimized = true; continue; } // Detect if we have scanned past the window start position just after an optimization scan. // If this is the case it may be because of the optimization. In that case we have to switch // optimization off and start over. if ((min.getFirstMillisecond() > startTimeMs) && doOptimize && justOptimized) { logFile = new File(fileName); fileScanner = new Scanner(logFile); doOptimize = false; continue; } justOptimized = false; // Check if value is within time window if ((min.getFirstMillisecond() > startTimeMs) && (min.getFirstMillisecond() < stopTime.getTime())) { // Parse the value double value = Double.parseDouble((line.substring(20)).replace(',', '.')); // Add the entry timeSeries.add(min, value); doOptimize = false; } } } catch (SeriesException se) { // Bad entry, for example due to duplicates at daylight saving time switch } catch (NumberFormatException nfe) { // Bad number format in a line, try to continue } } } catch (Exception e) { System.out.println(e.toString()); } finally { fileScanner.close(); } } catch (FileNotFoundException f) { System.out.println(f.toString()); } // Create a collection for plotting TimeSeriesCollection data = new TimeSeriesCollection(); data.addSeries(timeSeries); JFreeChart chart; int xSize = 750; int ySize = 450; // Customize colors and look of the Graph. if (look.equals("mobtemp")) { // Look for the mobile GUI chart = ChartFactory.createTimeSeriesChart(null, null, null, data, false, false, false); XYPlot plot = chart.getXYPlot(); ValueAxis timeAxis = plot.getDomainAxis(); timeAxis.setAxisLineVisible(false); ValueAxis valueAxis = plot.getRangeAxis(0); valueAxis.setAxisLineVisible(false); xSize = 175; ySize = 180; } else { // Create a Chart with time range as heading SimpleDateFormat localFormat = new SimpleDateFormat(); String heading = localFormat.format(startTime) + " - " + localFormat.format(stopTime); chart = ChartFactory.createTimeSeriesChart(heading, null, null, data, false, false, false); Paint background = new Color(0x9D8140); chart.setBackgroundPaint(background); TextTitle title = chart.getTitle(); // fix title Font titleFont = title.getFont(); titleFont = titleFont.deriveFont(Font.PLAIN, (float) 14.0); title.setFont(titleFont); title.setPaint(Color.darkGray); XYPlot plot = chart.getXYPlot(); plot.setBackgroundPaint(background); plot.setDomainGridlinePaint(Color.darkGray); ValueAxis timeAxis = plot.getDomainAxis(); timeAxis.setAxisLineVisible(false); ValueAxis valueAxis = plot.getRangeAxis(0); valueAxis.setAxisLineVisible(false); plot.setRangeGridlinePaint(Color.darkGray); XYItemRenderer renderer = plot.getRenderer(0); renderer.setSeriesPaint(0, Color.darkGray); xSize = 750; ySize = 450; } try { res.setContentType("image/png"); res.setHeader("Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0"); res.setHeader("Pragma", "no-cache"); res.setStatus(HttpServletResponse.SC_OK); ChartUtilities.writeChartAsPNG(p, chart, xSize, ySize); } catch (IOException e) { System.err.println("Problem occurred creating chart."); } p.flush(); p.close(); return; }
From source file:com.xpn.xwiki.plugin.charts.ChartCustomizer.java
public static void customizeTitle(TextTitle title, ChartParams params, String prefix) { if (params.get(prefix + ChartParams.TITLE_FONT_SUFFIX) != null) { title.setFont(params.getFont(prefix + ChartParams.TITLE_FONT_SUFFIX)); } else {// ww w . j av a2 s . c o m title.setFont(JFreeChart.DEFAULT_TITLE_FONT); } if (params.get(prefix + ChartParams.TITLE_POSITION_SUFFIX) != null) { title.setPosition(params.getRectangleEdge(prefix + ChartParams.TITLE_POSITION_SUFFIX)); } if (params.get(prefix + ChartParams.TITLE_HORIZONTAL_ALIGNMENT_SUFFIX) != null) { title.setHorizontalAlignment( params.getHorizontalAlignment(prefix + ChartParams.TITLE_HORIZONTAL_ALIGNMENT_SUFFIX)); } if (params.get(prefix + ChartParams.TITLE_VERTICAL_ALIGNMENT_SUFFIX) != null) { title.setVerticalAlignment( params.getVerticalAlignment(prefix + ChartParams.TITLE_VERTICAL_ALIGNMENT_SUFFIX)); } if (params.get(prefix + ChartParams.TITLE_COLOR_SUFFIX) != null) { title.setPaint(params.getColor(prefix + ChartParams.TITLE_COLOR_SUFFIX)); } if (params.get(prefix + ChartParams.TITLE_BACKGROUND_COLOR_SUFFIX) != null) { title.setBackgroundPaint(params.getColor(prefix + ChartParams.TITLE_BACKGROUND_COLOR_SUFFIX)); } if (params.get(prefix + ChartParams.TITLE_PADDING_SUFFIX) != null) { title.setPadding(params.getRectangleInsets(prefix + ChartParams.TITLE_PADDING_SUFFIX)); } if (params.get(prefix + ChartParams.TITLE_URL_SUFFIX) != null) { title.setURLText(params.getString(prefix + ChartParams.TITLE_URL_SUFFIX)); } }
From source file:org.jfree.chart.demo.SurveyResultsDemo.java
/** * Creates a chart.//from w w w . j a v a 2s . c o m * * @param dataset the dataset. * * @return The chart. */ private JFreeChart createChart(final CategoryDataset dataset) { final JFreeChart chart = ChartFactory.createBarChart(null, // chart title null, // domain axis label null, // range axis label dataset, // data PlotOrientation.HORIZONTAL, // orientation false, // include legend true, false); chart.setBackgroundPaint(Color.white); final TextTitle title = new TextTitle("Figure 7 | I. Resources - The site offers users relevant, " + "informative and educational resources"); title.setHorizontalAlignment(HorizontalAlignment.LEFT); title.setBackgroundPaint(Color.red); title.setPaint(Color.white); chart.setTitle(title); final CategoryPlot plot = (CategoryPlot) chart.getPlot(); plot.setOutlinePaint(null); plot.setDomainGridlinesVisible(true); plot.setDomainGridlinePosition(CategoryAnchor.END); plot.setDomainGridlineStroke(new BasicStroke(0.5f)); plot.setDomainGridlinePaint(Color.black); plot.setRangeGridlinesVisible(false); plot.clearRangeMarkers(); final CategoryAxis domainAxis = plot.getDomainAxis(); domainAxis.setVisible(false); domainAxis.setCategoryMargin(0.50); plot.getRangeAxis().setVisible(false); final CategoryItemRenderer renderer = plot.getRenderer(); renderer.setSeriesPaint(0, new Color(0x9C, 0xA4, 0x4A)); renderer.setOutlineStroke(null); renderer.setBaseOutlineStroke(null); renderer.setItemLabelsVisible(true); renderer.setItemLabelFont(new Font("SansSerif", Font.BOLD, 10)); final ItemLabelPosition position = new ItemLabelPosition(ItemLabelAnchor.INSIDE3, TextAnchor.CENTER_RIGHT); renderer.setPositiveItemLabelPosition(position); final CategoryTextAnnotation a1 = new CategoryTextAnnotation("1. White papers are available.", "Category 1", 0.0); a1.setFont(new Font("SansSerif", Font.BOLD, 12)); a1.setTextAnchor(TextAnchor.BOTTOM_LEFT); a1.setCategoryAnchor(CategoryAnchor.START); plot.addAnnotation(a1); final CategoryTextAnnotation a2 = new CategoryTextAnnotation( "2. White papers enhance users " + "understanding of the firm and its expertise.", "Category 2", 0.0); a2.setFont(new Font("SansSerif", Font.PLAIN, 12)); a2.setTextAnchor(TextAnchor.BOTTOM_LEFT); a2.setCategoryAnchor(CategoryAnchor.START); plot.addAnnotation(a2); final CategoryTextAnnotation a3 = new CategoryTextAnnotation( "3. White papers are relevant to " + "the firm's prospects and clients.", "Category 3", 0.0); a3.setFont(new Font("SansSerif", Font.PLAIN, 12)); a3.setTextAnchor(TextAnchor.BOTTOM_LEFT); a3.setCategoryAnchor(CategoryAnchor.START); plot.addAnnotation(a3); final CategoryTextAnnotation a4 = new CategoryTextAnnotation( "4. White papers are relevant to " + "the firm's positioning.", "Category 4", 0.0); a4.setFont(new Font("SansSerif", Font.PLAIN, 12)); a4.setTextAnchor(TextAnchor.BOTTOM_LEFT); a4.setCategoryAnchor(CategoryAnchor.START); plot.addAnnotation(a4); final CategoryTextAnnotation a5 = new CategoryTextAnnotation("5. Case studies are available.", "Category 5", 0.0); a5.setFont(new Font("SansSerif", Font.BOLD, 12)); a5.setTextAnchor(TextAnchor.BOTTOM_LEFT); a5.setCategoryAnchor(CategoryAnchor.START); plot.addAnnotation(a5); final CategoryTextAnnotation a6 = new CategoryTextAnnotation( "6. Case studies enhance users " + "understanding of the firm and its expertise.", "Category 6", 0.0); a6.setFont(new Font("SansSerif", Font.PLAIN, 12)); a6.setTextAnchor(TextAnchor.BOTTOM_LEFT); a6.setCategoryAnchor(CategoryAnchor.START); plot.addAnnotation(a6); final CategoryTextAnnotation a7 = new CategoryTextAnnotation( "7. Case studies are relevant to " + "the firm's prospects and clients.", "Category 7", 0.0); a7.setFont(new Font("SansSerif", Font.PLAIN, 12)); a7.setTextAnchor(TextAnchor.BOTTOM_LEFT); a7.setCategoryAnchor(CategoryAnchor.START); plot.addAnnotation(a7); final CategoryTextAnnotation a8 = new CategoryTextAnnotation( "8. White papers are relevant to the firm's positioning.", "Category 8", 0.0); a8.setFont(new Font("SansSerif", Font.PLAIN, 12)); a8.setTextAnchor(TextAnchor.BOTTOM_LEFT); a8.setCategoryAnchor(CategoryAnchor.START); plot.addAnnotation(a8); final CategoryTextAnnotation a9 = new CategoryTextAnnotation("9. Case studies are available.", "Category 9", 0.0); a9.setFont(new Font("SansSerif", Font.BOLD, 12)); a9.setTextAnchor(TextAnchor.BOTTOM_LEFT); a9.setCategoryAnchor(CategoryAnchor.START); plot.addAnnotation(a9); final CategoryTextAnnotation a10 = new CategoryTextAnnotation( "10. Case studies enhance users " + "understanding of the firm and its expertise.", "Category 10", 0.0); a10.setFont(new Font("SansSerif", Font.PLAIN, 12)); a10.setTextAnchor(TextAnchor.BOTTOM_LEFT); a10.setCategoryAnchor(CategoryAnchor.START); plot.addAnnotation(a10); final CategoryTextAnnotation a11 = new CategoryTextAnnotation( "11. Case studies are relevant " + "to the firm's prospects and clients.", "Category 11", 0.0); a11.setFont(new Font("SansSerif", Font.PLAIN, 12)); a11.setTextAnchor(TextAnchor.BOTTOM_LEFT); a11.setCategoryAnchor(CategoryAnchor.START); plot.addAnnotation(a11); final CategoryTextAnnotation a12 = new CategoryTextAnnotation( "12. White papers are relevant to the firm's positioning.", "Category 12", 0.0); a12.setFont(new Font("SansSerif", Font.PLAIN, 12)); a12.setTextAnchor(TextAnchor.BOTTOM_LEFT); a12.setCategoryAnchor(CategoryAnchor.START); plot.addAnnotation(a12); final CategoryTextAnnotation a13 = new CategoryTextAnnotation( "13. Users can easily access " + "resources based on viewer interest.", "Category 13", 0.0); a13.setFont(new Font("SansSerif", Font.BOLD, 12)); a13.setTextAnchor(TextAnchor.BOTTOM_LEFT); a13.setCategoryAnchor(CategoryAnchor.START); plot.addAnnotation(a13); final CategoryTextAnnotation a14 = new CategoryTextAnnotation( "14. Access to additional hyperlinks enhances users's ability to find relevant " + "information.", "Category 14", 0.0); a14.setFont(new Font("SansSerif", Font.BOLD, 12)); a14.setTextAnchor(TextAnchor.BOTTOM_LEFT); a14.setCategoryAnchor(CategoryAnchor.START); plot.addAnnotation(a14); final CategoryTextAnnotation a15 = new CategoryTextAnnotation("15. OVERALL EFFECTIVENESS.", "Overall", 0.0); a15.setFont(new Font("SansSerif", Font.BOLD, 12)); a15.setTextAnchor(TextAnchor.BOTTOM_LEFT); a15.setCategoryAnchor(CategoryAnchor.START); plot.addAnnotation(a15); return chart; }
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);//from ww w.j a va2 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.sf.jasperreports.components.spiderchart.FillSpiderChart.java
protected JFreeChart evaluateChart(byte evaluation) throws JRException { maxValue = (Double) fillContext.evaluate(getPlot().getMaxValueExpression(), evaluation); titleText = (String) fillContext.evaluate(getChartSettings().getTitleExpression(), evaluation); subtitleText = (String) fillContext.evaluate(getChartSettings().getSubtitleExpression(), evaluation); anchorName = (String) fillContext.evaluate(getChartSettings().getAnchorNameExpression(), evaluation); hyperlinkReference = (String) fillContext.evaluate(getChartSettings().getHyperlinkReferenceExpression(), evaluation);//from www. j a v a 2 s . c o m hyperlinkAnchor = (String) fillContext.evaluate(getChartSettings().getHyperlinkAnchorExpression(), evaluation); hyperlinkPage = (Integer) fillContext.evaluate(getChartSettings().getHyperlinkPageExpression(), evaluation); hyperlinkTooltip = (String) fillContext.evaluate(getChartSettings().getHyperlinkTooltipExpression(), evaluation); hyperlinkParameters = JRFillHyperlinkHelper.evaluateHyperlinkParameters(getChartSettings(), expressionEvaluator, evaluation); dataset.evaluateDatasetRun(evaluation); dataset.finishDataset(); chartHyperlinkProvider = new CategoryChartHyperlinkProvider(dataset.getItemHyperlinks()); bookmarkLevel = getChartSettings().getBookmarkLevel(); SpiderWebPlot spiderWebPlot = new SpiderWebPlot((DefaultCategoryDataset) dataset.getCustomDataset()); if (getPlot().getAxisLineColor() != null) { spiderWebPlot.setAxisLinePaint(getPlot().getAxisLineColor()); } if (getPlot().getAxisLineWidth() != null) { spiderWebPlot.setAxisLineStroke(new BasicStroke(getPlot().getAxisLineWidth())); } if (getPlot().getBackcolor() != null) { spiderWebPlot.setBackgroundPaint(getPlot().getBackcolor()); } if (getPlot().getBackgroundAlpha() != null) { spiderWebPlot.setBackgroundAlpha(getPlot().getBackgroundAlpha()); } if (getPlot().getForegroundAlpha() != null) { spiderWebPlot.setForegroundAlpha(getPlot().getForegroundAlpha()); } if (getPlot().getHeadPercent() != null) { spiderWebPlot.setHeadPercent(getPlot().getHeadPercent()); } if (getPlot().getInteriorGap() != null) { spiderWebPlot.setInteriorGap(getPlot().getInteriorGap()); } if (getPlot().getLabelColor() != null) { spiderWebPlot.setLabelPaint(getPlot().getLabelColor()); } if (getPlot().getLabelFont() != null) { spiderWebPlot.setLabelFont(JRFontUtil.getAwtFont(getPlot().getLabelFont(), Locale.getDefault())); } if (getPlot().getLabelGap() != null) { spiderWebPlot.setAxisLabelGap(getPlot().getLabelGap()); } if (maxValue != null) { spiderWebPlot.setMaxValue(maxValue); } if (getPlot().getRotation() != null) { spiderWebPlot.setDirection(getPlot().getRotation().getRotation()); } if (getPlot().getStartAngle() != null) { spiderWebPlot.setStartAngle(getPlot().getStartAngle()); } if (getPlot().getTableOrder() != null) { spiderWebPlot.setDataExtractOrder(getPlot().getTableOrder().getOrder()); } if (getPlot().getWebFilled() != null) { spiderWebPlot.setWebFilled(getPlot().getWebFilled()); } spiderWebPlot.setToolTipGenerator(new StandardCategoryToolTipGenerator()); spiderWebPlot.setLabelGenerator(new StandardCategoryItemLabelGenerator()); Font titleFont = getChartSettings().getTitleFont() != null ? JRFontUtil.getAwtFont(getChartSettings().getTitleFont(), Locale.getDefault()) : TextTitle.DEFAULT_FONT; JFreeChart jfreechart = new JFreeChart(titleText, titleFont, spiderWebPlot, true); if (chartSettings.getBackcolor() != null) { jfreechart.setBackgroundPaint(chartSettings.getBackcolor()); } RectangleEdge titleEdge = getEdge(getChartSettings().getTitlePosition(), RectangleEdge.TOP); if (titleText != null) { TextTitle title = jfreechart.getTitle(); title.setText(titleText); if (getChartSettings().getTitleColor() != null) { title.setPaint(getChartSettings().getTitleColor()); } title.setFont(titleFont); title.setPosition(titleEdge); jfreechart.setTitle(title); } if (subtitleText != null) { TextTitle subtitle = new TextTitle(subtitleText); subtitle.setText(subtitleText); if (getChartSettings().getSubtitleColor() != null) { subtitle.setPaint(getChartSettings().getSubtitleColor()); } if (getChartSettings().getSubtitleColor() != null) { Font subtitleFont = getChartSettings().getSubtitleFont() != null ? JRFontUtil.getAwtFont(getChartSettings().getSubtitleFont(), Locale.getDefault()) : TextTitle.DEFAULT_FONT; subtitle.setFont(subtitleFont); } subtitle.setPosition(titleEdge); jfreechart.addSubtitle(subtitle); } // Apply all of the legend formatting options LegendTitle legend = jfreechart.getLegend(); if (legend != null) { legend.setVisible((chartSettings.getShowLegend() == null || chartSettings.getShowLegend())); if (legend.isVisible()) { if (getChartSettings().getLegendColor() != null) { legend.setItemPaint(getChartSettings().getLegendColor()); } if (getChartSettings().getLegendBackgroundColor() != null) { legend.setBackgroundPaint(getChartSettings().getLegendBackgroundColor()); } if (getChartSettings().getLegendFont() != null) { legend.setItemFont( JRFontUtil.getAwtFont(getChartSettings().getLegendFont(), Locale.getDefault())); } legend.setPosition(getEdge(getChartSettings().getLegendPosition(), RectangleEdge.BOTTOM)); } } return jfreechart; }
From source file:com.bdaum.zoom.report.internal.wizards.ReportComponent.java
private static void applyProperties(JFreeChart chart, Map<String, Object> properties) { if (properties != null) { String text = (String) properties.get(TITLE); if (text != null) { TextTitle title = chart.getTitle(); if (title == null) { title = new TextTitle(); chart.setTitle(title);// w ww . j a va 2 s . c o m } title.setText(text); Font titleFont = (Font) properties.get(TITLEFONT); if (titleFont != null) title.setFont(titleFont); Paint paint = (Paint) properties.get(TITLECOLOR); if (paint != null) title.setPaint(paint); } else chart.setTitle((TextTitle) null); Plot plot = chart.getPlot(); Paint paint = (Paint) properties.get(BGCOLOR); if (paint != null) plot.setBackgroundPaint(paint); paint = (Color) properties.get(OUTLINEPAINT); if (paint != null) plot.setOutlinePaint(paint); Stroke stroke = (Stroke) properties.get(OUTLINESTROKE); if (stroke != null) plot.setOutlineStroke(stroke); PlotOrientation orientation = (PlotOrientation) properties.get(ORIENTATION); Axis domainAxis = null; Axis rangeAxis = null; if (plot instanceof CategoryPlot) { CategoryPlot p = (CategoryPlot) plot; domainAxis = p.getDomainAxis(); rangeAxis = p.getRangeAxis(); if (orientation != null) p.setOrientation(orientation); } else if (plot instanceof XYPlot) { XYPlot p = (XYPlot) plot; domainAxis = p.getDomainAxis(); rangeAxis = p.getRangeAxis(); if (orientation != null) p.setOrientation(orientation); } if (domainAxis != null) applyAxisProperties(domainAxis, "x", properties); //$NON-NLS-1$ if (rangeAxis != null) applyAxisProperties(rangeAxis, "y", properties); //$NON-NLS-1$ Boolean anti = (Boolean) properties.get(ANTIALIAS); if (anti != null) chart.setAntiAlias(anti); paint = (Paint) properties.get(CANVASPAINT); if (paint != null) chart.setBackgroundPaint(paint); } }
From source file:org.jfree.eastwood.ChartEngine.java
/** * Creates and returns a new <code>JFreeChart</code> instance that * reflects the specified parameters (which should be equivalent to * a parameter map returned by HttpServletRequest.getParameterMap() for * a valid URI for the Google Chart API. * * @param params the parameters (<code>null</code> not permitted). * @param font the font to use to draw titles, labels and legends. * * @return A chart corresponding to the specification in the supplied * parameters./*from w w w . j ava2 s.c o m*/ */ public static JFreeChart buildChart(Map params, Font font) { if (params == null) { throw new IllegalArgumentException("Null 'params' argument."); } JFreeChart chart = null; // *** CHART TYPE ** String[] chartType = (String[]) params.get("cht"); int dataType = -1; // 0 = PieDataset; 1 = XYDataset; 2 = special // pie charts: 'p' and 'p3' if (chartType[0].equals("p")) { chart = createPieChart(); dataType = 0; } else if (chartType[0].equals("p3")) { chart = createPieChart3D(); dataType = 0; } // line chart: 'lc' else if (chartType[0].equals("lc")) { chart = createLineChart(); dataType = 1; } // sparkline: 'ls' else if (chartType[0].equals("ls")) { chart = createSparklineChart(); dataType = 1; } // xy chart: 'lxy' else if (chartType[0].equals("lxy")) { chart = createLineChart(); dataType = 3; } // bar charts: 'bhs', 'bhg', 'bhs' and 'bhg' else if (chartType[0].equals("bhs")) { chart = createStackedBarChart(PlotOrientation.HORIZONTAL); dataType = 2; } else if (chartType[0].equals("bhg")) { chart = createBarChart(PlotOrientation.HORIZONTAL); dataType = 2; } else if (chartType[0].equals("bvs")) { chart = createStackedBarChart(PlotOrientation.VERTICAL); dataType = 2; } else if (chartType[0].equals("bvg")) { chart = createBarChart(PlotOrientation.VERTICAL); dataType = 2; } else if (chartType[0].equals("bhs3")) { chart = createStackedBarChart3D(PlotOrientation.HORIZONTAL); dataType = 2; } else if (chartType[0].equals("bhg3")) { chart = createBarChart3D(PlotOrientation.HORIZONTAL); dataType = 2; } else if (chartType[0].equals("bvs3")) { chart = createStackedBarChart3D(PlotOrientation.VERTICAL); dataType = 2; } else if (chartType[0].equals("bvg3")) { chart = createBarChart3D(PlotOrientation.VERTICAL); dataType = 2; } // scatter chart: 's' else if (chartType[0].equals("s")) { chart = createScatterChart(); dataType = 4; } else if (chartType[0].equals("v")) { throw new RuntimeException("Venn diagrams not implemented."); // TODO: fix this. } else { throw new RuntimeException("Unknown chart type: " + chartType[0]); } chart.getPlot().setOutlineVisible(false); // *** CHART AXES *** List axes = new java.util.ArrayList(); String[] axisStr = (String[]) params.get("chxt"); if (axisStr != null) { if (chart.getPlot() instanceof XYPlot) { XYPlot plot = (XYPlot) chart.getPlot(); processAxisStr(plot, axisStr[0], axes); } else if (chart.getPlot() instanceof CategoryPlot) { CategoryPlot plot = (CategoryPlot) chart.getPlot(); if (plot.getOrientation() == PlotOrientation.VERTICAL) { processAxisStrV(plot, axisStr[0], axes); } else { processAxisStrH(plot, axisStr[0], axes); } } } // *** AXIS RANGES *** String[] axisRangeStr = (String[]) params.get("chxr"); if (axisRangeStr != null) { String[] ranges = breakString(axisRangeStr[0], '|'); for (int i = 0; i < ranges.length; i++) { int comma1 = ranges[i].indexOf(','); int comma2 = ranges[i].indexOf(',', comma1 + 1); int axisIndex = Integer.parseInt(ranges[i].substring(0, comma1)); float lowerBound = Float.parseFloat(ranges[i].substring(comma1 + 1, comma2)); float upperBound = Float.parseFloat(ranges[i].substring(comma2 + 1)); Axis axis = (Axis) axes.get(axisIndex); if (axis instanceof GValueAxis) { GValueAxis gaxis = (GValueAxis) axis; gaxis.setLabelAxisStart(lowerBound); gaxis.setLabelAxisEnd(upperBound); } } } // *** AXIS LABELS *** String[] axisLabelStr = (String[]) params.get("chxl"); if (axisLabelStr != null) { Pattern p = Pattern.compile("\\d+:\\|"); Matcher m = p.matcher(axisLabelStr[0]); if (m.find()) { int keyStart = m.start(); int labelStart = m.end(); while (m.find(labelStart)) { String keyStr = axisLabelStr[0].substring(keyStart, labelStart - 2); int axisIndex = Integer.parseInt(keyStr); keyStart = m.start(); String labelStr = axisLabelStr[0].substring(labelStart, keyStart - 1); String[] labels = breakString(labelStr, '|'); GLabelledAxis axis = (GLabelledAxis) axes.get(axisIndex); axis.setTickLabels(Arrays.asList(labels)); labelStart = m.end(); } // process the final item String keyStr = axisLabelStr[0].substring(keyStart, labelStart - 2); String labelStr = axisLabelStr[0].substring(labelStart); int axisIndex = Integer.parseInt(keyStr); if (labelStr.endsWith("|")) { labelStr = labelStr.substring(0, labelStr.length() - 1); } String[] labels = breakString(labelStr, '|'); GLabelledAxis axis = (GLabelledAxis) axes.get(axisIndex); axis.setTickLabels(Arrays.asList(labels)); } else { throw new RuntimeException("No matching pattern!"); } } // ** EXPLICIT AXIS LABEL POSITIONS String[] axisPositionStr = (String[]) params.get("chxp"); if (axisPositionStr != null) { String[] positions = breakString(axisPositionStr[0], '|'); for (int i = 0; i < positions.length; i++) { int c1 = positions[i].indexOf(','); int axisIndex = Integer.parseInt(positions[i].substring(0, c1)); String remainingStr = positions[i].substring(c1 + 1); String[] valueStr = breakString(remainingStr, ','); List tickValues = new java.util.ArrayList(valueStr.length); Axis axis = (Axis) axes.get(axisIndex); if (axis instanceof GValueAxis) { GValueAxis gaxis = (GValueAxis) axes.get(axisIndex); for (int j = 0; j < valueStr.length; j++) { float pos = Float.parseFloat(valueStr[j]); tickValues.add(new Float(pos)); } gaxis.setTickLabelPositions(tickValues); } // FIXME: what about a CategoryAxis? } } // *** CHART TITLE *** String[] titleStr = (String[]) params.get("chtt"); if (titleStr != null) { // process the title String[] s = breakString(titleStr[0], '|'); for (int i = 0; i < s.length; i++) { TextTitle t = new TextTitle(s[i].replace('+', ' ')); t.setPaint(Color.darkGray); // Google seems to use 14pt fonts for titles and 12pt fonts for // all other text. Make sure this relationship remains. t.setFont(font.deriveFont(font.getSize2D() * 14f / 12f)); chart.addSubtitle(t); } // and the font and colour String[] fontStr = (String[]) params.get("chts"); if (fontStr != null) { int c1 = fontStr[0].indexOf(','); String colorStr = null; String fontSizeStr = null; if (c1 != -1) { colorStr = fontStr[0].substring(0, c1); fontSizeStr = fontStr[0].substring(c1 + 1); } else { colorStr = fontStr[0]; } Color color = parseColor(colorStr); int size = 12; if (fontSizeStr != null) { size = Integer.parseInt(fontSizeStr); } for (int i = 0; i < chart.getSubtitleCount(); i++) { Title t = chart.getSubtitle(i); if (t instanceof TextTitle) { TextTitle tt = (TextTitle) t; tt.setPaint(color); tt.setFont(font.deriveFont((float) size)); } } } } // *** CHART DATA *** String[] dataStr = (String[]) params.get("chd"); String scalingStr = null; if (dataStr.length > 0 && dataStr[0].startsWith("t:")) { // Only look at chds when text encoding is used String[] chds = (String[]) params.get("chds"); if (chds != null && chds.length > 0) { scalingStr = chds[0]; } } // we'll also process an optional second dataset that is provided as // an Eastwood extension...this isn't part of the Google Chart API String[] d2Str = (String[]) params.get("ewd2"); // 'p' and 'p3' - create PieDataset if (dataType == 0) { PieDataset dataset = DataUtilities.parsePieDataset(dataStr[0], scalingStr); PiePlot plot = (PiePlot) chart.getPlot(); plot.setDataset(dataset); // ignore d2Str as there is currently no need for a second pie // dataset. } // 'lc' - create XYDataset else if (dataType == 1) { XYPlot plot = (XYPlot) chart.getPlot(); XYDataset dataset = DataUtilities.parseXYDataset(dataStr[0], scalingStr); plot.setDataset(dataset); if (d2Str != null) { // an Eastwood extension XYDataset d2 = DataUtilities.parseXYDataset(d2Str[0], scalingStr); plot.setDataset(1, d2); } } // 'bhs', 'bhg', 'bvs', 'bvg' else if (dataType == 2) { CategoryPlot plot = (CategoryPlot) chart.getPlot(); CategoryDataset dataset = DataUtilities.parseCategoryDataset(dataStr[0], scalingStr); plot.setDataset(dataset); if (d2Str != null) { // an Eastwood extension CategoryDataset d2 = DataUtilities.parseCategoryDataset(d2Str[0], scalingStr); plot.setDataset(1, d2); } } // 'lxy' else if (dataType == 3) { XYPlot plot = (XYPlot) chart.getPlot(); XYDataset dataset = DataUtilities.parseXYDataset2(dataStr[0], scalingStr); plot.setDataset(dataset); if (d2Str != null) { // an Eastwood extension XYDataset d2 = DataUtilities.parseXYDataset2(d2Str[0], scalingStr); plot.setDataset(1, d2); } } else if (dataType == 4) { XYPlot plot = (XYPlot) chart.getPlot(); XYSeriesCollection dataset = DataUtilities.parseScatterDataset(dataStr[0], scalingStr); if (dataset.getSeriesCount() > 1) { dataset.removeSeries(1); } plot.setDataset(dataset); if (d2Str != null) { // an Eastwood extension XYDataset d2 = DataUtilities.parseXYDataset2(d2Str[0], scalingStr); plot.setDataset(1, d2); } } if (chart.getPlot() instanceof XYPlot) { XYPlot plot = (XYPlot) chart.getPlot(); plot.getDomainAxis().setLabelFont(font); plot.getDomainAxis().setTickLabelFont(font); plot.getRangeAxis().setLabelFont(font); plot.getRangeAxis().setTickLabelFont(font); } else if (chart.getPlot() instanceof CategoryPlot) { CategoryPlot plot = (CategoryPlot) chart.getPlot(); plot.getDomainAxis().setLabelFont(font); plot.getDomainAxis().setTickLabelFont(font); plot.getRangeAxis().setLabelFont(font); plot.getRangeAxis().setTickLabelFont(font); } // *** CHART COLOURS *** String[] colorStr = (String[]) params.get("chco"); if (colorStr != null) { Color[] colors = parseColors(colorStr[0]); if (dataType == 0) { PiePlot plot = (PiePlot) chart.getPlot(); applyColorsToPiePlot(plot, colors); } else { AbstractRenderer renderer = null; if (chart.getPlot() instanceof CategoryPlot) { CategoryPlot plot = (CategoryPlot) chart.getPlot(); renderer = (AbstractRenderer) plot.getRenderer(); renderer.setBasePaint(colors[0]); } else if (chart.getPlot() instanceof XYPlot) { XYPlot plot = (XYPlot) chart.getPlot(); renderer = (AbstractRenderer) plot.getRenderer(); renderer.setBasePaint(colors[colors.length - 1]); } for (int i = 0; i < colors.length; i++) { renderer.setSeriesPaint(i, colors[i]); } } } else { Plot plot = chart.getPlot(); if (plot instanceof PiePlot) { applyColorsToPiePlot((PiePlot) chart.getPlot(), new Color[] { new Color(255, 153, 0) }); } } // *** CHART LINE STYLES *** String[] lineStr = (String[]) params.get("chls"); if (lineStr != null && chart.getPlot() instanceof XYPlot) { Stroke[] strokes = parseLineStyles(lineStr[0]); XYPlot plot = (XYPlot) chart.getPlot(); XYItemRenderer renderer = plot.getRenderer(); for (int i = 0; i < strokes.length; i++) { renderer.setSeriesStroke(i, strokes[i]); } renderer.setBaseStroke(strokes[strokes.length - 1]); } // *** CHART GRID LINES if (dataType != 0) { String[] gridStr = (String[]) params.get("chg"); if (gridStr != null) { processGridLinesSpec(gridStr[0], chart); } } // *** CHART LABELS if (dataType == 0) { // pie chart String[] labelStr = (String[]) params.get("chl"); if (labelStr != null) { String[] s = breakString(labelStr[0], '|'); List labels = Arrays.asList(s); PiePlot plot = (PiePlot) chart.getPlot(); if (labels.size() > 0) { plot.setLabelGenerator(new GPieSectionLabelGenerator(labels)); plot.setLabelFont(font); plot.setLabelPaint(Color.darkGray); } } } String[] legendStr = (String[]) params.get("chdl"); if (legendStr != null) { // process the title String[] s = breakString(legendStr[0], '|'); List labels = Arrays.asList(s); if (labels.size() > 0) { Plot p = chart.getPlot(); if (p instanceof CategoryPlot) { CategoryPlot plot = (CategoryPlot) chart.getPlot(); BarRenderer renderer = (BarRenderer) plot.getRenderer(); renderer.setLegendItemLabelGenerator(new GSeriesLabelGenerator(labels)); renderer.setBaseSeriesVisibleInLegend(false); for (int i = 0; i < labels.size(); i++) { renderer.setSeriesVisibleInLegend(i, Boolean.TRUE); } } else if (p instanceof XYPlot) { XYPlot plot = (XYPlot) chart.getPlot(); XYItemRenderer renderer = plot.getRenderer(); renderer.setLegendItemLabelGenerator(new GSeriesLabelGenerator(labels)); renderer.setBaseSeriesVisibleInLegend(false); for (int i = 0; i < labels.size(); i++) { renderer.setSeriesVisibleInLegend(i, Boolean.TRUE); } } else if (p instanceof PiePlot) { PiePlot plot = (PiePlot) chart.getPlot(); plot.setLegendLabelGenerator(new GPieSectionLabelGenerator(labels)); } LegendTitle legend = new LegendTitle(chart.getPlot()); RectangleEdge pos = RectangleEdge.RIGHT; String[] chdlp = (String[]) params.get("chdlp"); if (chdlp != null) { if ("b".equals(chdlp[0])) { pos = RectangleEdge.BOTTOM; } else if ("t".equals(chdlp[0])) { pos = RectangleEdge.TOP; } else if ("l".equals(chdlp[0])) { pos = RectangleEdge.LEFT; } } legend.setPosition(pos); legend.setItemFont(font); legend.setItemPaint(Color.darkGray); chart.addSubtitle(legend); } } // *** CHART MARKERS *** String[] markerStr = (String[]) params.get("chm"); if (markerStr != null) { String[] markers = breakString(markerStr[0], '|'); for (int i = 0; i < markers.length; i++) { addMarker(markers[i], chart); } } // *** CHART FILL ***/ String[] fillStr = (String[]) params.get("chf"); if (fillStr != null) { // process the 1 or 2 fill specs int i = fillStr[0].indexOf('|'); if (i == -1) { processFillSpec(fillStr[0], chart); } else { String fs1 = fillStr[0].substring(0, i); String fs2 = fillStr[0].substring(i + 1); processFillSpec(fs1, chart); processFillSpec(fs2, chart); } } // process the 'ewtr' tag, if present processEWTR(params, chart); return chart; }
From source file:org.toobsframework.pres.chart.ChartBuilder.java
private JFreeChart finishChart(IRequest componentRequest, ChartDefinition chartDef, Plot plot, Map params) { JFreeChart chart = new JFreeChart( ChartUtil.evaluateTextLabel(componentRequest, chartDef.getTitle(), params), ChartUtil.getFont(chartDef.getTitle(), JFreeChart.DEFAULT_TITLE_FONT), plot, chartDef.isShowLegend());//from w w w . j a v a2s . c o m if (chartDef.getSubtitle() != null) { TextTitle subtitle = new TextTitle( ChartUtil.evaluateTextLabel(componentRequest, chartDef.getSubtitle(), params)); if (chartDef.getSubtitle().getFont() != null) { subtitle.setFont(ChartUtil.getFont(chartDef.getSubtitle(), null)); } subtitle.setPosition(ChartUtil.getPosition(chartDef.getSubtitle().getPosition())); subtitle.setPadding( ChartUtil.getRectangle(componentRequest, chartDef.getSubtitle().getPadding(), params)); subtitle.setVerticalAlignment( ChartUtil.getVerticalAlignment(chartDef.getSubtitle().getVerticalAlignment())); subtitle.setPaint(ChartUtil.getColor(chartDef.getSubtitle().getColor())); chart.addSubtitle(subtitle); } chart.setBackgroundPaint(ChartUtil.getColor(chartDef.getBackgroundColor())); if (chartDef.getTitle() != null && chartDef.getTitle().getColor() != null) { chart.getTitle().setPaint(ChartUtil.getColor(chartDef.getTitle().getColor())); } if (chartDef.isShowLegend()) { ChartUtil.configureLegend(componentRequest, chart, chartDef.getLegend(), params); } return chart; }
From source file:org.pentaho.chart.plugin.jfreechart.JFreeChartFactoryEngine.java
protected void initChart(JFreeChart chart, ChartModel chartModel) { if (chartModel.getBackground() instanceof Integer) { chart.setBackgroundPaint(new Color(0x00FFFFFF & (Integer) chartModel.getBackground())); } else {//from w ww . j a v a 2s .co m chart.setBackgroundPaint(Color.WHITE); } if ((chartModel.getTitle() != null) && (chartModel.getTitle().getText() != null) && (chartModel.getTitle().getText().trim().length() > 0)) { Font font = ChartUtils.getFont(chartModel.getTitle().getFontFamily(), chartModel.getTitle().getFontStyle(), chartModel.getTitle().getFontWeight(), chartModel.getTitle().getFontSize()); if (font != null) { chart.getTitle().setFont(font); } RectangleEdge rectangleEdge = RectangleEdge.TOP; if (chartModel.getTitle().getLocation() != null) { switch (chartModel.getTitle().getLocation()) { case RIGHT: rectangleEdge = RectangleEdge.BOTTOM; break; case LEFT: rectangleEdge = RectangleEdge.LEFT; break; case BOTTOM: rectangleEdge = RectangleEdge.BOTTOM; break; } } chart.getTitle().setPosition(rectangleEdge); if (RectangleEdge.isTopOrBottom(rectangleEdge)) { HorizontalAlignment horizontalAlignment = HorizontalAlignment.CENTER; if (chartModel.getTitle().getAlignment() != null) { switch (chartModel.getTitle().getAlignment()) { case LEFT: horizontalAlignment = horizontalAlignment.LEFT; break; case RIGHT: horizontalAlignment = horizontalAlignment.RIGHT; break; } } chart.getTitle().setHorizontalAlignment(horizontalAlignment); } } if ((chartModel.getLegend() != null) && chartModel.getLegend().getVisible()) { Font font = ChartUtils.getFont(chartModel.getLegend().getFontFamily(), chartModel.getLegend().getFontStyle(), chartModel.getLegend().getFontWeight(), chartModel.getLegend().getFontSize()); if (font != null) { chart.getLegend().setItemFont(font); } if (!chartModel.getLegend().getBorderVisible()) { chart.getLegend().setFrame(BlockBorder.NONE); } } chart.setBorderVisible(chartModel.getBorderVisible()); if (chartModel.getBorderColor() instanceof Integer) { chart.setBorderPaint(new Color(0x00FFFFFF & (Integer) chartModel.getBorderColor())); } for (StyledText subtitle : chartModel.getSubtitles()) { if ((subtitle.getText()) != null && (subtitle.getText().trim().length() > 0)) { TextTitle textTitle = new TextTitle(subtitle.getText()); Font font = ChartUtils.getFont(subtitle.getFontFamily(), subtitle.getFontStyle(), subtitle.getFontWeight(), subtitle.getFontSize()); if (font != null) { textTitle.setFont(font); } if (subtitle.getColor() != null) { textTitle.setPaint(new Color(0x00FFFFFF & subtitle.getColor())); } if (subtitle.getBackgroundColor() != null) { textTitle.setBackgroundPaint(new Color(0x00FFFFFF & subtitle.getBackgroundColor())); } chart.addSubtitle(textTitle); } } }