Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
162 views
in Technique[技术] by (71.8m points)

java - How to determine min and max value for ItemMargin property in StackedBarChart using jFreeChart

I am using jFreeChart api to make one application such that on movement of slider the gap width in between the two bars gets increased or decreased according to the value of the slider. But I found with different charts and different data item margin property get varied. To demonstrate the problem I am attaching the following code:-

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.LegendItemCollection;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.SubCategoryAxis;
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;

public class DomainTranslateDemo extends ApplicationFrame {

    private static class DemoPanel extends JPanel implements ChangeListener {

        private static int SLIDER_INITIAL_VALUE = 50;
        private JSlider slider;
        private DateAxis domainAxis;
        private int lastValue = SLIDER_INITIAL_VALUE;
        private GroupedStackedBarRenderer renderer;

        // one month (milliseconds, seconds, minutes, hours, days)
        private int delta = 1000 * 60 * 60 * 24 * 30;

        public DemoPanel() {
            super(new BorderLayout());
            final CategoryDataset dataset = createDataset();
            final JFreeChart chart = createChart(dataset);
            final ChartPanel chartPanel = new ChartPanel(chart);
            Dimension Dim = new Dimension(590, 350);
            chartPanel.setPreferredSize(Dim);
            add(chartPanel);

            JPanel dashboard = new JPanel(new BorderLayout());
            dashboard.setBorder(BorderFactory.createEmptyBorder(0, 4, 4, 4));   

            this.slider = new JSlider(0, 100, SLIDER_INITIAL_VALUE);
            this.slider.addChangeListener(this);
            dashboard.add(this.slider);
            add(dashboard, BorderLayout.SOUTH);
        }

        private CategoryDataset createDataset() {
            DefaultCategoryDataset result = new DefaultCategoryDataset();

            result.addValue(1.5, "5YEUR", "5Y");
            result.addValue(-1.5, "5YUSD", "5Y");

            result.addValue(0.5, "5Yx5YEUR", "5Yx5Y");
            result.addValue(-0.5, "5Yx5YUSD", "5Yx5Y");

            result.addValue(1.5, "10Yx5YEUR", "10Yx5Y");
            result.addValue(-1.5, "10Yx5YUSD", "10Yx5Y");

            result.addValue(1.5, "15Yx5YEUR", "15Yx5Y");
            result.addValue(-1.5, "15Yx5YUSD", "15Yx5Y");

            result.addValue(1.5, "20Yx5YEUR", "20Yx5Y");
            result.addValue(-1.5, "20Yx5YUSD", "20Yx5Y");

            result.addValue(1.5, "20Yx5YEUR", "25Yx5Y");
            result.addValue(-1.5, "20Yx5YUSD", "25Yx5Y");

            //result.addValue(11.9, "Product 3 (US)", "Jan 04");

            return result;
        }

        private JFreeChart createChart(final CategoryDataset dataset) {

            final JFreeChart chart = ChartFactory.createStackedBarChart(
                    "Max Bar Width", // chart title
                    "Category", // domain axis label
                    "Value", // range axis label
                    dataset, // data
                    PlotOrientation.VERTICAL, // the plot orientation
                    true, // legend
                    true, // tooltips
                    false // urls
                    );

            renderer = new GroupedStackedBarRenderer();
            KeyToGroupMap map = new KeyToGroupMap("G1");
            map.mapKeyToGroup("5YEUR", "G1");
            map.mapKeyToGroup("5YUSD", "G1");

            map.mapKeyToGroup("5Yx5YEUR", "G2");
            map.mapKeyToGroup("5Yx5YUSD", "G2");

            map.mapKeyToGroup("10Yx5YEUR", "G3");
            map.mapKeyToGroup("10Yx5YUSD", "G3");

            map.mapKeyToGroup("15Yx5YEUR", "G4");
            map.mapKeyToGroup("15Yx5YUSD", "G4");

            map.mapKeyToGroup("20Yx5YEUR", "G5");
            map.mapKeyToGroup("20Yx5YUSD", "G5");

            map.mapKeyToGroup("25Yx5YEUR", "G6");
            map.mapKeyToGroup("25Yx5YUSD", "G6");


            renderer.setSeriesToGroupMap(map);
            double maxBarWidth = renderer.getMaximumBarWidth();

            //renderer.setMaximumBarWidth(1);
            renderer.setItemMargin(-20);

           /* SubCategoryAxis domainAxis = new SubCategoryAxis("Product / Month");
            domainAxis.setCategoryMargin(0.05);
            domainAxis.addSubCategory("Product 1");
            domainAxis.addSubCategory("Product 2");
            domainAxis.addSubCategory("Product 3");*/

            CategoryPlot plot = (CategoryPlot) chart.getPlot();
            //plot.setDomainAxis(domainAxis);
            plot.setRenderer(renderer);
            plot.setFixedLegendItems(createLegendItems());

            return chart;

        }

        /**
         * @return The legend items.
         */
        private LegendItemCollection createLegendItems() {
            LegendItemCollection result = new LegendItemCollection();
            return result;
        }

        @Override
        public void stateChanged(ChangeEvent event) {
            int value = this.slider.getValue();
           /* long minimum = domainAxis.getMinimumDate().getTime();
            long maximum = domainAxis.getMaximumDate().getTime();
            if (value<lastValue) { // left
                minimum = minimum - delta;
                maximum = maximum - delta;
            } else { // right
                minimum = minimum + delta;
                maximum = maximum + delta;
            }
            DateRange range = new DateRange(minimum,maximum);
            domainAxis.setRange(range);*/
            System.out.println("value:"+value);
            renderer.setItemMargin(value/100.0);

        }

    }

