Reputation: 591
I'm trying to create a bar chart that generates a dataset from within a for loop.
String scores = scoreText.getText();
String[] data = scores.split(",");
DefaultCategoryDataset barChartDataset = null;
for (int l = 0; l < data.length; l++) {
barChartDataset = new DefaultCategoryDataset();
// barChartDataset.setValue(new Double(data[l]), "Scores", stu);
barChartDataset.addValue(new Double(data[l]), "Scores", stu);
System.out.println(data[l]);
}
JFreeChart barChart = ChartFactory.createBarChart3D("Summary", "Name", "Scores", barChartDataset, PlotOrientation.VERTICAL, false, true, false);
The data is 10,5
. Now when the data goes through all of this and the graph is generated, only the bar for the value 5 is shown. Where is the separate bar for the value of 10? Does anyone know what I'm doing wrong? Any help is appreciated. Thanks
EDIT: Here is the code for the bar graph:
String scores = scoreText.getText();
String[] data = scores.split(",");
DefaultCategoryDataset barChartDataset = new DefaultCategoryDataset();
//JFreeChart barChart = null;
for (int l = 0; l < data.length; l++) {
//barChartDataset.addValue(new Double(data[l]), "Scores", stu);
barChartDataset.setValue(new Double(data[l]), "Scores", stu);
System.out.println(new Double(data[l]));
}
JFreeChart barChart = ChartFactory.createBarChart3D("Summary", "Name", "Scores", barChartDataset, PlotOrientation.VERTICAL, false, true, false);
barChart.setBackgroundPaint(Color.YELLOW);
barChart.getTitle().setPaint(Color.RED);
final CategoryPlot categoryPlot = barChart.getCategoryPlot();
BarRenderer barRenderer = (BarRenderer) categoryPlot.getRenderer();
DecimalFormat decimalFormat = new DecimalFormat("#.##");
barRenderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator("{2}", decimalFormat));
categoryPlot.setRenderer(barRenderer);
barRenderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.HALF_ASCENT_CENTER));
barRenderer.setItemLabelsVisible(true);
barChart.getCategoryPlot().setRenderer(barRenderer);
ValueMarker marker = new ValueMarker(7);
marker.setLabel("Required Level");
marker.setLabelAnchor(RectangleAnchor.BOTTOM_RIGHT);
marker.setLabelTextAnchor(TextAnchor.TOP_RIGHT);
marker.setPaint(Color.BLACK);
categoryPlot.addRangeMarker(marker);
categoryPlot.setRangeGridlinePaint(Color.BLUE);
//The JFrame that the bar chart will be in.
ChartFrame chartFrame = new ChartFrame("Bar Chart for Parameters", barChart);
chartFrame.setVisible(true);
chartFrame.setSize(600, 600);
Upvotes: 2
Views: 2135
Reputation: 3512
I guess you are doing a small mistake, that is with in for loop for each iteration of loop you are creating a new DefaultCategoryDataset instance
. So each time each item is added to a separate DefaultCategoryDataset
object and the final DefaultCategoryDataset
instance having the last value is utilized to create the chart, that is the only reason you are getting only last value in your chart.
Solution is create DefaultCategoryDataset object outside and before the for loop only once like:
DefaultCategoryDataset barChartDataset = new DefaultCategoryDataset();
for (int l = 0; l < data.length; l++) {
// barChartDataset.setValue(new Double(data[l]), "Scores", stu);
barChartDataset.addValue(new Double(data[l]), "Scores", stu);
System.out.println(data[l]);
}
JFreeChart barChart = ChartFactory.createBarChart3D("Summary", "Name", "Scores", barChartDataset, PlotOrientation.VERTICAL, false, true, false);
Here is the code snippet I have in one of my application and it is working fine:
DefaultCategoryDataset dataset= new DefaultCategoryDataset();
// Get today as a Calendar....
Calendar today = Calendar.getInstance();
for(int i=0; i<15 ;i++)
{
//get util.Date class object for today date.....
java.util.Date today_date=new java.util.Date(today.getTimeInMillis());
//convert date in string format to display on chart.....
String today_string_date = new SimpleDateFormat("dd/MM/yy").format(today_date);
// set values to DefaultCategoryDataset to display on chart...
dataset.setValue(rs1.getInt("login_count"),"Login Frequency", today_string_date);
today.add(Calendar.DATE, -1);
}// for closing...
JFreeChart chart = ChartFactory.createBarChart3D("ISIS:Overall login history for last 15 days", "Date -->", "No of user(s) login per day -->", dataset, PlotOrientation.VERTICAL, true, true, false);
CategoryPlot p = chart.getCategoryPlot();
NumberAxis rangeAxis = (NumberAxis) p.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
BarRenderer renderer = (BarRenderer) p.getRenderer();
DecimalFormat decimalformat1 = new DecimalFormat("##");
renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator("{2}", decimalformat1));
renderer.setItemLabelsVisible(true);
ChartUtilities.saveChartAsPNG(new File(filePath +"/chart1.png"), chart ,1250, 400);
I hope it will solve your problem.
Upvotes: 2