Skip to main content

A Dash component for displaying financial charts using KLineChart

Project description

Dash KLineChart Component

npm version PyPI version License: MIT

一个基于 KLineChart 的自定义 Dash 组件,用于在 Python Dash 应用中展示专业的金融图表。

🚀 项目特性

  • 📈 专业金融图表:基于 KLineChart 10.0.0-alpha8,支持 K线图、蜡烛图等金融图表
  • 🎯 完整技术指标:内置 MA、EMA、RSI、MACD、BOLL 等常用技术指标
  • 📱 响应式设计:支持桌面端和移动端,自适应不同屏幕尺寸
  • 🎨 主题定制:支持明暗主题切换和自定义样式
  • ⚡ 高性能渲染:基于 HTML5 Canvas,支持大数据集流畅渲染
  • 🔄 实时更新:支持实时数据更新和动态交互
  • 🐍 Python 友好:完全兼容 Dash 生态系统,支持 Pandas DataFrame

📦 安装

pip install dash-kline-charts

📌 注意: KLineChart JavaScript 库会自动包含在组件中,无需额外安装或配置。组件会自动加载所需的 klinecharts.min.js 文件。

🔧 快速开始

基础用法

import dash
from dash import html, dcc, Input, Output
import dash_kline_charts as dkc
import pandas as pd

# 创建示例数据
data = [
    {'timestamp': 1609459200000, 'open': 100, 'high': 110, 'low': 95, 'close': 105, 'volume': 1000},
    {'timestamp': 1609545600000, 'open': 105, 'high': 115, 'low': 100, 'close': 110, 'volume': 1200},
    # ... 更多数据
]

app = dash.Dash(__name__)

app.layout = html.Div([
    dkc.DashKLineChart(
        id='kline-chart',
        data=data,
        style={'height': '600px'},
        config={
            'grid': {'show': True},
            'candle': {'type': 'candle_solid'},
            'theme': 'dark'
        }
    )
])

if __name__ == '__main__':
    app.run_server(debug=True)

添加技术指标

dkc.DashKLineChart(
    id='kline-chart',
    data=data,
    indicators=[
        {'name': 'MA', 'params': [5, 10, 20]},
        {'name': 'RSI', 'params': [14]},
        {'name': 'MACD', 'params': [12, 26, 9]}
    ],
    style={'height': '600px'}
)

实时数据更新

@app.callback(
    Output('kline-chart', 'data'),
    Input('interval-component', 'n_intervals')
)
def update_data(n):
    # 获取最新数据
    new_data = get_latest_market_data()
    return new_data

📋 API 文档

DashKLineChart 属性

属性 类型 默认值 描述
id str - 组件唯一标识符
data list [] 图表数据,OHLC 格式
config dict {} 图表配置选项
style dict {} 组件样式设置
indicators list [] 技术指标配置
theme str 'light' 主题设置 ('light' 或 'dark')
responsive bool True 是否启用响应式设计

数据格式

data = [
    {
        'timestamp': 1609459200000,  # 时间戳(毫秒)
        'open': 100.0,              # 开盘价
        'high': 110.0,              # 最高价
        'low': 95.0,                # 最低价
        'close': 105.0,             # 收盘价
        'volume': 1000              # 成交量
    },
    # ... 更多数据点
]

配置选项

config = {
    'grid': {
        'show': True,
        'horizontal': {'show': True},
        'vertical': {'show': True}
    },
    'candle': {
        'type': 'candle_solid',      # 蜡烛图类型
        'bar': {'upColor': '#26A69A', 'downColor': '#EF5350'}
    },
    'crosshair': {
        'show': True,
        'horizontal': {'show': True},
        'vertical': {'show': True}
    },
    'yAxis': {
        'show': True,
        'position': 'right'
    },
    'xAxis': {
        'show': True,
        'position': 'bottom'
    }
}

🧪 支持的技术指标

  • 移动平均线: MA, EMA, SMA
  • 趋势指标: MACD, RSI, KDJ
  • 成交量指标: VOL, OBV
  • 波动率指标: BOLL, ATR
  • 自定义指标: 支持自定义技术指标

🎨 主题定制

预设主题

# 明亮主题
theme = 'light'

# 暗黑主题
theme = 'dark'

自定义主题

custom_theme = {
    'background': '#1e1e1e',
    'grid': '#333333',
    'candle': {
        'up': '#26A69A',
        'down': '#EF5350'
    },
    'text': '#ffffff'
}

