0

I realize that this question has been asked before, but other solutions have not worked out for me and I'm hoping others could find where the inconsistencies may lie.

I'm using JFreeChart in iText and building a bar chart using this method below:

DefaultCategoryDataset dataSet = new DefaultCategoryDataset();

String rowKey = "score";

for(int i = 0; i <= 100; i++)
{
    double frequency = Collections.frequency(scoreList,i);
    dataSet.setValue(frequency, rowKey, i+"");
}

JFreeChart chart = ChartFactory.createBarChart("", "Score Distribution (%)", "Count", dataSet, PlotOrientation.VERTICAL, false, true, false);

It's a pretty simple way to make a chart (although I don't know what the false, true, false means within the createBarChart() method). The problem is that there are 101 values on the x-axis and they don't all fit onto the screen/page.

I've tried drawing only every 2/3/n-th value, but that causes the graph to become something different entirely, so I only want the labels to appear less, not the actual values and graph bars themselves.

Every other solution presented on stackoverflow has suggested using XYPlot. But when I try to use XYPlot, I get: java.lang.ClassCastException: org.jfree.chart.plot.CategoryPlot cannot be cast to org.jfree.chart.plot.XYPlot. Although the language here appears straightforward, I don't know why this error appears. Is it because it's strictly a bar graph that's causing this error?

I've been able to do this:

CategoryPlot plotCat =  (CategoryPlot) chart.getPlot();
ValueAxis yAxis = plotCat.getRangeAxis();
CategoryAxis xAxis = (CategoryAxis) plotCat.getDomainAxis();

So it seems like the x- and y-axes require different types of axis names? I'm confused about this too.

In short: how do I skip printing every n-th label on my x-axis for a bar chart?

user2323030
  • 1,193
  • 4
  • 16
  • 37

2 Answers2

1

A Category Plot/Axis implies the data is split into separate unrelated groups e.g. states Texas, California, New York etc.. therefore the chart has no sensible way to hide some.

This is the same difference as between discrete and continuous Data.

0

I found a way to work around this (Hide some category labels in JFreeChart to avoid overlapping). You can change the color of the values you want hidden and make them disappear that way. It's probably not the best method, but it does work.

If someone else has a better answer, please chime in.

Community
  • 1
  • 1
user2323030
  • 1,193
  • 4
  • 16
  • 37