2 * draws any number of line plots; for now only for numeric x and y values.
3 * import solGS.linePlot and call it like solGS.linePlot(lineData);
4 * lineData argument has the ff structure
10 'x_axis_label': xLabel,
11 'y_axis_label': yLabel,
12 'axis_label_color: axisLabelColor,
30 * Isaak Y Tecle <iyt2@cornell.edu>
34 var solGS = solGS || function solGS () {};
36 solGS.linePlot = function (allData) {
38 var linePlot = drawLines(allData);
40 function getXValues (xy) {
44 for (var i=0; i < xy.length; i++) {
46 x = x.replace(/^\s+|\s+$/g, '');
56 function getYValues (xy) {
60 for (var i=0; i < xy.length; i++) {
72 function getLegendValues (allData) {
75 var linesCount = Object.keys(allData.lines).length;
77 setLineColors(allData);
79 for (var i=0; i < linesCount; i++) {
80 var lc = allData.lines[i].color;
81 var l = allData.lines[i].legend;
91 function getExtremeValues (alldata) {
93 var linesCount = Object.keys(allData.lines).length;
98 for ( var i=0; i < linesCount; i++) {
99 var data = allData.lines[i].data;
100 var x = getXValues(data);
101 var y = getYValues(data);
103 eX.push(d3.min(x), d3.max(x));
104 eY.push(d3.min(y), d3.max(y));
107 return { 'x' : eX, 'y': eY };
112 function setLineColors (allData) {
115 '#86B404', '#F7D358',
116 '#5C5858', '#4863A0',
117 '#A18648', '#8C001A',
120 var linesCount = Object.keys(allData.lines).length;
122 for ( var i=0; i < linesCount; i++) {
124 if (!allData.lines[i].color) {
125 allData.lines[i].color = colors[i];
131 function drawLines (allData) {
133 var svgId = allData.div_id;
134 var title = allData.plot_title;
138 var pad = {'left':60, 'top':40, 'right':20, 'bottom': 40};
139 var totalH = height + pad.top + pad.bottom;
140 var totalW = width + pad.left + pad.right;
142 jQuery(svgId).append('<div id=line_plot></div>');
144 var plotId = '#line_plot';
145 var axisColor = allData.axis_label_color;
147 var yLabel = allData.y_axis_label;
148 var xLabel = allData.x_axis_label;
149 var title = allData.plot_title;
151 var legendValues = getLegendValues(allData);
153 var extremes = getExtremeValues(allData);
154 var extremesX = extremes.x;
155 var extremesY = extremes.y;
157 var xScale = d3.scale.linear()
158 .domain([d3.min(extremesX), d3.max(extremesX)])
161 var yScale = d3.scale.linear()
162 .domain([0, d3.max(extremesY)])
165 var line = d3.svg.line()
166 .interpolate('basis')
174 var svg = d3.select(svgId)
176 .attr("width", totalW)
177 .attr("height", totalH);
179 var graph = svg.append("g")
181 .attr("transform", "translate(" + pad.left + "," + pad.top + ")");
183 var xAxis = d3.svg.axis()
188 var yAxis = d3.svg.axis()
194 .attr("class", "x axis")
195 .attr("transform", "translate(0," + height + ")")
197 .attr("fill", axisColor)
198 .style({"text-anchor":"start", "fill": axisColor});
201 .attr("class", "y axis")
202 .attr("transform", "translate(0,0)")
204 .attr("fill", axisColor);
206 var linesCount = Object.keys(allData.lines).length;
208 for ( var i=0; i < linesCount; i++) {
210 var path = graph.append("path")
211 .attr("d", line(allData.lines[i].data))
212 .attr("stroke", allData.lines[i].color)
213 .attr("stroke-width", "3")
214 .attr("fill", "none");
216 var totalLength = path.node().getTotalLength();
218 path.attr("stroke-dasharray", totalLength + " " + totalLength)
219 .attr("stroke-dashoffset", totalLength)
223 .attr("stroke-dashoffset", 0);
228 .attr("fill", axisColor)
234 .attr("id", "xLabel")
235 .attr("fill", axisColor)
237 .attr("x", width * 0.5)
238 .attr("y", height + 40);
241 .attr("id", "yLabel")
242 .attr("fill", axisColor)
244 .attr("transform", "translate(" + -40 + "," + height * 0.5 + ")" + " rotate(-90)");
246 var legendTxt = graph.append("g")
247 .attr("transform", "translate(" + (width - 150) + "," + (height * 0.25) + ")")
248 .attr("id", "normalLegend");
250 legendTxt.selectAll("text")
254 .attr("fill", function (d) {
257 .attr("font-weight", "bold")
259 .attr("y", function (d, i) {
266 return {'graph': graph, 'xScale': xScale, 'yScale': yScale};