Skip to content
Advertisement

How to change JFreeChart vertical bar to a solid color?

I have the following sample code for a JFreeChart, how to change the vertical bars to a solid color [ right now it has some white color in each bar ] ?

enter image description here

import java.awt.event.*;
import java.text.DecimalFormat;
import javax.swing.*;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.date.*;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYBarRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.IntervalXYDataset;
import org.jfree.data.xy.XYDataset;
 
public class Price_Volume_Chart_Demo extends JPanel                                       // A demonstration application showing how to create a price-volume chart.   
{
  static boolean EXIT_ON_CLOSE_B=true;

  public Price_Volume_Chart_Demo(String title)
  {
    JFreeChart chart=createChart();
    ChartPanel panel=new ChartPanel(chart,true,true,true,false,true);
    panel.setPreferredSize(new java.awt.Dimension(1000,500));
    add(panel);
  }

  private JFreeChart createChart()
  {
    XYDataset priceData=createPriceDataset();
    String title="Eurodollar Futures Contract (MAR03)";
    JFreeChart chart=ChartFactory.createTimeSeriesChart(title,
                                                        "Date",
                                                        getYLabel("Price ( $ )"),
                                                        priceData,
                                                        true,
                                                        true,
                                                        true
                                                       );
    XYPlot plot=chart.getXYPlot();
    NumberAxis rangeAxis1=(NumberAxis)plot.getRangeAxis();
    rangeAxis1.setLowerMargin(0.40);  // to leave room for volume bars   
    DecimalFormat format=new DecimalFormat("00.00");
    rangeAxis1.setNumberFormatOverride(format);

    XYItemRenderer renderer1=plot.getRenderer();
    /*
            renderer1.setToolTipGenerator(   
                new StandardXYToolTipGenerator(   
                    StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT,   
                    new SimpleDateFormat("d-MMM-yyyy"), new DecimalFormat("0.00")   
                )
            );   
     */
    NumberAxis rangeAxis2=new NumberAxis("Volume");
    rangeAxis2.setUpperMargin(1.00);  // to leave room for price line   
    plot.setRangeAxis(1,rangeAxis2);
    plot.setDataset(1,createVolumeDataset());
    plot.setRangeAxis(1,rangeAxis2);
    plot.mapDatasetToRangeAxis(1,1);
    XYBarRenderer renderer2=new XYBarRenderer(0.20);
    renderer2.setShadowVisible(false); 
    /*
            renderer2.setToolTipGenerator(   
                new StandardXYToolTipGenerator(   
                    StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT,   
                    new SimpleDateFormat("d-MMM-yyyy"), new DecimalFormat("0,000.00")   
                )   
            );   
     */
    plot.setRenderer(1,renderer2);
    return chart;
  }

  private static XYDataset createPriceDataset()
  {
    // create dataset 1...
    TimeSeries series1=new TimeSeries("Price");

    series1.add(new Day(2,MonthConstants.JANUARY,2002),95.565);
    series1.add(new Day(3,MonthConstants.JANUARY,2002),95.640);
    series1.add(new Day(4,MonthConstants.JANUARY,2002),95.710);

    series1.add(new Day(7,MonthConstants.JANUARY,2002),95.930);
    series1.add(new Day(8,MonthConstants.JANUARY,2002),95.930);
    series1.add(new Day(9,MonthConstants.JANUARY,2002),95.960);
    series1.add(new Day(10,MonthConstants.JANUARY,2002),96.055);
    series1.add(new Day(11,MonthConstants.JANUARY,2002),96.335);

    series1.add(new Day(14,MonthConstants.JANUARY,2002),96.290);
    series1.add(new Day(15,MonthConstants.JANUARY,2002),96.275);
    series1.add(new Day(16,MonthConstants.JANUARY,2002),96.240);
    series1.add(new Day(17,MonthConstants.JANUARY,2002),96.080);
    series1.add(new Day(18,MonthConstants.JANUARY,2002),96.145);

    return new TimeSeriesCollection(series1);
  }

