singmal Posted November 23, 2009 Share Posted November 23, 2009 I did design a Multi Axis JFreechart where the Y1 is a Bar chart and Y2 is a line chart in iReport and it works just fine which the input datasource. But to add itemLabels on the JFreeCharts I realized that we need to use ChartCustomizers. I did use them , but the problem Iam facing is 1) Customizer(s) is always displaying same BarChart values for linechart as well as item Labels.2) Also the line chart is not clear for some reason when displayed. Attached files as to how Iam trying to do it. Please let me know if anyone have a solution to this. -MallikCode:Added code for my java customizers--------------------package chart.customizers;public class BarChartLabelCustomizer implements JRChartCustomizer{ public BarChartLabelCustomizer(){ super(); } public void customize(JFreeChart chart, JRChart jasperChart) { BarRenderer3D renderer = (BarRenderer3D) chart.getCategoryPlot().getRenderer(); renderer.setBaseItemLabelsVisible(true); renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); renderer.setBaseItemLabelFont(new Font("Helvetica",Font.PLAIN,4)); renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.CENTER,TextAnchor.BOTTOM_CENTER,TextAnchor.CENTER,80.1)); }}-------------------Second Cutomizer-----package chart.customizers;public class DualYLineLabelCustomizer extends JRAbstractChartCustomizer{ public DualYLineLabelCustomizer(){ super(); } public void customize(JFreeChart chart, JRChart jasperChart) { CategoryItemRenderer renderer = ((CategoryPlot) chart.getPlot()).getRenderer(1); renderer.setBaseItemLabelsVisible(true); renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); renderer.setBaseItemLabelFont(new Font("Helvetica",Font.PLAIN,4)); renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.INSIDE1,TextAnchor.BOTTOM_CENTER)); renderer.setSeriesPaint(0,new Color(246,189,15)); renderer.setSeriesPaint(0,new Color(0,0,0)); renderer.setSeriesStroke(0, new BasicStroke(3)); }} Link to comment Share on other sites More sharing options...
pascalthome Posted January 11, 2012 Share Posted January 11, 2012 Old post but I recently had a similar problem and found a workaround by myself. Hope it can help somebody. My solution was to build only one customizer and attached it to the parent graph (Multi Axis Chart).You can then access the 2 axis and the 2 renderers with their number (0: first graph, 1: second graph etc...)For example plot.getRangeAxis(0) and plot.getRenderer(0) for the first graph.Code below is an sample of a customized line & bar graph PascalCode: public void customize(JFreeChart jfc, JRChart jrc) throws SystemException { CategoryPlot plot = (CategoryPlot) jfc.getPlot(); CategoryDataset dataset = plot.getDataset(); // Retrieve fields value from report iqmin = (Integer) getFieldValue("iqmin"); iqavg = (Double) getVariableValue("iqavg"); // 2 Axis objects NumberAxis lineaxis = (NumberAxis) plot.getRangeAxis(0); NumberAxis baraxis= (NumberAxis) plot.getRangeAxis(1); // Line Renderer LineAndShapeRenderer linerenderer = (LineAndShapeRenderer) plot.getRenderer(0); //Custom Bar Renderer or standard: BarRenderer barrenderer = (BarRenderer) plot.getRenderer(1); ThresholdBarRenderer barrenderer = (ThresholdBarRenderer) new ThresholdBarRenderer(); plot.setRenderer(1, barrenderer); // Retrieve Series Paint from Chart Theme (don't work with barrenderer... strange) barokcolor=linerenderer.getSeriesPaint(0); barnotokcolor=linerenderer.getSeriesPaint(1); iqmincolor=linerenderer.getSeriesPaint(2); iqavgcolor=linerenderer.getSeriesPaint(3); nbvisitscolor=linerenderer.getSeriesPaint(4); // Gradient&Light effects on bars barrenderer.setGradientPaintTransformer( new StandardGradientPaintTransformer( GradientPaintTransformType.HORIZONTAL)); barrenderer.setBarPainter( new GradientBarPainter(0.07,0.14,0.80) ); // Axis labels color lineaxis.setLabelPaint(nbvisitscolor); baraxis.setLabelPaint(barokcolor); // Custom line shape linerenderer.setDrawOutlines(true); linerenderer.setUseFillPaint(true); linerenderer.setBaseFillPaint(Color.white); linerenderer.setSeriesStroke(0, new BasicStroke(3.0f)); linerenderer.setSeriesOutlineStroke(0, new BasicStroke(2.0f)); linerenderer.setSeriesShape(0, new Ellipse2D.Double(-5.0, -5.0, 10.0, 10.0)); //A marker on the bar axis "plot.addRangeMarker(1" (0 for line axis) ValueMarker LineMin = new ValueMarker(iqmin,iqmincolor,new BasicStroke(1.1F)); LineMin.setLabel("Minimum"); LineMin.setLabelPaint(iqmincolor); LineMin.setLabelFont(new Font("Dialog", 0, 7)); LineMin.setLabelTextAnchor(TextAnchor.TOP_LEFT); LineMin.setLabelAnchor(RectangleAnchor.BOTTOM_LEFT); plot.addRangeMarker(1,LineMin, Layer.BACKGROUND) ; // Bar width barrenderer.setMaximumBarWidth(0.15); // Bar values label int labelfontsize=(dataset.getColumnCount()<8)?10:7;; barrenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); barrenderer.setBaseItemLabelFont(new Font("Dialog", 0, labelfontsize)); barrenderer.setBaseItemLabelsVisible(true); ItemLabelPosition p = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER); barrenderer.setBasePositiveItemLabelPosition(p); } Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now