problem in setting different labels on each bar
-
- Posts: 5
- Joined: Sun Jul 16, 2017 12:06 am
- antibot: No, of course not.
problem in setting different labels on each bar
I am new in java and Jfree chart API. I have a problem labeling in bar chart to each bar. My aim is:
I am reading an excel where I have 3 columns.
Column 1: x axis
Column 2: Y axis
Column 3: on top of each corresponding bar.
I am able to draw a bar chart but I am not able to display the label.
I might be wrong here but what I know is, to achieve this a subclass needs to be created which will extends StandardCategoryItemLabelGenerator. And, have to override createItemArray() method. But, I am not able to do it.
I'd be grateful for any help that can give me right direction.
best regards,
I am reading an excel where I have 3 columns.
Column 1: x axis
Column 2: Y axis
Column 3: on top of each corresponding bar.
I am able to draw a bar chart but I am not able to display the label.
I might be wrong here but what I know is, to achieve this a subclass needs to be created which will extends StandardCategoryItemLabelGenerator. And, have to override createItemArray() method. But, I am not able to do it.
I'd be grateful for any help that can give me right direction.
best regards,
-
- Posts: 1634
- Joined: Sat Feb 17, 2007 1:51 pm
Re: problem in setting different labels on each bar
If you show the code that you have, I will look at it and trhe suggest how a suitable label generator implementation may look like.
-
- Posts: 5
- Joined: Sun Jul 16, 2017 12:06 am
- antibot: No, of course not.
Re: problem in setting different labels on each bar
Hi Buddy,
Thanks for your reply. I have added my code here. Your pointer might have save me.
best regards,
Thanks for your reply. I have added my code here. Your pointer might have save me.
Code: Select all
InputStream chart_file_input = new FileInputStream("barChart1.xls");
HSSFWorkbook my_workbook = new HSSFWorkbook(chart_file_input);
HSSFSheet my_sheet = my_workbook.getSheetAt(0);
DefaultCategoryDataset my_bar_chart_dataset = new DefaultCategoryDataset();
DefaultCategoryDataset my_bar_chart_dataset1 = new DefaultCategoryDataset();
//ChartDataSet my_bar_chart_dataset = new ChartDataSet();
Iterator<Row> rowIterator = my_sheet.iterator();
String chart_label="a";
double chart_data=1.0;
double bar_data=1.0;
double mark = 35;
String series1 = "Above Threshold";
String series2 = "Below Threshold";
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
// if (cell.getColumnIndex() == 1) {
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
{
if (cell.getColumnIndex() == 1) {
chart_data = cell.getNumericCellValue();
if (chart_data > mark) {
my_bar_chart_dataset.addValue(chart_data, series1, chart_label);
} else if (chart_data < mark) {
my_bar_chart_dataset.addValue(chart_data, series2, chart_label);
}
}
break;
}
case Cell.CELL_TYPE_STRING:
chart_label = cell.getStringCellValue();
break;
}
}
}
JFreeChart BarChartObject=ChartFactory.createBarChart(
"","Costitutents","Alpha Release",my_bar_chart_dataset,PlotOrientation.HORIZONTAL,true,true,false) ;
CategoryPlot plot = BarChartObject.getCategoryPlot();
plot.setBackgroundPaint(new Color(97,146,218));
plot.setRangeGridlinesVisible(false);
BarChartObject.setBorderVisible(true);
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
BarRenderer renderer = (BarRenderer) plot.getRenderer();
renderer.setDrawBarOutline(false);
// for color in bars
GradientPaint gp0 = new GradientPaint(0.0f, 0.0f, Color.green, 0.0f, 0.0f, Color.green);
GradientPaint gp1 = new GradientPaint(0.0f, 0.0f, Color.red, 0.0f, 0.0f, Color.red);
GradientPaint gp2 = new GradientPaint(0.0f, 0.0f, Color.yellow, 0.0f, 0.0f, Color.yellow);
renderer.setSeriesPaint(0, gp0);
renderer.setSeriesPaint(1, gp1);
renderer.setSeriesPaint(2, gp2);
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setBaseItemLabelsVisible(true);
ItemLabelPosition position = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE1, TextAnchor.TOP_CENTER, TextAnchor.TOP_CENTER, 0.0);
renderer.setBasePositiveItemLabelPosition(position);
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.STANDARD );
//domainAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0));
Marker start = new ValueMarker(mark);
start.setPaint(Color.black);
// Displaying threshold
BasicStroke stroke = new BasicStroke(1.0f,BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, new float[] {10.0f}, 0.0f);
start.setOutlineStroke(stroke);
plot.addRangeMarker(start);
int width=640;
int height=480;
ByteArrayOutputStream chart_out = new ByteArrayOutputStream();
ChartUtilities.writeChartAsPNG(chart_out,BarChartObject,width,height);
int my_picture_id = my_workbook.addPicture(chart_out.toByteArray(), Workbook.PICTURE_TYPE_PNG);
chart_out.close();
HSSFPatriarch drawing = my_sheet.createDrawingPatriarch();
ClientAnchor my_anchor = new HSSFClientAnchor();
my_anchor.setCol1(8);
my_anchor.setRow1(20);
HSSFPicture my_picture = drawing.createPicture(my_anchor, my_picture_id);
my_picture.resize();
chart_file_input.close();
FileOutputStream out = new FileOutputStream(new File("barChart1.xls"));
my_workbook.write(out);
out.close();
}catch(Exception e){
e.toString();
}
}
best regards,
-
- Posts: 1634
- Joined: Sat Feb 17, 2007 1:51 pm
Re: problem in setting different labels on each bar
You seem to only read two columns: one (Cell.CELL_TYPE_NUMERIC, columnIndex of 1) that contains the value, and a second one (Cell.CELL_TYPE_STRING, columnIndex irrelevant) that is used to generate the category keys.
Where is the third column that you want to use to label the bars?
Where is the third column that you want to use to label the bars?
-
- Posts: 5
- Joined: Sun Jul 16, 2017 12:06 am
- antibot: No, of course not.
Re: problem in setting different labels on each bar
As of now, I am not reading that column from the excel because I do not know how and where I will set that column value in my data set. As dataset addValue() function only adds 1 Value to specific series.
To elaborate, I have added a sample Excel file with bar chart at below URL:
http://i67.tinypic.com/16k2xwy.jpg
Here, I want to read Column[A] place it on domain Axis, Column: length of Bar, Column[C]: Display as a label on each bar.
Cell.CELL_TYPE_STRING, I am reading from my column[A] and placing that value on domain Axis. I am not sure that is the correct way though.
I dont have different series to add but, based on the Stoke(Threshold) which I have set there at 35 position; I have to color each bar either Red or Green.
I hope, I am able to explain you the problem statement in a right manner now.
To elaborate, I have added a sample Excel file with bar chart at below URL:
http://i67.tinypic.com/16k2xwy.jpg
Here, I want to read Column[A] place it on domain Axis, Column: length of Bar, Column[C]: Display as a label on each bar.
Cell.CELL_TYPE_STRING, I am reading from my column[A] and placing that value on domain Axis. I am not sure that is the correct way though.
I dont have different series to add but, based on the Stoke(Threshold) which I have set there at 35 position; I have to color each bar either Red or Green.
I hope, I am able to explain you the problem statement in a right manner now.
-
- Posts: 1634
- Joined: Sat Feb 17, 2007 1:51 pm
Re: problem in setting different labels on each bar
Thanks for the screenshot with the tabale layout.
You have one numerical value for each category. What you need to do is to store this value in a suitable data structure, use that to create a custom CategoryItemLabelGenerator, and add your label generator to the renderer. A suitable label generator does not need to extend StandardCategoryItemLabelGenerator, but can be written from scratch. Here is a quick and dirty solution that should work:
if you are unsure about how to read the additional column from your excel file, how to populate the label generator, or how to finally use the label generator, feel free to ask, though some of the topics are somewhat off-topic for a JFreeChart forum.
You have one numerical value for each category. What you need to do is to store this value in a suitable data structure, use that to create a custom CategoryItemLabelGenerator, and add your label generator to the renderer. A suitable label generator does not need to extend StandardCategoryItemLabelGenerator, but can be written from scratch. Here is a quick and dirty solution that should work:
Code: Select all
import org.jfree.data.category.CategoryDataset;
import org.jfree.chart.labels.CategoryItemLabelGenerator;
import java.util.ArrayList;
import java.text.NumberFormat;
public class NumericalCategoryItemlabelGenerator implements CategoryItemLabelGenerator{
private ArrayList<Number> numbers;
private NumberFormat format;
public NumericalCategoryItemlabelGenerator(NumberFormat format){
numbers = new ArrayList<Number>();
this.format = format;
}
public String generateLabel(CategoryDataset datase, int row, int column){
if(column >= numbers.size() || numbers.get(column) == null){
return null;
}
return format.format(numbers.get(column));
}
public String generateRowLabel(CategoryDataset dataset, int row){
return null;
}
public String generateColumnLabel(CategoryDataset dataset, int column){
return generateLabel(dataset, 0, column);
}
public void addNumber(Number num){
numbers.add(num);
}
}
-
- Posts: 5
- Joined: Sun Jul 16, 2017 12:06 am
- antibot: No, of course not.
Re: problem in setting different labels on each bar
Thanks alot for the great help.
I stored the extra column in an ArrayList<Double>. I need help in adding label generator to the renderer. (I have BarRenderer.)
I stored the extra column in an ArrayList<Double>. I need help in adding label generator to the renderer. (I have BarRenderer.)
-
- Posts: 5
- Joined: Sun Jul 16, 2017 12:06 am
- antibot: No, of course not.
Re: problem in setting different labels on each bar
I am able to add the generator to the renderer.
I am very grateful to you to provide you help.
I am very grateful to you to provide you help.