Example usage for org.jfree.chart.axis CategoryAnchor MIDDLE

List of usage examples for org.jfree.chart.axis CategoryAnchor MIDDLE

Introduction

In this page you can find the example usage for org.jfree.chart.axis CategoryAnchor MIDDLE.

Prototype

CategoryAnchor MIDDLE

To view the source code for org.jfree.chart.axis CategoryAnchor MIDDLE.

Click Source Link

Document

Middle of period.

Usage

From source file:edu.cuny.jfree.chart.annotations.CategoryIntervalAnnotation.java

public void draw(final Graphics2D g2, final CategoryPlot plot, final Rectangle2D dataArea,
        final CategoryAxis domainAxis, final ValueAxis rangeAxis) {

    final AlphaComposite alphaComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
            plot.getForegroundAlpha());//from www .j a v a2 s.  com
    final Composite oldComposite = g2.getComposite();
    g2.setComposite(alphaComposite);

    final CategoryDataset dataset = plot.getDataset();
    final int catIndex = dataset.getColumnIndex(category);
    final int catCount = dataset.getColumnCount();
    double lineX1 = 0.0D;
    double lineY = 0.0D;
    double lineX2 = 0.0D;
    final PlotOrientation orientation = plot.getOrientation();
    final RectangleEdge domainEdge = Plot.resolveDomainAxisLocation(plot.getDomainAxisLocation(), orientation);
    final RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation(plot.getRangeAxisLocation(), orientation);
    if (orientation == PlotOrientation.HORIZONTAL) {
        lineY = domainAxis.getCategoryJava2DCoordinate(CategoryAnchor.MIDDLE, catIndex, catCount, dataArea,
                domainEdge);
        lineX1 = rangeAxis.valueToJava2D(value1, dataArea, rangeEdge);
        lineX2 = rangeAxis.valueToJava2D(value2, dataArea, rangeEdge);
    } else if (orientation == PlotOrientation.VERTICAL) {
        lineY = rangeAxis.valueToJava2D(value1, dataArea, rangeEdge);
        lineX1 = domainAxis.getCategoryJava2DCoordinate(CategoryAnchor.MIDDLE, catIndex, catCount, dataArea,
                domainEdge);
        lineX2 = domainAxis.getCategoryJava2DCoordinate(CategoryAnchor.MIDDLE, catIndex, catCount, dataArea,
                domainEdge);
    }
    g2.setPaint(paint);
    g2.setStroke(stroke);
    g2.drawLine((int) lineX1, (int) lineY, (int) lineX2, (int) lineY);

    g2.setComposite(oldComposite);
}

From source file:peakml.util.jfreechart.FastErrorBarPlot.java

@Override
public void draw(Graphics2D g2, Rectangle2D area, Point2D anchor, PlotState parentState,
        PlotRenderingInfo info) {//  w ww . j  av  a2s. co  m
    // add the plot area to the info (used amongst other by the axis for zooming)
    if (info != null)
        info.setPlotArea(area);

    // add the insets (if any)
    RectangleInsets insets = getInsets();
    insets.trim(area);

    // draw the axis and add the dataArea to the info (used amongst other by the axis for zooming)
    AxisSpace space = new AxisSpace();
    space = xaxis.reserveSpace(g2, this, area, RectangleEdge.BOTTOM, space);
    space = yaxis.reserveSpace(g2, this, area, RectangleEdge.LEFT, space);

    Rectangle2D dataArea = space.shrink(area, null);
    if (info != null)
        info.setDataArea(dataArea);

    // flood fill the whole area with the background color
    drawBackground(g2, dataArea);

    // draw the axis
    xaxis.draw(g2, dataArea.getMaxY(), area, dataArea, RectangleEdge.BOTTOM, info);
    yaxis.draw(g2, dataArea.getMinX(), area, dataArea, RectangleEdge.LEFT, info);

    // sanity check
    if (dataseries.size() == 0)
        return;

    // clip the draw area
    Shape originalclip = g2.getClip();
    g2.clip(dataArea);

    // create the strokes
    BasicStroke stroke_solid = new BasicStroke(1.f, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_ROUND, 1.f);
    BasicStroke stroke_dashed = new BasicStroke(1.f, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_ROUND, 1.f,
            new float[] { 2, 4 }, 0);

    g2.setStroke(stroke_solid);

    // count the number of labels
    int categoryCount = 0;
    if (showall) {
        for (Data data : dataseries)
            categoryCount += data.yvalues.length;
    } else
        categoryCount = dataseries.size();

    // draw all the values
    int pos = 0;
    boolean dashed = false;
    double prevx = -1, prevy = -1;
    for (Data data : dataseries) {
        if (data.yvalues.length == 0) {
            dashed = true;
            pos++;
            continue;
        }

        double mean[] = showall ? data.yvalues : new double[] { data.getMeanY() };
        double min[] = showall ? data.yvalues : new double[] { data.getMinY() };
        double max[] = showall ? data.yvalues : new double[] { data.getMaxY() };
        for (int i = 0; i < mean.length; ++i) {
            double ypos, xpos = xaxis.getCategoryJava2DCoordinate(CategoryAnchor.MIDDLE, pos++, categoryCount,
                    dataArea, RectangleEdge.BOTTOM);

            // draw the mean value
            g2.setColor(Color.RED);
            ypos = yaxis.valueToJava2D(mean[i], dataArea, RectangleEdge.LEFT);
            g2.drawLine((int) xpos - 2, (int) ypos, (int) xpos + 2, (int) ypos);

            // conect the dots
            if (prevx != -1 && prevy != -1) {
                g2.setColor(Color.BLACK);
                if (dashed)
                    g2.setStroke(stroke_dashed);
                g2.drawLine((int) prevx, (int) prevy, (int) xpos, (int) ypos);
                if (dashed) {
                    dashed = false;
                    g2.setStroke(stroke_solid);
                }

            }
            prevy = ypos;
            prevx = xpos;

            // draw the outer values
            g2.setColor(Color.LIGHT_GRAY);
            double ypos_min = yaxis.valueToJava2D(min[i], dataArea, RectangleEdge.LEFT);
            g2.drawLine((int) xpos - 2, (int) ypos_min, (int) xpos + 2, (int) ypos_min);
            double ypos_max = yaxis.valueToJava2D(max[i], dataArea, RectangleEdge.LEFT);
            g2.drawLine((int) xpos - 2, (int) ypos_max, (int) xpos + 2, (int) ypos_max);
            g2.drawLine((int) xpos, (int) ypos_min, (int) xpos, (int) ypos_max);
        }
    }

    // reset
    g2.setClip(originalclip);
}

From source file:org.talend.dataprofiler.chart.TOPChartService.java

@Override
public void createAnnotOnGantt(Object chart, List<Object[]> rowList, int multiDateColumn, int nominal) {
    Map<String, RowColumPair> hightlightSeriesMap = new HashMap<String, RowColumPair>();
    CategoryPlot xyplot = (CategoryPlot) ((JFreeChart) chart).getPlot();
    CategoryTextAnnotation an;/*w  w w.  jav a  2 s .  c om*/
    for (int seriesCount = 0; seriesCount < ((TaskSeriesCollection) xyplot.getDataset())
            .getSeriesCount(); seriesCount++) {
        int indexOfRow = 0;
        int columnCount = 0;
        for (int itemCount = 0; itemCount < ((TaskSeriesCollection) xyplot.getDataset()).getSeries(seriesCount)
                .getItemCount(); itemCount++, columnCount++) {
            Task task = ((TaskSeriesCollection) xyplot.getDataset()).getSeries(seriesCount).get(itemCount);
            String taskDescription = task.getDescription();
            String[] taskArray = taskDescription.split("\\|"); //$NON-NLS-1$
            boolean isSameTime = task.getDuration().getStart().getTime() == task.getDuration().getEnd()
                    .getTime();
            if (!isSameTime && (rowList.get(indexOfRow))[multiDateColumn - 3] != null
                    && (rowList.get(indexOfRow))[multiDateColumn - 2] != null
                    && !((rowList.get(indexOfRow))[multiDateColumn]).equals(new BigDecimal(0L))) {
                RowColumPair pair = new RowColumPair();
                pair.setRow(seriesCount);
                pair.setColumn(columnCount);

                hightlightSeriesMap.put(String.valueOf(seriesCount) + String.valueOf(columnCount), pair);

                an = new CategoryTextAnnotation("#nulls = " + (rowList.get(indexOfRow))[multiDateColumn], //$NON-NLS-1$
                        taskDescription, task.getDuration().getStart().getTime());
                an.setTextAnchor(TextAnchor.CENTER_LEFT);
                an.setCategoryAnchor(CategoryAnchor.MIDDLE);
                xyplot.addAnnotation(an);
            }
            if (taskArray.length == nominal) {
                indexOfRow++;

                if (rowList.size() != indexOfRow && ((rowList.get(indexOfRow))[multiDateColumn - 3] == null
                        || (rowList.get(indexOfRow))[multiDateColumn - 2] == null)) {
                    indexOfRow++;
                }
            }
        }
    }
    CustomHideSeriesGanttRender renderer = new CustomHideSeriesGanttRender(hightlightSeriesMap);
    xyplot.setRenderer(renderer);
    renderer.setBaseToolTipGenerator(new CategoryToolTipGenerator() {

        @Override
        public String generateToolTip(CategoryDataset dataset, int row, int column) {
            TaskSeriesCollection taskSeriesColl = (TaskSeriesCollection) dataset;
            List<Task> taskList = new ArrayList<Task>();
            for (int i = 0; i < taskSeriesColl.getSeriesCount(); i++) {
                for (int j = 0; j < taskSeriesColl.getSeries(i).getItemCount(); j++) {
                    taskList.add(taskSeriesColl.getSeries(i).get(j));
                }
            }
            Task task = taskList.get(column);
            // Task task = taskSeriesColl.getSeries(row).get(column);
            String taskDescription = task.getDescription();

            Date startDate = task.getDuration().getStart();
            Date endDate = task.getDuration().getEnd();
            return taskDescription + ",     " + startDate + "---->" + endDate; //$NON-NLS-1$ //$NON-NLS-2$
            // return "this is a tooltip";
        }
    });
    xyplot.getDomainAxis().setMaximumCategoryLabelWidthRatio(10.0f);

}

From source file:com.intel.stl.ui.common.view.ComponentFactory.java

@SuppressWarnings("unchecked")
public static JFreeChart createTopNBarChart2(String yAxisLabel, CategoryDataset dataset) {
    JFreeChart jfreechart = ChartFactory.createBarChart(null, null, yAxisLabel, dataset,
            PlotOrientation.HORIZONTAL, false, true, false);
    CategoryPlot categoryplot = jfreechart.getCategoryPlot();
    categoryplot.setBackgroundPaint(null);
    categoryplot.setOutlinePaint(null);/*ww  w  . j av a2 s.  c o  m*/
    categoryplot.setDomainGridlinesVisible(true);
    categoryplot.setDomainGridlinePosition(CategoryAnchor.END);
    categoryplot.setDomainGridlineStroke(new BasicStroke(0.5F));
    categoryplot.setDomainGridlinePaint(UIConstants.INTEL_BORDER_GRAY);
    categoryplot.setRangeGridlinesVisible(false);
    categoryplot.clearRangeMarkers();
    CategoryAxis categoryaxis = categoryplot.getDomainAxis();
    categoryaxis.setVisible(false);
    categoryaxis.setCategoryMargin(0.75D);

    NumberAxis axis = (NumberAxis) categoryplot.getRangeAxis();
    axis.setRangeType(RangeType.POSITIVE);
    axis.setVisible(false);

    BarRenderer barrenderer = (BarRenderer) categoryplot.getRenderer();
    barrenderer.setShadowVisible(false);
    barrenderer.setSeriesPaint(0, UIConstants.INTEL_BLUE);
    barrenderer.setDrawBarOutline(false);
    barrenderer.setBaseItemLabelsVisible(true);
    barrenderer.setBaseItemLabelFont(UIConstants.H5_FONT);
    barrenderer.setBarPainter(new StandardBarPainter());

    List<String> names = dataset.getColumnKeys();
    for (String name : names) {
        CategoryTextAnnotation categorytextannotation = new CategoryTextAnnotation(name, name, 0.0D);
        categorytextannotation.setFont(UIConstants.H6_FONT);
        categorytextannotation.setTextAnchor(TextAnchor.BOTTOM_LEFT);
        categorytextannotation.setCategoryAnchor(CategoryAnchor.MIDDLE);
        categoryplot.addAnnotation(categorytextannotation);
    }
    return jfreechart;
}