JfreeChart: Stacked Bar Chart and CategoryAxis sho

2019-01-09 19:18发布

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!

2条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-01-09 19:24

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

查看更多
Fickle 薄情
3楼-- · 2019-01-09 19:39

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));
查看更多
登录 后发表回答