博客
关于我
利用JFreeChart绘制股票K线图
阅读量:84 次
发布时间:2019-02-26

本文共 4025 字,大约阅读时间需要 13 分钟。

JFreeChart股票K线图绘制实例

一、JFreeChart基础概念

JFreeChart是一个强大且灵活的Java图形库,广泛应用于数据可视化领域。其核心组件包括:

  • Chart 蝶形图:作为图形容器,负责整体布局。
  • Plot 画布:定义图形区域,包含数据系列和轴标。
  • Axis 轴:定义数据的维度,例如时间轴和数值轴。
  • Renderer 画图器:决定数据如何呈现,例如柱状图、折线图等。
  • Dataset 数据集:存储用于生成图形的数据,包括K线图的开盘、收盘价等。
  • 二、数据处理与准备

  • 数据收集

    • 中国股票市场的K线图通常包含以下字段:
      • 开盘价(Open)
      • 最高价(High)
      • 最低价(Low)
      • 收盘价(Close)
    • 成交量数据(Volume)需单独处理。
  • 数据格式转换

    • 使用SimpleDateFormat统一日期格式,确保时间轴的一致性。
    • 数据存储为OHLCSeries对象,包含K线图的四个数据点。
  • 异常值处理

    • 通过循环遍历数据集,获取最大值和最小值,确保图形显示范围合理。
  • 三、图形定制与实现

  • K线图绘制

    • 使用CandlestickRenderer作为K线图的绘制器,支持上影线和下影线的颜色设置。
    • 根据中国股票市场的习惯,设置上涨K线为红色,下跌K线为绿色。
  • 时间轴设置

    • 禁用自动时间范围,手动设置为具体日期范围。
    • 使用SegmentedTimeline规则,排除周六和周日,确保图形连续性。
  • 成交量显示

    • 使用柱状图绘制成交量数据,与K线图并列显示。
    • 采用相同的颜色方案,保持图形整体美观。
  • 图形布局

    • 使用CombinedDomainXYPlot实现K线图和成交量的联合显示。
    • 调整图形间隔和间距,确保图表美观且信息密度合理。
  • 四、代码实现

    import org.jfree.data.time.Day;import org.jfree.data.time.ohlc.OHLCSeries;import org.jfree.data.time.ohlc.OHLCSeriesCollection;import org.jfree.chart.renderer.xy.CandlestickRenderer;import org.jfree.chart.axis.DateAxis;import org.jfree.chart.plot.XYPlot;import org.jfree.chart.axis.NumberAxis;import java.text.SimpleDateFormat;import java.awt.Color;public class KLineCombineChart {    public static void main(String[] args) {        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");        OHLCSeries series = new OHLCSeries("K线数据");        // 添加K线数据点        series.add(new Day(28, 9, 2007), 9.2, 9.58, 9.16, 9.34);        // 其他数据点(见完整代码)        // 添加成交量数据点        TimeSeries volumeSeries = new TimeSeries("成交量数据");        volumeSeries.add(new Day(28, 9, 2007), 260659400 / 100);        // 其他数据点(见完整代码)        // 创建数据集        OHLCSeriesCollection seriesCollection = new OHLCSeriesCollection();        seriesCollection.addSeries(series);        TimeSeriesCollection volumeCollection = new TimeSeriesCollection();        volumeCollection.addSeries(volumeSeries);        // 设置K线图的颜色        final CandlestickRenderer candlestickRender = new CandlestickRenderer();        candlestickRender.setUseOutlinePaint(true);        candlestickRender.setAutoWidthMethod(CandlestickRenderer.WIDTHMETHOD_AVERAGE);        candlestickRender.setDownPaint(Color.GREEN);        candlestickRender.setUpPaint(Color.RED);        // 设置时间轴        DateAxis x1Axis = new DateAxis();        x1Axis.setAutoRange(false);        x1Axis.setRange(dateFormat.parse("2007-08-20"), dateFormat.parse("2007-09-29"));        x1Axis.setTimeline(SegmentedTimeline.newMondayThroughFridayTimeline());        // 设置Y轴        NumberAxis y1Axis = new NumberAxis();        y1Axis.setAutoRange(false);        y1Axis.setRange(minValue * 0.9, highValue * 1.1);        y1Axis.setTickUnit(new NumberTickUnit((highValue * 1.1 - minValue * 0.9) / 10));        // 创建K线图        XYPlot plot1 = new XYPlot(seriesCollection, x1Axis, y1Axis, candlestickRender);        // 创建成交量柱状图        XYBarRenderer volumeRenderer = new XYBarRenderer() {            private static final long serialVersionUID = 1L;            public Paint getItemPaint(int i, int j) {                if (seriesCollection.getCloseValue(i, j) > seriesCollection.getOpenValue(i, j)) {                    return candlestickRender.getUpPaint();                } else {                    return candlestickRender.getDownPaint();                }            }        };        XYPlot plot2 = new XYPlot(volumeCollection, null, y2Axis, volumeRenderer);        // 组合图表        CombinedDomainXYPlot combinedPlot = new CombinedDomainXYPlot(x1Axis);        combinedPlot.add(plot1, 2);        combinedPlot.add(plot2, 1);        combinedPlot.setGap(10);        // 创建图表        JFreeChart chart = new JFreeChart("中国联通股票", Chart.DEFAULT_TITLE_FONT, combinedPlot, false);        ChartFrame frame = new ChartFrame("中国联通股票", chart);        frame.pack();        frame.setVisible(true);    }}

    五、改进与经验分享

  • K线边框处理

    • 修改CandlestickRenderer.java,确保上影线和下影线颜色与K线一致,提升图表美观度。
  • 图表维度管理

    • 手动设置时间轴范围,避免自动调整带来的周六周日显示问题。
  • 成交量显示优化

    • 通过自定义柱状图-renderer,实现与K线图颜色一致的成交量显示。
  • 图表间距调整

    • 使用setGap方法,优化图表间距,确保图表美观且信息展示效果理想。
  • 六、总结

    通过以上实现,可以轻松绘制出符合中国股票市场特点的K线图和成交量图表。代码清晰,注释详尽,直接在Eclipse中运行即可,省去繁琐配置步骤。如需进一步优化或添加功能,欢迎在社区交流并共同改进!

    转载地址:http://exmz.baihongyu.com/

    你可能感兴趣的文章
    No module named tensorboard.main在安装tensorboardX的时候遇到的问题
    查看>>
    No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
    查看>>
    No new migrations found. Your system is up-to-date.
    查看>>
    No qualifying bean of type XXX found for dependency XXX.
    查看>>
    No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
    查看>>
    No resource identifier found for attribute 'srcCompat' in package的解决办法
    查看>>
    no session found for current thread
    查看>>
    No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
    查看>>
    NO.23 ZenTaoPHP目录结构
    查看>>
    no1
    查看>>
    NO32 网络层次及OSI7层模型--TCP三次握手四次断开--子网划分
    查看>>
    NOAA(美国海洋和大气管理局)气象数据获取与POI点数据获取
    查看>>
    NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
    查看>>
    node exporter完整版
    查看>>
    Node JS: < 一> 初识Node JS
    查看>>
    Node Sass does not yet support your current environment: Windows 64-bit with Unsupported runtime(72)
    查看>>
    Node 裁切图片的方法
    查看>>
    Node+Express连接mysql实现增删改查
    查看>>
    node, nvm, npm,pnpm,以前简单的前端环境为什么越来越复杂
    查看>>
    Node-RED中Button按钮组件和TextInput文字输入组件的使用
    查看>>