使用 SVG 创建自定义交互式仪表盘
Create a Custom Interactive dashboard using SVG

原始链接: https://0xmm.in/posts/custom_dash/

为了监控其自托管基础设施,作者最初考虑了一个复杂的CSS解决方案,但很快意识到自己的前端能力有限。同事Adokshaj Bhandarkar建议使用SVG(可缩放矢量图形)作为更易于管理的替代方案。 SVG允许自定义设计——作者成功创建了一个路由器模型来可视化端口状态——并且易于与前端代码集成。通过为每个SVG元素(如单个端口)分配唯一的ID,作者可以动态更改它们的“填充”颜色来表示状态:红色表示关闭,绿色表示开启。 这种方法,通过使用`chokidar`进行实时文件监控的演示,允许通过编辑SVG文件来实时更新仪表板。作者强调SVG是一种强大且易于访问的解决方案,即使没有广泛的前端经验,也可以创建自定义基础设施可视化。

## 使用 SVG 创建交互式仪表盘 一篇 Hacker News 的讨论强调了一种利用 SVG 构建定制交互式仪表盘的实用方法,尤其适用于基础设施监控。一位用户成功创建了一个城市供水网络的实时显示,从 SQL 数据库中提取数据并在 draw.io 创建的详细 SVG 图形上进行可视化。该方案绕过了昂贵的 SCADA 软件许可,并提供了即时加载时间以及平移和缩放功能。 对话涉及了 AutoCAD 等传统系统在这些图形方面的局限性,并暗示了未来可能出现的、由 JSON 定义、可搜索的“图形”以及 LLM 集成。 许多评论者分享了类似的项目,包括将 SVG 用于具有可组合逻辑的 UI 元素(Breadboard.io),以及利用 Grafana 插件创建视觉上直观的仪表盘。 一个关键的收获是 SVG 在特定用例中的强大和简单性,它为复杂的仪表盘工具提供了一种轻量级的替代方案。 讨论还提到了实际考虑因素,例如用户体验(通过 CSS 动画避免闪烁)和数据验证。
相关文章

原文

Since I started self hosting a few services, I wanted a custom dashboard something like Grafana Canvas where I could monitor my entire infra and visulaize all my devices and their status on a single screen.

At first, I thought, “Hey, I’ll just design the whole device model with CSS!”

But then reality hit me… that would probably take a decade for someone like me who doesn’t have a frontend background 😅 Then I thought, “Why not ask someone who actually knows a thing or two about frontend?”

So, I reached out to a fullstack developer Adokshaj Bhandarkar probably the most frontend savvy person I know, who knows the ins and outs of it like a pro.

After explaining my idea (and all my doubts) to him, he quickly pointed me toward SVG. Using SVG, I realized I could design a custom infrastructure dashboard tailored exactly for my use cases.

These SVGs can be manipulated through the DOM and easily integrated with any frontend. Plus, you can design custom SVGs using tools like Illustrator. So, I went ahead and designed my own router model, where I can visualize port statuses whether an Ethernet port is connected, disconnected, or administratively disabled, etc.

I may not be a professional designer, but hey, I somehow pulled off the stunt and designed the router model myself! 😄

So, how can SVGs actually be manipulated? Well, each SVG element has an id and a fill attribute. Using the id, we can target specific elements in my case, each port on the router model has a unique ID. By changing the fill color dynamically, we can represent different states:

Red for a port that’s shutdown, Green for a port that’s online or up.

Here’s how you can try out the above PoC code:

  • Run the server: Start by running server.js.

  • Open the dashboard: Then open index.html in your browser.

  • Live SVG monitoring: The SVG files are being watched using npm *chokidar.

  • Test it out: Open mikrotik_plain.svg , change some colors, and save the file you’ll see the updates reflected in real time on your Browser!

SVG in action Final result

联系我们 contact @ memedata.com