problem with xyplot for constant y values

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Post Reply
tsitko
Posts: 1
Joined: Thu Aug 23, 2018 3:35 pm
antibot: No, of course not.

problem with xyplot for constant y values

Post by tsitko » Thu Aug 23, 2018 3:56 pm

Greetings - When I use an XYPlot that has the same "y" values for each x, the plot is drawn with extremely large bars which almost make it look like a solid bar across all the x values. If I add a datapoint (0,0), the bars will be displayed at a normal size.

I've attached two images - one with the dummy value added, and one without.

I've also attached the code that produced both of these. I *think* it has something to do with the renderer, but I just can't seem to find what's causing this.

Image
Image

Code: Select all

import javax.swing.*;

import java.awt.*;

import org.jfree.chart.*;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StackedXYBarRenderer;
import org.jfree.chart.renderer.xy.StandardXYBarPainter;
import org.jfree.data.xy.CategoryTableXYDataset;


public class TestForm extends JPanel {

    public TestForm() {
        createUIComponents();
    }

    public static void StartGUI() {
        JPanel mainPanel = new TestForm();

        JFrame frame = new JFrame("Test Code");
        frame.setContentPane(mainPanel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }



    private JPanel buildGraph(boolean dummy) {
        CategoryTableXYDataset dataset = new CategoryTableXYDataset();

        dataset.add(2.0,50.0, "RAIN");
        dataset.add(25.0,50.0, "RAIN");
        dataset.add(50.0,50.0, "RAIN");

        if (dummy){
            dataset.add(0.0, 0.0, "RAIN");
        }

        StackedXYBarRenderer renderer = new StackedXYBarRenderer();
        renderer.setShadowVisible(false);
        renderer.setBarPainter(new StandardXYBarPainter());


        XYPlot plot = new XYPlot(dataset, new NumberAxis("x"), new NumberAxis("y"), renderer);




        JFreeChart chart = new JFreeChart(plot);
        ChartPanel cp = new ChartPanel(chart);

        return cp;
    }

    private void createUIComponents() {
        JTabbedPane tabbedPane1 = new JTabbedPane();
        JPanel GraphTab1 = new JPanel();
        JPanel GraphTab2 = new JPanel();

        tabbedPane1.addTab("Graph - original dataset", GraphTab1);
        GraphTab1.add(buildGraph(false));

        tabbedPane1.addTab("Graph - add (0,0)", GraphTab2);
        GraphTab2.add(buildGraph(true));

        this.setLayout(new BorderLayout());
        this.add(tabbedPane1, BorderLayout.CENTER);
    }
}

Post Reply