Change Chart Title During Runtime

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Reggie3
Posts: 12
Joined: Mon Mar 10, 2008 5:19 pm

Change Chart Title During Runtime

Post by Reggie3 » Mon Mar 10, 2008 5:43 pm

I need to change the title of my chart at runtime based on data from the dataset. I'm able to successfully change the dataset, and the bars on my chart reflect that, however the title stays the same. How do I get the entire chart to redraw / repaint (or am I looking at the problem wrong)? Here is my code:

Code: Select all

public static JFreeChart CreateChart( String strSessionMouseID){
		Connection conn = null;
		ResultSet rsRecordSet=null;
		Statement statStatement = null;
		DecimalFormat dfPercent = new DecimalFormat("0.0%");
		
		String strQuery = "Select SESSIONTRIALNUM, SESSIONDAYNUM, FAILURERATE FROM " 
			+ GlobalDataStore.strRunDataTblName + " WHERE  SESSIONMOUSEID LIKE '" + strSessionMouseID + "%'";		
		conn = DerbyUtils.BootDatabase(conn);
		String strChartName = strSessionMouseID + " Trial Data";

		System.out.println(strQuery);
		try {
			statStatement = conn.createStatement();
			rsRecordSet = statStatement.executeQuery(strQuery);	//execute the query

			if (!rsRecordSet.next()){
                System.out.println("No mice in database.");
            }
			else{
				dataset.clear();
				do{
					dataset.addValue(Double.valueOf(rsRecordSet.getString("FAILURERATE")), 
						rsRecordSet.getString("SESSIONTRIALNUM"), rsRecordSet.getString("SESSIONDAYNUM"));
				    }while (rsRecordSet.next());
			}
			rsRecordSet.close();
			
			//chart = ChartFactory.createLineChart(
			chart = ChartFactory.createBarChart(
		            strChartName,         // chart title
		            "Day",               	// domain axis label
		            "Miss Percentage",      // range axis label
		            dataset,                  // data
		            PlotOrientation.VERTICAL, // orientation
		            true,                     // include legend
		            true,                     // tooltips?
		            false                     // URLs?
		        );
			
			chart.setTitle(strChartName);
			CategoryPlot plot = (CategoryPlot) chart.getPlot();
			BarRenderer bRenderer = (BarRenderer)plot.getRenderer();
//			LineAndShapeRenderer renderer = (LineAndShapeRenderer)plot.getRenderer();
			
			CategoryItemRenderer cIRenderer = plot.getRenderer();
			CategoryItemLabelGenerator generator = new StandardCategoryItemLabelGenerator("{2}", dfPercent);
			cIRenderer.setItemLabelGenerator(generator);
			ShowBarLabels(true);
			
			plot.setDataset(dataset);
			//bRenderer.setItemMargin(0.5);
			
			conn.close();
			if(chart == null) System.out.println("Chart==null");
			return chart;
		} catch (SQLException e) {
			System.out.println("Error in CreateChart");
			e.printStackTrace();
			return null;
		}
	}

Locked