|
1 | 1 | # react-native-d3-chart |
2 | 2 |
|
3 | | -Create performant charts with zooming, panning and localization |
| 3 | +Create performant charts with zooming, panning and localization using D3.js in a WebView. Perfect for time-series data visualization with smooth interactions. |
| 4 | + |
| 5 | +## Preview |
| 6 | + |
| 7 | +https://github.com/user-attachments/assets/e2a03e9f-a29c-465c-b966-e729e1f6565f |
| 8 | + |
| 9 | +_Interactive chart with zoom, pan, and multi-dataset support_ |
| 10 | + |
| 11 | +## Features |
| 12 | + |
| 13 | +- 📊 **Multi-dataset support** - Display multiple data series on the same chart |
| 14 | +- 🔍 **Zoom and pan** - Interactive zooming and panning with smooth animations |
| 15 | +- 🌍 **Localization** - Built-in support for different locales and custom calendar strings |
| 16 | +- 🎨 **Customizable styling** - Full control over colors, margins, and appearance |
| 17 | +- 📱 **Cross-platform** - Works seamlessly on iOS and Android |
| 18 | +- ⚡ **High performance** - Leverages D3.js for smooth rendering of large datasets |
| 19 | +- 🔧 **Zero configuration** - Assets are automatically bundled during installation |
4 | 20 |
|
5 | 21 | ## Installation |
6 | 22 |
|
7 | 23 | ```sh |
8 | 24 | npm install react-native-d3-chart |
| 25 | +# or |
| 26 | +yarn add react-native-d3-chart |
9 | 27 | ``` |
10 | 28 |
|
11 | | -## Usage |
| 29 | +**Note**: This library requires `react-native-webview`. If you don't have it installed: |
12 | 30 |
|
13 | | -TODO |
| 31 | +```sh |
| 32 | +npm install react-native-webview |
| 33 | +# or |
| 34 | +yarn add react-native-webview |
| 35 | +``` |
| 36 | + |
| 37 | +## Quick Start |
14 | 38 |
|
15 | | -```js |
| 39 | +```tsx |
| 40 | +import React, { useState, useMemo } from 'react'; |
| 41 | +import { View } from 'react-native'; |
16 | 42 | import Chart from 'react-native-d3-chart'; |
| 43 | + |
| 44 | +export default function App() { |
| 45 | + const [width, setWidth] = useState(0); |
| 46 | + const height = width * 0.6; // 16:10 aspect ratio |
| 47 | + |
| 48 | + // Generate some sample data |
| 49 | + const datasets = useMemo( |
| 50 | + () => [ |
| 51 | + { |
| 52 | + measurementName: 'Temperature', |
| 53 | + color: '#e66', |
| 54 | + unit: '°C', |
| 55 | + decimals: 1, |
| 56 | + points: [ |
| 57 | + { timestamp: Date.now() - 3600000, value: 22.5 }, |
| 58 | + { timestamp: Date.now() - 1800000, value: 23.1 }, |
| 59 | + { timestamp: Date.now(), value: 24.3 }, |
| 60 | + ], |
| 61 | + }, |
| 62 | + ], |
| 63 | + [] |
| 64 | + ); |
| 65 | + |
| 66 | + const timeDomain = useMemo( |
| 67 | + () => ({ |
| 68 | + type: 'hour', |
| 69 | + start: Date.now() - 3600000, // 1 hour ago |
| 70 | + end: Date.now(), |
| 71 | + }), |
| 72 | + [] |
| 73 | + ); |
| 74 | + |
| 75 | + const colors = { |
| 76 | + background: '#fff', |
| 77 | + highlightLine: '#000', |
| 78 | + border: '#555', |
| 79 | + cursorStroke: '#0ff', |
| 80 | + highlightLabel: '#000', |
| 81 | + highlightTime: '#444', |
| 82 | + }; |
| 83 | + |
| 84 | + return ( |
| 85 | + <View |
| 86 | + style={{ flex: 1, padding: 20 }} |
| 87 | + onLayout={(e) => setWidth(e.nativeEvent.layout.width - 40)} |
| 88 | + > |
| 89 | + <Chart |
| 90 | + width={width} |
| 91 | + height={height} |
| 92 | + colors={colors} |
| 93 | + datasets={datasets} |
| 94 | + timeDomain={timeDomain} |
| 95 | + noDataString="No data available" |
| 96 | + /> |
| 97 | + </View> |
| 98 | + ); |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +> 💡 **Want to see more?** Check out the [complete example app](./example/src/App.tsx) for advanced usage with multiple datasets, time domain switching, and interactive controls. |
| 103 | +
|
| 104 | +## API Reference |
| 105 | + |
| 106 | +### Chart Props |
| 107 | + |
| 108 | +| Prop | Type | Required | Description | |
| 109 | +| ------------------ | ----------------- | -------- | ---------------------------------------------------------------------------------- | |
| 110 | +| `width` | `number` | ✅ | Chart width in pixels | |
| 111 | +| `height` | `number` | ✅ | Chart height in pixels | |
| 112 | +| `datasets` | `Dataset[]` | ✅ | Array of data series to display | |
| 113 | +| `colors` | `ChartColors` | ✅ | Color configuration for chart elements | |
| 114 | +| `timeDomain` | `TimeDomain` | ✅ | Control intial zoom level / scale of X-axis, doesn't have to fit the whole dataset | |
| 115 | +| `noDataString` | `string` | ✅ | Message to show when no data is available | |
| 116 | +| `zoomEnabled` | `boolean` | ❌ | Enable zoom guesture | |
| 117 | +| `locale` | `string` | ❌ | Locale for date/time formatting (default: 'en') | |
| 118 | +| `marginHorizontal` | `number` | ❌ | Horizontal margin in pixels | |
| 119 | +| `calendarStrings` | `CalendarStrings` | ❌ | Custom calendar strings for localization | |
| 120 | +| `onZoomStarted` | `() => void` | ❌ | Callback when zoom interaction starts | |
| 121 | +| `onZoomEnded` | `() => void` | ❌ | Callback when zoom interaction ends | |
| 122 | + |
| 123 | +### Types |
| 124 | + |
| 125 | +#### Dataset |
| 126 | + |
| 127 | +```typescript |
| 128 | +type Dataset = { |
| 129 | + measurementName: string; // Display name for this data series |
| 130 | + color: string; // Hex color for the line and labels |
| 131 | + points: Point[]; // Array of data points |
| 132 | + unit: string; // Unit symbol (e.g., '°C', 'kg', 'm/s') |
| 133 | + decimals: number; // Number of decimal places to show |
| 134 | + minDeltaY?: number; // Minimum Y-axis change to show, limit Y-zoom |
| 135 | + decimalSeparator?: '.' | ','; // Decimal separator |
| 136 | + domain?: { |
| 137 | + // Custom Y-axis range |
| 138 | + bottom: number; |
| 139 | + top: number; |
| 140 | + }; |
| 141 | +}; |
17 | 142 | ``` |
18 | 143 |
|
| 144 | +#### Point |
| 145 | + |
| 146 | +```typescript |
| 147 | +type Point = { |
| 148 | + timestamp: number; // Unix timestamp in milliseconds |
| 149 | + value: number | null; // Data value (null for gaps) |
| 150 | +}; |
| 151 | +``` |
| 152 | + |
| 153 | +#### TimeDomain |
| 154 | + |
| 155 | +```typescript |
| 156 | +type TimeDomain = { |
| 157 | + type: string; // Domain type (e.g., 'hour', 'day', 'week') |
| 158 | + start: number; // Start timestamp (ms) |
| 159 | + end: number; // End timestamp (ms) |
| 160 | +}; |
| 161 | +``` |
| 162 | + |
| 163 | +#### ChartColors |
| 164 | + |
| 165 | +```typescript |
| 166 | +type ChartColors = { |
| 167 | + background: string; // Chart background color |
| 168 | + highlightLine: string; // Crosshair line color |
| 169 | + border: string; // Chart border color |
| 170 | + highlightLabel: string; // Value label text color |
| 171 | + highlightTime: string; // Time label text color |
| 172 | + cursorStroke: string; // Cursor/crosshair circle color |
| 173 | +}; |
| 174 | +``` |
| 175 | + |
| 176 | +#### CalendarStrings |
| 177 | + |
| 178 | +```typescript |
| 179 | +type CalendarStrings = { |
| 180 | + days: string[]; // Full day names (Sunday first) |
| 181 | + shortDays: string[]; // Short day names (Sun first) |
| 182 | + months: string[]; // Full month names (January first) |
| 183 | + shortMonths: string[]; // Short month names (Jan first) |
| 184 | +}; |
| 185 | +``` |
| 186 | + |
| 187 | +## Advanced Usage |
| 188 | + |
| 189 | +### Multiple Datasets |
| 190 | + |
| 191 | +```tsx |
| 192 | +const datasets = [ |
| 193 | + { |
| 194 | + measurementName: 'Temperature', |
| 195 | + color: '#e66', |
| 196 | + unit: '°C', |
| 197 | + decimals: 1, |
| 198 | + points: temperatureData, |
| 199 | + }, |
| 200 | + { |
| 201 | + measurementName: 'Humidity', |
| 202 | + color: '#66e', |
| 203 | + unit: '%', |
| 204 | + decimals: 0, |
| 205 | + points: humidityData, |
| 206 | + }, |
| 207 | +]; |
| 208 | +``` |
| 209 | + |
| 210 | +### Zoom Callbacks |
| 211 | + |
| 212 | +```tsx |
| 213 | +<Chart |
| 214 | + // ... other props |
| 215 | + zoomEnabled |
| 216 | + onZoomStarted={() => console.log('Zoom started')} |
| 217 | + onZoomEnded={() => console.log('Zoom ended')} |
| 218 | +/> |
| 219 | +``` |
| 220 | + |
| 221 | +## Requirements |
| 222 | + |
| 223 | +- React Native >= 0.60 |
| 224 | +- react-native-webview >= 11.0.0 |
| 225 | +- iOS 11.0+ / Android API 21+ |
| 226 | + |
19 | 227 | ## Contributing |
20 | 228 |
|
21 | 229 | See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow. |
|
0 commit comments