JfreeChart: Stacked Bar Chart and CategoryAxis showing dates

家住魔仙堡 提交于 2019-11-26 14:55:08

问题


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!


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/12178960/jfreechart-stacked-bar-chart-and-categoryaxis-showing-dates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!