Creating bars in a XY Bar Chart

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
DerPe
Posts: 12
Joined: Mon Aug 14, 2017 10:00 am
antibot: No, of course not.

Creating bars in a XY Bar Chart

Post by DerPe » Tue Aug 15, 2017 4:16 pm

Hi guys,

I have the following modified example for creating a XY Bar chart:

Code: Select all

package testing;


import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.block.BlockBorder;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import generateProfiles.Reader_ExternalSignals;

public class LoadProfileChart extends JFrame {

	
	ChartPanel chartPanel;
	
    public LoadProfileChart(double [] timeSeries) {

        createChart(timeSeries);
    }
    
    
    public ChartPanel getChartPanel () {
    	return this.chartPanel;
    }

    private void createChart(double [] timeSeries) {

        XYDataset dataset = createDataset( timeSeries);
        JFreeChart chart = createChart(dataset);
        chartPanel = new ChartPanel(chart);
        chartPanel.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
        chartPanel.setBackground(Color.white);
        

         
    }

    private XYDataset createDataset(double [] timeSeries) {

        XYSeries series = new XYSeries("");
     
        
        
        for (int i=0; i<timeSeries.length; i++) {
        	series.add(i,timeSeries[i]);
        }
        
     
     

        XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(series);


        return dataset;
    }


	private JFreeChart createChart(XYDataset dataset) {

        JFreeChart chart = ChartFactory.createXYLineChart(
                "Load Profile", 
                "Minute of the day", 
                "Power in J/minute", 
                dataset, 
                PlotOrientation.VERTICAL,
                true, 
                true, 
                false 
        );

        XYPlot plot = chart.getXYPlot();

        XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
        renderer.setSeriesPaint(0, Color.BLUE);
        renderer.setSeriesStroke(0, new BasicStroke(2.0f));
        renderer.setBaseShapesFilled(false);
        renderer.setDrawOutlines(false);


        plot.setRenderer(renderer);
        plot.setBackgroundPaint(Color.white);


        

        
        plot.setRangeGridlinesVisible(true);
        plot.setRangeGridlinePaint(Color.BLACK);

        plot.setDomainGridlinesVisible(true);
        plot.setDomainGridlinePaint(Color.BLACK);

        chart.getLegend().setFrame(BlockBorder.NONE);
        chart.getLegend().setVisible(false);

        chart.setTitle(new TextTitle("Electrical Load Profile",
                        new Font("Serif", java.awt.Font.BOLD, 14)
                )
        );

        return chart;

    }

 
}

