[SOLVED]not possible to change dates in the chartpanel

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
andkirp
Posts: 2
Joined: Fri Feb 27, 2015 8:49 am
antibot: No, of course not.

[SOLVED]not possible to change dates in the chartpanel

Post by andkirp » Fri Feb 27, 2015 6:31 pm

Hi , I'm trying to set a JfreeChart panel into my java.swing application through a JPanel, I choose some dates and then pass then to the ChartPanel creator,it creates the chartpanel, but when after that, I change those dates and repeat the process,the previous dates I used are still there in the new ChartPanel.
Is impossible to do what I'm trying? I'm using JFreechart 1.0.16 and Jcommon 1.0.15 in eclipse.
my code is this:

public class ventanaXY2{

static XYPlot plot = null;
public static JPanel init(ArrayList<StringBuffer> GraficoEmpresas,String args,ArrayList<StringBuffer> fechas) {

int cantidad = GraficoEmpresas.size();
int cantidad2 = fechas.size();
float valorMax=0;
float[][] resultArray = new float[cantidad][cantidad2];
float[] result = new float[cantidad2];
for (int x=0;x<cantidad;x++){
System.out.println("entramos "+x+" veces");
if(GraficoEmpresas.get(x).toString().equals("0")){
} else {
for (int y=0;y<cantidad2;y++){
//this is just a database call:
result[y] = llamadaGraficos.main(GraficoEmpresas.get(x),args,fechas.get(y));

}

for (int v=0;v<result.length;v++){
resultArray[x][v]=result[v];

}
}
}
float dato = cantidad2;//nº de Y-axis
float[] datos=new float[cantidad2];
int ronda = cantidad2;

int datasetIndex = 0;

TimeSeriesCollection dataset1 = createRandomDataset(GraficoEmpresas.get(0).toString(), fechas, resultArray, datasetIndex);
JFreeChart chart = ChartFactory.createTimeSeriesChart(args, "Dias", "Valor", dataset1, true, true, false);
chart.setBackgroundPaint(Color.white);
plot = chart.getXYPlot();
plot.setBackgroundPaint(Color.lightGray);
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);

plot.setRenderer(datasetIndex, new StandardXYItemRenderer(3));
datasetIndex++;
for (int x=1;x<cantidad;x++){
plot.setDataset(datasetIndex, createRandomDataset(GraficoEmpresas.get(x).toString(),fechas,resultArray,x));
plot.setRenderer(datasetIndex, new StandardXYItemRenderer(3));
datasetIndex++;
}
BufferedImage image = chart.createBufferedImage(1600,1400);
JLabel label = new JLabel(new ImageIcon(image));
JPanel panel2 = new JPanel();
panel2.add(label);
return panel2;
}
private static TimeSeriesCollection createRandomDataset(String name, ArrayList<StringBuffer> fechas, float[][] resultArray, int x) {
final TimeSeries series = new TimeSeries(name);
int cantidad2 = fechas.size();
RegularTimePeriod t = new Day();
for (int y=0;y<cantidad2;y++){//for each day:
Date fecha;
SimpleDateFormat formatoDelTexto = new SimpleDateFormat("dd-MM-yyyy");
fecha = new Date();
try {
fecha = formatoDelTexto.parse(fechas.get(y).toString());
} catch (ParseException e) {
e.printStackTrace();
}
RegularTimePeriod p = new Day (fecha);
series.addOrUpdate(p,resultArray[x][y]);
}
return new TimeSeriesCollection(series);
}
}
I return this chartPanel and add to my JFrame,then I change the dates,remove that JPanel, call again this class and add it again.And as I sayed before, the range of dates is the mixture of previous and new ones, I mean, if I send for example the range dates from 24 to 27 it shows the dates from 24 to 27, then I send the range from 23 to 27 and t shows from 23 to 27,but if I send from 19 to 22 it shows from 19 to 27.
Am I doing something wrong?
if anybody can help me I would apreciate it
Last edited by andkirp on Mon Mar 02, 2015 10:22 am, edited 1 time in total.

John Matthews
Posts: 513
Joined: Wed Sep 12, 2007 3:18 pm

Re: not possible to change dates in the chartpanel

Post by John Matthews » Sun Mar 01, 2015 12:20 am

If you remove() a Component from its enclosing Container, you'll need to validate() the container and (possibly) repaint() it. Alternatively, update the underlying model and the listening view will update itself in situ, as shown here.

andkirp
Posts: 2
Joined: Fri Feb 27, 2015 8:49 am
antibot: No, of course not.

Re: not possible to change dates in the chartpanel

Post by andkirp » Mon Mar 02, 2015 10:22 am

Resolved. Sorry for losing your time, the fail wasn't the way using freechart, i was "sure" the passing dates method was well but it was NOT,so the prolem was in my code when passing the dates.

Locked