Skip to content

Commit f703e32

Browse files
authored
Merge pull request #21 from gaarutyunov/claude/add-guix-webgpu-docs-01QQtC83RdPUSoF2t8ZNdYJW
docs: Add WebGPU charting documentation for 2D charts
2 parents 0d1a872 + 51e0cf7 commit f703e32

2 files changed

Lines changed: 325 additions & 1 deletion

File tree

README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- 🛠️ **Developer Tools** - Watch mode, incremental compilation, and hot reload
1313
- 🎨 **Template Interpolation** - Backtick strings with expression interpolation
1414
- 🎮 **WebGPU Support** - First-class 3D graphics with scene graphs, PBR materials, and lighting
15+
- 📊 **2D Charts** - GPU-accelerated charting with candlestick, line, and bar charts
1516

1617
## Quick Start
1718

@@ -491,6 +492,64 @@ python3 -m http.server 8080
491492

492493
**Requirements**: Chrome 113+, Edge 113+, or Safari Technology Preview with WebGPU support.
493494

495+
### WebGPU Chart Example
496+
497+
See `examples/webgpu-chart/` for a complete 2D charting example demonstrating:
498+
499+
- GPU-accelerated rendering for high-performance charts
500+
- Candlestick charts for OHLCV (Open-High-Low-Close-Volume) data
501+
- Declarative chart definition with Guix components
502+
- Time-based X-axis with automatic formatting
503+
- Price formatting on Y-axis
504+
- Grid lines and axis labels
505+
- Responsive design
506+
507+
**Quick example**:
508+
509+
```go
510+
// Define chart data
511+
chartData := []chart.OHLCV{
512+
{Timestamp: 1701388800000, Open: 37500, High: 38200, Low: 37100, Close: 37800, Volume: 28500000000},
513+
{Timestamp: 1701475200000, Open: 37800, High: 39100, Low: 37600, Close: 38900, Volume: 32100000000},
514+
// ... more data
515+
}
516+
517+
// Create chart declaratively
518+
Chart(ChartBackground(0.08, 0.09, 0.12, 1.0)) {
519+
XAxis(
520+
AxisPosition("bottom"),
521+
TimeScale(true),
522+
GridLines(true),
523+
)
524+
525+
YAxis(
526+
AxisPosition("right"),
527+
GridLines(true),
528+
)
529+
530+
CandlestickSeries(
531+
ChartData(chartData),
532+
UpColor(0.18, 0.80, 0.44, 1.0), // Green for bullish candles
533+
DownColor(0.91, 0.27, 0.38, 1.0), // Red for bearish candles
534+
WickColor(0.6, 0.6, 0.65, 1.0),
535+
BarWidth(0.8),
536+
)
537+
}
538+
```
539+
540+
To run:
541+
542+
```bash
543+
cd examples/webgpu-chart
544+
go generate
545+
GOOS=js GOARCH=wasm go build -o main.wasm
546+
cp $(go env GOROOT)/misc/wasm/wasm_exec.js .
547+
python3 -m http.server 8080
548+
# Open http://localhost:8080
549+
```
550+
551+
**Requirements**: Chrome 113+, Edge 113+, or Safari Technology Preview with WebGPU support.
552+
494553
For detailed WebGPU documentation, see [docs/WEBGPU.md](docs/WEBGPU.md).
495554

496555
## Binary Size Optimization

docs/WEBGPU.md

Lines changed: 266 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ WebGPU is a modern graphics API for the web that provides:
2626

2727
### Guix WebGPU Features
2828

29+
#### 3D Graphics
2930
-**Declarative 3D API**: Scene graph with meshes, cameras, and lights
3031
-**PBR Materials**: Physically-based rendering with metalness/roughness
3132
-**Built-in Geometries**: Box, sphere, plane primitives
@@ -39,6 +40,18 @@ WebGPU is a modern graphics API for the web that provides:
3940
- 🚧 **Textures**: Image and procedural textures (in progress)
4041
- 🚧 **Post-processing**: Effects and filters (planned)
4142

43+
#### 2D Charts
44+
-**Candlestick Charts**: OHLCV (Open-High-Low-Close-Volume) visualization
45+
-**Declarative API**: Component-based chart definition
46+
-**Axis System**: Time-based and numeric scales with automatic formatting
47+
-**Grid Lines**: Configurable grid lines for both axes
48+
-**GPU Acceleration**: All rendering happens on GPU for high performance
49+
-**Responsive Design**: Adapts to different canvas sizes
50+
- 🚧 **Line Charts**: Continuous data visualization (in progress)
51+
- 🚧 **Bar Charts**: Categorical data visualization (planned)
52+
- 🚧 **Area Charts**: Filled line charts (planned)
53+
- 🚧 **Multiple Series**: Overlay multiple data series (planned)
54+
4255
## Architecture
4356

4457
### Runtime Structure
@@ -443,10 +456,14 @@ renderer.Cleanup()
443456

444457
## Examples
445458

446-
### Rotating Cube
459+
### Rotating Cube (3D)
447460

448461
See [`examples/webgpu-cube/`](../examples/webgpu-cube/) for a complete rotating cube example with controls.
449462

463+
### Candlestick Chart (2D)
464+
465+
See [`examples/webgpu-chart/`](../examples/webgpu-chart/) for a complete 2D charting example with Bitcoin price data.
466+
450467
### Custom Animation
451468

452469
```go
@@ -484,6 +501,254 @@ for x := -2; x <= 2; x++ {
484501
}
485502
```
486503

504+
## 2D Charts
505+
506+
Guix provides GPU-accelerated 2D charting capabilities for high-performance data visualization.
507+
508+
### Chart Types
509+
510+
#### Candlestick Charts
511+
512+
Candlestick charts visualize OHLCV (Open-High-Low-Close-Volume) data, commonly used for financial markets:
513+
514+
```go
515+
// Define OHLCV data
516+
chartData := []chart.OHLCV{
517+
{
518+
Timestamp: 1701388800000, // Unix timestamp in milliseconds
519+
Open: 37500,
520+
High: 38200,
521+
Low: 37100,
522+
Close: 37800,
523+
Volume: 28500000000,
524+
},
525+
// ... more data points
526+
}
527+
528+
// Create chart with declarative syntax
529+
Chart(ChartBackground(0.08, 0.09, 0.12, 1.0)) {
530+
// X-Axis with time scale
531+
XAxis(
532+
AxisPosition("bottom"),
533+
TimeScale(true), // Format timestamps as dates
534+
GridLines(true), // Show vertical grid lines
535+
)
536+
537+
// Y-Axis with automatic formatting
538+
YAxis(
539+
AxisPosition("right"),
540+
GridLines(true), // Show horizontal grid lines
541+
)
542+
543+
// Candlestick series
544+
CandlestickSeries(
545+
ChartData(chartData),
546+
UpColor(0.18, 0.80, 0.44, 1.0), // Green for bullish (close > open)
547+
DownColor(0.91, 0.27, 0.38, 1.0), // Red for bearish (close < open)
548+
WickColor(0.6, 0.6, 0.65, 1.0), // Gray for wicks (high/low lines)
549+
BarWidth(0.8), // Width of candle body (0.0-1.0)
550+
)
551+
}
552+
```
553+
554+
### Chart Components
555+
556+
Charts are built from declarative components:
557+
558+
#### Chart Container
559+
560+
The root `Chart` component defines the overall chart configuration:
561+
562+
```go
563+
Chart(
564+
ChartBackground(r, g, b, a), // Background color (RGBA)
565+
) {
566+
// Child components (axes, series)
567+
}
568+
```
569+
570+
#### Axes
571+
572+
Define X and Y axes with position, scale type, and grid lines:
573+
574+
```go
575+
// X-Axis (horizontal)
576+
XAxis(
577+
AxisPosition("bottom"), // "bottom" or "top"
578+
TimeScale(true), // Time-based scale (converts timestamps)
579+
GridLines(true), // Show grid lines
580+
)
581+
582+
// Y-Axis (vertical)
583+
YAxis(
584+
AxisPosition("right"), // "left" or "right"
585+
GridLines(true), // Show grid lines
586+
)
587+
```
588+
589+
#### Series
590+
591+
Series components render the actual data:
592+
593+
```go
594+
// Candlestick series for OHLCV data
595+
CandlestickSeries(
596+
ChartData(data), // []chart.OHLCV data
597+
UpColor(r, g, b, a), // Color for bullish candles
598+
DownColor(r, g, b, a), // Color for bearish candles
599+
WickColor(r, g, b, a), // Color for wick lines
600+
BarWidth(width), // Width of candle bodies (0.0-1.0)
601+
)
602+
```
603+
604+
### Using Charts in Components
605+
606+
Integrate charts into Guix components:
607+
608+
```go
609+
func App() (Component) {
610+
chartData := GetBitcoinData()
611+
612+
Div(ID("app")) {
613+
H1 { "Bitcoin Price Chart" }
614+
615+
// Canvas element for chart rendering
616+
Canvas(
617+
ID("chart-canvas"),
618+
Width(1200),
619+
Height(700),
620+
) {
621+
// Embed chart using GPUChart
622+
GPUChart(NewBitcoinChart(chartData))
623+
}
624+
}
625+
}
626+
627+
// Define chart in separate component
628+
func BitcoinChart(data *ChartData) (Chart) {
629+
Chart(ChartBackground(0.08, 0.09, 0.12, 1.0)) {
630+
XAxis(AxisPosition("bottom"), TimeScale(true), GridLines(true))
631+
YAxis(AxisPosition("right"), GridLines(true))
632+
633+
CandlestickSeries(
634+
ChartData(data.Bitcoin),
635+
UpColor(0.18, 0.80, 0.44, 1.0),
636+
DownColor(0.91, 0.27, 0.38, 1.0),
637+
WickColor(0.6, 0.6, 0.65, 1.0),
638+
BarWidth(0.8),
639+
)
640+
}
641+
}
642+
```
643+
644+
### Chart Data Format
645+
646+
#### OHLCV Structure
647+
648+
```go
649+
type OHLCV struct {
650+
Timestamp int64 // Unix timestamp in milliseconds
651+
Open float64 // Opening price
652+
High float64 // Highest price in period
653+
Low float64 // Lowest price in period
654+
Close float64 // Closing price
655+
Volume float64 // Trading volume
656+
}
657+
```
658+
659+
#### Example Data
660+
661+
```go
662+
data := []chart.OHLCV{
663+
{
664+
Timestamp: 1701388800000, // Dec 1, 2024 00:00:00 UTC
665+
Open: 37500,
666+
High: 38200,
667+
Low: 37100,
668+
Close: 37800,
669+
Volume: 28500000000,
670+
},
671+
{
672+
Timestamp: 1701475200000, // Dec 2, 2024 00:00:00 UTC
673+
Open: 37800,
674+
High: 39100,
675+
Low: 37600,
676+
Close: 38900,
677+
Volume: 32100000000,
678+
},
679+
}
680+
```
681+
682+
### Chart Configuration
683+
684+
#### Colors
685+
686+
Colors are specified as RGBA floats (0.0-1.0):
687+
688+
```go
689+
// Predefined colors
690+
Green := Color(0.18, 0.80, 0.44, 1.0) // Bullish
691+
Red := Color(0.91, 0.27, 0.38, 1.0) // Bearish
692+
Gray := Color(0.60, 0.60, 0.65, 1.0) // Neutral
693+
Dark := Color(0.08, 0.09, 0.12, 1.0) // Background
694+
```
695+
696+
#### Bar Width
697+
698+
Control the width of candlestick bodies:
699+
700+
```go
701+
BarWidth(0.8) // 80% of available space (default)
702+
BarWidth(0.5) // 50% - thinner candles with more spacing
703+
BarWidth(1.0) // 100% - candles touch each other
704+
```
705+
706+
### Chart Rendering
707+
708+
Charts are automatically rendered by the WebGPU runtime:
709+
710+
1. **Data Processing**: Convert OHLCV data to GPU buffers
711+
2. **Axis Calculation**: Determine scales, ranges, and tick positions
712+
3. **GPU Upload**: Transfer vertex data to GPU memory
713+
4. **Shader Execution**: Execute WGSL shaders for rendering
714+
5. **Presentation**: Display result on canvas
715+
716+
### Chart Performance
717+
718+
GPU-accelerated charts provide excellent performance:
719+
720+
- **High Data Volume**: Handle thousands of candles smoothly
721+
- **Real-time Updates**: Update data without performance degradation
722+
- **Smooth Rendering**: 60 FPS rendering with requestAnimationFrame
723+
- **Memory Efficient**: Data stored in GPU buffers
724+
725+
### Future Chart Types
726+
727+
Upcoming chart types:
728+
729+
```go
730+
// Line chart (in progress)
731+
LineSeries(
732+
ChartData(data),
733+
LineColor(r, g, b, a),
734+
LineWidth(width),
735+
)
736+
737+
// Bar chart (planned)
738+
BarSeries(
739+
ChartData(data),
740+
BarColor(r, g, b, a),
741+
BarWidth(width),
742+
)
743+
744+
// Area chart (planned)
745+
AreaSeries(
746+
ChartData(data),
747+
FillColor(r, g, b, a),
748+
LineColor(r, g, b, a),
749+
)
750+
```
751+
487752
## Performance Tips
488753

489754
### 1. Minimize Buffer Updates

0 commit comments

Comments
 (0)