Example usage for org.jfree.chart.renderer.xy XYLineAndShapeRenderer setBaseNegativeItemLabelPosition

List of usage examples for org.jfree.chart.renderer.xy XYLineAndShapeRenderer setBaseNegativeItemLabelPosition

Introduction

In this page you can find the example usage for org.jfree.chart.renderer.xy XYLineAndShapeRenderer setBaseNegativeItemLabelPosition.

Prototype

public void setBaseNegativeItemLabelPosition(ItemLabelPosition position) 

Source Link

Document

Sets the base item label position for negative values and sends a RendererChangeEvent to all registered listeners.

Usage

From source file:it.eng.spagobi.engines.chart.bo.charttypes.targetcharts.SparkLine.java

@Override
public JFreeChart createChart(DatasetMap datasets) {
    logger.debug("IN");
    XYDataset dataset = (XYDataset) datasets.getDatasets().get("1");

    final JFreeChart sparkLineGraph = ChartFactory.createTimeSeriesChart(null, null, null, dataset, legend,
            false, false);/*from   www. j a  v  a 2 s  .c om*/
    sparkLineGraph.setBackgroundPaint(color);

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

    sparkLineGraph.setBorderVisible(false);
    sparkLineGraph.setBorderPaint(Color.BLACK);
    XYPlot plot = sparkLineGraph.getXYPlot();
    plot.setOutlineVisible(false);
    plot.setInsets(new RectangleInsets(0.0, 0.0, 0.0, 0.0));
    plot.setBackgroundPaint(null);
    plot.setDomainGridlinesVisible(false);
    plot.setDomainCrosshairVisible(false);
    plot.setRangeGridlinesVisible(false);
    plot.setRangeCrosshairVisible(false);
    plot.setBackgroundPaint(color);

    // calculate the last marker color
    Paint colorLast = getLastPointColor();

    // Calculate average, minimum and maximum to draw plot borders.
    boolean isFirst = true;
    double avg = 0, min = Double.POSITIVE_INFINITY, max = Double.NEGATIVE_INFINITY;
    int count = 0;
    for (int i = 0; i < timeSeries.getItemCount(); i++) {
        if (timeSeries.getValue(i) != null) {
            count++;
            if (isFirst) {
                min = timeSeries.getValue(i).doubleValue();
                max = timeSeries.getValue(i).doubleValue();
                isFirst = false;
            }
            double n = timeSeries.getValue(i).doubleValue();
            //calculate avg, min, max
            avg += n;
            if (n < min)
                min = n;
            if (n > max)
                max = n;
        }
    }
    // average
    avg = avg / (double) count;

    // calculate min and max between thresholds!
    boolean isFirst2 = true;
    double lb = 0, ub = 0;
    for (Iterator iterator = thresholds.keySet().iterator(); iterator.hasNext();) {
        Double thres = (Double) iterator.next();
        if (isFirst2 == true) {
            ub = thres.doubleValue();
            lb = thres.doubleValue();
            isFirst2 = false;
        }
        if (thres.doubleValue() > ub)
            ub = thres.doubleValue();
        if (thres.doubleValue() < lb)
            lb = thres.doubleValue();
    }

    plot.getRangeAxis().setRange(new Range(Math.min(lb, min - 2), Math.max(ub, max + 2) + 2));

    addMarker(1, avg, Color.GRAY, 0.8f, plot);
    //addAvaregeSeries(series, plot);
    addPointSeries(timeSeries, plot);

    int num = 3;
    for (Iterator iterator = thresholds.keySet().iterator(); iterator.hasNext();) {
        Double thres = (Double) iterator.next();
        TargetThreshold targThres = thresholds.get(thres);
        Color color = Color.WHITE;
        if (targThres != null && targThres.getColor() != null) {
            color = targThres.getColor();
        }
        if (targThres.isVisible()) {
            addMarker(num++, thres.doubleValue(), color, 0.5f, plot);
        }
    }

    ValueAxis domainAxis = plot.getDomainAxis();
    domainAxis.setVisible(false);
    domainAxis.setUpperMargin(0.2);
    NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    rangeAxis.setVisible(false);

    plot.getRenderer().setSeriesPaint(0, Color.BLACK);
    XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(true, false) {
        public boolean getItemShapeVisible(int _series, int item) {
            TimeSeriesDataItem tsdi = timeSeries.getDataItem(item);
            if (tsdi == null)
                return false;
            Month period = (Month) tsdi.getPeriod();
            int currMonth = period.getMonth();
            int currYear = period.getYearValue();
            int lastMonthFilled = lastMonth.getMonth();
            int lastYearFilled = lastMonth.getYearValue();
            boolean isLast = false;
            if (currYear == lastYearFilled && currMonth == lastMonthFilled) {
                isLast = true;
            }
            return isLast;
        }
    };
    renderer.setSeriesPaint(0, Color.decode("0x000000"));

    renderer.setBaseShapesVisible(true);
    renderer.setBaseShapesFilled(true);
    renderer.setDrawOutlines(true);
    renderer.setUseFillPaint(true);
    renderer.setBaseFillPaint(colorLast);
    renderer.setBaseOutlinePaint(Color.BLACK);
    renderer.setUseOutlinePaint(true);
    renderer.setSeriesShape(0, new Ellipse2D.Double(-4.0, -4.0, 8.0, 8.0));

    if (wlt_mode.doubleValue() == 0) {
        renderer.setBaseItemLabelsVisible(Boolean.FALSE, true);
    } else {
        renderer.setBaseItemLabelsVisible(Boolean.TRUE, true);
        renderer.setBaseItemLabelFont(
                new Font(styleValueLabels.getFontName(), Font.PLAIN, styleValueLabels.getSize()));
        renderer.setBaseItemLabelPaint(styleValueLabels.getColor());
        renderer.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator("{2}", new DecimalFormat("0.###"),
                new DecimalFormat("0.###")) {
            public String generateLabel(CategoryDataset dataset, int row, int column) {
                if (dataset.getValue(row, column) == null || dataset.getValue(row, column).doubleValue() == 0)
                    return "";
                String columnKey = (String) dataset.getColumnKey(column);
                int separator = columnKey.indexOf('-');
                String month = columnKey.substring(0, separator);
                String year = columnKey.substring(separator + 1);
                int monthNum = Integer.parseInt(month);
                if (wlt_mode.doubleValue() >= 1 && wlt_mode.doubleValue() <= 4) {
                    if (wlt_mode.doubleValue() == 2 && column % 2 == 0)
                        return "";

                    Calendar calendar = Calendar.getInstance();
                    calendar.set(Calendar.MONTH, monthNum - 1);
                    SimpleDateFormat dataFormat = new SimpleDateFormat("MMM");
                    return dataFormat.format(calendar.getTime());
                } else
                    return "" + monthNum;
            }
        });
    }

    if (wlt_mode.doubleValue() == 3) {
        renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12,
                org.jfree.ui.TextAnchor.BOTTOM_CENTER, org.jfree.ui.TextAnchor.BOTTOM_RIGHT, Math.PI / 2));
        renderer.setBaseNegativeItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE6,
                org.jfree.ui.TextAnchor.TOP_CENTER, org.jfree.ui.TextAnchor.HALF_ASCENT_LEFT, Math.PI / 2));

    } else if (wlt_mode.doubleValue() == 4) {
        renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12,
                org.jfree.ui.TextAnchor.BOTTOM_CENTER, org.jfree.ui.TextAnchor.BOTTOM_RIGHT, Math.PI / 4));
        renderer.setBaseNegativeItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE6,
                org.jfree.ui.TextAnchor.TOP_CENTER, org.jfree.ui.TextAnchor.HALF_ASCENT_LEFT, Math.PI / 4));
    }

    if (legend == true) {
        LegendItemCollection collection = createThresholdLegend(plot);
        LegendItem item = new LegendItem("Avg", "Avg", "Avg", "Avg", new Rectangle(10, 10), colorAverage);
        collection.add(item);
        plot.setFixedLegendItems(collection);

    }

    plot.setRenderer(0, renderer);
    logger.debug("OUT");
    return sparkLineGraph;
}