Example usage for org.jfree.ui HorizontalAlignment RIGHT

List of usage examples for org.jfree.ui HorizontalAlignment RIGHT

Introduction

In this page you can find the example usage for org.jfree.ui HorizontalAlignment RIGHT.

Prototype

HorizontalAlignment RIGHT

To view the source code for org.jfree.ui HorizontalAlignment RIGHT.

Click Source Link

Document

Right alignment.

Usage

From source file:com.tonbeller.jpivot.chart.ChartComponent.java

/**
 * Entry point for producing charts, called by wcf render tag.
 * Produces a jfreechart dataset from olap model, then creates a chart and
 * writes it to the servlet container temp directory.
 * Returns a DOM document for Renderer to transform into html.
 * Requires that jfreechart servlet is installed in this application context.
 *///from w w  w  .  j a  v a  2  s .  c  o  m
public Document render(RequestContext context) throws Exception {
    // check if we need to produce a new chart
    if (dirty) {
        // clear old listeners
        dispatcher.clear();
        this.result = olapModel.getResult();
        this.cellIterator = result.getCells().iterator();
        this.dimCount = result.getAxes().length;
        DefaultCategoryDataset dataset = null;
        switch (dimCount) {
        case 1:
            logger.info("1-dim data");
            dataset = build1dimDataset();
            break;
        case 2:
            logger.info("2-dim data");
            dataset = build2dimDataset();
            break;
        default:
            logger.error("less than 1 or more than 2 dimensions");
            throw new IllegalArgumentException("ChartRenderer requires a 1 or 2 dimensional result");
        }
        // re-set dirty flag
        dirty = false;

        // create font
        titleFont = new java.awt.Font(fontName, fontStyle, fontSize);

        CategoryURLGenerator urlGenerator = new jpivotCategoryURLGenerator(webControllerURL);

        // Create the chart object
        JFreeChart chart = null;
        switch (chartType) {
        case 1:
            chart = ChartFactory.createBarChart(chartTitle, titleFont, horizAxisLabel, vertAxisLabel, dataset,
                    PlotOrientation.VERTICAL, showLegend, showTooltips, drillThroughEnabled, urlGenerator);
            break;
        case 2:
            chart = ChartFactory.createBarChart3D(chartTitle, titleFont, horizAxisLabel, vertAxisLabel, dataset,
                    PlotOrientation.VERTICAL, showLegend, showTooltips, drillThroughEnabled, urlGenerator);
            break;
        case 3:
            chart = ChartFactory.createBarChart(chartTitle, titleFont, horizAxisLabel, vertAxisLabel, dataset,
                    PlotOrientation.HORIZONTAL, showLegend, showTooltips, drillThroughEnabled, urlGenerator);
            break;
        case 4:
            chart = ChartFactory.createBarChart3D(chartTitle, titleFont, horizAxisLabel, vertAxisLabel, dataset,
                    PlotOrientation.HORIZONTAL, showLegend, showTooltips, drillThroughEnabled, urlGenerator);
            break;
        case 5:
            chart = ChartFactory.createStackedBarChart(chartTitle, titleFont, horizAxisLabel, vertAxisLabel,
                    dataset, PlotOrientation.VERTICAL, showLegend, showTooltips, drillThroughEnabled,
                    urlGenerator);
            break;
        case 6:
            chart = ChartFactory.createStackedBarChart3D(chartTitle, titleFont, horizAxisLabel, vertAxisLabel,
                    dataset, PlotOrientation.VERTICAL, showLegend, showTooltips, drillThroughEnabled,
                    urlGenerator);
            break;
        case 7:
            chart = ChartFactory.createStackedBarChart(chartTitle, titleFont, horizAxisLabel, vertAxisLabel,
                    dataset, PlotOrientation.HORIZONTAL, showLegend, showTooltips, drillThroughEnabled,
                    urlGenerator);
            break;
        case 8:
            chart = ChartFactory.createStackedBarChart3D(chartTitle, titleFont, horizAxisLabel, vertAxisLabel,
                    dataset, PlotOrientation.HORIZONTAL, showLegend, showTooltips, drillThroughEnabled,
                    urlGenerator);
            break;
        case 9:
            chart = ChartFactory.createLineChart(chartTitle, titleFont, horizAxisLabel, vertAxisLabel, dataset,
                    PlotOrientation.VERTICAL, showLegend, showTooltips, drillThroughEnabled, urlGenerator);
            break;
        case 10:
            chart = ChartFactory.createLineChart(chartTitle, titleFont, horizAxisLabel, vertAxisLabel, dataset,
                    PlotOrientation.HORIZONTAL, showLegend, showTooltips, drillThroughEnabled, urlGenerator);
            break;
        case 11:
            chart = ChartFactory.createAreaChart(chartTitle, titleFont, horizAxisLabel, vertAxisLabel, dataset,
                    PlotOrientation.VERTICAL, showLegend, showTooltips, drillThroughEnabled, urlGenerator);
            break;
        case 12:
            chart = ChartFactory.createAreaChart(chartTitle, titleFont, horizAxisLabel, vertAxisLabel, dataset,
                    PlotOrientation.HORIZONTAL, showLegend, showTooltips, drillThroughEnabled, urlGenerator);
            break;

        case 13:
            chart = ChartFactory.createStackedAreaChart(chartTitle, titleFont, horizAxisLabel, vertAxisLabel,
                    dataset, PlotOrientation.VERTICAL, showLegend, showTooltips, drillThroughEnabled,
                    urlGenerator);
            break;
        case 14:
            chart = ChartFactory.createStackedAreaChart(chartTitle, titleFont, horizAxisLabel, vertAxisLabel,
                    dataset, PlotOrientation.HORIZONTAL, showLegend, showTooltips, drillThroughEnabled,
                    urlGenerator);
            break;

        case 15:
            chart = ChartFactory.createPieChart(chartTitle, titleFont, dataset, TableOrder.BY_COLUMN,
                    showLegend, showTooltips, drillThroughEnabled,
                    new jpivotPieURLGenerator(TableOrder.BY_COLUMN, dataset, webControllerURL));
            break;
        case 16:
            chart = ChartFactory.createPieChart(chartTitle, titleFont, dataset, TableOrder.BY_ROW, showLegend,
                    showTooltips, drillThroughEnabled,
                    new jpivotPieURLGenerator(TableOrder.BY_ROW, dataset, webControllerURL));
            break;
        case 17:
            chart = ChartFactory.create3DPieChart(chartTitle, titleFont, dataset, TableOrder.BY_COLUMN,
                    showLegend, showTooltips, drillThroughEnabled,
                    new jpivotPieURLGenerator(TableOrder.BY_COLUMN, dataset, webControllerURL));

            break;
        case 18:
            chart = ChartFactory.create3DPieChart(chartTitle, titleFont, dataset, TableOrder.BY_ROW, showLegend,
                    showTooltips, drillThroughEnabled,
                    new jpivotPieURLGenerator(TableOrder.BY_ROW, dataset, webControllerURL));

            break;

        default:
            throw new Exception("An unknown Chart Type was requested");
        }
        try {
            chart.setBackgroundPaint(new java.awt.Color(bgColorR, bgColorG, bgColorB));

            java.awt.Font slicerFont = new java.awt.Font(slicerFontName, slicerFontStyle, slicerFontSize);
            java.awt.Font axisFont = new java.awt.Font(axisFontName, axisFontStyle, axisFontSize);
            java.awt.Font axisTickFont = new java.awt.Font(axisTickFontName, axisTickFontStyle,
                    axisTickFontSize);
            java.awt.Font legendFont = new java.awt.Font(legendFontName, legendFontStyle, legendFontSize);
            Plot plot = chart.getPlot();

            if (plot instanceof CategoryPlot) {
                CategoryPlot catPlot = (CategoryPlot) plot;
                catPlot.getDomainAxis().setLabelFont(axisFont);
                catPlot.getRangeAxis().setLabelFont(axisFont);
                catPlot.getDomainAxis().setTickLabelFont(axisTickFont);
                catPlot.getRangeAxis().setTickLabelFont(axisTickFont);
                catPlot.getDomainAxis().setMaximumCategoryLabelWidthRatio(100.0f);
                double angle = -2.0 * Math.PI / 360.0 * (double) tickLabelRotate;
                CategoryLabelPositions oldp = catPlot.getDomainAxis().getCategoryLabelPositions();
                CategoryLabelPositions newp = new CategoryLabelPositions(
                        oldp.getLabelPosition(RectangleEdge.TOP),
                        new CategoryLabelPosition(RectangleAnchor.TOP, TextBlockAnchor.TOP_RIGHT,
                                TextAnchor.TOP_RIGHT, angle, CategoryLabelWidthType.RANGE, 0.0f),
                        oldp.getLabelPosition(RectangleEdge.LEFT), oldp.getLabelPosition(RectangleEdge.RIGHT));
                catPlot.getDomainAxis().setCategoryLabelPositions(newp);
            } else if (plot instanceof PiePlot3D) {
                PiePlot3D piePlot = (PiePlot3D) plot;
                //piePlot.setSectionLabelFont(axisFont);
                piePlot.setLabelFont(axisFont);
                //piePlot.setSeriesLabelFont(axisTickFont);
                //piePlot.setSectionLabelType(piePlot.NO_LABELS);
                piePlot.setDirection(org.jfree.util.Rotation.CLOCKWISE);
                piePlot.setForegroundAlpha(0.5f);
                piePlot.setNoDataMessage("No data to display");
            } else if (plot instanceof PiePlot) {
                PiePlot piePlot = (PiePlot) plot;
                //piePlot.setSectionLabelFont(axisFont);
                //piePlot.setSeriesLabelFont(axisTickFont);
                piePlot.setLabelFont(axisFont);
                //piePlot.setSectionLabelType(piePlot.NO_LABELS);

            }
            LegendTitle legend = (LegendTitle) chart.getLegend();
            if (legend != null) {
                legend.setItemFont(legendFont);
                /*
                     RectangleAnchor legendRectAnchor=RectangleAnchor.BOTTOM;
                        
                switch (legendPosition){
                     case 0:
                        legendRectAnchor = RectangleAnchor.LEFT;
                        break;
                    case 1:
                       legendRectAnchor = RectangleAnchor.TOP;
                        break;
                    case 2:
                       legendRectAnchor = RectangleAnchor.RIGHT;
                        break;
                    case 3:
                       legendRectAnchor = RectangleAnchor.BOTTOM;
                        break;
                }
                legend.setLegendItemGraphicAnchor(legendRectAnchor);
                */
                RectangleEdge legendRectEdge = RectangleEdge.BOTTOM;
                switch (legendPosition) {
                case 0:
                    legendRectEdge = RectangleEdge.LEFT;
                    break;
                case 1:
                    legendRectEdge = RectangleEdge.TOP;
                    break;
                case 2:
                    legendRectEdge = RectangleEdge.RIGHT;
                    break;
                case 3:
                    legendRectEdge = RectangleEdge.BOTTOM;
                    break;
                }
                legend.setPosition(legendRectEdge);
            }
            if (showSlicer) {
                RectangleEdge slicerRectPos = RectangleEdge.BOTTOM;
                HorizontalAlignment slicerHorizAlignment = HorizontalAlignment.LEFT;

                switch (slicerPosition) {
                case 0:
                    slicerRectPos = RectangleEdge.TOP;
                    break;
                case 1:
                    slicerRectPos = RectangleEdge.BOTTOM;
                    break;
                case 2:
                    slicerRectPos = RectangleEdge.RIGHT;
                    break;
                case 3:
                    slicerRectPos = RectangleEdge.LEFT;
                    break;
                }

                switch (slicerAlignment) {
                case 4:
                    slicerHorizAlignment = HorizontalAlignment.CENTER;
                    break;
                case 3:
                    slicerHorizAlignment = HorizontalAlignment.LEFT;
                    break;
                case 2:
                    slicerHorizAlignment = HorizontalAlignment.RIGHT;
                    break;
                }
                TextTitle slicer = new TextTitle(buildSlicer(), slicerFont, Color.BLACK, slicerRectPos,
                        slicerHorizAlignment, VerticalAlignment.CENTER, new RectangleInsets(0, 0, 0, 0));

                slicer.setPosition(slicerRectPos);
                chart.addSubtitle(slicer);
            }
            info = new ChartRenderingInfo(new StandardEntityCollection());
            //  Write the chart image to the temporary directory
            HttpSession session = context.getSession();
            filename = ServletUtilities.saveChartAsPNG(chart, chartWidth, chartHeight, info, session);

        } catch (Exception e) {
            filename = "public_error_500x300.png";
            dirty = true;
        }
    }
    // new DOM document
    DocumentBuilder parser = XmlUtils.getParser();
    // get an image map for the chart, wrap it in xchart tags
    String xchart = "<xchart>" + writeImageMap(filename, info, false) + "</xchart>";
    /*
          if (logger.isDebugEnabled()) {
             logger.debug("Chart XML");
             logger.debug(xchart);
          }
    */
    // create an InputStream from the DOM document
    InputStream stream = new ByteArrayInputStream(xchart.getBytes("UTF-8"));

    document = parser.parse(stream);
    Element root = document.getDocumentElement();
    // create url for img tag
    String graphURL = getGraphURL(context);

    Element img = document.createElement("img");
    img.setAttribute("src", graphURL);
    img.setAttribute("width", new Integer(chartWidth).toString());
    img.setAttribute("height", new Integer(chartHeight).toString());
    img.setAttribute("style", "border:0;");
    img.setAttribute("usemap", "#" + filename);
    root.appendChild(img);

    return document;
}

