Jump to content

filtering chart labels


lotus4

Recommended Posts

 Hi, 

is it possibile to filter the label in a chart?

i ve got a lot of date minute per minute but i wanna show into my chart, not the whole timeline, like the attach, but only few date (e.g. every 30 min:

2011-24-02 00:00, 2011-24-02 00:30, 2011-24-02 10:00

and not

2011-24-02 00:01, 2011-24-02 00:02, 2011-24-02 10:03 etc..

 

I've use filtering clause but it filter result deleting it and so  distorting the graph curve.

 

Thanks in advance

Marco

Link to comment
Share on other sites

  • Replies 6
  • Created
  • Last Reply

Top Posters In This Topic

 One more hint:

i ve attached 2 files:

not_filtered.pdf: a graph without filters (the exact one)

 

filtered.pdf: a graph with a filter like this:

$F{HTTP_DATE}.getMinutes() == 0 || $F{HTTP_DATE}.getMinutes() == 30

which excludes most of the dates, but with them, values too, distorting the graph semantic

Link to comment
Share on other sites

You would need a Chart Customizer or a Chart Theme to get that type of behavior. It's certainly possible, but it would not be trivial.

In general, it's not reasonable to hide certain labels. If you display "AL, AR, CA" then can the reader guess that "AK and AZ" are the unlabeled states? In the case of dates and times, then it's entirely reasonable to hide some values. More importantly, dates have a numeric relationship with each other: Jan1 and Jan2 are closer together than Jan5 and Jan8. Most labels on the axis don't have such a relationship. 

That's why the timeseries chart exists and why it's so useful.

I guess I'm saying that there's not a huge demand for hiding some labels outside of dealing with datetimes. Likewise, there's not high demand for representing timeseries data in a barchart. But there are surely some instances when you might want those things. If you're in that situation then you'll need to code a Chart Theme to handle it.

Good luck,
Matt

Link to comment
Share on other sites

 You're right, matt.. it's not easy at all.

I've found, i dont know how :), this post:

http://jasperforge.org/plugins/espforum/view.php?group_id=102&forumid=103&topicid=49900

it deals about the same issue that i noticed.

 

User bklawans lists the guidelines to deal with it (i copy here for comfort):

Basically, you have to create a time based stacked area chart, then change the type of the x-axis to a time period axis.  This involves writing a chart customizer that makes some pretty big changes to the underlying JFreeChart

You want the area chart to be time based, so you have to author the report using a time series chart

 

The first thing your customizer has to do is convert the data set - JFreeChart requires a TimeTableXYDataset for stacked area charts, and JasperReports will have created the chart using a TimeSeriesCollection.  You can get the TimeSeriesCollection by calling chart.getPlot().getDataset() on the chart passed in to the customizer.  You create a new TimeTableXYDataset.  Then for each series in the original dataset loop through all the items in the series.  Add a new entry to your dataset using the period, value and key of the original item.

 

Set the dataset in the chart's plot to the one you just created.  Create a StackedXYAreaRender2, and set that in the plot.  To avoid some JFreeChart issues set the plot's dataset rendering order and series rendering order to FORWARD.

 

Finally, create a PeriodAxis, set the date intervals as appropriate for you chart, and then set that as the domain axis in the plot.

 

 

I've written this piece of code in my customize method:

 

 

            JRChartPlot plot = jasperChart.getPlot();

//dataset timeseries
JRFillTimeSeriesPlot  myPlot = (JRFillTimeSeriesPlot)plot;
JRFillTimeSeriesDataset inputDataSet = (JRFillTimeSeriesDataset)myPlot.getChart().getDataset();
JRTimeSeries[] timeSeriesArray = inputDataSet.getSeries();
System.out.println(inputDataSet.getSeries().length);
 
 
But it seems that my graph (a time series graph) implementing JRFillTimeSeriesPlot  and JRFillTimeSeriesDataset and no XYPlot and TimeSeriesCollection. Those Classes (JRFillTimeSeriesPlot and JRFillTimeSeriesDataset) have not so good API so i can't do nothing with it.
How could i loop through my dataset to copy its entries into a new dataset di associate to my original plot (note that i can't set a rendere to a JRFillTimeSeriesPlot .. o.O)?

 

 

Link to comment
Share on other sites

ah! i've found my error: the customize method has this signature:

 public void customize(JFreeChart jFreeChart, JRChart jasperChart) 

 

I've used jasperChart, while jFreeChart reference must be used. Anyway, this is the piece of code.

Now i'm facing with Period axis to set a specific interval of time, because now it prints a random interval series.

Code:
public class CustomReport implements JRChartCustomizer {	@Override	public void customize(JFreeChart jFreeChart, JRChart jasperChart) {		Plot plot = jFreeChart.getPlot();		XYPlot   myPlot = (XYPlot)plot;				//dataset timeseries		TimeSeriesCollection  inputDataSet = (TimeSeriesCollection )myPlot.getDataset();						//dataset per stacked area graph		TimeTableXYDataset ti = new TimeTableXYDataset();				List seriesList = inputDataSet.getSeries();		//loop through series items to copy to other dataset: TimeTableXYDataset		 Iterator it = seriesList.iterator();		    while (it.hasNext()) {		    	TimeSeries serie = (TimeSeries)it.next();		        		        int itemCount = serie.getItemCount();		        		        for(int i =0; i<itemCount; i++)		        {				        	//add serie to other dataset		        	ti.add(serie.getTimePeriod(i), serie.getValue(i), (String)serie.getKey(), false);		        	 		        }		       		    }		 				//stacked area render to draw the correct graph		    		   		StackedXYAreaRenderer2  s = new StackedXYAreaRenderer2();		myPlot.setDataset(ti);		myPlot.setRenderer(s);		myPlot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);		myPlot.setSeriesRenderingOrder(SeriesRenderingOrder.FORWARD);				//axis formatter		PeriodAxis p = new PeriodAxis("");//		p.setAutoRangeTimePeriodClass(Day.class);//		p.setMajorTickTimePeriodClass(Minute.class);		p.setFixedAutoRange(new Double("12.4"));		PeriodAxisLabelInfo[] info = new PeriodAxisLabelInfo[2];				info[0] = new PeriodAxisLabelInfo(Minute.class, new SimpleDateFormat("hh:mm"));		 //	       info[1] = new PeriodAxisLabelInfo(Year.class, new SimpleDateFormat("yyyy"));	    info[1] = new PeriodAxisLabelInfo(Day.class,  new SimpleDateFormat("dd-MMM-yyyy"));		p.setLabelInfo(info);		myPlot.setDomainAxis(p);					}
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...