Skip to content

Commit b5ac494

Browse files
committed
Update README with documentation for new features
- Document gap support for null/NaN/Infinity values - Add section for fillInvert style option (PR borisyankov#113) - Add section for SparklinesInteractiveLayer component (PR borisyankov#119) - Standardize all code examples with proper JSX syntax and function wrappers - Improve formatting with bold property names
1 parent 87945f8 commit b5ac494

File tree

1 file changed

+123
-25
lines changed

1 file changed

+123
-25
lines changed

README.md

Lines changed: 123 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,19 @@ function MyComponent() {
4141

4242
Sparklines component is a container with the following properties:
4343

44-
data - the data set used to build the sparkline
44+
**data** - the data set used to build the sparkline. Invalid values (null, NaN, Infinity, undefined) are supported and will create gaps in the visualization.
4545

46-
limit - optional, how many data points to display at once
46+
**limit** - optional, how many data points to display at once
4747

48-
width, height - dimensions of the generated sparkline in the SVG viewbox. This will be automatically scaled (i.e. responsive) inside the parent container by default.
48+
**width, height** - dimensions of the generated sparkline in the SVG viewbox. This will be automatically scaled (i.e. responsive) inside the parent container by default.
4949

50-
svgWidth, svgHeight - If you want absolute dimensions instead of a responsive component set these attributes.
50+
**svgWidth, svgHeight** - If you want absolute dimensions instead of a responsive component set these attributes.
5151

52-
[preserveAspectRatio](https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/preserveAspectRatio) - default: 'none', set this to modify how the sparkline should scale
52+
**[preserveAspectRatio](https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/preserveAspectRatio)** - default: 'none', set this to modify how the sparkline should scale
5353

54-
margin - optional, offset the chart
54+
**margin** - optional, offset the chart
5555

56-
min, max - optional, bound the chart
56+
**min, max** - optional, bound the chart
5757

5858

5959
#### Basic Sparkline
@@ -95,42 +95,140 @@ function MyComponent() {
9595
![](http://borisyankov.github.io/react-sparklines/img/spots.png)
9696

9797

98-
```
98+
```jsx
9999
import React from 'react';
100100
import { Sparklines, SparklinesLine, SparklinesSpots } from 'react-sparklines';
101-
...
102-
<Sparklines data={sampleData}>
103-
<SparklinesLine style={{ fill: "none" }} />
104-
<SparklinesSpots />
105-
</Sparklines>
101+
102+
function MyComponent() {
103+
const sampleData = [5, 10, 5, 20, 8, 15];
104+
return (
105+
<Sparklines data={sampleData}>
106+
<SparklinesLine style={{ fill: "none" }} />
107+
<SparklinesSpots />
108+
</Sparklines>
109+
);
110+
}
106111
```
107112

108113
#### Reference Line
109114

110115
![](http://borisyankov.github.io/react-sparklines/img/referenceline.png)
111116

112117

113-
```
118+
```jsx
114119
import React from 'react';
115120
import { Sparklines, SparklinesLine, SparklinesReferenceLine } from 'react-sparklines';
116-
...
117-
<Sparklines data={sampleData}>
118-
<SparklinesLine />
119-
<SparklinesReferenceLine type="mean" />
120-
</Sparklines>
121+
122+
function MyComponent() {
123+
const sampleData = [5, 10, 5, 20, 8, 15];
124+
return (
125+
<Sparklines data={sampleData}>
126+
<SparklinesLine />
127+
<SparklinesReferenceLine type="mean" />
128+
</Sparklines>
129+
);
130+
}
121131
```
122132

123133
#### Normal Band
124134

125135
![](http://borisyankov.github.io/react-sparklines/img/normalband.png)
126136

127137

128-
```
138+
```jsx
129139
import React from 'react';
130140
import { Sparklines, SparklinesLine, SparklinesNormalBand } from 'react-sparklines';
131-
...
132-
<Sparklines data={sampleData}>
133-
<SparklinesLine style={{ fill: "none" }}/>
134-
<SparklinesNormalBand />
135-
</Sparklines>
141+
142+
function MyComponent() {
143+
const sampleData = [5, 10, 5, 20, 8, 15];
144+
return (
145+
<Sparklines data={sampleData}>
146+
<SparklinesLine style={{ fill: "none" }}/>
147+
<SparklinesNormalBand />
148+
</Sparklines>
149+
);
150+
}
151+
```
152+
153+
#### Inverted Fill
154+
155+
You can invert the fill direction for line charts using the `fillInvert` style option:
156+
157+
```jsx
158+
import React from 'react';
159+
import { Sparklines, SparklinesLine } from 'react-sparklines';
160+
161+
function MyComponent() {
162+
return (
163+
<Sparklines data={[5, 10, 5, 20, 8, 15]}>
164+
<SparklinesLine style={{ fillInvert: true }} color="blue" />
165+
</Sparklines>
166+
);
167+
}
168+
```
169+
170+
#### Handling Gaps in Data
171+
172+
Invalid values (null, NaN, Infinity, undefined) in your data will automatically create visual gaps in the sparkline:
173+
174+
```jsx
175+
import React from 'react';
176+
import { Sparklines, SparklinesLine } from 'react-sparklines';
177+
178+
function MyComponent() {
179+
// Data with gaps
180+
const dataWithGaps = [5, 10, null, 20, NaN, 15, 8];
181+
182+
return (
183+
<Sparklines data={dataWithGaps}>
184+
<SparklinesLine color="blue" />
185+
</Sparklines>
186+
);
187+
}
136188
```
189+
190+
This works with `SparklinesLine`, `SparklinesCurve`, and `SparklinesBars` components.
191+
192+
#### Interactive Layer
193+
194+
Add interactive hover and click functionality to your sparklines:
195+
196+
```jsx
197+
import React, { useState } from 'react';
198+
import { Sparklines, SparklinesLine, SparklinesInteractiveLayer } from 'react-sparklines';
199+
200+
function MyComponent() {
201+
const [activePoint, setActivePoint] = useState(null);
202+
const data = [5, 10, 5, 20, 8, 15];
203+
204+
const handleMouseMove = (dataValue, point, index, event) => {
205+
setActivePoint({ value: dataValue, index });
206+
};
207+
208+
const handleMouseLeave = () => {
209+
setActivePoint(null);
210+
};
211+
212+
const handleClick = (dataValue, point, index, event) => {
213+
console.log('Clicked point:', dataValue, 'at index:', index);
214+
};
215+
216+
return (
217+
<div>
218+
<Sparklines data={data}>
219+
<SparklinesLine color="blue" />
220+
<SparklinesInteractiveLayer
221+
onMouseMove={handleMouseMove}
222+
onMouseLeave={handleMouseLeave}
223+
onClick={handleClick}
224+
/>
225+
</Sparklines>
226+
{activePoint && (
227+
<div>Value: {activePoint.value}, Index: {activePoint.index}</div>
228+
)}
229+
</div>
230+
);
231+
}
232+
```
233+
234+
The interactive layer shows a red circle and dashed line at the active point position.

0 commit comments

Comments
 (0)