Skip to content

Commit 6ada384

Browse files
committed
Made sparkline component responsive by default for #49
1 parent f9cd645 commit 6ada384

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ data - the data set used to build the sparkline
4141

4242
limit - optional, how many data points to display at once
4343

44-
width, height - dimensions of the component
44+
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.
45+
46+
svgWidth, svgHeight - If you want absolute dimensions instead of a responsive component set these attributes.
47+
48+
[preserveAspectRatio](https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/preserveAspectRatio) - default: 'none', set this to modify how the sparkline should scale
4549

4650
margin - optional, offset the chart
4751

demo/demo.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ const RealWorld1 = () =>
249249
</Sparklines>
250250

251251
const RealWorld2 = () =>
252-
<Sparklines data={sampleData100} width={200}>
252+
<Sparklines data={sampleData100} svgWidth={200}>
253253
<SparklinesLine style={{ stroke: "#2991c8", fill: "none"}} />
254254
<SparklinesSpots />
255255
<SparklinesNormalBand style={{ fill: "#2991c8", fillOpacity: .1 }} />

demo/index.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
}
6868
.row > div {
6969
flex: 1;
70+
margin-right: 5px;
7071
}
7172
.row > pre {
7273
flex: 2;
@@ -347,7 +348,7 @@ <h2>Real world examples</h2>
347348
<div class="row">
348349
<div id="realworld2"></div>
349350
<pre class="prettyprint"><xmp>
350-
<Sparklines data={sampleData100} width={200}>
351+
<Sparklines data={sampleData100} svgWidth={200}>
351352
<SparklinesLine style={{ stroke: "#2991c8", fill: "none"}} />
352353
<SparklinesSpots />
353354
<SparklinesNormalBand style={{ fill: "#2991c8", fillOpacity: .1 }} />

src/Sparklines.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ class Sparklines extends React.Component {
1414
limit: React.PropTypes.number,
1515
width: React.PropTypes.number,
1616
height: React.PropTypes.number,
17+
svgWidth: React.PropTypes.number,
18+
svgHeight: React.PropTypes.number,
19+
preserveAspectRatio: React.PropTypes.string,
1720
margin: React.PropTypes.number,
1821
style: React.PropTypes.object,
1922
min: React.PropTypes.number,
@@ -24,6 +27,8 @@ class Sparklines extends React.Component {
2427
data: [],
2528
width: 240,
2629
height: 60,
30+
//Scale the graphic content of the given element non-uniformly if necessary such that the element's bounding box exactly matches the viewport rectangle.
31+
preserveAspectRatio: 'none', //https://www.w3.org/TR/SVG/coords.html#PreserveAspectRatioAttribute
2732
margin: 2
2833
};
2934

@@ -34,6 +39,9 @@ class Sparklines extends React.Component {
3439
shouldComponentUpdate(nextProps) {
3540
return nextProps.width != this.props.width ||
3641
nextProps.height != this.props.height ||
42+
nextProps.svgWidth != this.props.svgWidth ||
43+
nextProps.svgHeight != this.props.svgHeight ||
44+
nextProps.preserveAspectRatio != this.props.preserveAspectRatio ||
3745
nextProps.margin != this.props.margin ||
3846
nextProps.min != this.props.min ||
3947
nextProps.max != this.props.max ||
@@ -43,14 +51,18 @@ class Sparklines extends React.Component {
4351
}
4452

4553
render() {
46-
const { data, limit, width, height, margin, style, max, min } = this.props;
54+
const { data, limit, width, height, svgWidth, svgHeight, preserveAspectRatio, margin, style, max, min } = this.props;
4755

4856
if (data.length === 0) return null;
4957

5058
const points = DataProcessor.dataToPoints(data, limit, width, height, margin, max, min);
5159

60+
const svgOpts = { style: style, viewBox: `0 0 ${width} ${height}`, preserveAspectRatio: preserveAspectRatio };
61+
if( svgWidth > 0 ) svgOpts.width = svgWidth;
62+
if( svgHeight > 0 ) svgOpts.height = svgHeight;
63+
5264
return (
53-
<svg width={width} height={height} style={style} viewBox={`0 0 ${width} ${height}`}>
65+
<svg {...svgOpts} >
5466
{
5567
React.Children.map(this.props.children, function(child) {
5668
return React.cloneElement(child, { points, width, height, margin });

0 commit comments

Comments
 (0)