Getting same month value 4 time on x axis for 1 month

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
manish
Posts: 10
Joined: Tue Aug 01, 2006 7:24 am

Getting same month value 4 time on x axis for 1 month

Post by manish » Mon Aug 21, 2006 2:36 pm

Hi,
I am getting duplicate date value in x axis.

here in dataset i am getting one value for 1 month from database but when want to print on chart, getting same month value 4 times in x - axis.
For Example if I am using Time range(1-2006 to 7- 2006 ) and have one value fr 3-2006 and print on chart it shows the 3-2006 four time on X -axis
pease help me out>>>>>>..

my sample code is here:-
//code to show chart

if(listPBM.size()>0)
{

timeSeriesChart = createChart(sFromDate,sToDate,createDataset(sNDC,listPBM,sFromMonth,sFromYear,sToMonth,sToYear));

request.setAttribute("timeSeriesChart",timeSeriesChart);

response.setContentType( "image/png" );

BufferedImage buf = timeSeriesChart.createBufferedImage(600, 420, null);
PngEncoder encoder = new PngEncoder( buf, false, 0, 9 );
response.getOutputStream().write( encoder.pngEncode() );
System.out.println("in servlet ");

//RequestDispatcher requestDispatcher = getServletContext().getRequestDispatcher("/TimeSeriesReportResult.jsp");
//requestDispatcher.forward(request,response);

}



//code to get chart


private static JFreeChart createChart(String sFromDate,String sToDate, XYDataset dataset) {

final JFreeChart chart = ChartFactory.createTimeSeriesChart(
"Time Series Report From "+sFromDate+ " To " +sToDate ,
"Time in Month-Year",
"Amount in $ ",
dataset,
true,
true,
false
);
XYPlot plot = (XYPlot) chart.getPlot();
plot.setDomainCrosshairVisible(true);
plot.setRangeCrosshairVisible(true);
XYItemRenderer r = plot.getRenderer();
if (r instanceof XYLineAndShapeRenderer) {
XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;
renderer.setBaseShapesVisible(true);
renderer.setBaseShapesFilled(true);

}
DateAxis axis = (DateAxis) plot.getDomainAxis();
axis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));

return chart;
}



// code to get dataset

private XYDataset createDataset(String sNDC, List listPBM, String sFromMonth, String sFromYear, String sToMonth, String sToYear) {

TimeSeriesCollection dataset = new TimeSeriesCollection();

try {
Map mapMaxAvgAmount = new HashMap();
TimeSeries series = null;
if(listPBM.size()>0)
{
for(int i=0;i<listPBM.size();i++)
{
series = new TimeSeries((String)listPBM.get(i), Month.class);
dataset.addSeries(series);
//series.add(new Month(Integer.parseInt(sFromMonth), Integer.parseInt(sFromYear)),new Double("0.0").doubleValue());
//series.add(new Month(Integer.parseInt(sToMonth), Integer.parseInt(sToYear)),new Double("0.0").doubleValue());
mapMaxAvgAmount = (Map)timeSeriesBean.getMaxAvgAmountForNDC(sNDC,(String)listPBM.get(i),sFromMonth,sFromYear,sToMonth,sToYear);
System.out.println("in Servlet mapMaxAvgAmount++++++++"+mapMaxAvgAmount);
if(mapMaxAvgAmount.size()>0)
{
Set entries = mapMaxAvgAmount.entrySet();
Iterator iterator = entries.iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry)iterator.next();

String sMonthYear = (String)entry.getKey();

int iLenght = sMonthYear.length();

String sMonth = sMonthYear.substring(0 ,iLenght-4 );
String sYear = sMonthYear.substring(iLenght-4 ,iLenght );

System.out.println("In SErvlet>>>>>>>>>>sMonth==="+sMonth+"sYear++++"+sYear);

series.add(new Month(Integer.parseInt(sMonth), Integer.parseInt(sYear)),new Double((String) entry.getValue()).doubleValue());
// series.add(new Month(5, 2004), 17.3);

// dataset.addSeries(series);
}
}

}
}
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return dataset;
}





[/code]

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Mon Aug 21, 2006 3:19 pm

Comment out this line:

Code: Select all

 axis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy")); 
...and observe the difference. Setting the date format override only overrides the formatting of tick labels, not the gap between tick marks. What is happening is that you are getting tick marks 1 week apart, but formatting the labels in a way that can't distinguish between them (because your date format doesn't include the day-of-the-month).

You can use the setTickUnitCollection() method to specify a set of standard ticks sizes and formats - that's what you need to do here.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

manish
Posts: 10
Joined: Tue Aug 01, 2006 7:24 am

Post by manish » Tue Aug 22, 2006 6:39 am

Hi dave,
Thank u For reply.
but when i comment code:-

axis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));

i am getting X-axis with day-month fashion.
But I want to show value respect to Month And Year.
yu have said abt setTickUnitCollection() ,
so can u put some sample code that show how i can replace the
axis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));
thank in advance.

jwenting
Posts: 157
Joined: Sat Jul 15, 2006 7:46 am

Post by jwenting » Tue Aug 22, 2006 7:15 am

Your problem here has to do with that dateformat.
Since you have multiple datapoints per month you'll always end up with multiple tickmarks (and labels) for each month unless you create your own TimeLine and axis which explicitly set it to 1 month intervals.

manish
Posts: 10
Joined: Tue Aug 01, 2006 7:24 am

Post by manish » Tue Aug 22, 2006 8:51 am

hi jwenting ;
thank u for reply.
but i am getting only one value for a month from database.
thts y in my code:-
mapMaxAvgAmount will contain" month year" as a key and amount as value.

And as dave replied when i used
// axis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));
DateTickUnit dateTickUnit = new DateTickUnit(DateTickUnit.MONTH+1,DateTickUnit.YEAR+1,new SimpleDateFormat());
axis.setTickUnit(dateTickUnit);
return chart;

its also not working.

manish
Posts: 10
Joined: Tue Aug 01, 2006 7:24 am

Post by manish » Wed Aug 23, 2006 7:28 am

Hi,
I have sort out the soln of problem.
Thank U for reply specially for dave.
the repaced code is:-


DateAxis axis = (DateAxis) plot.getDomainAxis();

DateTickUnit dateTickUnit = new DateTickUnit(1,1,new SimpleDateFormat("MMM-yyyy"));
axis.setTickUnit(dateTickUnit);

return chart;

Locked