Java 生成「股票日 K 线图(投资研究)」PDF 示例(K)
K1 页 A4 · 123 KB
某股票 2024 年 12 月日 K 线:红绿实体表示当日涨跌、上下影线表示最高与最低价,横轴标注交易日,图下列出月初上行、中旬回调与均线支撑建议。

输出规格
- PDF 页数
- 1
- 图表类型
- K
- 模板行数
- 36
- 源码行数
- 106
Java 源码 · JQuickBiz60KChartTest.java
以下代码即生成上方 PDF 的完整测试类,可直接复制运行;Maven 依赖见快速开始章节。
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright (c) [2025-2099] Martin ([email protected])
*/
package com.github.paohaijiao.demo.biz;
import com.github.paohaijiao.JOption;
import com.github.paohaijiao.axis.JCategoryAxis;
import com.github.paohaijiao.config.JPdfConfig;
import com.github.paohaijiao.demo.constant.JQuickConstant;
import com.github.paohaijiao.enums.JChartType;
import com.github.paohaijiao.executor.JQuickPdfFactory;
import com.github.paohaijiao.factory.JChartRendererFactory;
import com.github.paohaijiao.series.JCandlestick;
import org.junit.Test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
/**
* 业务场景 Demo 60:股票日 K 线图(投资研究)
*
* <p>业务场景:投资研究部门以 K 线(蜡烛图)展示某股票 2024 年 12 月的日交易数据,
* 实体为开收盘价区间,上下影线为最高与最低价,用于技术分析。</p>
* <p>实际图表类型:{@link JChartType#K} K 线图。</p>
*
* <p>模板:{@code report/biz/60_k_chart.txt};图表以 SVG 文本形式绑定到模板的
* {@code ${svg}} 占位符,输出到 {@code D:\test\biz\60_k_chart.pdf}。</p>
*
* <p>图表尺寸说明:图表按 700pt × 380pt 的横版比例设计;模板中 {@code <svg>}
* 只声明宽度、不声明高度,宽度被页面自动收窄时高度会按原比例同步缩放。</p>
*
* @author Martin
* @version 1.0.0
*/
public class JQuickBiz60KChartTest {
public static final String path = JQuickConstant.path;
@Test
public void biz60KChart() throws IOException {
// 2024 年 12 月交易日(共 22 个交易日,此处简化取前 15 个)
String[] dates = {"12-02", "12-03", "12-04", "12-05", "12-06", "12-09", "12-10",
"12-11", "12-12", "12-13", "12-16", "12-17", "12-18", "12-19", "12-20"};
// 每组数据:[open, close, min, max]
double[][] kData = {
{32.10, 32.85, 31.90, 33.00},
{32.80, 33.40, 32.50, 33.60},
{33.30, 32.90, 32.60, 33.50},
{32.95, 33.80, 32.80, 34.00},
{33.70, 34.20, 33.50, 34.50},
{34.10, 33.60, 33.20, 34.30},
{33.55, 34.10, 33.30, 34.40},
{34.00, 34.60, 33.80, 34.80},
{34.50, 35.20, 34.30, 35.50},
{35.10, 34.70, 34.40, 35.30},
{34.65, 35.00, 34.20, 35.20},
{34.95, 35.40, 34.80, 35.70},
{35.30, 34.80, 34.50, 35.60},
{34.75, 35.10, 34.40, 35.30},
{35.00, 35.60, 34.90, 35.90}
};
JCandlestick candlestick = new JCandlestick("日 K 线");
// 注意:JCandlestick.data(open, close, min, max) 内部 super.data(kData) 会把
// Object[] 按可变参数展开成 4 个独立元素,导致渲染器拿不到 Object[] 格式的 K 线数据。
// 这里直接向 data 列表中逐条添加 Object[]{open, close, min, max}。
for (double[] d : kData) {
candlestick.data().add(new Object[]{d[0], d[1], d[2], d[3]});
}
JOption option = new JOption();
option.title("某股票 2024 年 12 月日 K 线图", "单位:元 | 数据来源:行情系统");
option.xAxis(new JCategoryAxis().data((Object[]) dates));
option.series(candlestick);
String svg = JChartRendererFactory.renderChart(JChartType.K, option);
File dir = new File(path + "biz");
if (!dir.exists()) {
dir.mkdirs();
}
FileOutputStream fileOutputStream = new FileOutputStream(path + "biz/60_k_chart.pdf");
JPdfConfig config = new JPdfConfig();
config.getLayoutConfig().setFlexLayout(true);
JQuickPdfFactory factory = new JQuickPdfFactory(config);
factory.bind("svg", svg);
byte[] bytes = factory.executeResource("report/biz/60_k_chart.txt");
fileOutputStream.write(bytes);
fileOutputStream.close();
}
}最小调用(3 行)
不想读完整测试类?核心就是这三行:绑定图表 SVG,执行模板,拿到 PDF 字节流。可直接复制到你的项目里。
// config 可省略;svg 来自 JChartRendererFactory.renderChart(...),见下方完整源码
factory.bind("svg", svg);
byte[] pdf = new JQuickPdfFactory(config).executeResource("report/biz/60_k_chart.txt");模板文件 · 60_k_chart.txt
类 HTML + 内联 CSS 的模板语法,绑定数据后渲染为 PDF。下方为完整模板,可直接复制,也可下载原始文件。
<pdf>
<body>
// 60 K线图(蜡烛图)Demo:展示一段时间内的开盘、收盘、最高、最低价格
<div style="background:#263238; padding:12px; borderRadius:4px; marginBottom:10px; keepTogether:true">
<div style="display:flex; justifyContent:space-between">
<div style="width:320px">
<h1 style="color:white; fontSize:17; bold:true; marginBottom:3px">'某股票 2024 年 12 月日 K 线图'</h1>
<p style="color:#B0BEC5; fontSize:9">'红色/绿色实体表示当日涨跌,上下影线表示最高与最低价'</p>
</div>
<div style="width:220px; textAlignment:right; borderLeft:1px solid #546E7A; paddingLeft:10px">
<p style="color:white; fontSize:9">'交易周期:2024 年 12 月'</p>
<p style="color:#B0BEC5; fontSize:9">'数据频率:日线'</p>
<p style="color:#B0BEC5; fontSize:9">'编制:投资研究部'</p>
</div>
</div>
</div>
<div style="marginBottom:10px; padding:10px; backgroundColor:#F7F9FC; borderRadius:4px; borderTop:2px solid #263238">
<h2 style="color:#263238; marginBottom:6px; fontSize:13; bold:true">'一、12 月日 K 线走势'</h2>
<div style="display:flex; justifyContent:center">
<svg style="width:700pt">${svg}</svg>
</div>
<p style="fontSize:9; color:#6c757d; italic:true; marginTop:6px">'图 1 K 线图:实体为开收盘价区间,上影线为最高价,下影线为最低价(数据来源:行情系统)'</p>
</div>
<list style="listStyleType:disc; paddingLeft:16px; fontSize:10">
<li style="marginBottom:6px"><span style="bold:true; color:#263238">'月初震荡上行:'</span>'连续收阳,成交量温和放大,多头占优。'</li>
<li style="marginBottom:6px"><span style="bold:true; color:#263238">'中旬高位回调:'</span>'出现长上影线,上方抛压较重,需警惕短期见顶。'</li>
<li style="marginBottom:6px"><span style="bold:true; color:#263238">'建议:'</span>'关注 20 日均线支撑,若有效跌破可考虑减仓避险。'</li>
</list>
<lineSeparator style="strokeColor:#DCE3EC"></lineSeparator>
<p style="color:#6c757d; fontSize:8; textAlignment:center; marginTop:6px">'生成时间:2025年1月6日 | 批准人:研究总监'</p>
</body>
</pdf>同分组示例
本页 PDF、预览图、模板与源码均由 JQuick-Pdf 真实运行产出,可自由复制到你的项目中使用。