|
| 1 | +# grafana-react |
| 2 | + |
| 3 | +React-based DSL for creating Grafana dashboards. Write dashboards as JSX components and compile them to Grafana JSON. |
| 4 | + |
| 5 | +<p align="center"> |
| 6 | + <img src="docs/public/img/hero-code.svg" alt="JSX code for Stat panel" height="180" /> |
| 7 | + |
| 8 | + <img src="docs/public/img/hero-grafana-stat-panel.png" alt="Rendered Grafana Stat panel" height="140" /> |
| 9 | +</p> |
| 10 | + |
| 11 | +## Features |
| 12 | + |
| 13 | +- **Declarative JSX Syntax** - Write dashboards using familiar React patterns |
| 14 | +- **Component Composition** - Create reusable dashboard components |
| 15 | +- **Type Safety** - Full TypeScript support with comprehensive types |
| 16 | +- **CLI Tool** - Build, validate, and watch dashboard files |
| 17 | +- **Zero Runtime** - Compiles to static JSON, no React in production |
| 18 | + |
| 19 | +## Installation |
| 20 | + |
| 21 | +```bash |
| 22 | +npm install grafana-react react |
| 23 | +``` |
| 24 | + |
| 25 | +## Quick Start |
| 26 | + |
| 27 | +```tsx |
| 28 | +import { Dashboard, Row, Stat, Timeseries, Variable } from 'grafana-react'; |
| 29 | + |
| 30 | +export default function MyDashboard() { |
| 31 | + return ( |
| 32 | + <Dashboard uid="my-dashboard" title="My Dashboard" datasource="prometheus"> |
| 33 | + <Variable name="instance">label_values(up, instance)</Variable> |
| 34 | + |
| 35 | + <Row title="Summary"> |
| 36 | + <Stat |
| 37 | + title="CPU %" |
| 38 | + unit="percent" |
| 39 | + thresholds={{ 70: 'yellow', 90: 'red' }} |
| 40 | + > |
| 41 | + 100 - avg(rate(cpu_idle[$__rate_interval])) * 100 |
| 42 | + </Stat> |
| 43 | + </Row> |
| 44 | + |
| 45 | + <Row title="Details"> |
| 46 | + <Timeseries title="CPU Over Time" unit="percent" stack legend="right"> |
| 47 | + sum(rate(node_cpu_seconds_total[5m])) by (mode) * 100 |
| 48 | + </Timeseries> |
| 49 | + </Row> |
| 50 | + </Dashboard> |
| 51 | + ); |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +Build to JSON using the CLI: |
| 56 | + |
| 57 | +```bash |
| 58 | +grafana-react build my-dashboard.tsx output/my-dashboard.json |
| 59 | +``` |
| 60 | + |
| 61 | +Or programmatically with `renderToString`: |
| 62 | + |
| 63 | +```ts |
| 64 | +import React from 'react'; |
| 65 | +import { renderToString } from 'grafana-react'; |
| 66 | + |
| 67 | +// Use the JSON string directly, e.g. add as a ConfigMap with Pulumi |
| 68 | +const json = renderToString(React.createElement(MyDashboard)); |
| 69 | +``` |
| 70 | + |
| 71 | +## Documentation |
| 72 | + |
| 73 | +Full documentation is available at: **[kiwi-research.github.io/grafana-react](https://kiwi-research.github.io/grafana-react)** |
| 74 | + |
| 75 | +- [Getting Started](https://kiwi-research.github.io/grafana-react/getting-started/installation/) |
| 76 | +- [Components Reference](https://kiwi-research.github.io/grafana-react/components/structure/) |
| 77 | +- [CLI Usage](https://kiwi-research.github.io/grafana-react/guides/cli-usage/) |
| 78 | +- [Reusable Components](https://kiwi-research.github.io/grafana-react/guides/reusable-components/) |
| 79 | + |
| 80 | +## CLI Commands |
| 81 | + |
| 82 | +```bash |
| 83 | +grafana-react build <input.tsx> [output.json] # Build single dashboard |
| 84 | +grafana-react build-all <dir> [output-dir] # Build all dashboards |
| 85 | +grafana-react validate <input.tsx> # Validate without output |
| 86 | +grafana-react watch <dir> [output-dir] # Watch and rebuild |
| 87 | +``` |
| 88 | + |
| 89 | +## License |
| 90 | + |
| 91 | +MIT |
0 commit comments