akngs/d3-boxplot

How to implement without NPM?

IwanHB opened this issue · 3 comments

Hi there

Thanks for your great work!
I would like to use your code to make a boxplot on my website, if that's ok?

I am, however, having difficulties using the code. I am not using NPM, Angular, React or anything, just standard html and js files. I am also quite new to GitHub, sorry for that.

I downloaded the files and tried the code snippet given as an example, but it didn't work for me:

<!DOCTYPE html>
<meta charset="utf-8">
<html>
<head>
<title>Boxplot</title>
</head>
<body>

<div id="svg_container"></div>

<script src="https://d3js.org/d3.v7.min.js"></script>
<script src="boxplot.js"></script>

<script>

var margin = {top: 10, right: 30, bottom: 30, left: 40},
  width = 400 - margin.left - margin.right,
  height = 400 - margin.top - margin.bottom;

var svg = d3.select("#svg_container")
.append("svg")
  .attr("width", width + margin.left + margin.right)
  .attr("height", height + margin.top + margin.bottom)
.append("g")
  .attr("transform",
        "translate(" + margin.left + "," + margin.top + ")");

let data = [1, 2, 3, 4, 5]
let stats = d3.boxplotStats(data)
let x = d3.scaleLinear()
  .domain(d3.extent(data))
  .range([0, 300])
let plot = d3.boxplot().scale(x)
d3.select('svg').datum(stats).call(plot)
  
</script>

</body>
</html>

Can you please tell what I need to do to make use of the code files? Thanks.

akngs commented

Apologies for the late reply.

If it's okay to ignore Internet Explorer, you may use ESM - although this is not ideal for production-grade code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>d3-boxplot demo</title>
    <script type="module">
        import * as d3 from 'https://unpkg.com/d3?module';
        import * as d3bp from 'https://unpkg.com/d3-boxplot?module';

        let margin = {top: 10, right: 30, bottom: 30, left: 40},
            width = 400 - margin.left - margin.right,
            height = 400 - margin.top - margin.bottom;

        let svg = d3.select("#svg_container")
            .append("svg")
            .attr("width", width + margin.left + margin.right)
            .attr("height", height + margin.top + margin.bottom)
            .append("g")
            .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

        let data = [1, 2, 3, 4, 5];
        let stats = d3bp.boxplotStats(data);
        let x = d3.scaleLinear()
            .domain(d3.extent(data))
            .range([0, 300]);
        let plot = d3bp.boxplot().scale(x);
        d3.select("svg").datum(stats).call(plot);
    </script>
</head>
<body>
    <div id="svg_container"></div>
</body>
</html>

That is quite all right.

Thanks for the reply anyway.

I made my own version "from scratch", since I couldn't use yours.
It ended up being not as advanced as yours :-) but it fulfils my requirements for my website.

https://www.medicalstat.org/PerformDataAnalysis.html

image

akngs commented

Grad to hear. Your own version just looks great to me! 👍🏼