Changes in ow/static/js/ow.js [1fe89ea:5cf5787] in OpenWorkouts-current
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
ow/static/js/ow.js
r1fe89ea r5cf5787 31 31 var openstreetmap_attr = 'Map data © <a href="http://www.osm.org">OpenStreetMap</a>'; 32 32 33 // Some constants reused through the code33 // Some vars reused through the code 34 34 var map; 35 35 var gpx; … … 147 147 Build a d3 bar chart, populated with data from the given url. 148 148 */ 149 var chart = d3.select( "svg"),150 margin = {top: 17, right: 0, bottom: 20, left:0},149 var chart = d3.select(chart_selector), 150 margin = {top: 20, right: 20, bottom: 30, left: 50}, 151 151 width = +chart.attr("width") - margin.left - margin.right, 152 152 height = +chart.attr("height") - margin.top - margin.bottom, … … 211 211 }) 212 212 .attr("y", function (d) { 213 /* 214 Get the value for the current bar, then get the maximum 215 value to be displayed in the bar, which is used to 216 calculate the proper position of the label for this bar, 217 relatively to its height (1% above the bar) 218 */ 219 var max = y.domain()[1]; 220 return y(d.distance + y.domain()[1] * 0.02); 221 }) 213 return y(Number(d.distance) + 5); 214 }) 222 215 .text(function(d) { 223 216 if (Number(d.distance) > 0) { … … 234 227 235 228 }; 229 230 231 owjs.year_chart = function(spec) { 232 233 "use strict"; 234 235 // parameters provided when creating an "instance" of the chart 236 var chart_selector = spec.chart_selector, 237 filters_selector = spec.filters_selector, 238 switcher_selector = spec.switcher_selector, 239 urls = spec.urls, 240 current_month = spec.current_month, 241 current_week = spec.current_week, 242 y_axis_labels = spec.y_axis_labels, 243 filter_by = spec.filter_by, 244 url = spec.url; 245 246 // Helpers 247 function select_x_axis_label(d) { 248 /* Given a value, return the label associated with it */ 249 return d3.select('.x-axis-b') 250 .selectAll('text') 251 .filter(function(x) { return x == d.name; }); 252 }; 253 254 function get_y_value(d, filter_by) { 255 return Number(d[filter_by]); 256 }; 257 258 function get_y_axis_label(filter_by) { 259 return y_axis_labels[filter_by]; 260 }; 261 262 function get_name_for_x(d) { 263 if (d.week == undefined || d.week == 0) { 264 return d.name; 265 } 266 else { 267 return d.id.split('-')[2]; 268 } 269 } 270 271 // Methods 272 var filters_setup = function filters_setup() { 273 $(filters_selector).on('click', function(e) { 274 e.preventDefault(); 275 filter_by = $(this).attr('class').split('-')[1] 276 var chart = d3.select(chart_selector); 277 chart.selectAll("*").remove(); 278 render(filter_by, url); 279 }); 280 }; 281 282 var switcher_setup = function switcher_setup() { 283 $(switcher_selector).on('click', function(e) { 284 e.preventDefault(); 285 url = $(this).attr('class').split('-')[1] 286 var chart = d3.select(chart_selector); 287 chart.selectAll("*").remove(); 288 render(filter_by, url); 289 }); 290 }; 291 292 var render = function render(filter_by, url) { 293 /* 294 Build a d3 bar chart, populated with data from the given url. 295 */ 296 var chart = d3.select(chart_selector), 297 margin = {top: 20, right: 20, bottom: 30, left: 50}, 298 width = +chart.attr("width") - margin.left - margin.right, 299 height = +chart.attr("height") - margin.top - margin.bottom, 300 g = chart.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")"), 301 x = d3.scaleBand().rangeRound([0, width]).padding(0.1), 302 y = d3.scaleLinear().rangeRound([height, 0]); 303 304 d3.json(urls[url]).then(function (data) { 305 x.domain(data.map(function (d) { 306 return get_name_for_x(d); 307 // return d.name; 308 })); 309 310 y.domain([0, d3.max(data, function (d) { 311 return get_y_value(d, filter_by); 312 })]); 313 314 g.append("g") 315 .attr('class', 'x-axis-b') 316 .attr("transform", "translate(0," + height + ")") 317 .call(d3.axisBottom(x)) 318 319 g.append("g") 320 .call(d3.axisLeft(y)) 321 .append("text") 322 .attr("fill", "#000") 323 .attr("transform", "rotate(-90)") 324 .attr("y", 6) 325 .attr("dy", "0.71em") 326 .attr("text-anchor", "end") 327 .text(get_y_axis_label(filter_by)); 328 329 g.selectAll(".bar") 330 .data(data) 331 .enter().append("rect") 332 .attr("class", function(d) { 333 var sel_week = current_month + '-' + current_week; 334 if (d.id == current_month || d.id == sel_week){ 335 /* Bar for the currently selected month or week */ 336 select_x_axis_label(d).attr('style', "font-weight: bold;"); 337 return 'bar current'; 338 } 339 else { 340 if (!current_week && d.id.indexOf(current_month) >=0 ) { 341 /* 342 User selected a month, then switched to weekly 343 view, we do highlight all the bars for weeks in 344 that month 345 */ 346 select_x_axis_label(d).attr('style', "font-weight: bold;"); 347 return 'bar current'; 348 } 349 else { 350 /* Non-selected bar */ 351 return 'bar'; 352 } 353 354 } 355 }) 356 .attr("x", function (d) { 357 return x(get_name_for_x(d)); 358 }) 359 .attr("y", function (d) { 360 return y(get_y_value(d, filter_by)); 361 }) 362 .attr("width", x.bandwidth()) 363 .attr("height", function (d) { 364 return height - y(get_y_value(d, filter_by)); 365 }) 366 .on('mouseover', function(d) { 367 if (d.id != current_month){ 368 select_x_axis_label(d).attr('style', "font-weight: bold;"); 369 } 370 }) 371 .on('mouseout', function(d) { 372 if (d.id != current_month){ 373 select_x_axis_label(d).attr('style', "font-weight: regular;"); 374 } 375 }) 376 .on('click', function(d) { 377 window.location.href = d.url; 378 }); 379 380 if (url == 'monthly') { 381 g.selectAll(".text") 382 .data(data) 383 .enter() 384 .append("text") 385 .attr("class","label") 386 .attr("x", function (d) { 387 return x(get_name_for_x(d)) + x.bandwidth()/2; 388 }) 389 .attr("y", function (d) { 390 /* 391 Get the value for the current bar, then get the maximum 392 value to be displayed in the bar, which is used to 393 calculate the proper position of the label for this bar, 394 relatively to its height (1% above the bar) 395 */ 396 var value = get_y_value(d, filter_by); 397 var max = y.domain()[1]; 398 return y(value + y.domain()[1] * 0.01); 399 }) 400 .text(function(d) { 401 var value = get_y_value(d, filter_by) 402 if ( value > 0) { 403 return value; 404 } 405 }); 406 } 407 408 if (url == 'weekly') { 409 g.selectAll(".tick") 410 .each(function (d, i) { 411 /* 412 Remove from the x-axis tickets those without letters 413 on them (useful for the weekly chart) 414 */ 415 if (d !== parseInt(d, 10)) { 416 if(!d.match(/[a-z]/i)) { 417 this.remove(); 418 } 419 } 420 }); 421 } 422 }); 423 }; 424 425 var that = {} 426 that.filters_setup = filters_setup; 427 that.switcher_setup = switcher_setup; 428 that.render = render; 429 return that 430 431 };
Note: See TracChangeset
for help on using the changeset viewer.