Skip to content

Commit 34837e6

Browse files
committed
ready to publish
1 parent f886791 commit 34837e6

File tree

2 files changed

+113
-13
lines changed

2 files changed

+113
-13
lines changed

README.md

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,42 @@
11
# JSON to SVG
22

3-
Generate SVG from JSON
3+
Generates SVG from JSON
44

5+
Installation: 'npm i json-to-svg'
56

67
```javascript
78

9+
import svgen from 'json-to-svg';
10+
811
import * as d3 from "d3";
912

10-
const { svgen } = require('json-to-svg')
13+
console.log(
14+
15+
svgen(d3,{"target":"histogram","color":"blue","barwidth":20,"intergap":5,"dataset":[25,67,45,19,89],"size":2,"animate":false})
16+
17+
)
18+
19+
```
20+
21+
## HTTP server
22+
23+
```javascript
24+
25+
import svgen from 'json-to-svg'
26+
27+
import * as d3 from "d3";
28+
29+
import { webpage, getHtml } from 'htwrite'
30+
31+
import http from 'http'
32+
33+
let xml = svgen(d3,{"target":"histogram","color":"blue","barwidth":20,"intergap":5,"dataset":[25,67,45,19,89],"size":2,"animate":false})
34+
35+
http.createServer(function (req, res) {
36+
res.write(webpage("demo",'',xml));
37+
res.end();
38+
}).listen(8080);
1139

12-
console.log( svgen(d3,{"target":"histogram","color":"blue","barwidth":20,"intergap":5,"dataset":[25,67,45,19,89],"size":2,"animate":true}) )
40+
console.log(`server started at http://localhost:8080`)
1341

1442
```

index.js

Lines changed: 82 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,25 +85,97 @@ const svgen = (d3,jsondata={})=>{
8585
}
8686
else if( jsondata?.target === 'histogram'){
8787

88+
let size = ( jsondata.hasOwnProperty('size') ) ? parseFloat(jsondata.size) : 1;
89+
8890
const dmax = d3.max(jsondata?.dataset);
8991

92+
let animate = ( jsondata.hasOwnProperty('animate') ) ? jsondata.animate : false;
93+
94+
if(animate){
95+
96+
function enterAnim(enter){
97+
98+
enter.append('rect')
99+
.attr('x', (d, i) => i * (jsondata?.intergap+jsondata?.barwidth) * size)
100+
.attr('width', jsondata?.barwidth*size)
101+
.attr("y", d =>(dmax - d)*size)
102+
.attr("height", d => d*size)
103+
.attr("class", 'bar')
104+
105+
106+
.call( rect =>
107+
rect.append('animate')
108+
.attr('attributeName', "height")
109+
.attr('values', (d)=> `0;1;${ d*size }` )
110+
.attr('keyTimes', (d,i)=> `0;${Math.trunc(1000*(1-(1/(3*i+3))))/1000};1` ) // ="0; 0.25; 0.5; 0.75; 1"
111+
.attr('dur', (d,i)=>`${i*2+2}s`)
112+
.attr('repeatcount', 1)
113+
114+
)
115+
116+
.call( rect =>
117+
rect.append('animate')
118+
.attr('attributeName', "y")
119+
.attr('values', (d)=> `${dmax*size};${dmax*size};${ (dmax - d)*size }` )
120+
.attr('keyTimes', (d,i)=> `0;${Math.trunc(1000*(1-(1/(3*i+3))))/1000};1` )
121+
.attr('dur', (d,i)=>`${i*2+2}s`)
122+
.attr('repeatcount', 1)
123+
)
124+
}
125+
126+
svg.append('g')
127+
.attr('fill', jsondata?.color)
128+
.attr('transform', `translate(${200/size},${200/size})`)
129+
.selectAll('rect')
130+
.data(jsondata?.dataset)
131+
.join(
132+
enter=>enterAnim(enter)
133+
)
134+
/* .selectAll('animate')
135+
.data([d => d], (h,i)=>{ return { "begin":i, "height":h} } ) //
136+
.join(
137+
enter=>enter.append('animate')
138+
.attr('attributeName', "height")
139+
.attr('values', (d)=> `0;${ d.height }` ) // `0;50` function(d){ return `0;${ d }` }
140+
.attr('begin', d => `2s` ) // `${d.begin}s` () =>{ indx++; `${ indx }s`; }
141+
.attr('dur', "1s")
142+
.attr('repeatcount', 1)
143+
) */
144+
}
145+
else{
146+
147+
svg.append('g')
148+
.attr('fill', jsondata?.color)
149+
.attr('transform', `translate(${200/size},${200/size})`)
150+
.selectAll('rect')
151+
.data(jsondata?.dataset)
152+
.join('rect')
153+
.attr('x', (d, i) => i * (jsondata?.intergap+jsondata?.barwidth) * size)
154+
.attr('width', jsondata?.barwidth*size)
155+
.attr("y", d =>(dmax - d)*size)
156+
.attr("height", d => d*size)
157+
}
158+
90159
svg.append('g')
91-
.attr('fill', jsondata?.color)
92-
.attr('transform', `translate(200,200)`)
93-
.selectAll('rect')
94-
.data(jsondata?.dataset)
95-
.join('rect')
96-
.attr('x', (d, i) => i * (jsondata?.intergap+jsondata?.barwidth))
97-
.attr('width', jsondata?.barwidth)
98-
.attr("y", d =>(dmax - d))
99-
.attr("height", d => d)
160+
.attr('fill', jsondata?.color)
161+
.attr('transform', `translate(${200/size},${200/size})`)
162+
.selectAll('text')
163+
.data(jsondata?.dataset)
164+
.join('text')
165+
.attr('x', (d, i) => i * (jsondata?.intergap+jsondata?.barwidth) * size)
166+
.attr("y", d =>(dmax - d)*size - 20)
167+
.style("font-size", 10+size*2)
168+
.style('font-family', 'sans-serif')
169+
.text(d => d)
100170

101171

102172
}
173+
103174

104175
console.log(svg.node().outerHTML)
105176

106177
return svg.node().outerHTML;
107178
}
108179

109-
module.exports = { svgen }
180+
181+
module.exports = svgen;

0 commit comments

Comments
 (0)