Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

customer segment diagram background #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
138 changes: 138 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<title>Customer segment diagram</title>
<script src="https://d3js.org/d3.v4.min.js"></script>
<style>

body {
margin: 0 0 0 0;
position: fixed;
left: 0px;
right: 0px;
top: 0px;
bottom: 0px;
}

svg {
width:100%;
height:100%;
}

#header {
display: flex;
height:50px;
align-self: center;
align-items: center;
background-color: lightgrey;
padding-left: 10px;
margin : 20px 20px 20px 20px;
}

</style>
</head>
<body>
<div id='header'>Customer Segment</div>
<svg></svg>
<script>
const hearderDiv = document.getElementById('header');
const hearderWidth = hearderDiv.clientWidth;
const hearderHeight = hearderDiv.clientHeight;
const margin = 20 + 1; //20 from #header style,+1 for padding

// initialize
const svg = d3.select('svg'),
bigCircle = svg.append('circle'),
smallCircle = svg.append('circle'),
lineGraph1 = svg.append('path'),
lineGraph2 = svg.append('path'),
lineGraph3 = svg.append('path');

function draw(){
//calculate the center (cx,cy) of the circles
//height = 1/2* (window height - header height - margin)
const cx = window.innerWidth/2;
const cy = window.innerHeight/2 - hearderHeight/2 - margin;

// angles for lines 2 and 3
const cos1 = Math.cos(45);
const sin1 = Math.sin(45);
const cos2 = Math.cos(-45);
const sin2 = Math.sin(-45);

let bigR; //big radius
if(cx>cy){ // right radius when resizing
bigR = cy;
} else{
bigR = cx;
}
const smallR = (bigR-1)*0.1; //small radius 10% of the big one

smallCircle
.attr('cx', cx)
.attr('cy', cy)
.attr('r',smallR)
.attr('stroke', 'steelblue')
.style('fill', 'none');

bigCircle
.attr('cx', cx)
.attr('cy', cy)
.attr('r', bigR-1) //-1 for styling reasons
.attr('stroke', 'steelblue')
.style('fill', 'none');

//the three lines of the form y1-y0 = dy/dx(x1-xo)
//with [{x0: ...,yo: ...},{x1: ...,y1: ...}]
// offsets -0.5,+1,+1.5 and -2.5 are for styling reasons
const lineData = [
[
{ 'x': cx - smallR - 0.5, 'y': cy },
{ 'x': cx - bigR + 1.5, 'y': cy }
],
[
{ 'x': cx + cos2*smallR + 1, 'y': cy - sin2*smallR },
{'x': cx + cos2*bigR - 2.5 , 'y': cy - sin2*bigR }
],
[
{ 'x': cx + cos1*smallR + 1, 'y': cy - sin1*smallR },
{ 'x': cx + cos1*bigR - 2.5 , 'y': cy - sin1*bigR }
]
];

//This is the accessor function we talked about above
const lineFunction = d3.line()
.x(function(d) { return d.x; })
.y(function(d) { return d.y; });

//The lines SVG Path we draw
lineGraph1
.attr('d', lineFunction(lineData[0]))
.attr('stroke', 'red')
.attr('stroke-width', 2)
.attr('fill', 'none');

lineGraph2
.attr('d', lineFunction(lineData[1]))
.attr('stroke', 'red')
.attr('stroke-width', 2)
.attr('fill', 'none');

lineGraph3
.attr('d', lineFunction(lineData[2]))
.attr('stroke', 'red')
.attr('stroke-width', 2)
.attr('fill', 'none');
}

// Draw for the first time to initialize.
draw();
// Redraw based on the new size whenever the browser window is resized.
window.addEventListener("resize", draw);

</script>
</body>
</html>