Example usage for org.jfree.chart.plot Marker setLabelOffsetType

List of usage examples for org.jfree.chart.plot Marker setLabelOffsetType

Introduction

In this page you can find the example usage for org.jfree.chart.plot Marker setLabelOffsetType.

Prototype

public void setLabelOffsetType(LengthAdjustmentType adj) 

Source Link

Document

Sets the label offset type and sends a MarkerChangeEvent to all registered listeners.

Usage

From source file:it.eng.spagobi.engines.chart.bo.charttypes.scattercharts.MarkerScatter.java

public JFreeChart createChart(DatasetMap datasets) {

    DefaultXYDataset dataset = (DefaultXYDataset) datasets.getDatasets().get("1");

    JFreeChart chart = ChartFactory.createScatterPlot(name, yLabel, xLabel, dataset, PlotOrientation.HORIZONTAL,
            false, true, false);//from w  ww. j  ava2 s. c  o m

    TextTitle title = setStyleTitle(name, styleTitle);
    chart.setTitle(title);
    chart.setBackgroundPaint(Color.white);
    if (subName != null && !subName.equals("")) {
        TextTitle subTitle = setStyleTitle(subName, styleSubTitle);
        chart.addSubtitle(subTitle);
    }

    XYPlot plot = (XYPlot) chart.getPlot();
    plot.setForegroundAlpha(0.65f);

    XYItemRenderer renderer = plot.getRenderer();

    //defines colors 
    int seriesN = dataset.getSeriesCount();
    if ((colorMap != null && colorMap.size() > 0) || (defaultColor != null && !defaultColor.equals(""))) {
        for (int i = 0; i < seriesN; i++) {
            String serieName = (String) dataset.getSeriesKey(i);
            Color color = new Color(Integer.decode(defaultColor).intValue());
            if (colorMap != null && colorMap.size() > 0)
                color = (Color) colorMap.get(serieName);
            if (color != null)
                renderer.setSeriesPaint(i, color);
        }
    }

    // add un interval  marker for the Y axis...
    if (yMarkerStartInt != null && yMarkerEndInt != null && !yMarkerStartInt.equals("")
            && !yMarkerEndInt.equals("")) {
        Marker intMarkerY = new IntervalMarker(Double.parseDouble(yMarkerStartInt),
                Double.parseDouble(yMarkerEndInt));
        intMarkerY.setLabelOffsetType(LengthAdjustmentType.EXPAND);
        intMarkerY.setPaint(
                new Color(Integer.decode((yMarkerIntColor.equals("")) ? "0" : yMarkerIntColor).intValue()));
        //intMarkerY.setLabel(yMarkerLabel);
        intMarkerY.setLabelAnchor(RectangleAnchor.BOTTOM_RIGHT);
        intMarkerY.setLabelTextAnchor(TextAnchor.TOP_RIGHT);
        plot.addDomainMarker(intMarkerY, Layer.BACKGROUND);
    }
    // add un interval  marker for the X axis...
    if (xMarkerStartInt != null && xMarkerEndInt != null && !xMarkerStartInt.equals("")
            && !xMarkerEndInt.equals("")) {
        Marker intMarkerX = new IntervalMarker(Double.parseDouble(xMarkerStartInt),
                Double.parseDouble(xMarkerEndInt));
        intMarkerX.setLabelOffsetType(LengthAdjustmentType.EXPAND);
        intMarkerX.setPaint(
                new Color(Integer.decode((xMarkerIntColor.equals("")) ? "0" : xMarkerIntColor).intValue()));
        //intMarkerX.setLabel(xMarkerLabel);
        intMarkerX.setLabelAnchor(RectangleAnchor.BOTTOM_RIGHT);
        intMarkerX.setLabelTextAnchor(TextAnchor.TOP_RIGHT);
        plot.addRangeMarker(intMarkerX, Layer.BACKGROUND);
    }
    // add a labelled marker for the Y axis...
    if (yMarkerValue != null && !yMarkerValue.equals("")) {
        Marker markerY = new ValueMarker(Double.parseDouble(yMarkerValue));
        markerY.setLabelOffsetType(LengthAdjustmentType.EXPAND);
        if (!yMarkerColor.equals(""))
            markerY.setPaint(new Color(Integer.decode(yMarkerColor).intValue()));
        markerY.setLabel(yMarkerLabel);
        markerY.setLabelFont(new Font("Arial", Font.BOLD, 11));
        markerY.setLabelAnchor(RectangleAnchor.TOP_RIGHT);
        markerY.setLabelTextAnchor(TextAnchor.TOP_RIGHT);
        plot.addDomainMarker(markerY, Layer.BACKGROUND);
    }
    // add a labelled marker for the X axis...
    if (xMarkerValue != null && !xMarkerValue.equals("")) {
        Marker markerX = new ValueMarker(Double.parseDouble(xMarkerValue));
        markerX.setLabelOffsetType(LengthAdjustmentType.EXPAND);
        if (!xMarkerColor.equals(""))
            markerX.setPaint(new Color(Integer.decode(xMarkerColor).intValue()));
        markerX.setLabel(xMarkerLabel);
        markerX.setLabelAnchor(RectangleAnchor.BOTTOM_RIGHT);
        markerX.setLabelTextAnchor(TextAnchor.TOP_RIGHT);
        plot.addRangeMarker(markerX, Layer.BACKGROUND);
    }

    if (xRangeLow != null && !xRangeLow.equals("") && xRangeHigh != null && !xRangeHigh.equals("")) {
        if (Double.valueOf(xRangeLow).doubleValue() > xMin)
            xRangeLow = String.valueOf(xMin);
        if (Double.valueOf(xRangeHigh).doubleValue() < xMax)
            xRangeHigh = String.valueOf(xMax);
        ValueAxis rangeAxis = plot.getRangeAxis();
        //rangeAxis.setRange(Double.parseDouble(xRangeLow), Double.parseDouble(xRangeHigh));
        rangeAxis.setRangeWithMargins(Double.parseDouble(xRangeLow), Double.parseDouble(xRangeHigh));
    } else {
        NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setAutoRange(true);
        rangeAxis.setRange(xMin, xMax);
    }

    if (yRangeLow != null && !yRangeLow.equals("") && yRangeHigh != null && !yRangeHigh.equals("")) {
        if (Double.valueOf(yRangeLow).doubleValue() > yMin)
            yRangeLow = String.valueOf(yMin);
        if (Double.valueOf(yRangeHigh).doubleValue() < yMax)
            yRangeHigh = String.valueOf(yMax);
        NumberAxis domainAxis = (NumberAxis) plot.getDomainAxis();
        //domainAxis.setRange(Double.parseDouble(yRangeLow), Double.parseDouble(yRangeHigh));
        domainAxis.setRangeWithMargins(Double.parseDouble(yRangeLow), Double.parseDouble(yRangeHigh));
        domainAxis.setAutoRangeIncludesZero(false);
    } else {
        NumberAxis domainAxis = (NumberAxis) plot.getDomainAxis();
        domainAxis.setAutoRange(true);
        domainAxis.setRange(yMin, yMax);
        domainAxis.setAutoRangeIncludesZero(false);
    }

    //add annotations if requested
    if (viewAnnotations != null && viewAnnotations.equalsIgnoreCase("true")) {
        if (annotationMap == null || annotationMap.size() == 0)
            logger.error("Annotations on the chart are requested but the annotationMap is null!");
        else {
            int cont = 1;
            for (Iterator iterator = annotationMap.keySet().iterator(); iterator.hasNext();) {
                String text = (String) iterator.next();
                String pos = (String) annotationMap.get(text);
                double x = Double.parseDouble(pos.substring(0, pos.indexOf("__")));
                double y = Double.parseDouble(pos.substring(pos.indexOf("__") + 2));
                //default up position
                XYTextAnnotation annotation = new XYTextAnnotation(text, y - 1,
                        x + ((text.length() > 20) ? text.length() / 3 + 1 : text.length() / 2 + 1));
                if (cont % 2 != 0)
                    //dx
                    annotation = new XYTextAnnotation(text, y,
                            x + ((text.length() > 20) ? text.length() / 3 + 1 : text.length() / 2 + 1));
                else
                    //sx
                    //annotation = new XYTextAnnotation(text, y, x-((text.length()%2==0)?text.length():text.length()-1));
                    annotation = new XYTextAnnotation(text, y, x - (text.length() - 1));

                annotation.setFont(new Font("SansSerif", Font.PLAIN, 11));
                //annotation.setRotationAngle(Math.PI / 4.0);
                annotation.setRotationAngle(0.0); // horizontal
                plot.addAnnotation(annotation);
                cont++;
            }
            renderer.setShape(new Ellipse2D.Double(-3, -5, 8, 8));
        }
    } else if (viewAnnotations != null && viewAnnotations.equalsIgnoreCase("false")) {
        renderer.setShape(new Ellipse2D.Double(-3, -5, 8, 8));
    }
    if (legend == true) {

        drawLegend(chart);
    }
    return chart;
}