|
| 1 | + |
| 2 | +//import * as d3 from "d3"; |
| 3 | + |
| 4 | +const jsdom = require("jsdom"); |
| 5 | +const { JSDOM } = jsdom; |
| 6 | + |
| 7 | +const { document } = (new JSDOM(`<!DOCTYPE html><html><body></body></html>`)).window; |
| 8 | + |
| 9 | + |
| 10 | +const svgen = (d3,jsondata={})=>{ |
| 11 | + |
| 12 | + let svgwidth = 500 |
| 13 | + let svgheight = 500 |
| 14 | + |
| 15 | + let svg = d3.select(document.body) |
| 16 | + .append('svg') |
| 17 | + .attr('width', svgwidth) |
| 18 | + .attr('height', svgheight); |
| 19 | + |
| 20 | + if( jsondata.hasOwnProperty('bgcolor') ){ |
| 21 | + |
| 22 | + svg.append('rect') |
| 23 | + .attr('width', svgwidth) |
| 24 | + .attr('height', svgheight) |
| 25 | + .attr('fill', jsondata.bgcolor); |
| 26 | + } |
| 27 | + |
| 28 | + |
| 29 | + if( jsondata?.target === 'demo'){ |
| 30 | + |
| 31 | + svg.append('circle') |
| 32 | + .attr('cx', 250) |
| 33 | + .attr('cy', 150) |
| 34 | + .attr('r', 55) |
| 35 | + .attr('fill', 'blue'); |
| 36 | + } |
| 37 | + else if( jsondata?.target === 'text'){ |
| 38 | + |
| 39 | + svg.append('text') |
| 40 | + .attr("y", 150) |
| 41 | + .attr("x", 150) |
| 42 | + .attr("font-size", 20) |
| 43 | + .attr("fill", "red") |
| 44 | + .text(jsondata?.content) |
| 45 | + |
| 46 | + } |
| 47 | + else if( jsondata?.target === 'circles'){ |
| 48 | + |
| 49 | + svg.append('g') |
| 50 | + //.attr('fill', origcolor) |
| 51 | + //.attr('transform', `translate(${200 * zoom}, ${200 * zoom})`) |
| 52 | + .selectAll('circle') |
| 53 | + .data(jsondata?.dataset) |
| 54 | + .join('circle') |
| 55 | + .attr('cx', d => d?.cx) |
| 56 | + .attr('cy', d => d?.cy) |
| 57 | + .attr('r', d => d?.r) |
| 58 | + .attr('fill', d => d?.color) |
| 59 | + |
| 60 | + } |
| 61 | + else if( jsondata?.target === 'table'){ |
| 62 | + |
| 63 | + let toptext = Object.keys(jsondata?.dataset[0]) |
| 64 | + |
| 65 | + /* svg.append('table') |
| 66 | + .append("thead") |
| 67 | + .join("tr") |
| 68 | + .selectAll("th") |
| 69 | + .data(toptext) |
| 70 | + .join("th") |
| 71 | + .text(d => d) |
| 72 | + .style("background-color", "#aaa") |
| 73 | + .style("color", "#fff") |
| 74 | + .append("tbody") |
| 75 | + .selectAll("tr") |
| 76 | + .data(jsondata?.dataset) |
| 77 | + .join("tr") |
| 78 | + .selectAll("td") |
| 79 | + .data(row => Object.values(row)) |
| 80 | + .join("td") |
| 81 | + .text(d => d); // d.value */ |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | + } |
| 86 | + else if( jsondata?.target === 'histogram'){ |
| 87 | + |
| 88 | + const dmax = d3.max(jsondata?.dataset); |
| 89 | + |
| 90 | + 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) |
| 100 | + |
| 101 | + |
| 102 | + } |
| 103 | + |
| 104 | + console.log(svg.node().outerHTML) |
| 105 | + |
| 106 | + return svg.node().outerHTML; |
| 107 | +} |
| 108 | + |
| 109 | +module.exports = { svgen } |
0 commit comments