  private static IntervalXYDataset createVolumeDataset()
  {
    // create dataset 2...
    TimeSeries series1=new TimeSeries("Volume");

    series1.add(new Day(2,MonthConstants.JANUARY,2002),41020);
    series1.add(new Day(3,MonthConstants.JANUARY,2002),45586);
    series1.add(new Day(4,MonthConstants.JANUARY,2002),81672);

    series1.add(new Day(7,MonthConstants.JANUARY,2002),81975);
    series1.add(new Day(8,MonthConstants.JANUARY,2002),79692);
    series1.add(new Day(9,MonthConstants.JANUARY,2002),53187);
    series1.add(new Day(10,MonthConstants.JANUARY,2002),87929);
    series1.add(new Day(11,MonthConstants.JANUARY,2002),107047);

    series1.add(new Day(14,MonthConstants.JANUARY,2002),86276);
    series1.add(new Day(15,MonthConstants.JANUARY,2002),79005);
    series1.add(new Day(16,MonthConstants.JANUARY,2002),80632);
    series1.add(new Day(17,MonthConstants.JANUARY,2002),88797);
    series1.add(new Day(18,MonthConstants.JANUARY,2002),57179);

    return new TimeSeriesCollection(series1);
  }

  String getYLabel(String Text)
  {
    String Result="";
    
    for (int i=0;i<Text.length();i++) Result+=Text.charAt(i)+(i<Text.length()-1?"u2009":"");
//    Out(Result);
    return Result;
  }

  private static void out(String message) { System.out.print(message); }
  private static void Out(String message) { System.out.println(message); }

  // Create the GUI and show it. For thread safety, this method should be invoked from the event-dispatching thread.
  static void Create_And_Show_GUI()
  {
    final Price_Volume_Chart_Demo demo=new Price_Volume_Chart_Demo("Price Volume Chart Demo");

    JFrame frame=new JFrame("Time_Series_Chart Frame");
    frame.add(demo);
    frame.addWindowListener( new WindowAdapter()
    {
      public void windowActivated(WindowEvent e) { }
      public void windowClosed(WindowEvent e) { }
      public void windowClosing(WindowEvent e) { if (EXIT_ON_CLOSE_B) System.exit(0); }
      public void windowDeactivated(WindowEvent e) { }
      public void windowDeiconified(WindowEvent e) { demo.repaint(); }
      public void windowGainedFocus(WindowEvent e) { demo.repaint(); }
      public void windowIconified(WindowEvent e) { }
      public void windowLostFocus(WindowEvent e) { }
      public void windowOpening(WindowEvent e) { demo.repaint(); }
      public void windowOpened(WindowEvent e) { }
      public void windowResized(WindowEvent e) { demo.repaint(); }
      public void windowStateChanged(WindowEvent e) { demo.repaint(); }
    });
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }

  public static void main(String[] args)
  {
    // Schedule a job for the event-dispatching thread : creating and showing this application's GUI.
    SwingUtilities.invokeLater(new Runnable() { public void run() { Create_And_Show_GUI(); } });
  }
}

Advertisement

Answer

As shown here, use a StandardXYBarPainter to get a flat effect instead of the default gradient.

XYBarRenderer renderer2=new XYBarRenderer(0.20);
renderer2.setBarPainter(new StandardXYBarPainter());
renderer2.setShadowVisible(false); 

I tried, but didn’t work: renderer2.setDefaultBarPainter(new StandardXYBarPainter()).

Note that XYBarRenderer.setDefaultBarPainter() is a static method. It should be called before instantiating the renderer.

XYBarRenderer.setDefaultBarPainter(new StandardXYBarPainter());
XYBarRenderer renderer2=new XYBarRenderer(0.20);

Either formulation produces the desired result:

image

As an aside, plot.setRangeAxis(1,rangeAxis2) needs to be called only once.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement