stopbugginme Posted April 26, 2011 Share Posted April 26, 2011 Hi all, Is it possible to add a label in each point in a Line Chart? In case i have to customize the chart...is there any example on how to do it?I guess this is a common issue, how did tyou solve this? Link to comment Share on other sites More sharing options...
stopbugginme Posted April 26, 2011 Author Share Posted April 26, 2011 Code:import java.text.NumberFormat; import org.jfree.chart.JFreeChart; import org.jfree.chart.labels.AbstractCategoryItemLabelGenerator; import org.jfree.chart.labels.CategoryItemLabelGenerator; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.renderer.category.LineAndShapeRenderer; import org.jfree.data.category.CategoryDataset; import net.sf.jasperreports.engine.JRAbstractChartCustomizer; import net.sf.jasperreports.engine.JRChart; /** * @author mdahlman This chart customizer allows the report developer to set the * following JFreeChart property which is not otherwise set-able in * iReport: * * Tick Label Font * * This property applies only to Pie plots. Charts that do not use pie * plots will ignore these properties. The properties should be set * using the "Properties expressions" field in iReport. This results in * .jrxml like the following: * * <property name="TickLabelFontName" value="Verdana"/> <property * name="TickLabelFontStyle" value="Font.PLAIN"/> <property * name="TickLabelFontSize" value="8"/> * */ public class CustomizedLineChart extends JRAbstractChartCustomizer { @Override public void customize(JFreeChart chart, JRChart jasperChart) { // TODO Auto-generated method stub CategoryPlot categoryPlot = (CategoryPlot) chart.getPlot(); LineAndShapeRenderer lineAndShapeRenderer = (LineAndShapeRenderer) categoryPlot.getRenderer(); lineAndShapeRenderer.setBaseItemLabelGenerator(new CustomLabelGenerator()); lineAndShapeRenderer.setBaseItemLabelsVisible(true); } // Clase customizada, para implementar la visibilidad de los valores del grafico static class CustomLabelGenerator extends AbstractCategoryItemLabelGenerator implements CategoryItemLabelGenerator { /** * Creates a new generator that only displays labels that are greater * than or equal to the threshold value. * * @param threshold the threshold value. */ public CustomLabelGenerator() { super("", NumberFormat.getInstance()); } /** * Generates a label for the specified item. The label is typically a * formatted version of the data value, but any text can be used. * * @param dataset the dataset (<code>null</code> not permitted). * @param series the series index (zero-based). * @param category the category index (zero-based). * * @return the label (possibly <code>null</code>). */ public String generateLabel(CategoryDataset dataset, int series,int category) { String result = null; Number value = dataset.getValue(series, category); result = value.toString(); // could apply formatting here return result; } /* (non Javadoc) * @see org.jfree.chart.labels.CategoryItemLabelGenerator#generateRowLabel(org.jfree.data.category.CategoryDataset, int) */ public String generateRowLabel(CategoryDataset arg0, int arg1) { // TODO Stub del metodo generado automaticamente return null; } /* (non Javadoc) * @see org.jfree.chart.labels.CategoryItemLabelGenerator#generateColumnLabel(org.jfree.data.category.CategoryDataset, int) */ public String generateColumnLabel(CategoryDataset arg0, int arg1) { // TODO Stub del metodo generado automaticamente return null; } } } Link to comment Share on other sites More sharing options...
colddata Posted May 12, 2012 Share Posted May 12, 2012 stopbugginme, you are awesome!!! Thank you so much for sharing this. Spent hours googling for something that would point me in the right direction. Link to comment Share on other sites More sharing options...
DECISIT Posted June 27, 2013 Share Posted June 27, 2013 Or much simpler:package com.ireport.cust;import net.sf.jasperreports.engine.JRChart;import net.sf.jasperreports.engine.JRChartCustomizer;import org.jfree.chart.JFreeChart;import org.jfree.chart.labels.CategoryItemLabelGenerator;import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;import org.jfree.chart.plot.CategoryPlot;import org.jfree.chart.renderer.category.LineAndShapeRenderer;/** * * @author DECISIT */public class LineChartItemLabelCustomizer implements JRChartCustomizer { @Override public void customize(JFreeChart chart, JRChart jasperChart) { CategoryPlot categoryPlot; LineAndShapeRenderer lineAndShapeRenderer; categoryPlot = (CategoryPlot) chart.getPlot(); lineAndShapeRenderer= (LineAndShapeRenderer) categoryPlot.getRenderer(); lineAndShapeRenderer.setBaseItemLabelsVisible(Boolean.TRUE); lineAndShapeRenderer.setBaseItemLabelGenerator((CategoryItemLabelGenerator) new StandardCategoryItemLabelGenerator()); } }Don't forget to add the created jar to iReport classpath :) Link to comment Share on other sites More sharing options...
lfernandortiz Posted December 18, 2014 Share Posted December 18, 2014 Muchas gracias Decisit, me fue muy útil la clase que proporcionaron. A thousand Thanks!. Link to comment Share on other sites More sharing options...
ayazkhan.mca Posted November 24, 2017 Share Posted November 24, 2017 is it possible to give label name in bubble chart without coding or without scripting? Link to comment Share on other sites More sharing options...
ayazkhan.mca Posted November 24, 2017 Share Posted November 24, 2017 is it possible to give label name in bubble chart without coding or without scripting? 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