【问题标题】:Margin plot and chart in JFreeChartJFreeChart 中的边距图和图表
【发布时间】:2013-05-05 15:51:30
【问题描述】:

当最大值为固定值时,是否可以使位于条形图中条形之外的所有条形标题可见?

外观示例以及我希望它的外观示例。它不应该是任何超过 750 的值(检查 USA,最后一行):

如果有可能在绘图和图形之间留出空白以使所有标签可见(或任何其他解决方案),有什么提示或想法吗?

【问题讨论】:

    标签: jfreechart


    【解决方案1】:

    更新:空白边距(范围轴上没有值)

    好的,所以我会保留setUpperBound(),但会乱用NumberAxis。

    这是完整的代码:

    import java.awt.Graphics2D;
    import java.awt.geom.Rectangle2D;
    
    import org.jfree.chart.*;
    import org.jfree.chart.axis.*;
    import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
    import org.jfree.chart.plot.*;
    import org.jfree.chart.renderer.category.CategoryItemRenderer;
    import org.jfree.data.category.*;
    import org.jfree.ui.*;
    
    @SuppressWarnings("serial")
    public class BarWithMargin extends ApplicationFrame {
        public BarWithMargin(final String title) {
            super(title);
            final JFreeChart chart = constructChart();
            final ChartPanel panel = new ChartPanel(chart);
            setContentPane(panel);
        }
    
    
        JFreeChart constructChart() {
            JFreeChart chart = ChartFactory.createBarChart(
                "","","",getDataSet(),PlotOrientation.HORIZONTAL,false,false,false
            );
    
            CategoryPlot cp = chart.getCategoryPlot();
    
            CategoryItemRenderer cir = cp.getRenderer(); 
            cir.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
            cir.setBaseItemLabelsVisible(true);
    
            final double upperMargin = 80;
            final double maxOfDataset = 750;
            final double upperLimit = maxOfDataset+upperMargin;
    
            ValueAxis va = new NumberAxis(){
                protected void drawAxisLine(Graphics2D g2, double cursor, Rectangle2D dataArea, RectangleEdge edge) {
                    int margin = (int)(dataArea.getWidth() * upperMargin / upperLimit);
    
                    Rectangle2D newArea = new Rectangle2D.Double(
                        dataArea.getX(), dataArea.getY(), dataArea.getWidth()-margin, dataArea.getHeight());
                    super.drawAxisLine(g2, cursor, newArea, edge);
                }
    
                protected int calculateVisibleTickCount() {
                    return (int)Math.ceil(super.calculateVisibleTickCount() * maxOfDataset / upperLimit);
                }
            };
    
            va.setLabel(cp.getRangeAxis().getLabel());
            va.setUpperBound(upperLimit);
            cir.getPlot().setRangeAxis(va);
    
            return chart;
        }
    
        private CategoryDataset getDataSet() {
            DefaultCategoryDataset ds = new DefaultCategoryDataset();
            ds.addValue(59, "Country", "Norway");
            ds.addValue(69, "Country", "Switzerland");
            ds.addValue(85, "Country", "France");
            ds.addValue(93, "Country", "Syria");
            ds.addValue(96, "Country", "Germany");
            ds.addValue(111, "Country", "China");
            ds.addValue(116, "Country", "Australia");
            ds.addValue(121, "Country", "Egypt");
            ds.addValue(129, "Country", "England & Wales");
            ds.addValue(157, "Country", "New Zealand");
            ds.addValue(205, "Country", "Chile");
            ds.addValue(229, "Country", "Iran");
            ds.addValue(359, "Country", "Singapore");
            ds.addValue(404, "Country", "South Africa");
            ds.addValue(405, "Country", "Ukraine");
            ds.addValue(750, "Country", "USA");
            return ds;
        }    
    
    
        public static void main(String[] args) {
            BarWithMargin demo = new BarWithMargin("Bar with margin");
            demo.pack();
            RefineryUtilities.centerFrameOnScreen(demo);
            demo.setVisible(true);
        }
    }
    

    【讨论】:

    • +1 代表sscce;次要 nit—setItemLabelsVisible() 已弃用。
    • 好的,好的...我懒得实际点击已弃用的警告...将其替换为setBaseItemLabelsVisible
    • 谢谢。但我的目标是,如图所示,我不希望将 x 轴设置为上限并且同时具有边距。而且我不认为您的解决方案涵盖了这一点?通过将上限设置为 850 来解决它,然后制作一个覆盖 850 的矩形。可行但有点难看的解决方案。
    • 我已经更改了实现。这和你的代码相似吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-22
    相关资源
    最近更新 更多