-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcustomer_purchase_graph.html
101 lines (83 loc) · 2.91 KB
/
customer_purchase_graph.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<!DOCTYPE html>
<!--
This file displays the customer vip data in a histogram. It may be used as a template to generate other line graphs.
-->
<html>
<head>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.x.axis path {
display: none;
}
.line {
fill: none;
stroke: steelblue;
stroke-width: 1.5px;
}
</style>
</head>
<body>
<!-- Title of the graph. You may edit the text contained in the h2 element to read as you would like -->
<h2 style="position:fixed;top:0;left:49%;">Purchases by Customer</h2>
<script src="d3.v3.js"></script>
<script>
var margin = {top: 50, right: 50, bottom: 50, left: 50},
width = window.innerWidth - margin.left - margin.right,
height = window.innerHeight - margin.top - margin.bottom;
var x = d3.scale.linear()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var line = d3.svg.line()
.x(function(d) { return x(d.customer); })// Change customer to the name of your first column of data
.y(function(d) { return y(d.purchases); });// Change purchases to the name of your second column of data
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right - 50)
.attr("height", height + margin.top + margin.bottom - 50)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// Change the 'vip_data.tsv' string to the name of your data file. Your data file should be in the same
// directory as this file and contain two columns of data.
d3.tsv("vip_data.tsv", function(error, data) {
data.forEach(function(d) {
d.customer = d.customer;//Change customer to the name of your first column of data
d.purchases = +d.purchases;// Change purchases to the name of your second column of data
});
x.domain(d3.extent(data, function(d) { return d.customer; }));//Change customer to the name of your first column of data
y.domain(d3.extent(data, function(d) { return d.purchases; }));// Change purchases to the name of your second column of data
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Purchases");
svg.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line);
});
</script>
</body>
</html>