How to zoom in along categoty axis orientation (X-AXIS)?

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
veronica
Posts: 4
Joined: Sat Aug 05, 2006 2:09 pm

How to zoom in along categoty axis orientation (X-AXIS)?

Post by veronica » Mon Sep 04, 2006 10:59 am

Dear Administrator,

I'm a software developer working for a software developing firm and I precicate your product of JFreeChart very much.
However, I happened to a demand from my customers, which seems a big big trouble to me! So, I decided to turn to you for help!

Ok, My problem is as following:
When I draw a StackedBarChart according to the example of StackedBarChartDemo5.java, I found that I could NOT zoom in
along X Axis orientation by moving my mouse on the ploting panel (you call it Category Axis).

My Demand:
I would be pleased if you can give me a solution to solve the problem of zooming in along X Axis by moving the mouse!
You also could provide me a method as zooming in a rectangle area by moving the mouse!


I believe you're so kind to give me a hand as soon as possible, and I'm looking forward to your feedback!

Thank you once again!

Sincerely, yours
Veronica
Sept. 4, 2006

Code: Select all

package demo;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.SubCategoryAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.GroupedStackedBarRenderer;
import org.jfree.data.KeyToGroupMap;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import org.jfree.ui.TextAnchor;

/**
 * A simple demonstration application showing how to create a stacked bar chart
 * using data from a {@link CategoryDataset}.
 */
public class StackedBarChartDemo5 extends ApplicationFrame {

    /**
     * Creates a new demo.
     *
     * @param title  the frame title.
     */
    public StackedBarChartDemo5(String title) {
        super(title);
        JPanel chartPanel = createDemoPanel();
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        setContentPane(chartPanel);
    }
    
    /**
     * Creates a sample dataset.
     * 
     * @return A sample dataset.
     */
    private static CategoryDataset createDataset() {
        DefaultCategoryDataset result = new DefaultCategoryDataset();
        result.addValue(3396.0, "S1", "C1");
        result.addValue(1580.0, "S2", "C1");
        result.addValue(76.0, "S3", "C1");
        result.addValue(10100.0, "S4", "C1");
        result.addValue(3429.0, "S1", "C2");
        result.addValue(1562.0, "S2", "C2");
        result.addValue(61.0, "S3", "C2");
        result.addValue(-10100.0, "S4", "C2");        
        return result;
    }
    
    /**
     * Creates a sample chart.
     * 
     * @param dataset  the dataset for the chart.
     * 
     * @return A sample chart.
     */
    private static JFreeChart createChart(CategoryDataset dataset) {

        JFreeChart chart = ChartFactory.createStackedBarChart(
            "Stacked Bar Chart Demo 5",  // chart title
            "Category",                  // domain axis label
            "Value",                     // range axis label
            dataset,                     // data
            PlotOrientation.VERTICAL,    // the plot orientation
            true,                        // legend
            true,                        // tooltips
            false                        // urls
        );
        
        GroupedStackedBarRenderer renderer = new GroupedStackedBarRenderer();
        KeyToGroupMap map = new KeyToGroupMap("G1");
        map.mapKeyToGroup("S1", "G1");
        map.mapKeyToGroup("S2", "G1");
        map.mapKeyToGroup("S3", "G2");
        map.mapKeyToGroup("S4", "G3");
        renderer.setSeriesToGroupMap(map); 
        renderer.setItemLabelGenerator(
                new StandardCategoryItemLabelGenerator());
        renderer.setItemLabelsVisible(true);
        renderer.setPositiveItemLabelPositionFallback(new ItemLabelPosition(
                ItemLabelAnchor.OUTSIDE12, TextAnchor.BOTTOM_CENTER));
        
        renderer.setItemMargin(0.10);
        SubCategoryAxis domainAxis = new SubCategoryAxis("Category / Group");
        domainAxis.setCategoryMargin(0.05);
        domainAxis.addSubCategory("G1");
        domainAxis.addSubCategory("G2");
        domainAxis.addSubCategory("G3");
        
        CategoryPlot plot = (CategoryPlot) chart.getPlot();
        plot.setDomainAxis(domainAxis);
        plot.setRenderer(renderer);
        return chart;
        
    }
    
    /**
     * Creates a panel for the demo (used by SuperDemo.java).
     * 
     * @return A panel.
     */
    public static JPanel createDemoPanel() {
        JFreeChart chart = createChart(createDataset());
        return new ChartPanel(chart);
    }
    
    /**
     * Starting point for the demonstration application.
     *
     * @param args  ignored.
     */
    public static void main(String[] args) {
        StackedBarChartDemo5 demo = new StackedBarChartDemo5("Stacked Bar Chart Demo 5");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);
    }

}

veronica
Posts: 4
Joined: Sat Aug 05, 2006 2:09 pm

Post by veronica » Tue Sep 05, 2006 8:32 am

Any ideas will be appreciated!!!

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 » Tue Sep 05, 2006 4:08 pm

The CategoryAxis is not zoomable by design. I guess it would be possible to write a custom axis that displays only a subset of the categories, but it would be hard for the chart reader to know which categories have been omitted from the chart.
David Gilbert
JFreeChart Project Leader

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

Flow2k
Posts: 6
Joined: Tue Dec 19, 2006 1:29 pm

Remove Categories

Post by Flow2k » Thu Dec 28, 2006 3:31 pm

Hi,

maybe you could just remove the Categories you don't want to display.
And to "unzoom" just add them again

I used this method in one of my apps - and (imho) it's quite satisfying


KR
Florian

gwk
Posts: 2
Joined: Fri Feb 09, 2007 1:19 am
Location: Pittsburgh, PA

Post by gwk » Fri Feb 09, 2007 1:31 am

So it appears that I will have to write my own class, extending the axis in order to make my BarChart Zoomable along the x-axis?

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 » Sat Feb 10, 2007 7:25 am

gwk wrote:So it appears that I will have to write my own class, extending the axis in order to make my BarChart Zoomable along the x-axis?
Or create a bar chart with a value-based x-axis (that is, using XYBarRenderer on an XYPlot).
David Gilbert
JFreeChart Project Leader

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

Locked