Skip to content

Commit 9ca2b6a

Browse files
committed
Enable zoom, improve example
1 parent 7eac32e commit 9ca2b6a

3 files changed

Lines changed: 69 additions & 13 deletions

File tree

example/src/App.tsx

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,62 @@ const chartColors: ChartProps['colors'] = {
1212
border: '#555',
1313
cursorStroke: '#0ff',
1414
highlightLabel: '#000',
15-
highlightTime: '#ff0',
15+
highlightTime: '#444',
16+
};
17+
18+
// Generate data points every minute from a month ago to now
19+
const generateDataPoints = ({
20+
startingValue = 400,
21+
minimum = 0,
22+
radomFactor = 20,
23+
} = {}) => {
24+
const points = [];
25+
const now = Date.now();
26+
const monthAgo = now - 30 * 24 * 60 * 60 * 1000; // 30 days ago
27+
let value = startingValue;
28+
29+
for (let timestamp = monthAgo; timestamp <= now; timestamp += 60 * 1000) {
30+
const randomVariation = (Math.random() - 0.5) * radomFactor;
31+
value += randomVariation;
32+
33+
// randomVariation was negative and value went below minimum
34+
if (value < minimum) {
35+
// invert direction to keep above minimum
36+
value -= 2 * randomVariation;
37+
}
38+
39+
points.push({ timestamp, value });
40+
}
41+
42+
return points;
1643
};
1744

1845
const datasets: Dataset[] = [
1946
{
20-
unit: 'units',
21-
points: [],
47+
unit: 'kg',
48+
points: generateDataPoints(),
2249
decimals: 0,
2350
color: '#e66',
2451
measurementName: 'Red',
2552
},
53+
{
54+
unit: 'cm',
55+
points: generateDataPoints({
56+
startingValue: 160,
57+
minimum: 50,
58+
radomFactor: 6,
59+
}),
60+
decimals: 0,
61+
color: '#66e',
62+
measurementName: 'Blue',
63+
},
64+
{
65+
unit: 'kg',
66+
points: generateDataPoints(),
67+
decimals: 0,
68+
color: '#6e6',
69+
measurementName: 'Green',
70+
},
2671
];
2772

2873
export default function App() {
@@ -56,23 +101,23 @@ export default function App() {
56101
onLayout={(e) => setWidth(e.nativeEvent.layout.width)}
57102
>
58103
<Chart
104+
zoomEnabled
59105
width={width}
60106
height={height}
61107
datasets={datasets}
62108
colors={chartColors}
63109
timeDomain={timeDomain}
64110
marginHorizontal={PADDING}
111+
noDataString="No data available"
65112
/>
66-
<View style={{ height: 10 }} />
113+
<View style={styles.spacer} />
67114
<View style={styles.timeDomainRow}>
68115
{TIME_DOMAIN_TYPES.map((type) => (
69116
<TouchableOpacity
70117
key={type}
71118
style={[
72119
styles.timeDomainItem,
73-
type === timeDomainType && {
74-
backgroundColor: '#f0f',
75-
},
120+
type === timeDomainType && styles.timeDomainItemActive,
76121
]}
77122
onPress={() => setTimeDomainType(type)}
78123
>
@@ -98,6 +143,9 @@ const styles = StyleSheet.create({
98143
paddingVertical: PADDING,
99144
backgroundColor: '#fff',
100145
},
146+
spacer: {
147+
height: 10,
148+
},
101149
timeDomainRow: {
102150
flexDirection: 'row',
103151
paddingHorizontal: PADDING,
@@ -109,6 +157,9 @@ const styles = StyleSheet.create({
109157
justifyContent: 'center',
110158
alignItems: 'center',
111159
},
160+
timeDomainItemActive: {
161+
backgroundColor: '#b22',
162+
},
112163
timeDomainItemText: {
113164
fontSize: 13,
114165
},

src/Chart.tsx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export default function Chart({
1919
height,
2020
datasets,
2121
timeDomain,
22+
zoomEnabled,
2223
noDataString,
2324
calendarStrings,
2425
locale = 'en',
@@ -38,7 +39,6 @@ export default function Chart({
3839
}
3940

4041
const props: HtmlProps = {
41-
zoomEnabled: false,
4242
width,
4343
height,
4444
locale,
@@ -47,6 +47,7 @@ export default function Chart({
4747
noDataString,
4848
marginHorizontal,
4949
calendar: calendarStrings,
50+
zoomEnabled: !!zoomEnabled,
5051
colors: chartColors,
5152
};
5253
currentTimeDomain.current = timeDomain;
@@ -73,6 +74,7 @@ export default function Chart({
7374
datasets,
7475
timeDomain,
7576
chartColors,
77+
zoomEnabled,
7678
noDataString,
7779
calendarStrings,
7880
marginHorizontal,
@@ -101,11 +103,13 @@ export default function Chart({
101103
);
102104

103105
const source = useMemo(() => {
104-
const baseUrl = Platform.select({
105-
ios: 'assets',
106-
android: 'file:///android_asset',
107-
});
108-
return { uri: `${baseUrl}/chart.html` };
106+
// Assets are copied to platform-specific locations
107+
const uri = Platform.select({
108+
ios: 'react-native-d3-chart-chart.html', // Direct file reference in iOS bundle
109+
android: 'file:///android_asset/react-native-d3-chart/chart.html',
110+
}) as string;
111+
112+
return { uri };
109113
}, []);
110114

111115
const textZoom = Math.min(MAX_TEXT_ZOOM, PixelRatio.getFontScale() * 100);

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export type ChartProps = {
3535
noDataString: string;
3636
timeDomain: TimeDomain;
3737
locale?: string;
38+
zoomEnabled?: boolean;
3839
marginHorizontal?: number;
3940
calendarStrings?: CalendarStrings;
4041
onZoomEnded?: () => void;

0 commit comments

Comments
 (0)