简体   繁体   中英

Skip Category axis labels in a JFree Bar Chart

I have a bar chart with one 'series' (in jfree paralance) called 'Brutto' and many categories; one for each month since 2008. I've rotated the month labels UP-90 but they are all squished together and hard to read so I'd like to skip some of them (for example every other one or every third one but it doesn't really matter as long as it's neat and readable and preferably dynamic for future additions).

I am using JfreeChart 1.0.16 and I thought I had discovered the answer when I came across Ray Lukas' patch on SourceForge and the related post on the jfree forum: http://www.jfree.org/phpBB2/viewtopic.php?f=3&t=15345&start=15

Unfortunately this patch/class doesn't appear to work with 1.0.16; I've tried the demo code as well as adapting the demo for my use case and while it doesn't throw any errors it also appears to do absolutely nothing to my chart. From SourceForge it looks like the patch was last updated in 2012.

Is there some way to skip Category labels in 1.0.16 or 1.0.17? If not, is there a work around or a way to update Ray's patch to work with 1.0.16?

I've paid for the JFree dev guide and demo source but haven't seen anything useful there - let me know if I missed something in there.

Update

For code to play with you can use Ray's demo code with a dataset more similar to mine:

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);    
}

}

Here i am Giving You One Code try it once and may be that will work and will give you what you want.

Example:

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

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

try it that will remove Category axis labels.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM