股票持仓截图生成器手机版, 股票持仓图生成器免费,交割单生成器制作工具

简介: 代码实现了一个完整的股票持仓截图生成器,包含数据模拟、表格绘制、汇总计算和水印添加功能。

下载地址【已上传】:https://www.pan38.com/share.php?code=JCnzE 提取码:6666
声明:所下载的文件以及如下所示代码仅供学习参考用途,作者并不提供软件的相关服务。

代码实现了一个完整的股票持仓截图生成器,包含数据模拟、表格绘制、汇总计算和水印添加功能。使用时只需创建StockPortfolioGenerator实例并调用generate()方法即可生成图片,exportAsImage()方法可导出为图片数据URL。

class StockPortfolioGenerator {
constructor(options = {}) {
this.width = options.width || 800;
this.height = options.height || 600;
this.backgroundColor = options.backgroundColor || '#f5f5f5';
this.textColor = options.textColor || '#333';
this.primaryColor = options.primaryColor || '#1890ff';
this.stocks = options.stocks || this.generateRandomStocks();
this.canvas = document.createElement('canvas');
this.ctx = this.canvas.getContext('2d');
this.canvas.width = this.width;
this.canvas.height = this.height;
}

generateRandomStocks(count = 10) {
    const stocks = [];
    const stockNames = ['腾讯控股', '阿里巴巴', '贵州茅台', '美团-W', '京东集团', 
                      '中国平安', '招商银行', '宁德时代', '比亚迪', '小米集团'];

    for (let i = 0; i < count; i++) {
        const costPrice = (Math.random() * 100 + 50).toFixed(2);
        const currentPrice = (costPrice * (0.9 + Math.random() * 0.2)).toFixed(2);
        const shares = Math.floor(Math.random() * 1000) + 100;

        stocks.push({
            name: stockNames[i % stockNames.length],
            code: `SH${600000 + i}`,
            costPrice: parseFloat(costPrice),
            currentPrice: parseFloat(currentPrice),
            shares: shares,
            marketValue: (currentPrice * shares).toFixed(2),
            profit: ((currentPrice - costPrice) * shares).toFixed(2),
            profitRate: (((currentPrice - costPrice) / costPrice) * 100).toFixed(2)
        });
    }
    return stocks;
}

drawHeader() {
    this.ctx.fillStyle = this.primaryColor;
    this.ctx.fillRect(0, 0, this.width, 60);

    this.ctx.fillStyle = '#fff';
    this.ctx.font = 'bold 24px Arial';
    this.ctx.textAlign = 'center';
    this.ctx.fillText('股票持仓明细', this.width / 2, 40);

    this.ctx.font = '14px Arial';
    this.ctx.fillText(`生成时间: ${new Date().toLocaleString()}`, this.width / 2, 80);
}

drawTable() {
    const columns = [
        { title: '股票名称', key: 'name', width: 120 },
        { title: '股票代码', key: 'code', width: 100 },
        { title: '持仓数量', key: 'shares', width: 100 },
        { title: '成本价', key: 'costPrice', width: 100 },
        { title: '当前价', key: 'currentPrice', width: 100 },
        { title: '市值', key: 'marketValue', width: 120 },
        { title: '盈亏', key: 'profit', width: 120 },
        { title: '盈亏率', key: 'profitRate', width: 100 }
    ];

    // 绘制表头
    this.ctx.fillStyle = '#e6f7ff';
    this.ctx.fillRect(0, 100, this.width, 40);

    this.ctx.fillStyle = this.textColor;
    this.ctx.font = 'bold 14px Arial';
    this.ctx.textAlign = 'center';

    let x = 20;
    columns.forEach(col => {
        this.ctx.fillText(col.title, x + col.width / 2, 125);
        x += col.width;
    });

    // 绘制表格内容
    this.ctx.font = '14px Arial';
    this.ctx.textAlign = 'right';

    this.stocks.forEach((stock, rowIndex) => {
        const y = 150 + rowIndex * 30;

        // 交替行颜色
        this.ctx.fillStyle = rowIndex % 2 === 0 ? '#fff' : '#f9f9f9';
        this.ctx.fillRect(0, y - 20, this.width, 30);

        // 绘制单元格内容
        this.ctx.fillStyle = this.textColor;
        let x = 20;

        columns.forEach(col => {
            let value = stock[col.key];
            if (col.key === 'profit') {
                this.ctx.fillStyle = value >= 0 ? '#f5222d' : '#52c41a';
            } else if (col.key === 'profitRate') {
                value = value + '%';
                this.ctx.fillStyle = stock.profit >= 0 ? '#f5222d' : '#52c41a';
            } else {
                this.ctx.fillStyle = this.textColor;
            }

            this.ctx.fillText(value, x + col.width - 10, y);
            x += col.width;
        });
    });
}

drawSummary() {
    const totalMarketValue = this.stocks.reduce((sum, stock) => sum + parseFloat(stock.marketValue), 0);
    const totalProfit = this.stocks.reduce((sum, stock) => sum + parseFloat(stock.profit), 0);
    const avgProfitRate = (totalProfit / (totalMarketValue - totalProfit) * 100).toFixed(2);

    const y = 150 + this.stocks.length * 30 + 30;

    this.ctx.fillStyle = '#e6f7ff';
    this.ctx.fillRect(0, y - 20, this.width, 60);

    this.ctx.fillStyle = this.textColor;
    this.ctx.font = 'bold 16px Arial';
    this.ctx.textAlign = 'left';
    this.ctx.fillText('持仓汇总:', 20, y);

    this.ctx.font = '14px Arial';
    this.ctx.fillText(`总市值: ${totalMarketValue.toFixed(2)}`, 20, y + 25);
    this.ctx.fillText(`总盈亏: ${totalProfit.toFixed(2)}`, 200, y + 25);
    this.ctx.fillText(`平均盈亏率: ${avgProfitRate}%`, 380, y + 25);
}

drawWatermark() {
    this.ctx.fillStyle = 'rgba(0, 0, 0, 0.1)';
    this.ctx.font = 'bold 48px Arial';
    this.ctx.textAlign = 'center';
    this.ctx.textBaseline = 'middle';
    this.ctx.save();
    this.ctx.translate(this.width / 2, this.height / 2);
    this.ctx.rotate(-Math.PI / 6);
    this.ctx.fillText('模拟数据', 0, 0);
    this.ctx.restore();
}

generate() {
    // 绘制背景
    this.ctx.fillStyle = this.backgroundColor;
    this.ctx.fillRect(0, 0, this.width, this.height);

    // 绘制各个部分
    this.drawHeader();
    this.drawTable();
    this.drawSummary();
    this.drawWatermark();

    return this.canvas;
}

exportAsImage() {
    return this.canvas.toDataURL('image/png');
}

}

