Skip to content

Commit 7490634

Browse files
committed
feat: add generators and auto color scale
1 parent 0be9dbf commit 7490634

8 files changed

Lines changed: 899 additions & 11 deletions

File tree

README.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,20 @@ Add to your Gemfile:
4242
gem "trackplot"
4343
```
4444

45+
Then run the install generator:
46+
47+
```bash
48+
bin/rails generate trackplot:install
49+
```
50+
51+
The generator auto-detects your JS setup (importmap vs jsbundling), installs the right packages, and adds `import "trackplot"` to `application.js`.
52+
53+
Pass `--stimulus` to also get a Stimulus controller with auto-refresh polling and export actions:
54+
55+
```bash
56+
bin/rails generate trackplot:install --stimulus
57+
```
58+
4559
### With importmap (default Rails 7+)
4660

4761
You're done. The engine auto-registers D3 from CDN and pins the trackplot module.
@@ -361,6 +375,38 @@ Custom theme (merges with defaults):
361375

362376
Theme properties: `colors`, `background`, `text_color`, `axis_color`, `grid_color`, `tooltip_bg`, `tooltip_text`, `tooltip_border`, `font`.
363377

378+
### Color Scales
379+
380+
Generate color palettes programmatically instead of hand-picking every color:
381+
382+
```ruby
383+
# Light-to-dark ramp from a single color
384+
Trackplot::ColorScale.sequential("#6366f1", count: 6)
385+
# => ["#d8d9fb", "#b1b2f7", "#8a8cf3", "#6366f1", "#3c3de0", "#2627a0"]
386+
387+
# Two-color diverging gradient (light midpoint)
388+
Trackplot::ColorScale.diverging("#ef4444", "#3b82f6", count: 7)
389+
390+
# Evenly-spaced hues preserving saturation and lightness
391+
Trackplot::ColorScale.categorical("#6366f1", count: 8)
392+
```
393+
394+
Use them as theme colors:
395+
396+
```erb
397+
<%= trackplot_chart @data, theme: { colors: Trackplot::ColorScale.sequential("#6366f1") } do |c| %>
398+
...
399+
<% end %>
400+
```
401+
402+
| Method | Description |
403+
|--------|-------------|
404+
| `.sequential(hex, count: 8)` | Light-to-dark palette varying lightness from 0.90 to 0.25 |
405+
| `.diverging(hex1, hex2, count: 8)` | Two-color gradient with light midpoint |
406+
| `.categorical(hex, count: 8)` | Evenly-spaced hues (360°/count steps) |
407+
408+
All methods accept standard `#RGB` or `#RRGGBB` hex strings. `count: 0` returns `[]`, `count: 1` returns a single color, and invalid hex raises `ArgumentError`.
409+
364410
## Accessibility
365411

366412
Charts support ARIA attributes for screen readers:
@@ -491,6 +537,32 @@ document.addEventListener("trackplot:render", function(e) {
491537
})
492538
```
493539

540+
## Stimulus Controller
541+
542+
Run `bin/rails generate trackplot:install --stimulus` to get a controller with auto-refresh polling and export actions:
543+
544+
```html
545+
<div data-controller="trackplot"
546+
data-trackplot-url-value="/api/chart_data.json"
547+
data-trackplot-interval-value="5000">
548+
<%= trackplot_chart @data do |c| %>
549+
<% c.line :revenue, curve: true %>
550+
<% c.axis :x, data_key: :month %>
551+
<% c.axis :y %>
552+
<% end %>
553+
554+
<button data-action="trackplot#exportPng">Download PNG</button>
555+
<button data-action="trackplot#exportSvg">Download SVG</button>
556+
</div>
557+
```
558+
559+
| Value | Type | Description |
560+
|-------|------|-------------|
561+
| `url` | String | JSON endpoint to poll for fresh data |
562+
| `interval` | Number | Polling interval in ms (0 = disabled) |
563+
564+
Actions: `exportPng`, `exportSvg`, `refresh` (manual trigger).
565+
494566
## ViewComponent / Phlex Support
495567

496568
If you use [ViewComponent](https://viewcomponent.org/) or [Phlex](https://www.phlex.fun/), Trackplot provides optional integrations.
@@ -536,6 +608,25 @@ Pass options directly to `trackplot_chart`:
536608
| `description:` | `nil` | Accessibility description (requires `title:`) |
537609
| `empty_message:` | `"No data available"` | Message shown when data is empty |
538610

611+
## TypeScript
612+
613+
Trackplot ships TypeScript declarations for the npm package. Type-checked `querySelector` narrows to the correct element:
614+
615+
```typescript
616+
const chart = document.querySelector("trackplot-chart")
617+
// => TrackplotElement | null
618+
619+
chart?.addEventListener("trackplot:click", (e) => {
620+
e.detail.chartType // string
621+
e.detail.dataKey // string
622+
e.detail.value // unknown
623+
})
624+
625+
chart?.exportPNG(2, "report.png") // Promise<Blob | null>
626+
```
627+
628+
All config interfaces are exported: `ChartConfig`, `LineConfig`, `BarConfig`, `ThemeConfig`, `SparklineConfig`, etc.
629+
539630
## Development
540631

541632
Run the test suite:

0 commit comments

Comments
 (0)