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]
Getting same month value 4 time on x axis for 1 month
-
- JFreeChart Project Leader
- Posts: 11734
- Joined: Fri Mar 14, 2003 10:29 am
- antibot: No, of course not.
- Contact:
Comment out this line:
...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.
Code: Select all
axis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));
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
Read my blog
Support JFree via the Github sponsorship program
JFreeChart Project Leader


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.
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.
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.
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.