// 使用示例
const generator = new StockPortfolioGenerator({
width: 900,
height: 700,
stocks: [
{
name: '腾讯控股',
code: '00700',
costPrice: 350.50,
currentPrice: 420.75,
shares: 500,
marketValue: (420.75 500).toFixed(2),
profit: ((420.75 - 350.50)
500).toFixed(2),
profitRate: (((420.75 - 350.50) / 350.50) * 100).toFixed(2)
},
// 可以添加更多股票数据...
]
});

// 生成图片
generator.generate();
const imageDataUrl = generator.exportAsImage();
console.log('图片已生成,数据URL:', imageDataUrl);

相关文章
|
14天前
|
机器学习/深度学习 消息中间件 人工智能
别只会写脚本了!看看机器学习是怎么帮运维“摸鱼”的
别只会写脚本了!看看机器学习是怎么帮运维“摸鱼”的
47 13
|
人工智能 安全 Java
Serverless JManus: 企业生产级通用智能体运行时
JManus 是面向 Java 的企业级通用智能体框架,支持多 Agent 框架、MCP 协议和 PLAN-ACT 模式,具备高可用、弹性伸缩的特性。结合阿里云 Serverless 运行时 SAE 和 FC,实现稳定安全的智能体应用部署与运行。
151 22
|
12天前
|
应用服务中间件 nginx Docker
静态资源管理:Nginx在Docker中的部署
部署Nginx到Docker中作为静态资源服务器是一种既简单又高效的方法,可以节省时间和资源,并能确保一致性和可扩展性。我们通过编写Dockerfile指定了基础镜像和所需指令,编写Nginx配置管理请求处理,构建自定义Docker镜像,并运行容器以启动服务。这一过程即符合开发规范,也保证了资源的高效管理和访问速度。
64 13
|
13天前
|
人工智能 测试技术 编译器
Python语言从2.7到3.14的能力变化与演进逻辑
Python自2008年进入3.0时代以来,经历了持续演进与革新。十六年间,从语言设计、标准库优化到性能提升、虚拟机改进,Python不断适应人工智能、云计算和微服务等技术的发展需求。本文全面梳理了Python 3发布以来的重要变化,涵盖编程风格现代化、类型系统完善、类库生态调整、性能优化突破以及虚拟机技术创新等多个维度,展示了Python如何在保持简洁易用的同时,实现高效、稳定和可扩展的工程能力。未来,Python将在性能、类型安全和云原生等方面持续进化,进一步巩固其在现代软件开发中的核心地位。
201 29
|
1月前
|
自然语言处理 数据可视化 测试技术
告别‘人海战术’!基于EvalScope 的文生图模型智能评测新方案
生成式模型在文本生成图片等领域的快速发展,为社区带来了日新月异的诸多文生图模型。
235 20
|
13天前
|
人工智能 缓存 自然语言处理
AI 编程如何在团队中真正落地?
如果你是技术负责人、团队推动者或希望在团队中引入 AI 编程工具的工程师,这篇文章将为你提供一条可借鉴、可落地、可优化的路径。
235 23
AI 编程如何在团队中真正落地?
|
18天前
|
Prometheus Kubernetes 监控
Kubernetes技巧:使用Prometheus监控Pod性能指标。
记住,监控和可视化是维持健康集群的必备工具,而Prometheus加上Grafana就是这个任务上的黄金搭档。安装这两位侦探后,你的Kubernetes集群将会像一个经过精心维护的庞大机器一样,高效、有序地运转。
106 21
|
18天前
|
Linux 网络安全 数据安全/隐私保护
使用Linux系统的mount命令挂载远程服务器的文件夹。
如此一来,你就完成了一次从你的Linux发车站到远程服务器文件夹的有趣旅行。在这个技术之旅中,你既探索了新地方,也学到了如何桥接不同系统之间的距离。
74 21
|
9天前
|
缓存 大数据 PHP
PHP性能优化实战:告别缓慢脚本
PHP性能优化实战:告别缓慢脚本
166 89