Skip to content

Commit

Permalink
made it through decomposition effort for w3c#541
Browse files Browse the repository at this point in the history
  • Loading branch information
hamilton committed Nov 3, 2015
1 parent 6c3b3c6 commit baeb2e3
Show file tree
Hide file tree
Showing 3 changed files with 225 additions and 175 deletions.
197 changes: 111 additions & 86 deletions dist/metricsgraphics.js
Expand Up @@ -2068,105 +2068,130 @@ function init (args) {

MG.init = init;

function mg_plot_area_bottom (args) {
return args.height - args.bottom - args.buffer;
}

function mg_return_label (d) {
return d.label;
}

function markers(args) {
'use strict';
var svg = mg_get_svg_child_of(args.target);
var gm;
var gb;
function mg_remove_existing_markers (svg) {
svg.selectAll('.mg-markers').remove();
svg.selectAll('.mg-baselines').remove();
}

//remove existing markers and baselines
svg.selectAll('.mg-markers').remove();
svg.selectAll('.mg-baselines').remove();
function mg_in_range (args) {
return function (d) {
return (args.scales.X(d[args.x_accessor]) > args.buffer + args.left)
&& (args.scales.X(d[args.x_accessor]) < args.width - args.buffer - args.right);
};
}

if (args.markers) {
gm = svg.append('g')
.attr('class', 'mg-markers');
function mg_x_position (args) {
return function (d) {
return args.scales.X(d[args.x_accessor]);
};
}

gm.selectAll('.mg-markers')
.data(args.markers.filter(inRange))
.enter()
.append('line')
.attr('x1', xPositionFixed)
.attr('x2', xPositionFixed)
.attr('y1', args.top)
.attr('y2', function() {
return args.height - args.bottom - args.buffer;
})
.attr('class', function (d) {
return d.lineclass;
})
.attr('stroke-dasharray', '3,1');
function mg_x_position_fixed (args) {
var _mg_x_pos = mg_x_position(args);
return function (d) {
return _mg_x_pos(d).toFixed(2);
};
}

gm.selectAll('.mg-markers')
.data(args.markers.filter(inRange))
.enter()
.append('text')
.attr('class', function (d) {
return d.textclass ? 'mg-marker-text ' + d.textclass : 'mg-marker-text';
})
.attr('x', xPosition)
.attr('y', args.top * 0.95)
.attr('text-anchor', 'middle')
.text(function(d) {
return d.label;
})
.each(function(d) {
if(d.click) {
d3.select(this)
.style('cursor', 'pointer')
.on('click', d.click);
}
});
function mg_y_position_fixed (args) {
var _mg_y_pos = args.scales.Y;
return function (d) {
return _mg_y_pos(d.value).toFixed(2);
};
}

preventHorizontalOverlap(gm.selectAll('.mg-marker-text')[0], args);
}
function mg_place_markers (args, svg) {
var gm;
if (args.markers) {
gm = svg.append('g')
.attr('class', 'mg-markers');
mg_place_marker_lines(gm, args);
mg_place_marker_text(gm, args);
}
}

function xPosition(d) {
return args.scales.X(d[args.x_accessor]);
}
function mg_place_marker_lines (gm, args) {
var x_pos_fixed = mg_x_position_fixed(args);
gm.selectAll('.mg-markers')
.data(args.markers.filter(mg_in_range(args)))
.enter()
.append('line')
.attr('x1', x_pos_fixed)
.attr('x2', x_pos_fixed)
.attr('y1', args.top)
.attr('y2', mg_plot_area_bottom(args))
.attr('class', function (d) {
return d.lineclass;
})
.attr('stroke-dasharray', '3,1');
}

function xPositionFixed(d) {
return xPosition(d).toFixed(2);
}
function mg_place_marker_text (gm, args) {
gm.selectAll('.mg-markers')
.data(args.markers.filter(mg_in_range(args)))
.enter()
.append('text')
.attr('class', function (d) {
return d.textclass ? 'mg-marker-text ' + d.textclass : 'mg-marker-text';
})
.attr('x', mg_x_position(args))
.attr('y', args.top * 0.95)
.attr('text-anchor', 'middle')
.text(mg_return_label)
.each(function (d) {
if (d.click) d3.select(this).style('cursor', 'pointer').on('click', d.click);
});
preventHorizontalOverlap(gm.selectAll('.mg-marker-text')[0], args);
}

function inRange(d) {
return (args.scales.X(d[args.x_accessor]) > args.buffer + args.left)
&& (args.scales.X(d[args.x_accessor]) < args.width - args.buffer - args.right);
}
function mg_place_baselines (args, svg) {
var gb;
if (args.baselines) {
gb = svg.append('g')
.attr('class', 'mg-baselines');
mg_place_baseline_lines(gb, args);
mg_place_baseline_text(gb, args);
}
}

if (args.baselines) {
gb = svg.append('g')
.attr('class', 'mg-baselines');

gb.selectAll('.mg-baselines')
.data(args.baselines)
.enter().append('line')
.attr('x1', args.left + args.buffer)
.attr('x2', args.width-args.right-args.buffer)
.attr('y1', function(d){
return args.scales.Y(d.value).toFixed(2);
})
.attr('y2', function(d){
return args.scales.Y(d.value).toFixed(2);
});
function mg_place_baseline_lines (gb, args) {
var y_pos = mg_y_position_fixed(args);
gb.selectAll('.mg-baselines')
.data(args.baselines)
.enter().append('line')
.attr('x1', args.left + args.buffer)
.attr('x2', args.width - args.right - args.buffer)
.attr('y1', y_pos)
.attr('y2', y_pos);
}

gb.selectAll('.mg-baselines')
.data(args.baselines)
.enter().append('text')
.attr('x', args.width-args.right - args.buffer)
.attr('y', function(d){
return args.scales.Y(d.value).toFixed(2);
})
.attr('dy', -3)
.attr('text-anchor', 'end')
.text(function(d) {
return d.label;
});
}
function mg_place_baseline_text (gb, args) {
var y_pos = mg_y_position_fixed(args);
gb.selectAll('.mg-baselines')
.data(args.baselines)
.enter().append('text')
.attr('x', args.width - args.right - args.buffer)
.attr('y', y_pos)
.attr('dy', -3)
.attr('text-anchor', 'end')
.text(mg_return_label);
}

return this;
function markers (args) {
'use strict';
var svg = mg_get_svg_child_of(args.target);
mg_remove_existing_markers(svg);
mg_place_markers(args, svg);
mg_place_baselines(args, svg);
return this;
}

MG.markers = markers;
Expand Down

0 comments on commit baeb2e3

Please sign in to comment.