how to remove legend items?

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
alsw-2000
Posts: 16
Joined: Thu May 22, 2003 12:53 pm

how to remove legend items?

Post by alsw-2000 » Mon Feb 02, 2004 11:27 am

I have generated the following CombinedDomainCategoryPlot using jfree chart.
Is there any method to remove the last two legend items?

Image

here is the major source code to generate the plot:

Code: Select all

// get flow dataset
DefaultCategoryDataset flowDataset = new DefaultCategoryDataset();
if (cover_flow_rs != null){
	while (cover_flow_rs.next()){ 
 	   flowDataset.addValue(Double.parseDouble(cover_flow_rs.getString(FLOW_VOL_COL)), "Covering", cover_flow_rs.getString(CCY_COL).trim());
	}
}
if (cust_flow_rs != null){
	while (cust_flow_rs.next()){ 
 	   flowDataset.addValue(Double.parseDouble(cust_flow_rs.getString(FLOW_VOL_COL)), "Customer", cust_flow_rs.getString(CCY_COL).trim());
	}
}

// get volume dataset
DefaultCategoryDataset volumeDataset = new DefaultCategoryDataset();
if (cover_volume_rs != null){
	while (cover_volume_rs.next()){ 
 	   volumeDataset.addValue(Double.parseDouble(cover_volume_rs.getString(FLOW_VOL_COL)), "Covering", cover_volume_rs.getString(CCY_COL).trim());
	}
}
if (cust_volume_rs != null){
	while (cust_volume_rs.next()){ 
 	   volumeDataset.addValue(Double.parseDouble(cust_volume_rs.getString(FLOW_VOL_COL)), "Customer", cust_volume_rs.getString(CCY_COL).trim());
	}
}

// get subplots
BarRenderer flowBarRenderer = new BarRenderer();
barRenderer.setItemMargin(0.0);
CategoryPlot flowPlot= getFlowCategoryPlot(flowDataset, flowBarRenderer); // my function to get flow plot
BarRenderer volBarRenderer = new BarRenderer();
barRenderer.setItemMargin(0.0);
CategoryPlot volumePlot= getVolCategoryPlot(volumeDataset, volBarRenderer); // my function to get volume plot

// generate chart
CombinedDomainCategoryPlot combinedPlot = new CombinedDomainCategoryPlot(xAxis);

// add the subplots
combinedPlot.add(flowPlot, 2);
combinedPlot.add(volumePlot, 1);
Regards,
Andy

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 Feb 02, 2004 2:21 pm

This is the method in the CombinedDomainCategoryPlot that fetches the legend items:

Code: Select all

    /**
     * Returns a collection of legend items for the plot.
     *
     * @return the legend items.
     */
    public LegendItemCollection getLegendItems() {

        LegendItemCollection result = new LegendItemCollection();

        if (this.subplots != null) {
            Iterator iterator = subplots.iterator();
            while (iterator.hasNext()) {
                CategoryPlot plot = (CategoryPlot) iterator.next();
                LegendItemCollection more = plot.getLegendItems();
                result.addAll(more);
            }
        }

        return result;

    }
You would need to modify it (either by directly editing the source and recompiling JFreeChart, or by subclassing and overriding the method) so that it only looks at the first subplot rather than iterating through all of them.
David Gilbert
JFreeChart Project Leader

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

Locked