1

Is it somehow possible to make a chart with information about market depth in Java using JFreeChart?

Something like this:

enter image description here

Martin Ille
  • 6,747
  • 9
  • 44
  • 63

1 Answers1

1

We can use XYStepAreaRenderer. Here is complete code:

import java.awt.GridLayout;
import java.awt.LayoutManager;
import javax.swing.JFrame;
import org.jfree.chart.*;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYStepAreaRenderer;
import org.jfree.data.xy.*;

public class ChartMarketDepth extends JFrame {

    public ChartMarketDepth(LayoutManager layout) {
        super("market depth chart");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        add(createChart());
        setSize(600, 200);
        setLocationRelativeTo(null);
        pack();
        setVisible(true);
    }

    private static ChartPanel createChart() {

        // dataset & series
        XYSeriesCollection xYSeriesCollection = new XYSeriesCollection();
        XYSeries xyseries = new XYSeries("bid");
        xyseries.add(95D, 4D);
        xyseries.add(96D, 3D);
        xyseries.add(97D, 2D);
        xyseries.add(98D, 1D);
        XYSeries xyseries1 = new XYSeries("ask");
        xyseries1.add(100D, 1D);
        xyseries1.add(101D, 2D);
        xyseries1.add(102D, 3D);
        xyseries1.add(103D, 4D);
        xyseries1.add(104D, 5D);
        xYSeriesCollection.addSeries(xyseries);
        xYSeriesCollection.addSeries(xyseries1);

        // chart
        JFreeChart jfreechart = ChartFactory.createXYLineChart("", "Price", "Amount", xYSeriesCollection, PlotOrientation.VERTICAL, false, false, false);
        XYPlot xyplot = (XYPlot) jfreechart.getPlot();
        XYStepAreaRenderer xysteparearenderer = new XYStepAreaRenderer(2);
        xysteparearenderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator());
        xysteparearenderer.setDefaultEntityRadius(6);
        xyplot.setRenderer(xysteparearenderer);

        // chart panel
        final ChartPanel chartPanel = new ChartPanel(jfreechart);
        chartPanel.setMaximumDrawHeight(2000);
        chartPanel.setMaximumDrawWidth(3000);
        return chartPanel;
    }

    public static void main(String[] args) {
        new ChartMarketDepth(new GridLayout(1, 1));
    }
}

And the result:

enter image description here

Martin Ille
  • 6,747
  • 9
  • 44
  • 63
  • +1 for a [complete example](http://stackoverflow.com/help/mcve); see also [*Initial Threads*](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html) and these size [alternatives](http://stackoverflow.com/a/10277372/230513). – trashgod Nov 15 '14 at 04:00