Labels on line chart

A discussion forum for JFreeChart (a 2D chart library for the Java platform).

Labels on line chart

Postby abhay.asingh » Mon Mar 16, 2009 2:24 pm

Hi

I want to put labels on line chart at the end of each line with the last value point.
The reason is when i take print out in laser printer its hard to distinguish the lines.


Thanks
Abhay Singh
abhay.asingh
 
Posts: 58
Joined: Mon Dec 15, 2008 7:07 am
Location: India

Re: Labels on line chart

Postby reboot » Mon Mar 16, 2009 3:32 pm

Well if u want to show each value of the line chart u can use "search for CustomLabelGenerator on this forum", implements this class in your prject, after that

when u create your chart use this code

Code: Select all
CategoryPlot categoryPlot = (CategoryPlot)yourChart.getPlot();

LineAndShapeRenderer lineAndShapeRenderer =  (LineAndShapeRenderer)categoryPlot.getRenderer();

lineAndShapeRenderer.setBaseItemLabelGenerator(new CustomLabelGenerator());

lineAndShapeRenderer.setBaseItemLabelsVisible(true);



if u want to show only the last value... u'd controll to show only the last value inside the dataSet...
reboot
 
Posts: 46
Joined: Wed Jan 07, 2009 5:35 pm

Re: Labels on line chart

Postby abhay.asingh » Mon Mar 23, 2009 11:18 am

Hi

Thanks.

Have another querstion, how can we show legends at the end of line, means at the end of line i want to print series name for which line belongs to ??
Abhay Singh
abhay.asingh
 
Posts: 58
Joined: Mon Dec 15, 2008 7:07 am
Location: India

Re: Labels on line chart

Postby reboot » Mon Mar 23, 2009 6:04 pm

i think that u can only enable the lenged, assuming that each line is showed in the legend. Or u can add to the value labels, the series name before it, for each point in the line chart... to have at the end of each line the series name, u'd overwrite the render method... where if u are lableing the last value of the line, add also the series name...something similar...
reboot
 
Posts: 46
Joined: Wed Jan 07, 2009 5:35 pm

Re: Labels on line chart

Postby skunk » Mon Mar 23, 2009 6:27 pm

Try something like this
Code: Select all
renderer.setBaseItemLabelGenerator(new XYItemLabelGenerator() {
    public String generateLabel(XYDataset dataset, int series, int item) {
        if (item == dataset.getItemCount(series) - 1)
            return (String)dataset.getSeriesKey(series);
        else
            return null;
    }
});

Obviously, if you are using a CategoryPlot, then you would use a CategoryItemLabelGenerator instead
skunk
 
Posts: 1087
Joined: Thu Jun 02, 2005 10:14 pm
Location: Brisbane, Australia

Re: Labels on line chart

Postby abhay.asingh » Tue Mar 24, 2009 8:51 am

Hi

Yes i am using CategoryPlot but in CategoryItemLabelGenerator we don't have row count for specific series like we can't say rows in series-1 and rows in series-2 like that...
so i have worte this code, but this behave differently in mutlidimensionl data
Code: Select all
lineAndShapeRenderer.setBaseItemLabelGenerator(new CustomLabelGeneratorLine());

static class CustomLabelGeneratorLine extends AbstractCategoryItemLabelGenerator implements CategoryItemLabelGenerator {
                public CustomLabelGeneratorLine() {
                        super("", NumberFormat.getInstance());
                }

                public String generateLabel(CategoryDataset dataset, int series, int category) {
                        String result = null;
                        Number value = dataset.getValue(series, category);
                        DecimalFormat df = new DecimalFormat("##,###");
                        if((int)value.intValue() == 0)
                                result = "No-Data";
                        else if(category ==  (dataset.getColumnCount()-1))
                                result = df.format(value).toString()+"("+(String)dataset.getRowKey(series)+")";
                        else
                                result = df.format(value).toString();

                        return result;
                }
        }

This works fine if i have data like revenue year wise, but if have data like year, month and revenue the it doesn't works or data like
year, revenue, expances where data for revenue is present for all the year but expences are only for 1st few year.
the rowcount will be total number of years, though for 2nd series its less then the 1st one.

At last, but am i doing in correct way?
Abhay Singh
abhay.asingh
 
Posts: 58
Joined: Mon Dec 15, 2008 7:07 am
Location: India

Re: Labels on line chart

Postby tgrahl » Sun Oct 08, 2017 10:21 am

I am trying the example of skunk, but am not able to see any label on the last point in an XY line chart

Code: Select all
renderer.setBaseItemLabelGenerator(new XYItemLabelGenerator() {
    public String generateLabel(XYDataset dataset, int series, int item) {
        if (item == dataset.getItemCount(series) - 1)
            return (String)dataset.getSeriesKey(series);
        else
            return null;
    }
});


Have it working with the StandardXYItemLabelGenerator, but want to override the "generateLabel" to be able to create the label on the last point.
Or should I look on how to iterate through the points, and draw the table at the last point, instead of using LabelGenerator, would that work.


Code: Select all
XYPlot plot = (XYPlot) chart.getPlot();
renderer = (XYLineAndShapeRenderer) plot.getRenderer();
NumberFormat format = NumberFormat.getNumberInstance();
format.setMaximumFractionDigits(2); // etc.
renderer.setBaseItemLabelsVisible(true);
XYItemLabelGenerator generator =
    new StandardXYItemLabelGenerator("{0} {1} {2}", format, format);
renderer.setBaseItemLabelGenerator(generator);
tgrahl
 
Posts: 1
Joined: Sun Oct 08, 2017 10:09 am


Return to JFreeChart

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Joshi, Majestic-12 [Bot] and 12 guests