jfree / jfreechart

A 2D chart library for Java applications (JavaFX, Swing or server-side).
http://www.jfree.org/jfreechart/
GNU Lesser General Public License v2.1
1.22k stars 462 forks source link

jfreechart 1.0.19 to 1.5.3 issue - with struts2 - bar chart does not display #300

Open qtdnguyen opened 2 years ago

qtdnguyen commented 2 years ago

Hello everyone,

I have a bar graph displaying perfectly, using (jfreechart-1.0.19.jar), in struts 2.5.30 framework. When I try to upgrade jfreechart to version 1.5.3, the graph does not display at all, just a thumbnail there, but no display. No error or anything.

I would like to know if jfreechart-1.5.3.jar works with struts 2 I appreciate for any help or suggestion. Thanks.

I did the following modifications: . replace jfreechart-1.0.19.jar by jfreechart-1.5.3.jar . update declare code: import org.jfree.ui.Layer; to -> import org.jfree.chart.ui.Layer; . add lib xwork-core-2.1.6.jar . use java jdk-11

My code is exactly like in this url, except the mail function to adapt to struts: http://www.java2s.com/Code/Java/Chart/JFreeChartBarChartDemo1.htm

public String jfreechart_report() throws Exception {
HttpServletRequest req = ServletActionContext.getRequest();
HttpServletResponse res = ServletActionContext.getResponse(); BeanAccount account = GlobalMethods.get_account(req); HttpSession session = req.getSession(); Hashtable<String , Object> hash_params = new Hashtable<String , Object>();

  jfreechart_action = true;            
  this.dataset                               = (DefaultCategoryDataset)session.getAttribute("dataset");
  this.alist_paging_bean_program_based = (ArrayList<BeanDisplaytagReportProgramBasedLetterGrade>)session.getAttribute("alist_paging_bean_program_based");

  chart = createChart(dataset, alist_paging_bean_program_based);

  if (Utils.isEmpty(this.alist_paging_bean_program_based))
    chart_width = 500;
  else
  {
    chart_width = 500 + (18*4*(this.alist_paging_bean_program_based.size()));
  }

  return SUCCESS; 

}

private static CategoryDataset createDataset() {

  // row keys...
  String series1 = "First";
  String series2 = "Second";
  String series3 = "Third";

  // column keys...
  String category1 = "Category 1";
  String category2 = "Category 2";
  String category3 = "Category 3";
  String category4 = "Category 4";
  String category5 = "Category 5";

  // create the dataset...
  DefaultCategoryDataset dataset = new DefaultCategoryDataset();     

  dataset.addValue(1.0, series1, category1);
  dataset.addValue(5.0, series2, category1);
  dataset.addValue(4.0, series3, category1);

  dataset.addValue(4.0, series1, category2);
  dataset.addValue(7.0, series2, category2);
  dataset.addValue(3.0, series3, category2);

  dataset.addValue(3.0, series1, category3);
  dataset.addValue(6.0, series2, category3);
  dataset.addValue(2.0, series3, category3);

  dataset.addValue(5.0, series1, category4);
  dataset.addValue(8.0, series2, category4);
  dataset.addValue(3.0, series3, category4);

  dataset.addValue(5.0, series1, category5);      
  dataset.addValue(4.0, series2, category5);      
  dataset.addValue(0.0, series3, category5);

  return dataset;      

}

private static JFreeChart createChart ( CategoryDataset dataset, ArrayList alist_paging_bean_program_based ) {
// create the chart... JFreeChart chart = ChartFactory.createBarChart( "Report Program Based (Letter grade) - Display by course", // chart title "Category", // domain axis label "Percentage", // range axis label dataset, // data PlotOrientation.VERTICAL, // orientation true, // include legend true, // tooltips false // URLs );

  // set the background color for the chart...
  chart.setBackgroundPaint(Color.white);

  // get a reference to the plot for further customisation...      
  CategoryPlot plot = chart.getCategoryPlot();      
  plot.setBackgroundPaint(Color.white);
  plot.setDomainGridlinePaint(Color.lightGray);
  plot.setDomainGridlinesVisible(false);
  plot.setRangeGridlinePaint(Color.LIGHT_GRAY);
  plot.setRangeGridlinesVisible(true);      

  // set the range axis to display integers only...
  final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
  rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

  // disable bar outlines...
  BarRenderer renderer = (BarRenderer) plot.getRenderer();
  renderer.setDrawBarOutline(false);

  // show value on bar column - http://www.jfree.org/phpBB2/viewtopic.php?f=3&t=27915
  renderer.setSeriesItemLabelGenerator(0, new StandardCategoryItemLabelGenerator()); 
  renderer.setSeriesItemLabelGenerator(1, new StandardCategoryItemLabelGenerator()); 
  renderer.setSeriesItemLabelGenerator(2, new StandardCategoryItemLabelGenerator()); 
  renderer.setSeriesItemLabelGenerator(3, new StandardCategoryItemLabelGenerator()); 
  renderer.setSeriesItemLabelsVisible(0, true);
  renderer.setSeriesItemLabelsVisible(1, true);
  renderer.setSeriesItemLabelsVisible(2, true);
  renderer.setSeriesItemLabelsVisible(3, true);

  CategoryAxis domainAxis = plot.getDomainAxis();
  domainAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 2.0));

  NumberAxis yAxis = (NumberAxis) plot.getRangeAxis();
  yAxis.setLowerBound(0);
  yAxis.setUpperBound(100);
  // OPTIONAL CUSTOMISATION COMPLETED.

  // set space btw bars
CategoryAxis axis = plot.getDomainAxis();
axis.setLowerMargin(0.005);
axis.setUpperMargin(0.005);      
axis.setCategoryMargin(0.45);       
renderer.setItemMargin(0.2); 
chart.getCategoryPlot().setRenderer(renderer);

// set marker
String nameLabel = "";
Boolean bColorToogle = true;
Color colorMaker = new Color(220, 220, 220);
for (int i=0; i<alist_paging_bean_program_based.size(); i++)
{
    String category = alist_paging_bean_program_based.get(i).getCournam() + alist_paging_bean_program_based.get(i).getCourno() + " - " + alist_paging_bean_program_based.get(i).getIndicator_desc();

    CategoryMarker marker = new CategoryMarker(category);
    if (!nameLabel.equals(alist_paging_bean_program_based.get(i).getCournam() + alist_paging_bean_program_based.get(i).getCourno()))
    {
        marker.setLabel(alist_paging_bean_program_based.get(i).getCournam() + alist_paging_bean_program_based.get(i).getCourno() + "-" + alist_paging_bean_program_based.get(i).getCourse_sequence());
        if (bColorToogle)
        {
            colorMaker = new Color(220, 220, 220);
            bColorToogle = false;
        }
        else
        {
            colorMaker = new Color(230,230,250);
            bColorToogle = true;
        }
    }
    else
    {
        marker.setLabel("");    
    }
    nameLabel = alist_paging_bean_program_based.get(i).getCournam() + alist_paging_bean_program_based.get(i).getCourno();

    marker.setPaint(colorMaker);
    marker.setOutlinePaint(Color.red);
    marker.setAlpha(0.5f);
    marker.setLabelAnchor(RectangleAnchor.TOP);
    marker.setLabelTextAnchor(TextAnchor.TOP_CENTER);
    marker.setLabelOffsetType(LengthAdjustmentType.CONTRACT);
    chart.getCategoryPlot().addDomainMarker(marker, Layer.BACKGROUND);          
}   

  return chart;      

}