Jump to content
Changes to the Jaspersoft community edition download ×

Label in each point of a Line Chart


stopbugginme

Recommended Posts

  • Replies 6
  • Created
  • Last Reply

Top Posters In This Topic

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

  • 1 year later...
  • 1 year later...

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

  • 1 year later...
  • 2 years later...

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...