📱 响应式设计

组件自动适配不同屏幕尺寸:

dkc.DashKLineChart(
    id='kline-chart',
    data=data,
    responsive=True,
    style={
        'height': '400px',
        'width': '100%'
    }
)

🔧 开发环境设置

开发依赖

# 安装开发依赖
npm install

# 启动开发服务器
npm start

# 构建生产版本
npm run build

# 运行测试
npm test

Python 开发环境

# 创建虚拟环境
python -m venv venv
source venv/bin/activate  # Linux/Mac
venv\Scripts\activate     # Windows

# 安装依赖
pip install -r requirements.txt

# 运行示例
python examples/basic_example.py

🤝 贡献指南

  1. Fork 本项目
  2. 创建特性分支 (git checkout -b feature/AmazingFeature)
  3. 提交更改 (git commit -m 'Add some AmazingFeature')
  4. 推送到分支 (git push origin feature/AmazingFeature)
  5. 创建 Pull Request

开发规范

  • 遵循 PEP 8 代码风格
  • 编写单元测试
  • 更新文档
  • 提交前运行测试套件

📄 许可证

本项目基于 MIT 许可证开源 - 详见 LICENSE 文件。

📚 相关资源

🐛 问题反馈

如果您发现任何问题或有功能建议,请在 GitHub Issues 中提出。

📧 联系我们

📊 支持的图表类型

  • 蜡烛图 (candle_solid): 实心蜡烛图
  • 空心蜡烛图 (candle_stroke): 空心蜡烛图
  • OHLC 图 (ohlc): 开高低收图
  • 面积图 (area): 面积填充图

📐 技术指标

指标 名称 参数示例
MA 移动平均线 [5, 10, 20]
EMA 指数移动平均线 [12, 26]
RSI 相对强弱指数 [14]
MACD 移动平均收敛散度 [12, 26, 9]
BOLL 布林带 [20, 2]
KDJ 随机指标 [9, 3, 3]
VOL 成交量 []

🎨 主题支持

  • 明亮主题: 适合白天使用的明亮界面
  • 暗黑主题: 适合夜间使用的暗色界面
  • 自定义主题: 支持完全自定义颜色方案

🔧 系统要求

Python 环境

  • Python 3.7+
  • Dash 2.0+
  • Plotly 5.0+

浏览器支持

  • Chrome 80+
  • Firefox 75+
  • Safari 13+
  • Edge 80+

🏗️ 项目架构

dash-kline-charts/
├── src/                    # React 组件源码
├── dash_kline_charts/     # Python 包
├── examples/              # 示例应用
├── tests/                 # 测试文件
├── docs/                  # 文档
└── lib/                   # 构建输出

🧪 测试

JavaScript 测试

npm test
npm run test:watch
npm run test:coverage

Python 测试

python -m pytest tests/
python -m pytest tests/ --cov=dash_kline_charts

📚 文档

🔗 相关链接

🏷️ 版本历史

查看 CHANGELOG.md 了解详细的版本更新历史。

📄 许可证

本项目基于 MIT 许可证 开源。

🙏 致谢

  • KLineChart - 提供优秀的金融图表库
  • Dash - 提供强大的 Python web 应用框架
  • 所有为此项目做出贡献的开发者

📞 支持


⭐ 如果这个项目对您有帮助,请给我们一个 Star!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dash_kline_charts-0.1.0.tar.gz (108.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dash_kline_charts-0.1.0-py3-none-any.whl (84.7 kB view details)

Uploaded Python 3

File details

Details for the file dash_kline_charts-0.1.0.tar.gz.

File metadata

  • Download URL: dash_kline_charts-0.1.0.tar.gz
  • Upload date:
  • Size: 108.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.1

File hashes

Hashes for dash_kline_charts-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2c74e0e47d47c7c8e49aa26374047bde1073abc42471c0b58b93b63942d5ba3f
MD5 006f6751bace71c4573c807419080067
BLAKE2b-256 75dfc3bd3b28aa64b3f42b0624cffa28c767aca9d7f6dba736f4f1a2ed4015e9

See more details on using hashes here.

File details

Details for the file dash_kline_charts-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for dash_kline_charts-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 31c6e43187e6e67004d849885829a9fc09b09a147bb6e3665996ef61224ecedd
MD5 86c75eccf6fd81039191625031e4f003
BLAKE2b-256 7edce69286ebb66cf040269f0051e32189a9efce02a38b70728eb922c22d615a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page