hi
i succeded to show a graph using JFreeChart library
My problem is that i have dynamic data to show on the graph, every periode of time i had new data
hiow can i update my graph when receiving new data
please help me?
update my chart
-
- Posts: 12
- Joined: Tue Aug 25, 2009 9:12 am
- antibot: No, of course not.
-
- Posts: 3
- Joined: Mon Aug 31, 2009 2:25 pm
- antibot: No, of course not.
- Location: Halifax, Canada
- Contact:
Re: update my chart
if its DefaultCategoryDataset just use setValue on your dataSet and if its a timeseries then all you have to do is use TimeSeriesCollection.add() . The listeners will be called as you update or add.
-
- Posts: 12
- Joined: Tue Aug 25, 2009 9:12 am
- antibot: No, of course not.
Re: update my chart
i am using timeseries but the update is not done
there is my code:
there is my code:
Code: Select all
public TestJfreeChart()
{
}
public String generateChartMethod(){
JFreeChart chart = ChartFactory.createTimeSeriesChart(
"Test", // title
"Date", // x-axis label
"Nombre", // y-axis label
getDataset(), // data
true, // create legend?
true, // generate tooltips?
false // generate URLs?
);
XYPlot plot = chart.getXYPlot();
plot.setBackgroundPaint(Color.white);
plot.setDomainGridlinePaint(Color.GRAY);
plot.setRangeGridlinePaint(Color.GRAY);
HttpSession s=SessionUtil.getSession();
String adressePagesWeb=s.getServletContext().getRealPath("/")+"pages/";
try {
File file1 = new File(adressePagesWeb+"piechart.png");
ChartUtilities.saveChartAsPNG(file1, chart, 600, 400, null);
} catch (Exception e) {
e.printStackTrace();
}
return "jfreechart";
}
public XYDataset getDataset() {
HttpSession session=SessionUtil.getSession();
TimeSeriesCollection dataset;
try{
dataset=(TimeSeriesCollection)session.getAttribute("jfreechart");
dataset.toString();
}catch(NullPointerException e)
{
dataset=new TimeSeriesCollection();
TimeSeries s3 = new TimeSeries("Series 3");
s3.add(new Minute(1,new Hour()),5);
s3.add(new Minute(2,new Hour()),7);
s3.add(new Minute(3,new Hour()),8);
s3.add(new Minute(4,new Hour()),10);
s3.add(new Minute(5,new Hour()),20);
s3.add(new Minute(6,new Hour()),50);
s3.add(new Minute(7,new Hour()),55);
s3.add(new Minute(8,new Hour()),100);
s3.add(new Minute(22,new Hour()),150);
s3.add(new Minute(23,new Hour()),400);
s3.add(new Minute(35,new Hour()),450);
s3.add(new Minute(55,new Hour()),100);
dataset.addSeries(s3);
session.setAttribute("jfreechart",dataset);
}
return dataset;
}
public void setDataset(XYDataset dataset) {
this.dataset = dataset;
}
public void update()
{
HttpSession session=SessionUtil.getSession();
TimeSeriesCollection dataset;
Calendar calendrier = Calendar.getInstance();
int min=calendrier.MINUTE;
dataset=(TimeSeriesCollection)session.getAttribute("jfreechart");
try{
dataset.getSeries(0).add(new Minute(59,new Hour()),600);
session.setAttribute("jfreechart",dataset);
generateChartMethod();
}catch(Exception e){
System.out.println("erreur lors du mise à jour du graph");
e.printStackTrace();
}
}
public void datasetChanged(DatasetChangeEvent arg0) {
}
}
please help me
-
- Posts: 1634
- Joined: Sat Feb 17, 2007 1:51 pm
Re: update my chart
please continue here.