-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleLineChart.js
153 lines (132 loc) · 4.02 KB
/
SimpleLineChart.js
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
function SimpleLineChart(config)
{
// Default parameters.
var p =
{
parent : null,
labels : [ "X", "Y" ],
listeners : [],
data : [[0,0],[1,1],[2,4],[3,9],[4,16]],
width : 600,
height : 400,
xi : 0,
yi : 1,
mleft : 0,
mright : 0,
mtop : 0,
mbottom :0
};
// If we have user-defined parameters, override the defaults.
if (config !== "undefined")
{
for (var prop in config)
{
p[prop] = config[prop];
}
}
// Render this chart.
function chart()
{
var inner_width = p.width - p.mleft - p.mright;
var inner_height = p.height - p.mtop - p.mbottom;
var x = d3.scale.linear()
.domain(d3.extent(p.data, function(d){return d[p.xi];}))
.range([0, inner_width]);
var y = d3.scale.linear()
.domain([0, d3.max(p.data, function(d) {return d[p.yi];})])
.range([inner_height, 0]);
var lineFunction = d3.svg.line()
.x(function(d){return x(d[p.xi]);})
.y(function(d){return y(d[p.yi]);});
// Create the x axis at the bottom.
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
// Create the y axis to the left.
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var previous_g = p.parent.select('g')[0][0];
if(!previous_g){
var chartContainer = p.parent
.attr("width", p.width)
.attr("height", p.height)
.append("g")
.attr("transform", "translate(" + p.mleft + "," + p.mtop + ")");
// Draw the x axis.
chartContainer.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + inner_height + ")")
.call(xAxis)
.append('text')
.attr('class', 'xaxis_label')
.style("text-anchor", "middle")
.attr('x', inner_width / 2)
.attr('y', p.mbottom / 2)
.text(p.labels[p.xi]);
// Draw the y axis
chartContainer.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr('class', 'yaxis_label')
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("x", -p.mleft)
.attr("dy", "1em")
.style("text-anchor", "middle")
.text(p.labels[p.yi]);
chartContainer.append("path")
.attr("class", "line history")
.attr('d', lineFunction(p.data));
//Linear regression line
var lr_line = ss.linear_regression()
.data(p.data)
.line();
var lr_vals = _.map(p.data, function(val, i){return [val[0], lr_line(val[0])];});
chartContainer.append("path")
.attr("class", "line regression")
.attr('d', lineFunction(lr_vals));
}
else{
d3.select('.y.axis').transition().call(yAxis);
d3.select('.x.axis').transition().call(xAxis);
p.parent.select('g').select('.yaxis_label').text(p.labels[p.yi]);
d3.select('.line.history')
//.attr('class', 'line history')
.transition()
.attr("d", lineFunction(p.data));
var lr_line = ss.linear_regression()
.data(p.data)
.line();
var lr_vals = _.map(p.data, function(val, i){return [val[0], lr_line(val[0])];});
d3.select('.line.regression')
.transition()
.attr('d', lineFunction(lr_vals));
}
}
// Use this routine to retrieve and update attributes.
chart.attr = function(name, value)
{
// When no arguments are given, we return the current value of the
// attribute back to the caller.
if (arguments.length == 1)
{
return p[name];
}
// Given 2 arguments we set the name=value.
else if (arguments.length == 2)
{
p[name] = value;
}
// Return the chart object back so we can chain the operations together.
return chart;
}
// This routine supports the update operation for this chart. This is
// applicable when the chart should be partially updated.
chart.update = function()
{
}
// Return the instantiated chart object.
return chart;
}