-- 自定义函数:创建动态K线图
function createDynamicKLineChart(parent)
-- 创建一个节点来承载K线图
chart = GUI:Node_Create(parent, "aaa", 0, 0)
if not chart then
SL:Print("创建失败")
return nil
end
chart = GUI:Image_Create(chart, "pBg", -100, 0, "res/public/bg_npc_05.png")
jiage = GUI:Text_Create(parent, "PercentLab", 410, 600, 24, "#00fffff", "当前价格:")
local Btn1 = GUI:Button_Create(parent, "BtnOk1", 350, 100, "res/public/1900000673.png")
local Btn2 = GUI:Button_Create(parent, "BtnOk2", 550, 100, "res/public/1900000673.png")
GUI:Button_setTitleText(Btn1, "开仓")
GUI:Button_setTitleText(Btn2, "平仓")
if chart then
GUI:Image_setScale9Slice(chart, 10, 10, 10, 10)
GUI:setContentSize(chart, 800, 600) -- 设置背景图大小
GUI:setAnchorPoint(chart, 0, 0) -- 设置锚点为左下角
else
SL:Print("[ERROR] Failed to create KLineBg")
end
local width = 10 -- 每根K线的宽度
local spacing = 2 -- K线之间的间距
local baseY = 50 -- 基准Y坐标
local maxPrice = 0 -- 最高价
local minPrice = math.huge -- 最低价
local klineData = {} -- 存储K线数据
local maxKlines = 50 -- 最多显示50根K线
-- 自定义函数:生成随机K线数据
local function generateRandomKLine()
local lastClose = klineData[#klineData] and klineData[#klineData].close or 100 -- 初始价格为100
local open = lastClose
local close = open + math.random(-30, 30) -- 收盘价随机波动
local high = math.max(open, close) + math.random(0, 10) -- 最高价
local low = math.min(open, close) - math.random(0, 10) -- 最低价
return {open = open, close = close, high = high, low = low}
end
-- 自定义函数:更新K线图
local function updateKLineChart()
-- 生成新的K线数据
local newKLine = generateRandomKLine()
table.insert(klineData, newKLine)
-- 如果超过最大K线数量,移除最旧的数据
if #klineData > maxKlines then
table.remove(klineData, 1)
end
-- 清空之前的图形
chart:removeAllChildren() -- cocos标准语法
-- 重新计算最高价和最低价
maxPrice = 0
minPrice = math.huge
for _, kline in ipairs(klineData) do
if kline.high > maxPrice then
maxPrice = kline.high
end
if kline.low < minPrice then
minPrice = kline.low
end
end
-- 计算价格缩放比例
local yScale = 300 / (maxPrice - minPrice) -- 图表高度为300
-- 绘制左侧价格标识
local priceStep = (maxPrice - minPrice) / 5 -- 将价格分为5个区间
for i = 0, 5 do
local price = minPrice + priceStep * i
local y = baseY + (price - minPrice) * yScale
local priceLabel = GUI:Text_Create(chart, "PriceLabel_" .. i, 700, y, 14, "#FFFFFF", string.format("%.2f", price) .. "%")
if not priceLabel then
SL:Print("[ERROR] Failed to create PriceLabel_" .. i)
end
end
-- 绘制K线
for i, kline in ipairs(klineData) do
local open = kline.open
local close = kline.close
local high = kline.high
local low = kline.low
-- 计算K线的位置
local x = (i - 1) * (width + spacing) + 50 -- 左侧留出50像素空间
local yOpen = baseY + (open - minPrice) * yScale
local yClose = baseY + (close - minPrice) * yScale
local yHigh = baseY + (high - minPrice) * yScale
local yLow = baseY + (low - minPrice) * yScale
SL:print(i)
-- 格式化当前价格
local formattedNum = string.format("%.2f", yOpen)
SL:print(formattedNum) -- 输出:123.46
updatetext(parent, chart, "当前价格:" .. formattedNum)
-- 创建K线的实体(矩形表示K线实体)
klineRect = GUI:Layout_Create(chart, "KLineRect_" .. i, x, math.min(yOpen, yClose), width, math.abs(close - open) * yScale, false)
if klineRect then
GUI:Layout_setBackGroundColorType(klineRect, 1) -- 设置背景颜色类型为纯色
if close > open then
GUI:Layout_setBackGroundColor(klineRect, "#FF0000") -- 上涨为红色
else
GUI:Layout_setBackGroundColor(klineRect, "#00FF00") -- 下跌为绿色
end
if i == #klineData then
GUI:setOpacity(klineRect,0)
local aniAction=GUI:Timeline_FadeIn(klineRect,0.5, function()
--do something
SL:print("danchu!")
end)
else
SL:Print("[ERROR] Failed to create KLineRect_" .. i)
end
-- 创建K线的影线(线表示最高价和最低价)
local shadowLine = GUI:Layout_Create(chart, "ShadowLine_" .. i, x + width / 2, yLow, 1, yHigh - yLow, false)
if shadowLine then
GUI:Layout_setBackGroundColorType(shadowLine, 1) -- 设置背景颜色类型为纯色
if close > open then
GUI:Layout_setBackGroundColor(shadowLine, "#FF0000") -- 影线为红色
else
GUI:Layout_setBackGroundColor(shadowLine, "#00FF00") -- 影线为绿色
end
if i == #klineData then
GUI:setOpacity(klineRect,0)
local aniAction=GUI:Timeline_FadeIn(shadowLine,0.5, function()
--do something
SL:print("danchu!")
end)
end
else
SL:Print("[ERROR] Failed to create ShadowLine_" .. i)
end
end
end
-- 计算均线(简单移动平均线,SMA)
local smaPeriod = 5 -- 均线周期
local smaPoints = {} -- 存储均线点
for i = smaPeriod, #klineData do
local sum = 0
for j = i - smaPeriod + 1, i do
sum = sum + klineData[j].close
end
local sma = sum / smaPeriod
table.insert(smaPoints, sma)
end
-- -- 检测是否为最后一帧
-- if #klineData == maxKlines then
-- -- 添加淡出效果
-- local fadeOut = SL:createFadeOut(1.0) -- 1秒淡出
-- chart:runAction(fadeOut)
-- end
end
-- 每0.3秒更新一次K线图
SL:Schedule(updateKLineChart, 0.3) -- 每0.3秒调用一次updateKLineChart
return chart
end
-- 获取当前场景的父节点
local parent = GUI:Attach_Parent() -- 获取当前打开界面的挂接点
if not parent then
SL:Print("[ERROR] Failed to get parent node")
return
end
-- 创建动态K线图
local dynamicKLineChart = createDynamicKLineChart(parent)
if dynamicKLineChart then
GUI:setPosition(dynamicKLineChart, 100, 100) -- 设置K线图的位置
else
SL:Print("失败~")
end