使用JFreeChart的市场深度图表

时间:2014-11-15 02:07:02

标签: java jfreechart

是否有可能使用JFreeChart在Java中创建包含市场深度信息的图表?

这样的事情:

enter image description here

1 个答案:

答案 0 :(得分:2)

我们可以使用 XYStepAreaRenderer 。这是完整的代码:

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));
    }
}

结果:

enter image description here