From source file:org.jfree.chart.demo.JFreeChartDemoBase.java

/**
 * Creates and returns a sample time series chart.
 *
 * @return a sample time series chart.//  w  w  w.j  av  a 2 s.  com
 */
public JFreeChart createTimeSeries1Chart() {

    // create a default chart based on some sample data...
    final String title = this.resources.getString("timeseries.sample1.title");
    final String subtitle = this.resources.getString("timeseries.sample1.subtitle");
    final String domain = this.resources.getString("timeseries.sample1.domain");
    final String range = this.resources.getString("timeseries.sample1.range");
    final String copyrightStr = this.resources.getString("timeseries.sample1.copyright");
    final XYDataset data = DemoDatasetFactory.createTimeSeriesCollection3();
    final JFreeChart chart = ChartFactory.createTimeSeriesChart(title, domain, range, data, true, true, false);

    // then customise it a little...
    final TextTitle title2 = new TextTitle(subtitle, new Font("SansSerif", Font.PLAIN, 12));
    //      title2.setSpacer(new Spacer(Spacer.RELATIVE, 0.05, 0.05, 0.05, 0.0));
    chart.addSubtitle(title2);

    final TextTitle copyright = new TextTitle(copyrightStr, new Font("SansSerif", Font.PLAIN, 9));
    copyright.setPosition(RectangleEdge.BOTTOM);
    copyright.setHorizontalAlignment(HorizontalAlignment.RIGHT);
    chart.addSubtitle(copyright);

    chart.setBackgroundPaint(new GradientPaint(0, 0, Color.white, 0, 1000, Color.blue));
    final XYPlot plot = chart.getXYPlot();
    final DateAxis axis = (DateAxis) plot.getDomainAxis();
    axis.setVerticalTickLabels(true);
    return chart;

}

From source file:org.esa.beam.visat.toolviews.stat.ScatterPlotPanel.java

private void computeCoefficientOfDetermination() {
    int numberOfItems = scatterpointsDataset.getSeries(0).getItemCount();
    double arithmeticMeanOfX = 0; //arithmetic mean of X
    double arithmeticMeanOfY = 0; //arithmetic mean of Y
    double varX = 0; //variance of X
    double varY = 0; //variance of Y
    double coVarXY = 0; //covariance of X and Y;
    //compute arithmetic means
    for (int i = 0; i < numberOfItems; i++) {
        arithmeticMeanOfX += scatterpointsDataset.getXValue(0, i);
        arithmeticMeanOfY += scatterpointsDataset.getYValue(0, i);
    }// w  ww  .  j a  v a2s.  com
    arithmeticMeanOfX /= numberOfItems;
    arithmeticMeanOfY /= numberOfItems;
    //compute variances and covariance
    for (int i = 0; i < numberOfItems; i++) {
        varX += Math.pow(scatterpointsDataset.getXValue(0, i) - arithmeticMeanOfX, 2);
        varY += Math.pow(scatterpointsDataset.getYValue(0, i) - arithmeticMeanOfY, 2);
        coVarXY += (scatterpointsDataset.getXValue(0, i) - arithmeticMeanOfX)
                * (scatterpointsDataset.getYValue(0, i) - arithmeticMeanOfY);
    }
    //computation of coefficient of determination
    double r2 = Math.pow(coVarXY, 2) / (varX * varY);
    r2 = MathUtils.round(r2, Math.pow(10.0, 5));

    final double[] coefficients = Regression.getOLSRegression(scatterpointsDataset, 0);
    final double intercept = coefficients[0];
    final double slope = coefficients[1];
    final String linearEquation;
    if (intercept >= 0) {
        linearEquation = "y = " + (float) slope + "x + " + (float) intercept;
    } else {
        linearEquation = "y = " + (float) slope + "x - " + Math.abs((float) intercept);
    }

    TextTitle tt = new TextTitle(linearEquation + "\nR = " + r2);
    tt.setTextAlignment(HorizontalAlignment.RIGHT);
    tt.setFont(chart.getLegend().getItemFont());
    tt.setBackgroundPaint(new Color(200, 200, 255, 100));
    tt.setFrame(new BlockBorder(Color.white));
    tt.setPosition(RectangleEdge.BOTTOM);

    r2Annotation = new XYTitleAnnotation(0.98, 0.02, tt, RectangleAnchor.BOTTOM_RIGHT);
    r2Annotation.setMaxWidth(0.48);
    getPlot().addAnnotation(r2Annotation);
}

From source file:ro.nextreports.engine.chart.JFreeChartExporter.java

private void setTitle(JFreeChart jfreechart) {
    TextTitle title = new TextTitle(StringUtil.getI18nString(replaceParameters(chart.getTitle().getTitle()),
            I18nUtil.getLanguageByName(chart, language)));
    title.setFont(chart.getTitle().getFont());
    title.setPaint(chart.getTitle().getColor());
    if (chart.getTitle().getAlignment() == ChartTitle.LEFT_ALIGNMENT) {
        title.setHorizontalAlignment(HorizontalAlignment.LEFT);
    } else if (chart.getTitle().getAlignment() == ChartTitle.RIGHT_ALIGNMENT) {
        title.setHorizontalAlignment(HorizontalAlignment.RIGHT);
    } else {//from  w w w.ja va  2s.c om
        title.setHorizontalAlignment(HorizontalAlignment.CENTER);
    }
    jfreechart.setTitle(title);
}

From source file:org.jfree.chart.demo.JFreeChartDemoBase.java

/**
 * Creates a basic wafermap chart with a random dataset
 *
 * @return a wafermap chart//www .ja  va  2  s.  co m
 */
public JFreeChart createWaferMapChart() {
    final WaferMapDataset dataset = DemoDatasetFactory.createRandomWaferMapDataset(5);
    final JFreeChart chart = ChartFactory.createWaferMapChart("Wafer Map Demo", // title
            dataset, // wafermapdataset
            PlotOrientation.VERTICAL, // vertical = notchdown
            true, // legend
            false, // tooltips
            false);

    //        final Legend legend = chart.getLegend();
    //      legend.setAnchor(Legend.EAST);
    chart.setBackgroundPaint(new GradientPaint(0, 0, Color.white, 0, 1000, Color.blue));

    final TextTitle copyright = new TextTitle("JFreeChart WaferMapPlot", new Font("SansSerif", Font.PLAIN, 9));
    copyright.setPosition(RectangleEdge.BOTTOM);
    copyright.setHorizontalAlignment(HorizontalAlignment.RIGHT);
    chart.addSubtitle(copyright);

    return chart;
}