    public DomainTranslateDemo(String title) {
        super(title);
        setContentPane(new DemoPanel());
    }

    public static JPanel createDemoPanel() {
        return new DemoPanel();
    }

    public static void main(String[] args) {
        DomainTranslateDemo demo = new DomainTranslateDemo("Translate Demo");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);
    }

} 

From the above code It is evident that the two bars are not touching each other on slider value = 0. Similarly before 100% my bar is getting disappeared. I need one application in which

on min value of slider = all the bars need to touch each other

on max value of slider = It will show max distance in between the bars.

This will all depend on the min and max value of item margin property. I searched several docs related with this didn't find any convincing calculation on this.

Any kind of suggestion in this regard would be really helpful.

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think you just need to use more of the negative range, e.g. -50, 10.

image

SSCCE:

import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.LegendItemCollection;
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;

/** @see http://stackoverflow.com/a/17342522/230513 */
public class DomainTranslateDemo extends ApplicationFrame {

    private static class DemoPanel extends JPanel implements ChangeListener {

        private static int SLIDER_INITIAL_VALUE = -42;
        private static final double INCREMENT = 1 / 10.0;
        private JSlider slider;
        private GroupedStackedBarRenderer renderer;

        public DemoPanel() {
            super(new BorderLayout());
            final CategoryDataset dataset = createDataset();
            final JFreeChart chart = createChart(dataset);
            final ChartPanel chartPanel = new ChartPanel(chart);
            Dimension Dim = new Dimension(590, 350);
            chartPanel.setPreferredSize(Dim);
            add(chartPanel);

            JPanel dashboard = new JPanel(new BorderLayout());
            dashboard.setBorder(BorderFactory.createEmptyBorder(0, 4, 4, 4));

            this.slider = new JSlider(-50, 10, SLIDER_INITIAL_VALUE);
            this.slider.addChangeListener(this);
            dashboard.add(this.slider);
            add(dashboard, BorderLayout.SOUTH);
        }

        private CategoryDataset createDataset() {
            DefaultCategoryDataset result = new DefaultCategoryDataset();
            result.addValue(1.5, "5YEUR", "5Y");
            result.addValue(-1.5, "5YUSD", "5Y");
            result.addValue(0.5, "5Yx5YEUR", "5Yx5Y");
            result.addValue(-0.5, "5Yx5YUSD", "5Yx5Y");
            result.addValue(1.5, "10Yx5YEUR", "10Yx5Y");
            result.addValue(-1.5, "10Yx5YUSD", "10Yx5Y");
            result.addValue(1.5, "15Yx5YEUR", "15Yx5Y");
            result.addValue(-1.5, "15Yx5YUSD", "15Yx5Y");
            result.addValue(1.5, "20Yx5YEUR", "20Yx5Y");
            result.addValue(-1.5, "20Yx5YUSD", "20Yx5Y");
            result.addValue(1.5, "20Yx5YEUR", "25Yx5Y");
            result.addValue(-1.5, "20Yx5YUSD", "25Yx5Y");
            return result;
        }

        private JFreeChart createChart(final CategoryDataset dataset) {

            final JFreeChart chart = ChartFactory.createStackedBarChart(
                "Max Bar Width", // chart title
                "Category", // domain axis label
                "Value", // range axis label
                dataset, // data
                PlotOrientation.VERTICAL, // the plot orientation
                true, // legend
                true, // tooltips
                false // urls
                );

            renderer = new GroupedStackedBarRenderer();
            KeyToGroupMap map = new KeyToGroupMap("G1");
            map.mapKeyToGroup("5YEUR", "G1");
            map.mapKeyToGroup("5YUSD", "G1");
            map.mapKeyToGroup("5Yx5YEUR", "G2");
            map.mapKeyToGroup("5Yx5YUSD", "G2");
            map.mapKeyToGroup("10Yx5YEUR", "G3");
            map.mapKeyToGroup("10Yx5YUSD", "G3");
            map.mapKeyToGroup("15Yx5YEUR", "G4");
            map.mapKeyToGroup("15Yx5YUSD", "G4");
            map.mapKeyToGroup("20Yx5YEUR", "G5");
            map.mapKeyToGroup("20Yx5YUSD", "G5");
            map.mapKeyToGroup("25Yx5YEUR", "G6");
            map.mapKeyToGroup("25Yx5YUSD", "G6");
            renderer.setSeriesToGroupMap(map);
            double maxBarWidth = renderer.getMaximumBarWidth();
            renderer.setItemMargin(SLIDER_INITIAL_VALUE * INCREMENT);

            CategoryPlot plot = (CategoryPlot) chart.getPlot();
            plot.setRenderer(renderer);
            plot.setFixedLegendItems(createLegendItems());
            return chart;
        }

        private LegendItemCollection createLegendItems() {
            LegendItemCollection result = new LegendItemCollection();
            return result;
        }

        @Override
        public void stateChanged(ChangeEvent event) {
            int value = slider.getValue();
            System.out.println("value:" + value);
            renderer.setItemMargin(value * INCREMENT);
        }
    }

    public DomainTranslateDemo(String title) {
        super(title);
        setContentPane(new DemoPanel());
    }

    public static JPanel createDemoPanel() {
        return new DemoPanel();
    }

    public static void main(String[] args) {
        DomainTranslateDemo demo = new DomainTranslateDemo("Translate Demo");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...