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
Labels on line chart
-
- Posts: 58
- Joined: Mon Dec 15, 2008 7:07 am
- Location: India
- Contact:
Labels on line chart
Abhay Singh
Re: Labels on line chart
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
if u want to show only the last value... u'd controll to show only the last value inside the dataSet...
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...
-
- Posts: 58
- Joined: Mon Dec 15, 2008 7:07 am
- Location: India
- Contact:
Re: Labels on line chart
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 ??
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
Re: Labels on line chart
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...
Re: Labels on line chart
Try something like this
Obviously, if you are using a CategoryPlot, then you would use a CategoryItemLabelGenerator instead
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;
}
});
-
- Posts: 58
- Joined: Mon Dec 15, 2008 7:07 am
- Location: India
- Contact:
Re: Labels on line chart
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
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?
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;
}
}
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
Re: Labels on line chart
I am trying the example of skunk, but am not able to see any label on the last point in an XY line chart
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
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;
}
});
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);