List of usage examples for org.jfree.chart.plot CategoryPlot getCategories
public List getCategories()
From source file:org.opennms.web.charts.ChartUtils.java
/** * @param barChart TODO//from ww w.ja v a 2 s . co m * @param subLabelClass */ private static void addSubLabels(JFreeChart barChart, String subLabelClass) { ExtendedCategoryAxis subLabels; CategoryPlot plot = barChart.getCategoryPlot(); try { subLabels = (ExtendedCategoryAxis) Class.forName(subLabelClass).newInstance(); List<?> cats = plot.getCategories(); for (int i = 0; i < cats.size(); i++) { subLabels.addSubLabel((Comparable<?>) cats.get(i), cats.get(i).toString()); } plot.setDomainAxis(subLabels); } catch (InstantiationException e) { LOG.error("getBarChart: Couldn't instantiate configured CategorySubLabels class: {}", subLabelClass, e); } catch (IllegalAccessException e) { LOG.error("getBarChart: Couldn't instantiate configured CategorySubLabels class: {}", subLabelClass, e); } catch (ClassNotFoundException e) { LOG.error("getBarChart: Couldn't instantiate configured CategorySubLabels class: {}", subLabelClass, e); } }
From source file:org.opennms.netmgt.charts.ChartUtils.java
/** * @param barChart TODO//from ww w.j a v a 2s. co m * @param subLabelClass */ private static void addSubLabels(JFreeChart barChart, String subLabelClass) { ExtendedCategoryAxis subLabels; CategoryPlot plot = barChart.getCategoryPlot(); try { subLabels = (ExtendedCategoryAxis) Class.forName(subLabelClass).newInstance(); List<?> cats = plot.getCategories(); for (int i = 0; i < cats.size(); i++) { subLabels.addSubLabel((Comparable<?>) cats.get(i), cats.get(i).toString()); } plot.setDomainAxis(subLabels); } catch (InstantiationException e) { log().error("getBarChart: Couldn't instantiate configured CategorySubLabels class: " + subLabelClass, e); } catch (IllegalAccessException e) { log().error("getBarChart: Couldn't instantiate configured CategorySubLabels class: " + subLabelClass, e); } catch (ClassNotFoundException e) { log().error("getBarChart: Couldn't instantiate configured CategorySubLabels class: " + subLabelClass, e); } }
From source file:org.jfree.eastwood.GCategoryAxis.java
/** * Creates a temporary list of ticks that can be used when drawing the axis. * * @param g2 the graphics device (used to get font measurements). * @param state the axis state./*from w w w . j av a2s. c om*/ * @param dataArea the area inside the axes. * @param edge the location of the axis. * * @return A list of ticks. */ public List refreshTicks(Graphics2D g2, AxisState state, Rectangle2D dataArea, RectangleEdge edge) { List ticks = new java.util.ArrayList(); // sanity check for data area... if (dataArea.getHeight() <= 0.0 || dataArea.getWidth() < 0.0) { return ticks; } CategoryPlot plot = (CategoryPlot) getPlot(); List categories = null; if (this.labels == null) { categories = plot.getCategories(); } else { categories = new java.util.ArrayList(this.labels); // handle a little quirk in the Google Chart API - for a horizontal // bar chart, the labels on the axis get applied in reverse order // relative to the data values if (plot.getOrientation() == PlotOrientation.HORIZONTAL) { Collections.reverse(categories); } } double max = 0.0; if (categories != null) { CategoryLabelPosition position = getCategoryLabelPositions().getLabelPosition(edge); float r = getMaximumCategoryLabelWidthRatio(); if (r <= 0.0) { r = position.getWidthRatio(); } float l = 0.0f; if (position.getWidthType() == CategoryLabelWidthType.CATEGORY) { l = (float) calculateCategorySize(categories.size(), dataArea, edge); } else { if (RectangleEdge.isLeftOrRight(edge)) { l = (float) dataArea.getWidth(); } else { l = (float) dataArea.getHeight(); } } int categoryIndex = 0; Iterator iterator = categories.iterator(); while (iterator.hasNext()) { Comparable category = (Comparable) iterator.next(); TextBlock label = createLabel(category, l * r, edge, g2); if (edge == RectangleEdge.TOP || edge == RectangleEdge.BOTTOM) { max = Math.max(max, calculateTextBlockHeight(label, position, g2)); } else if (edge == RectangleEdge.LEFT || edge == RectangleEdge.RIGHT) { max = Math.max(max, calculateTextBlockWidth(label, position, g2)); } Tick tick = new CategoryTick(category, label, position.getLabelAnchor(), position.getRotationAnchor(), position.getAngle()); ticks.add(tick); categoryIndex = categoryIndex + 1; } } state.setMax(max); return ticks; }
From source file:nl.strohalm.cyclos.controls.reports.statistics.graphs.ChartPostProcessorImpl.java
/** * This method sets the x-axis labels in a rotated position. Use this only if there are a lot of bars in the graph, so there is very limited space * for eacht label on the x-axis. By rotating the labels, they wont overlap each other, and there will be more space per label. Set this in the * jsp via the param tag, using the keyword "xlabelRotation": <cewolf:chartpostprocessor id="chartPostProcessorImpl" > <cewolf:param * name="xlabelRotation" value="<%= new Double(0.60)%>"/> </cewolf:chartpostprocessor> The parameter is a double, indicating the rotation angle in * radials. <b>NOTE:</b> the rotation is set to 0.60 by default if the number of categories > 14; otherwise, the default rotation is 0. Setting * the parameter overrides this default. * @param plot//from w ww . ja va2 s.c o m * @param params */ @SuppressWarnings("rawtypes") private void setRotatedXaxisLabels(final CategoryPlot plot, final Map params) { Double rotation = (Double) params.get("xlabelRotation"); final int numberOfCategories = plot.getCategories().size(); if (rotation == null && numberOfCategories > ROTATE_XLABELS_IF_MORE_THAN) { rotation = new Double(0.60); } if (rotation != null && rotation != 0) { final CategoryAxis axis = plot.getDomainAxis(); axis.setCategoryLabelPositions( CategoryLabelPositions.createUpRotationLabelPositions(rotation.doubleValue())); } }
From source file:nl.strohalm.cyclos.controls.reports.statistics.graphs.ChartPostProcessorImpl.java
/** * This method sets the width of the bars, and the spacing between the bars. It sets a general, hard coded standard for this. In JFreeChart, you * cannot set the bar width. In stead, you set the gaps: - lowerMargin is the gap between the start of the x-axis and the first bar - upperMargin * is the gap between the end of the x-axis and the last bar - categoryMargin is the gap between the categories. Note that the number you provide * here, is divided over all the gaps. So if you set this margin to 0.5 (is 50%) and there are 6 categories, this means that there are 5 gaps, so * each gap will get 10%. - itemMargin is the gap between the bars within a category. Again, this number is divided over all the itemgaps in the * whole graph. The method also takes care that extreme cases (with a very small amount of bars) still look acceptable. * @param plot/*w w w. ja v a 2s . c om*/ */ private void setMargins(final CategoryPlot plot) { final CategoryAxis axis = plot.getDomainAxis(); axis.setTickLabelFont(new Font("SansSerif", Font.PLAIN, 9)); final int categoryCount = plot.getCategories().size(); final int seriesCount = plot.getDataset().getRowCount(); axis.setCategoryMargin(0.23); // sets spacing between categories on x% // set spacing between bars inside a catgory (in fractions, so 1 = 100%) final BarRenderer renderer = (BarRenderer) plot.getRenderer(); if (categoryCount == 1) { renderer.setItemMargin(0.15); } else { renderer.setItemMargin(0.03); } // extreme cases if (categoryCount * seriesCount < 4) { final double outerMargins = (4.0 - (categoryCount * seriesCount)) / 10.0; axis.setLowerMargin(outerMargins); axis.setUpperMargin(outerMargins); } }
From source file:edu.harvard.i2b2.analysis.ui.AnalysisComposite.java
/** * Creates a chart./* w ww. j ava 2 s. c o m*/ * * @param dataset * dataset. * * @return A chart. */ private JFreeChart createChart(CategoryDataset dataset, String title) { JFreeChart chart = ChartFactory.createBarChart(title, // chart // title "", // domain axis label "", // range axis label dataset, // data PlotOrientation.VERTICAL, // orientation false, // include legend true, // tooltips? false // URLs? ); CategoryPlot plot = (CategoryPlot) chart.getPlot(); plot.setBackgroundPaint(Color.lightGray); plot.setDomainGridlinePaint(Color.white); plot.setDomainGridlinesVisible(true); plot.setRangeGridlinePaint(Color.white); if (plot.getCategories().size() > 10) { plot.getDomainAxis().setCategoryLabelPositions(CategoryLabelPositions.DOWN_45); } Font f = chart.getTitle().getFont(); chart.getTitle().setFont(new java.awt.Font(f.getFamily(), 1, 12)); return chart; }