0

I have created a JFreeChart with the code below, but the Y-Axis marks are truncated. How should I display the chart even though the data points are overlapped in the Y-Axis? Basically, I want the Y-Axis points to be generated from my file, a proper range is populated and displayed in the chart.

private static JFreeChart buildChart(TimeSeriesCollection dataset,
    String title, boolean endPoints) throws IOException {

// Create the chart

    JFreeChart chart0 = ChartFactory.createTimeSeriesChart(
        title, "Hour", "Count", dataset, true, true, false);

// Setup the appearance of the chart
    chart0.setBackgroundPaint(Color.white);
    XYPlot plot = (XYPlot) chart0.getXYPlot();
    plot.setBackgroundPaint(Color.lightGray);
    plot.setDomainGridlinePaint(Color.white);
    plot.setRangeGridlinePaint(Color.white);
    plot.setAxisOffset(new RectangleInsets(10.0, 10.0, 10.0, 10.0));
    plot.setDomainCrosshairVisible(true);
    plot.setRangeCrosshairVisible(true);

// Display data points or just the lines?

    if (endPoints) {
        XYItemRenderer renderer = plot.getRenderer();
        if (renderer instanceof StandardXYItemRenderer) {
            StandardXYItemRenderer rr = (StandardXYItemRenderer) renderer;

            rr.setBaseShapesVisible(true);
            rr.setBaseShapesFilled(true);
            rr.setDrawSeriesLineAsPath(true);
            rr.setSeriesPaint(0, Color.blue.brighter());
            rr.setSeriesVisible(0, true); // default
            rr.setSeriesVisibleInLegend(0, true);  // default

            NumberAxis domainAxis = new NumberAxis();
            domainAxis.setUpperMargin(0.15);
            domainAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
            domainAxis = (NumberAxis) plot.getDomainAxis();
            domainAxis = (NumberAxis) plot.getRangeAxis();
            domainAxis.setAutoRangeIncludesZero(false);
        }
    }

 // Tell the chart how we would like dates to read
    DateAxis axis = (DateAxis) plot.getDomainAxis();
    axis.setAutoRange(true);

 //axis.getDefaultAutoRange();
    axis.setDateFormatOverride(new SimpleDateFormat("HH:mm"));

    try {

        ChartUtilities.saveChartAsJPEG(new File("suc.jpg"), 1.0f, chart0, 990, 700);
    } catch (IOException e) {
        e.printStackTrace();
    }

    return chart0;
}

Below is the image that is created, clearly you can see that the Y-Axis there is an overlap showing.

enter image description here

