Results 1 to 10 of 10
  1. #1
    chittora is offline Member
    Join Date
    Sep 2008
    Posts
    3
    Rep Power
    0

    Default problem in displaying text inside the item label - JFREECHART

    Hi All,
    I dont know whether this is the right place or not to post the queries related to jfreechart here .

    I am facing a problem in displaying the text inside the item label in jfreeChart. I am trying to print the text in 2 lines one below the other at the top of each bar in waterfall chart.
    Please find the code below :

    import java.awt.Color;
    import java.text.DecimalFormat;

    import org.apache.tools.ant.listener.AnsiColorLogger;
    import org.jfree.chart.ChartFactory;
    import org.jfree.chart.ChartPanel;
    import org.jfree.chart.JFreeChart;
    import org.jfree.chart.axis.CategoryAnchor;
    import org.jfree.chart.axis.CategoryAxis;
    import org.jfree.chart.axis.CategoryLabelPosition;
    import org.jfree.chart.axis.CategoryLabelPositions;
    import org.jfree.chart.axis.CategoryLabelWidthType;
    import org.jfree.chart.axis.NumberAxis;
    import org.jfree.chart.axis.ValueAxis;
    import org.jfree.chart.labels.CategoryLabelGenerator;
    import org.jfree.chart.labels.ItemLabelAnchor;
    import org.jfree.chart.labels.ItemLabelPosition;
    import org.jfree.chart.labels.StandardCategoryLabelGenera tor;
    import org.jfree.chart.plot.CategoryPlot;
    import org.jfree.chart.plot.Plot;
    import org.jfree.chart.plot.PlotOrientation;
    import org.jfree.chart.renderer.AbstractCategoryItemRende rer;
    import org.jfree.chart.renderer.AbstractRenderer;
    import org.jfree.chart.renderer.BarRenderer;
    import org.jfree.chart.renderer.category.CategoryItemRend erer;
    import org.jfree.chart.renderer.category.CategoryItemRend ererState;
    import org.jfree.chart.renderer.category.WaterfallBarRend erer;
    import org.jfree.data.category.CategoryDataset;
    import org.jfree.data.category.DefaultCategoryDataset;
    import org.jfree.text.TextBlockAnchor;
    import org.jfree.ui.ApplicationFrame;
    import org.jfree.ui.RectangleAnchor;
    import org.jfree.ui.RefineryUtilities;
    import org.jfree.ui.TextAnchor;

    import com.lowagie.text.Font;

    /**
    * A sample waterfall chart.
    */
    public class Waterfall extends ApplicationFrame {

    private static final int WIDTHTOTAL = 782;
    WaterfallBarRenderer bar = new WaterfallBarRenderer();
    private static final int GAP = 10;
    private static final int WIDTHPLOT = 420;
    private static final int MARGIN = ((WIDTHTOTAL - WIDTHPLOT) / 2);
    private static final int LEGENDPREFEREDSIZE = MARGIN - (2 * GAP);
    private ItemLabelPosition valNegPostion = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE6, TextAnchor.TOP_CENTER, TextAnchor.CENTER, 0.0);
    private ItemLabelPosition valPosPostion = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BOTTOM_CENTER, TextAnchor.CENTER, 0.0);
    private ItemLabelPosition perNegPostion = new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER, TextAnchor.CENTER, 0.0);
    private ItemLabelPosition perPosPostion = new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER, TextAnchor.CENTER, 0.0);
    /**
    * Creates a new WaterFall Chart demo.
    *
    * @param title the frame title.
    */
    public Waterfall(final String title) {

    super(title);

    final CategoryDataset dataset = createDataset();
    final JFreeChart chart = createChart(dataset);
    final ChartPanel chartPanel = new ChartPanel(chart);
    chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
    chartPanel.setEnforceFileExtensions(false);
    setContentPane(chartPanel);
    }

    /**
    * Creates a sample dataset for the demo.
    * data set of values
    * @return A sample dataset.
    */
    private CategoryDataset createDataset() {

    final DefaultCategoryDataset dataset = new DefaultCategoryDataset();

    dataset.addValue(4470, "S1", "1Odddddddddddddddd");// cumulative
    dataset.addValue(100, "S1", "ff3dddddddddddddddddd");
    dataset.addValue(200, "S1", "fff3dddddddddddddddddM");
    dataset.addValue(100, "S1", "f3ddddddddddddddddddddd");
    dataset.addValue(200, "S1", "f3Mddddddddddddddddddddddd");

    return dataset;

    }



    private JFreeChart createChart(final CategoryDataset dataset) {

    final JFreeChart chart = ChartFactory.createWaterfallChart( "",
    "",
    "",
    dataset,
    PlotOrientation.VERTICAL,
    false,
    true,
    false
    );

    CategoryPlot plot = chart.getCategoryPlot();
    plot.setBackgroundPaint(Color.white);
    plot.setDomainGridlinePaint(null);
    plot.setRangeGridlinePaint(null);

    NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    rangeAxis.setVisible(false);
    WaterfallBarRenderer renderer = (WaterfallBarRenderer) plot.getRenderer();
    renderer.setItemLabelPaint(Color.red);
    renderer.setPaint(Color.blue);

    CategoryLabelGenerator generator2 = new StandardCategoryLabelGenerator(
    "{2}", new DecimalFormat("0.00")
    );

    renderer.setItemLabelsVisible(true);
    renderer.setLabelGenerator(new LabelGenerator(0,dataset));
    renderer.setPositiveItemLabelPosition(valPosPostio n);
    renderer.setNegativeItemLabelPosition(valNegPostio n);
    renderer.setFirstBarPaint(Color.white);
    renderer.setLastBarPaint(Color.white);
    ((org.jfree.chart.renderer.category.BarRenderer) renderer).setMaxBarWidth(.03);
    ((org.jfree.chart.renderer.category.BarRenderer) renderer).setDrawBarOutline(true);
    renderer.setBaseItemLabelFont(new java.awt.Font("Arial",Font.NORMAL,8));

    /**
    * customizing the category axis
    */

    CategoryAxis domainAxis = plot.getDomainAxis();
    domainAxis.setLowerMargin(0);
    domainAxis.setCategoryLabelPositionOffset(0);
    domainAxis.setMaxCategoryLabelWidthRatio(5.0f);
    domainAxis.setMaxCategoryLabelLines(3);
    domainAxis.setCategoryLabelPositions(
    CategoryLabelPositions.UP_90
    );


    return chart;
    }




    static class LabelGenerator implements CategoryLabelGenerator {
    /** The threshold. */
    private double threshold;;
    private CategoryDataset dataset3;

    /**
    * Creates a new generator that only displays labels that are greater
    * than or equal to the threshold value.
    *
    * @param threshold the threshold value.
    */
    public LabelGenerator(double threshold ,CategoryDataset dataset ) {
    this.threshold = threshold;
    this.dataset3 = dataset;
    }

    /**
    * 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;

    // this.dataset3 = dataset;
    Number value = dataset3.getValue(series, category);

    if (value != null) {
    double v = value.doubleValue();
    if (v > this.threshold) {
    result = value.toString(); // could apply formatting here
    }
    }
    return result + "\n"+ "Diwakar";// it is neglecting the new line //character
    }
    }


    /**
    * Starting point for the demo.
    *
    * @param args ignored.
    */
    public static void main(final String[] args) {
    final Waterfall demo = new Waterfall("Waterfall Chart Demo 2");
    demo.pack();
    RefineryUtilities.centerFrameOnScreen(demo);
    demo.setVisible(true);
    }

    }

    the problem i am facing is it is neglecting the new line character . If any one knows how to display the text one below the other please help me out .

    Thanks in advance .

    Regards,
    Diw

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I dont know whether this is the right place or not to post the queries related to jfreechart here .
    It's the right place if you happen to get a decent answer to the question. I'm betting you won't but hoping you will.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default

    Could those with the API doc for this guy's problem, read the doc and post it here so he can read it?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Norm View Post
    Could those with the API doc for this guy's problem, read the doc and post it here so he can read it?
    That's not me as I don't use JFreeChart nor have these documents, but AFAIK, you have to pay for this documentation, and that makes me wonder if posting any of it here will run afoul of copyright law.

  6. #6
    chittora is offline Member
    Join Date
    Sep 2008
    Posts
    3
    Rep Power
    0

    Default

    Hi Friends
    If anyone knows how to do it , please do let me know , i am in urgent need of it

    Regards,
    Diw

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default

    chitora,
    Do you have the API doc for the class? What does it say?

  8. #8
    chittora is offline Member
    Join Date
    Sep 2008
    Posts
    3
    Rep Power
    0

    Default

    Hi Norm,
    I am able to achieve the required functionality by using 2 renders.Well i have the doc but it doesn't say anything about skipping the new line character in the label.

    Any way thanks for taking the pain ..

    Regards,
    Chittora

  9. #9
    Join Date
    Apr 2009
    Posts
    1
    Rep Power
    0

    Default Multiline label

    To display larg label in more than one line you can use following code

    XML Code:
    CategoryAxis domainAxis = plot.getDomainAxis();
     domainAxis.setMaximumCategoryLabelLines(3);
            plot.setOrientation(PlotOrientation.VERTICAL);
    cheers !!!

  10. #10
    ramadread is offline Member
    Join Date
    Jul 2009
    Posts
    8
    Rep Power
    0

Similar Threads

  1. POI changing text to a form label
    By natt in forum Advanced Java
    Replies: 2
    Last Post: 09-19-2008, 10:49 AM
  2. problem aligning both image/text inside a button
    By rick_cols in forum SWT / JFace
    Replies: 0
    Last Post: 09-18-2008, 08:20 PM
  3. Displaying text in a tabel
    By pele in forum SWT / JFace
    Replies: 5
    Last Post: 05-28-2008, 07:05 AM
  4. Displaying data into text area
    By abhiN in forum New To Java
    Replies: 1
    Last Post: 01-22-2008, 10:30 AM
  5. Displaying text box on MIDlet form
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-22-2007, 10:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •