跳过JFree条形图中的类别轴标签

时间:2013-12-20 12:15:47

标签: java label jfreechart categories bar-chart

我有一个条形图,其中一个'系列'(jfree paralance)称为'Brutto'和许多类别;自2008年以来每个月都有一个。我已经将UP-90的月份标签旋转了但是它们都被挤压在一起并且难以阅读所以我想跳过它们中的一些(例如每隔一个或每三个一个但它没有只要它整洁可读,并且最好是未来添加的动态,这真的很重要。)

我正在使用JfreeChart 1.0.16,当我在SourceForge上看到Ray Lukas的补丁以及jfree论坛上的相关帖子时,我认为我已经找到了答案:http://www.jfree.org/phpBB2/viewtopic.php?f=3&t=15345&start=15

不幸的是,这个补丁/类似乎不适用于1.0.16;我已经尝试了演示代码以及为我的用例调整演示,虽然它没有抛出任何错误,但它似乎对我的图表完全没有任何作用。从SourceForge看起来该补丁最后一次更新是在2012年。

有没有办法在1.0.16或1.0.17中跳过类别标签?如果没有,是否有解决方法或更新Ray的补丁以使用1.0.16?

我已经支付了JFree开发指南和演示源代码,但在那里没有看到任何有用的东西 - 请告诉我,如果我错过了那里的东西。

更新

对于要使用的代码,您可以使用Ray的演示代码,其数据集与我的数据集更相似:

public class CategoryLabelDemo extends ApplicationFrame {

/**
 * Creates a new demo instance.
 *
 * @param title  the frame title.
 */
public CategoryLabelDemo(String title) {
    super(title);
    DefaultCategoryDataset dataset = new DefaultCategoryDataset();
    dataset.addValue(-0.0023, value, Jun 2008);
dataset.addValue(-0.0039089907, value, Jul 2008);
dataset.addValue(-0.0035218354, value, Aug 2008);
dataset.addValue(-0.0092901141, value, Sep 2008);
dataset.addValue(-0.0245642646, value, Okt 2008);
dataset.addValue(-0.0032392894, value, Nov 2008);
dataset.addValue(0.0024111542, value, Dez 2008);
dataset.addValue(0.0062748379, value, Jan 2009);
dataset.addValue(0.0107046352, value, Feb 2009);
dataset.addValue(-0.0101799486, value, Mrz 2009);
dataset.addValue(6.233119E-4, value, Apr 2009);
dataset.addValue(0.0117317276, value, Mai 2009);
dataset.addValue(0.0069779374, value, Jun 2009);
dataset.addValue(0.0132477326, value, Jul 2009);
dataset.addValue(0.0062355426, value, Aug 2009);
dataset.addValue(0.0181909045, value, Sep 2009);
dataset.addValue(0.0049082164, value, Okt 2009);
dataset.addValue(0.0186578099, value, Nov 2009);
dataset.addValue(0.0059455313, value, Dez 2009);
dataset.addValue(8.5796E-4, value, Jan 2010);
dataset.addValue(0.0048576055, value, Feb 2010);
dataset.addValue(0.0230331754, value, Mrz 2010);
dataset.addValue(0.0128787177, value, Apr 2010);
dataset.addValue(-0.0101536773, value, Mai 2010);
dataset.addValue(0.0017558451, value, Jun 2010);
dataset.addValue(-0.0010147601, value, Jul 2010);
dataset.addValue(0.0093268076, value, Aug 2010);
dataset.addValue(0.0082342177, value, Sep 2010);
dataset.addValue(0.0078039927, value, Okt 2010);
dataset.addValue(7.203314E-4, value, Nov 2010);
dataset.addValue(0.0071081519, value, Dez 2010);
dataset.addValue(-0.0108996694, value, Jan 2011);
dataset.addValue(0.0059615211, value, Feb 2011);
dataset.addValue(-0.0045793302, value, Mrz 2011);
dataset.addValue(0.0128991521, value, Apr 2011);
dataset.addValue(0.0040074806, value, Mai 2011);
dataset.addValue(-0.0117083555, value, Jun 2011);
dataset.addValue(0.0096033028, value, Jul 2011);
dataset.addValue(-0.0115565828, value, Aug 2011);
dataset.addValue(-0.0178972929, value, Sep 2011);
dataset.addValue(0.0091575092, value, Okt 2011);
dataset.addValue(-0.0153357532, value, Nov 2011);
dataset.addValue(0.0110588886, value, Dez 2011);
dataset.addValue(0.0148573512, value, Jan 2012);
dataset.addValue(0.0104185378, value, Feb 2012);
dataset.addValue(-0.0038222222, value, Mrz 2012);
dataset.addValue(0.0026768984, value, Apr 2012);
dataset.addValue(-0.006763371, value, Mai 2012);
dataset.addValue(8.063794E-4, value, Jun 2012);
dataset.addValue(0.0189794091, value, Jul 2012);
dataset.addValue(7.028642E-4, value, Aug 2012);
dataset.addValue(0.0049165935, value, Sep 2012);
dataset.addValue(-0.0038441377, value, Okt 2012);
dataset.addValue(0.0055253464, value, Nov 2012);
dataset.addValue(-0.0018316616, value, Dez 2012);
dataset.addValue(0.0015728766, value, Jan 2013);
dataset.addValue(0.0035770372, value, Feb 2013);
dataset.addValue(0.0103451274, value, Mrz 2013);
dataset.addValue(-7.743934E-4, value, Apr 2013);
dataset.addValue(0.0051666236, value, Mai 2013);
dataset.addValue(-0.0231303007, value, Jun 2013);
dataset.addValue(0.0109620275, value, Jul 2013);
dataset.addValue(-0.0020818876, value, Aug 2013);
dataset.addValue(0.0067802503, value, Sep 2013);
dataset.addValue(0.0087204283, value, Okt 2013);
dataset.addValue(0.0053924506, value, Nov 2013);
dataset.addValue(-0.0103865146, value, Dez 2013);
    JFreeChart chart = createChart(dataset);
    ChartPanel chartPanel = new ChartPanel(chart);
    chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));

    setContentPane(chartPanel);

}

/**
 * Creates a chart.
 * 
 * @param dataset  the dataset.
 * 
 * @return A chart.
 */
private static JFreeChart createChart(CategoryDataset dataset) {

    JFreeChart chart = null;
    try {
        BarRenderer barRenderer = new BarRenderer();
        CategoryAxis customCategoryAxis = new CategoryAxisSkipLabels();  // new CategoryAxis("Category");
        ((CategoryAxisSkipLabels)customCategoryAxis).setTickMarksVisible(true);
        ((CategoryAxisSkipLabels)customCategoryAxis).setDisplaySkippedTickMarks(true);
        // ****************** set Layout Type and AlgorithmType
        ((CategoryAxisSkipLabels)customCategoryAxis).setAlgorithmType(CategoryAxisSkipLabels.N_STEP_ALGO);
        // this must be the last call to category axis as it may return CategoryAxis instead of CategoryAxisSkipLabels          
        //customCategoryAxis = ((CategoryAxisSkipLabels)customCategoryAxis).setTruncate(true);

            customCategoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90);
        NumberAxis numberAxis = new NumberAxis("Value");
        CategoryPlot categoryPlot = new CategoryPlot(
            dataset, customCategoryAxis, numberAxis, barRenderer
        );
        chart = new JFreeChart("Bar Chart", categoryPlot);
    } catch (Exception e) {
        System.out.print("crash");
    }
    // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...

    // set the background color for the chart...
    chart.setBackgroundPaint(Color.white);

    // get a reference to the plot for further customisation...
    CategoryPlot plot = (CategoryPlot) chart.getPlot();
    plot.setBackgroundPaint(Color.lightGray);
    plot.setRangeGridlinePaint(Color.white);
    plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
    // change the auto tick unit selection to integer units only...
    NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    LegendTitle legend = (LegendTitle) chart.getLegend();
    legend.setPosition(RectangleEdge.BOTTOM);
    legend.setHorizontalAlignment(HorizontalAlignment.LEFT);
    legend.setBorder(new BlockBorder(Color.white));
    legend.setBackgroundPaint(Color.white);

    BarRenderer renderer = (BarRenderer) plot.getRenderer();
    renderer.setDrawBarOutline(false);
    renderer.setLegendItemToolTipGenerator(new StandardCategorySeriesLabelGenerator("Tooltip: {0}"));
    // OPTIONAL CUSTOMISATION COMPLETED.

    return chart;

}


/**
 * Starting point for the demonstration application.
 *
 * @param args  ignored.
 */
public static void main(String[] args) {

    CategoryLabelDemo demo = new CategoryLabelDemo("Bar Chart Demo 2");
    demo.pack();
    RefineryUtilities.centerFrameOnScreen(demo);
    demo.setVisible(true);

}
public static void run(String title) throws Exception {
    CategoryLabelDemo demo = new CategoryLabelDemo(title);
    demo.pack();
    RefineryUtilities.centerFrameOnScreen(demo);
    demo.setVisible(true);    
}

}

1 个答案:

答案 0 :(得分:0)

在这里,我给你一个代码尝试一次,可能会工作,并会给你你想要的。

示例:

final JFreeChart chart = ChartFactory.createStackedBarChart("", "", "",
                dataset, PlotOrientation.VERTICAL, true, true, false);

 CategoryPlot plot = chart.getCategoryPlot();
 plot.getDomainAxis(1).setVisible(false);

尝试删除类别轴标签。