Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use D3js on server side to generate SVG directly?

I have used the following tutorial of D3js to generate my preferred chart. but I had to open HTML file to chart generated instead it should be part of the automated process to generate SVG file on server-side instead of front-end.

Tutorial : http://bl.ocks.org/nbremer/21746a9668ffdf6d8242

Result is an array of test data. in a real application, it was generated by several processes the final step is to make a sub-total and from sub-total generate SVG output and send it to a specific person.

<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    <title>Smoothed D3.js Radar Chart</title>

    <script src="./d3.js" charset="utf-8"></script>

    <style>
        body {
            font-family: 'Open Sans', sans-serif;
            font-size: 11px;
            font-weight: 300;
            fill: #242424;
            text-align: center;
            text-shadow: 0 1px 0 #fff, 1px 0 0 #fff, -1px 0 0 #fff, 0 -1px 0 #fff;
            cursor: default;
        }

        .legend {
            font-family: 'Raleway', sans-serif;
            fill: #333333;
        }

        .tooltip {
            fill: #333333;
        }
    </style>

</head>

<body>

    <div class="radarChart"></div>
    <a id="download" href="#">Download SVG</a>
    <script src="radarChart.js"></script>
    <script>

        const result = [1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 0, 2, 1, 1, 2, 0, 0, 2, 1, 1, 2, 0, 0, 2, 1, 1, 2, 0, 0, 2, 1, 1, 2, 2, 0, 0];

        var final = sort(result);

        var margin = {
                top: 100,
                right: 100,
                bottom: 100,
                left: 100
            },
            width = Math.min(700, window.innerWidth - 10) - margin.left - margin.right,
            height = Math.min(width, window.innerHeight - margin.top - margin.bottom - 20);

        var data = [
            [ 
                {
                    axis: "a",
                    value: final[0],
                },
                {
                    axis: "b",
                    value: final[1],
                },
                {
                    axis: "c",
                    value: final[2],
                },
                {
                    axis: "d",
                    value:final[3],
                },
                {
                    axis: "e",
                    value: final[4],
                },
                {
                    axis: "f",
                    value:final[5],
                },
                {
                    axis: "g",
                    value: final[6],
                },
                {
                    axis: "h",
                    value: final[7],
                }
            ]
        ];

        var color = d3.scale.ordinal()
            .range(["#EDC951", "#CC333F", "#00A0B0"]);

        var radarChartOptions = {
            w: width,
            h: height,
            margin: margin,
            maxValue: 0.5,
            levels: 5,
            roundStrokes: true,
            color: color
        };

    function sort(result) {
    var newr = [
    [0],
    [0],
    [0],
    [0],
    [0],
    [0],
    [0],
    [0]
];

var j = 0;
var p = 0;
for (var i = 0; i != 7; i++) {
    var ip = 0;
    for (var k = j; k != 7; k++) {
        newr[ip].push(result[p]);
        p++;
        ip++
    }
    j++;
}
for (var i = 0; i != 7; i++) {
    ip = i + 1;
    for (var k = i + 1; k != 8; k++) {
        newr[ip].unshift(result[p]);
        p++;
        ip++
    }
}
return subtotall(newr);
}

function subtotall(newr) {
var total = [0, 0, 0, 0, 0, 0, 0, 0];
for (var i = 0; i < 8; i++) {
    for (var j = 0; j < 8; j++) {
        if (newr[i][j] == 1) {
            total[i] = total[i] + 1;
        }
    }
}
for (var i = 0; i < 8; i++) {
    for (var j = 0; j < 8; j++) {
        if (newr[j][i] == 2) {
            total[i] = total[i] + 1;
        }
    }
}
return total;
}
        RadarChart(".radarChart", data, radarChartOptions);
    var svg = d3.select(".radarChart svg")
      .attr("id", "visualization")
      .attr("xmlns", "http://www.w3.org/2000/svg");


        d3.select("#download").on("click", function(){
      d3.select(this)
        .attr("href", 'data:application/octet-stream;base64,' + btoa(d3.select(".radarChart").html()))
        .attr("download", "viz.svg") 
    })
    </script>
</body>

</html>

I am not a professional coder but I try to be better.

like image 478
Parsaria Avatar asked Apr 24 '18 11:04

Parsaria


1 Answers

I made an example in case that helps:

#!/usr/bin/env node
var d3 = require("d3"),
    jsdom = require("jsdom");

const { JSDOM } = jsdom;

const { document } = (new JSDOM('')).window;
global.document = document;

var body = d3.select(document).select("body");

var width = 300;
var height = 300;
var svg = body.append("svg")
    .attr("width", width)
    .attr("height", height);
svg.append("line")
    .attr("x1", 100)
    .attr("y1", 100)
    .attr("x2", 200)
    .attr("y2", 200)
    .style("stroke", "rgb(255,0,0)")
    .style("stroke-width", 2);

const fs = require('fs');
fs.writeFileSync("test.svg", body.node().innerHTML)
like image 137
Jean Paul Avatar answered Oct 04 '22 04:10

Jean Paul