You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在AnyLogic GIS中实现区域颜色随资源库存动态变化?

在AnyLogic 8.3 GIS区域实现库存驱动的动态颜色变化

1. 给GIS区域绑定库存数据

  • 给你框选的每个GIS Shape或GIS Region对象添加自定义参数,比如命名为currentInventory(类型选int),用来存储该区域对应的实时库存数值。
  • 在后续资源分配逻辑中,库存增减时直接给这个参数赋值即可,比如regionA.currentInventory = regionA.currentInventory - 10。

2. 编写颜色映射函数

新建一个自定义函数(例如命名为getRegionColor),输入参数设为int inventory,返回类型选择Color,根据库存值返回对应颜色:

Color getRegionColor(int inventory) {
    // 可根据业务需求自行调整阈值和对应颜色
    if (inventory <= 0) {
        return Color.RED; // 库存耗尽
    } else if (inventory < 50) {
        return Color.ORANGE; // 库存偏低
    } else if (inventory < 100) {
        return Color.YELLOW; // 库存充足
    } else {
        return Color.GREEN; // 库存充裕
    }
}

如果需要平滑的渐变效果,也可以按库存占最大库存的比例计算RGB值:

Color getRegionColor(int inventory, int maxInventory) {
    double ratio = (double)inventory / maxInventory;
    int red = (int)(255 * (1 - ratio));
    int green = (int)(255 * ratio);
    return new Color(red, green, 0); // 实现从红到绿的渐变
}

3. 绑定区域颜色与函数

  • 选中目标GIS区域,在右侧属性面板找到Fill -> Color选项,点击颜色选择器旁的函数按钮(小fx图标)。
  • 在弹出的表达式编辑器中调用你编写的函数:
    • 单区域绑定单个库存:getRegionColor(this.currentInventory)
    • 使用渐变逻辑:getRegionColor(this.currentInventory, 150)(150为该区域设定的最大库存值)

4. 确保颜色实时刷新

  • 若库存变化后区域颜色未自动更新,可在库存更新代码后手动调用区域对象.repaint(),比如regionA.repaint()强制刷新显示。
  • 若存在大量GIS区域,可在Main代理的onTimeTick事件中添加循环逻辑,遍历所有区域调用repaint(),实现每帧自动刷新。

内容的提问来源于stack exchange,提问作者Ramiro Córdoba

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 09:05:07