ave4496
  • 2,950
  • 3
  • 21
  • 48
  • Please edit your question to include an [sscce](http://sscce.org/) that exhibits the problem you describe; use the formatting shown. – trashgod Dec 17 '12 at 18:58
  • @trashgod -- I have managed to sort this out, by reading the data points, finding the maximum number, then inserting maximum under xyplot setRange() method like so plot.getRangeAxis().setRange(1.0, SucMaxi ); Unfortunately i'm a nub using stackoverflow so i wasn't able to understand the last comment. – user1680417 Jan 07 '13 at 06:38

2 Answers2

1

I have managed to sort this out, by reading the data points, finding the maximum number, then inserting maximum under xyplot setRange() method.

You shouldn't have to do so. In the excerpt below, why are you getting the domain axis, replacing it with reference to the the range axis, and then altering the range axis? Did you mean to change the domain axis, instead? See this related example.

domainAxis = (NumberAxis) plot.getDomainAxis();
domainAxis = (NumberAxis) plot.getRangeAxis();
domainAxis.setAutoRangeIncludesZero(false);

Addendum: A minimal sscce showing auto-range of random data.

image

import java.awt.EventQueue;
import java.util.Random;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.Day;
import org.jfree.data.time.Hour;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;

/** @see https://stackoverflow.com/a/14198851/230513 */
public class Test {

    private static final int N = 10;
    private static final Random random = new Random();

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Test().display();
            }
        });
    }

    private void display() {
        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new ChartPanel(buildChart(createDataset(), "Title")));
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    private static TimeSeriesCollection createDataset() {

        final TimeSeries series = new TimeSeries("Data");
        Hour current = new Hour(0, new Day());
        for (int i = 0; i < N; i++) {
            series.add(current, random.nextGaussian());
            current = (Hour) current.next();
        }
        return new TimeSeriesCollection(series);
    }

    private static JFreeChart buildChart(
        TimeSeriesCollection dataset, String title) {
        JFreeChart chart = ChartFactory.createTimeSeriesChart(
            title, "Hour", "Count", dataset, true, true, false);
        XYPlot plot = chart.getXYPlot();
        plot.setDomainCrosshairVisible(true);
        plot.setRangeCrosshairVisible(true);
        return chart;
    }
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • I deleted that section, and read the data points from my file. For the range axis & domain axis, since i'm reading both axis from the file, i had to do this for the domain axis `DateAxis axis = (DateAxis) plot.getDomainAxis(); axis.setTickUnit(new DateTickUnit(DateTickUnitType.HOUR, 1)); axis.setDateFormatOverride(new SimpleDateFormat("HH:mm")); ` For the deleted section.. i left this for rendering `XYPlot plot = (XYPlot) chart0.getXYPlot(); plot.getDomainAxis().setAutoRange(true); plot.getRangeAxis().setRange(1.0, SucMaxi ); ` – user1680417 Jan 10 '13 at 12:56
  • Please edit your question to include an [sscce](http://sscce.org/) that shows your current approach. – trashgod Jan 10 '13 at 15:37
  • my current approach is posted below – user1680417 Jan 22 '13 at 13:12
0

This is my current approach on the chart how data is being rendered...

private static JFreeChart buildChart(TimeSeriesCollection dataset,
    String title, boolean endPoints) throws IOException {
// Create the chart
    JFreeChart chart0 = ChartFactory.createTimeSeriesChart(
        title,
        "Hour", "Count",
        dataset,
        true,
        true,
        false);

// Setup the appearance of the chart

    chart0.setBackgroundPaint(Color.white);
    XYPlot plot = (XYPlot) chart0.getXYPlot();
    plot.getDomainAxis().setAutoRange(true);
    plot.getRangeAxis().setRange(1.0, SucMaxi);
    plot.setBackgroundPaint(Color.lightGray);
    plot.setDomainGridlinePaint(Color.white);
    plot.setRangeGridlinePaint(Color.white);
    plot.getAxisOffset();
    plot.setAxisOffset(new RectangleInsets(10.0, 10.0, 10.0, 10.0));
    plot.setDomainCrosshairVisible(true);
    plot.setRangeCrosshairVisible(true);

// Display data points or just the lines?

    if (endPoints) {
        XYItemRenderer renderer = plot.getRenderer();
        if (renderer instanceof StandardXYItemRenderer) {
            StandardXYItemRenderer rr = (StandardXYItemRenderer) renderer;
            rr.setBaseShapesVisible(true);
            rr.setBaseShapesFilled(true);
            rr.setDrawSeriesLineAsPath(true);
            rr.setSeriesPaint(0, Color.blue.brighter());
            rr.setSeriesVisible(0, true); // default
            rr.setSeriesVisibleInLegend(0, true);  // default      
        }
    }

// Tell the chart how we would like dates to read

    DateAxis axis = (DateAxis) plot.getDomainAxis();

// Tick the X Axis by unit tick 1 hour
    axis.setTickUnit(new DateTickUnit(DateTickUnitType.HOUR, 1));
    axis.setDateFormatOverride(new SimpleDateFormat("HH:mm"));

    try {
        ChartUtilities.saveChartAsJPEG(
            new File("suc.jpg"), 1.0f, chart0, 1000, 700);
    } catch (IOException e) {
        e.printStackTrace();
    }

    return chart0;
}

enter image description here

trashgod
  • 203,806
  • 29
  • 246
  • 1,045