akngs / d3-boxplot

d3js box plot plugin
ISC License
23 stars 2 forks source link

How to implement without NPM? #7

Closed IwanHB closed 2 years ago

IwanHB commented 3 years ago

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 2 years ago

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>
IwanHB commented 2 years ago

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 2 years ago

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