1

I have created a stacked bar chart in which I show a count on the y axis and dates on the x axis. The problem is that when I have many dates on the x axis it gets very cluttered and impossible to read. I would like to show only some of the dates, e.g one date per week. Is that possible? I am using ChartFactory.createStackedBarChart() to create the chart, and I have the data in a DefaultCategoryDataSet.

Any input is appreciated!

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Liz
  • 625
  • 4
  • 9
  • 14

2 Answers2

3

For a CategoryAxis, which is used the for the domain axis in a StackedBarChart, you have considerable flexility with the method setCategoryLabelPositions(). Typical usage is illustrated in the BarChartDemo1 source, shown here.

CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setCategoryLabelPositions(
    CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0));
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • 1
    Last seen [here](http://sourceforge.net/p/jfreechart/code/3111/tree/branches/jfreechart-1.0.x-branch/source/org/jfree/chart/demo/BarChartDemo1.java). – Catalina Island Jul 15 '15 at 11:11
0

Have you tried overriding the generateLabel methods in the label generator? Something like:

chart.getCategoryPlot().getRenderer().setBaseItemLabelGenerator(
  new CategoryItemLabelGenerator() {

    public String generateColumnLabel(CategoryDataset dataset, Integer column) {
      if(column % 7 == 0)
        super.generateColumnLabel(dataset, column)
      else 
        ""
    }
  }
);

I haven't tested the code, but it should only output a label every 7 columns. More info on the label generator is here: http://www.jfree.org/jfreechart/api/javadoc/org/jfree/chart/labels/CategoryItemLabelGenerator.html

jcern
  • 7,798
  • 4
  • 39
  • 47