From source file:org.jfree.chart.demo.JFreeChartDemoBase.java

/**
 * Creates a basic wafermap chart with a random dataset
 *
 * @return a wafermap chart//from   ww  w.  j av  a 2s . c o  m
 */
public JFreeChart createWaferMapChartValueIndexed() {
    final WaferMapDataset dataset = DemoDatasetFactory.createRandomWaferMapDataset(500);
    final JFreeChart chart = ChartFactory.createWaferMapChart("Wafer Map Demo - Value Indexed", // title
            dataset, // wafermapdataset
            PlotOrientation.VERTICAL, // vertical = notchdown
            true, // legend
            false, // tooltips
            false);

    //      final Legend legend = chart.getLegend();
    //    legend.setAnchor(Legend.EAST);
    chart.setBackgroundPaint(new GradientPaint(0, 0, Color.white, 0, 1000, Color.blue));

    final TextTitle copyright = new TextTitle("JFreeChart WaferMapPlot", new Font("SansSerif", Font.PLAIN, 9));
    copyright.setPosition(RectangleEdge.BOTTOM);
    copyright.setHorizontalAlignment(HorizontalAlignment.RIGHT);
    chart.addSubtitle(copyright);

    return chart;
}

From source file:org.jfree.chart.demo.JFreeChartDemoBase.java

/**
 * Creates a basic wafermap chart with a random dataset
 *
 * @return a wafermap chart//  w w  w.  j  ava2 s.  co m
 */
public JFreeChart createWaferMapChartPositionIndexed() {
    final WaferMapDataset dataset = DemoDatasetFactory.createRandomWaferMapDataset(500);
    final WaferMapPlot plot = new WaferMapPlot(dataset);
    final WaferMapRenderer renderer = new WaferMapRenderer(35, WaferMapRenderer.POSITION_INDEX);
    plot.setRenderer(renderer);

    final JFreeChart chart = new JFreeChart("Wafer Map Demo - Position Indexed", JFreeChart.DEFAULT_TITLE_FONT,
            plot, true);

    //    final Legend legend = chart.getLegend();
    //  legend.setAnchor(Legend.EAST);
    chart.setBackgroundPaint(new GradientPaint(0, 0, Color.white, 0, 1000, Color.blue));

    final TextTitle copyright = new TextTitle("JFreeChart WaferMapPlot", new Font("SansSerif", Font.PLAIN, 9));
    copyright.setPosition(RectangleEdge.BOTTOM);
    copyright.setHorizontalAlignment(HorizontalAlignment.RIGHT);
    chart.addSubtitle(copyright);

    return chart;
}