Unfortunaley there are no real bars in the diagramm but a line. I want the bars to be filled like in this example (http://www.jfree.org/jfreechart/images/ ... tDemo1.png).

Would anyone mind telling me how I can do that? Thanks in advance

EDIT: I tried to modify the example by inserting:
renderer.setBaseShapesFilled(true);
renderer.setDrawOutlines(true);

But still the plot does not look like a real bar plot.

John Matthews
Posts: 513
Joined: Wed Sep 12, 2007 3:18 pm

Re: Creating bars in a XY Bar Chart

Post by John Matthews » Wed Aug 16, 2017 11:47 am

ChartFactory.createXYBarChart() might be a better starting point.

DerPe
Posts: 12
Joined: Mon Aug 14, 2017 10:00 am
antibot: No, of course not.

Re: Creating bars in a XY Bar Chart

Post by DerPe » Wed Aug 16, 2017 4:40 pm

Thanks John Matthews for your answer.

I changed the plot to a XYBarChart but the chart looks exacty the same. It is still a Line Chart and not a BarChart.

I have the following code:

Code: Select all

package testing;


import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.JFrame;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.block.BlockBorder;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.xy.IntervalXYDataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class LoadProfileChart extends JFrame {

	
	ChartPanel chartPanel;
	
    public LoadProfileChart(double [] timeSeries, Color color) {

        createChart(timeSeries, color);
    }
    
    
    public ChartPanel getChartPanel () {
    	return this.chartPanel;
    }

    private void createChart(double [] timeSeries, Color color) {

    	IntervalXYDataset dataset = createDataset( timeSeries);
        JFreeChart chart = createChart(dataset, color);
        chartPanel = new ChartPanel(chart);
        chartPanel.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
        chartPanel.setBackground(Color.white);
        

         
    }

    private IntervalXYDataset createDataset(double [] timeSeries) {

        XYSeries series = new XYSeries("");
     
        
        
        for (int i=0; i<timeSeries.length; i++) {
        	series.add(i,timeSeries[i]);
        }
        
     
     

        XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(series);


        return dataset;
    }


	private JFreeChart createChart(IntervalXYDataset dataset, Color color) {

        JFreeChart chart = ChartFactory.createXYBarChart(
                "Load Profilea",
                "Minute of the day", false, 
                "Power in J/minute", 
                dataset
                
        );

        XYPlot plot = chart.getXYPlot();

        XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
        renderer.setSeriesPaint(0, color);
        renderer.setSeriesStroke(0, new BasicStroke(2.0f));
        renderer.setBaseShapesFilled(false);
        renderer.setDrawOutlines(false);
       


        plot.setRenderer(renderer);
        plot.setBackgroundPaint(Color.white);


        
        plot.getDomainAxis().setUpperMargin(0);
        
        plot.setRangeGridlinesVisible(true);
        plot.setRangeGridlinePaint(Color.BLACK);

        plot.setDomainGridlinesVisible(true);
        plot.setDomainGridlinePaint(Color.BLACK);

        chart.getLegend().setFrame(BlockBorder.NONE);
        chart.getLegend().setVisible(false);

        chart.setTitle(new TextTitle("Electrical Load Profile",
                        new Font("Serif", java.awt.Font.BOLD, 16)
                )
        );

        return chart;

    }

 
}

John Matthews
Posts: 513
Joined: Wed Sep 12, 2007 3:18 pm

Re: Creating bars in a XY Bar Chart

Post by John Matthews » Wed Aug 16, 2017 6:25 pm

It looks like you're replacing the chart factory's XYBarRenderer with an instance of XYLineAndShapeRenderer. What happens if you don't?

DerPe
Posts: 12
Joined: Mon Aug 14, 2017 10:00 am
antibot: No, of course not.

Re: Creating bars in a XY Bar Chart

Post by DerPe » Fri Aug 18, 2017 8:47 am

hanks John Matthews for your answer,

No it in fact looks like a bar diagramm. But there is one problem. The bars are separated with thin white lines. Meaning that if let's say we have (x=1, y=1) and (x=2, y=2) then there is a small white gap between x=1 and x=2. How can I eliminate this gap such that the bars are 'connected' (as in the standard bar diagramm in Excel)?

John Matthews
Posts: 513
Joined: Wed Sep 12, 2007 3:18 pm

Re: Creating bars in a XY Bar Chart

Post by John Matthews » Fri Aug 18, 2017 10:54 am

At a guess, you're seeing the highlight from the renderer's default GradientBarPainter like this. You can switch to the StandardBarPainter like this.

DerPe
Posts: 12
Joined: Mon Aug 14, 2017 10:00 am
antibot: No, of course not.

Re: Creating bars in a XY Bar Chart

Post by DerPe » Mon Aug 21, 2017 8:11 am

At a guess, you're seeing the highlight from the renderer's default GradientBarPainter like this. You can switch to the StandardBarPainter like this.
Thanks John Matthews for your answer,

I tried to insert the code from the linked page (I had to change the name of the variable plot into plot1, because I already use the variable plot)

Code: Select all

 XYPlot plot = chart.getXYPlot();
        final CategoryPlot plot1 = chart.getCategoryPlot();
        ((BarRenderer) plot1.getRenderer()).setBarPainter(new StandardBarPainter());

      XYBarRenderer renderer = new XYBarRenderer();
        renderer.setSeriesPaint(0, color);
        renderer.setSeriesStroke(0, new BasicStroke(2.0f));
But I received the following error message:"Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: org.jfree.chart.plot.XYPlot cannot be cast to org.jfree.chart.plot.CategoryPlot"

John Matthews
Posts: 513
Joined: Wed Sep 12, 2007 3:18 pm

Re: Creating bars in a XY Bar Chart

Post by John Matthews » Mon Aug 21, 2017 10:22 am

Again, don't replace the XYBarRenderer; just give the existing renderer a different XYBarPainter.

Code: Select all

XYPlot plot = chart.getXYPlot();
XYBarRenderer renderer = (XYBarRenderer) plot.getRenderer();
renderer.setBarPainter(new StandardXYBarPainter());

DerPe
Posts: 12
Joined: Mon Aug 14, 2017 10:00 am
antibot: No, of course not.

Re: Creating bars in a XY Bar Chart

Post by DerPe » Fri Aug 25, 2017 9:07 am

Thanks John Matthews for your answers, now everything looks good 8) . I really appreciate your help. Good work:-)

Locked