Browse Source

Add legend.equally option for locate legends equllay - #62

pull/66/head
Masayuki Tanaka 11 years ago
parent
commit
908e828b58
  1. 91
      c3.js
  2. 4
      c3.min.js

91
c3.js

@ -77,7 +77,8 @@
// legend // legend
var __legend_show = getConfig(['legend', 'show'], true), var __legend_show = getConfig(['legend', 'show'], true),
__legend_position = getConfig(['legend', 'position'], 'bottom'), __legend_position = getConfig(['legend', 'position'], 'bottom'),
__legend_item_onclick = getConfig(['legend', 'item', 'onclick'], function () {}); __legend_item_onclick = getConfig(['legend', 'item', 'onclick'], function () {}),
__legend_equally = getConfig(['legend', 'equally'], false);
// axis // axis
var __axis_rotated = getConfig(['axis', 'rotated'], false), var __axis_rotated = getConfig(['axis', 'rotated'], false),
@ -358,6 +359,7 @@
return maxDataCount > 1 ? (base * ratio) / (maxDataCount - 1) : base; return maxDataCount > 1 ? (base * ratio) / (maxDataCount - 1) : base;
} }
function getLegendWidth() { function getLegendWidth() {
// TODO: legend width should be an option
return __legend_show ? isLegendRight ? 150 : currentWidth : 0; return __legend_show ? isLegendRight ? 150 : currentWidth : 0;
} }
function getLegendHeight() { function getLegendHeight() {
@ -2827,61 +2829,84 @@
function updateLegend(targets, options) { function updateLegend(targets, options) {
var ids = getTargetIds(targets), l; var ids = getTargetIds(targets), l;
var xForLegend, xForLegendText, xForLegendRect, yForLegend, yForLegendText, yForLegendRect; var xForLegend, xForLegendText, xForLegendRect, yForLegend, yForLegendText, yForLegendRect;
var item_height = 0, item_width = 0, paddingTop = 4, paddingRight = 30, margin, updateSizes; var paddingTop = 4, paddingRight = 30, margin;
var totalLength = 0, offsets = {}, widths = {}, heights = {};
var withTransition; var withTransition;
options = isUndefined(options) ? {} : options; options = isUndefined(options) ? {} : options;
withTransition = isDefined(options.withTransition) ? options.withTransition : true; withTransition = isDefined(options.withTransition) ? options.withTransition : true;
if (isLegendRight) { function getXOffset(id, i) {
xForLegend = function () { return legendWidth * 0.2; }; return __legend_equally ? widths[id] * i : offsets[id];
yForLegend = function (d, i) { return margin + item_height * i; };
} else {
xForLegend = function (d, i) { return margin + item_width * i; };
yForLegend = function () { return legendHeight * 0.2; };
} }
xForLegendText = function (d, i) { return xForLegend(d, i) + 14; }; function getYOffset(id, i) {
yForLegendText = function (d, i) { return yForLegend(d, i) + 9; }; return __legend_equally ? heights[id] * i : offsets[id];
xForLegendRect = function (d, i) { return xForLegend(d, i) - 4; }; }
yForLegendRect = function (d, i) { return yForLegend(d, i) - 7; }; function updatePositions(textElement, id) {
updateSizes = function (textElement) {
var box = textElement.getBBox(), var box = textElement.getBBox(),
width = Math.ceil((box.width + paddingRight) / 10) * 10, width = Math.ceil((box.width + paddingRight) / 10) * 10,
height = Math.ceil((box.height + paddingTop) / 10) * 10; height = Math.ceil((box.height + paddingTop) / 10) * 10,
if (width > item_width) { length = isLegendRight ? height : width,
item_width = width; areaLength = isLegendRight ? legendHeight : legendWidth,
maxWidth, maxHeight;
widths[id] = width;
heights[id] = height;
if (__legend_equally) {
maxWidth = d3.max(Object.keys(widths).map(function (key) { return widths[key]; }));
maxHeight = d3.max(Object.keys(heights).map(function (key) { return heights[key]; }));
if (width >= maxWidth) {
Object.keys(widths).forEach(function (key) { widths[key] = width; });
if (! isLegendRight) { if (! isLegendRight) {
margin = (legendWidth - item_width * Object.keys(targets).length) / 2; margin = (legendWidth - width * Object.keys(targets).length) / 2;
} }
} }
if (height > item_height) { if (height >= maxHeight) {
item_height = height; Object.keys(heights).forEach(function (key) { heights[key] = height; });
if (isLegendRight) { if (isLegendRight) {
margin = (legendHeight - item_height * Object.keys(targets).length) / 2; margin = (legendHeight - height * Object.keys(targets).length) / 2;
} }
} }
}; } else {
offsets[id] = totalLength;
totalLength += length;
margin = (areaLength - totalLength) / 2;
}
}
if (isLegendRight) {
xForLegend = function () { return legendWidth * 0.2; };
yForLegend = function (id, i) { return margin + getYOffset(id, i); };
} else {
xForLegend = function (id, i) { return margin + getXOffset(id, i); };
yForLegend = function () { return legendHeight * 0.2; };
}
xForLegendText = function (id, i) { return xForLegend(id, i) + 14; };
yForLegendText = function (id, i) { return yForLegend(id, i) + 9; };
xForLegendRect = function (id, i) { return xForLegend(id, i) - 4; };
yForLegendRect = function (id, i) { return yForLegend(id, i) - 7; };
// Define g for legend area // Define g for legend area
l = legend.selectAll('.legend-item') l = legend.selectAll('.legend-item')
.data(ids) .data(ids)
.enter().append('g') .enter().append('g')
.attr('class', function (d) { return 'legend-item legend-item-' + d; }) .attr('class', function (id) { return 'legend-item legend-item-' + id; })
.style('cursor', 'pointer') .style('cursor', 'pointer')
.on('click', function (d) { .on('click', function (id) {
__legend_item_onclick(d); __legend_item_onclick(id);
}) })
.on('mouseover', function (d) { .on('mouseover', function (id) {
focusLegend(d); focusLegend(id);
c3.focus(d); c3.focus(id);
}) })
.on('mouseout', function () { .on('mouseout', function () {
revertLegend(); revertLegend();
c3.revert(); c3.revert();
}); });
l.append('text') l.append('text')
.text(function (d) { return isDefined(__data_names[d]) ? __data_names[d] : d; }) .text(function (id) { return isDefined(__data_names[id]) ? __data_names[id] : id; })
.each(function () { updateSizes(this); }) .each(function (id) { updatePositions(this, id); })
.style("pointer-events", "none") .style("pointer-events", "none")
.attr('x', isLegendRight ? xForLegendText : -200) .attr('x', isLegendRight ? xForLegendText : -200)
.attr('y', isLegendRight ? -200 : yForLegend); .attr('y', isLegendRight ? -200 : yForLegend);
@ -2890,12 +2915,12 @@
.style('fill-opacity', 0) .style('fill-opacity', 0)
.attr('x', isLegendRight ? xForLegendRect : -200) .attr('x', isLegendRight ? xForLegendRect : -200)
.attr('y', isLegendRight ? -200 : yForLegendRect) .attr('y', isLegendRight ? -200 : yForLegendRect)
.attr('width', item_width + 14) .attr('width', function (id) { return widths[id]; })
.attr('height', 24); .attr('height', function (id) { return heights[id]; });
l.append('rect') l.append('rect')
.attr("class", "legend-item-tile") .attr("class", "legend-item-tile")
.style("pointer-events", "none") .style("pointer-events", "none")
.style('fill', function (d) { return color(d); }) .style('fill', function (id) { return color(id); })
.attr('x', isLegendRight ? xForLegendText : -200) .attr('x', isLegendRight ? xForLegendText : -200)
.attr('y', isLegendRight ? -200 : yForLegendText) .attr('y', isLegendRight ? -200 : yForLegendText)
.attr('width', 10) .attr('width', 10)
@ -2903,7 +2928,7 @@
legend.selectAll('text') legend.selectAll('text')
.data(ids) .data(ids)
.each(function () { updateSizes(this); }) .each(function (id) { updatePositions(this, id); })
.transition().duration(withTransition ? 250 : 0) .transition().duration(withTransition ? 250 : 0)
.attr('x', xForLegendText) .attr('x', xForLegendText)
.attr('y', yForLegendText); .attr('y', yForLegendText);

4
c3.min.js vendored

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save