From dcbc4efe550bb334368b052ee0e88fc68d27233b Mon Sep 17 00:00:00 2001 From: Gregory Eremin Date: Mon, 9 Mar 2015 21:58:23 +0700 Subject: [PATCH] Label positioning done right --- app/scripts/charts.jsx | 51 +++++++++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 15 deletions(-) diff --git a/app/scripts/charts.jsx b/app/scripts/charts.jsx index 5659f18..ea6011e 100644 --- a/app/scripts/charts.jsx +++ b/app/scripts/charts.jsx @@ -116,14 +116,15 @@ var BarChart = React.createClass({ max2 = (min < 0 ? max - min : max), width = Math.abs(val)/max2*maxWidth, height = this.barHeight, - x = (min >= 0 ? 0 : -min/max2*maxWidth - (val >= 0 ? 0 : width)), + offset = -min/max2*maxWidth, + x = (min >= 0 ? 0 : offset - (val >= 0 ? 0 : width)), y = this.y(i); // console.log(point.item, {val: val, max: max, x: x, y: y, width: width}) return ( ); } @@ -131,31 +132,51 @@ var BarChart = React.createClass({ var Bar = React.createClass({ mixins: [Router.Navigation], + handleClick: function(e) { this.transitionTo(this.props.link + this.props.point.item); }, render: function() { - var p = this.props.point, - val = p[this.props.metric], - w = this.props.width, - label = p.item + ': ' + val, - labelM = 5, // Margin - labelW = textWidth(label, 'Helvetica Neue', '16px') + 2*labelM, - textx = 2*labelM; - if (labelW + 2*labelM > w) { - textx = w + textx; + var val = this.props.point[this.props.metric], + item = this.props.point.item, + offset = this.props.offset, + width = this.props.width, + label = item + ': ' + val, + labelPadding = 5, + labelWidth = textWidth(label, 'Helvetica Neue', '16px') + 2*labelPadding, + labelOuterWidth = labelWidth + 2*labelPadding, + labelX = 0, + barX = this.props.x; + + if (labelOuterWidth <= width) { + if (offset > 0) { + if (barX === offset) { + labelX = barX + 2*labelPadding; + } else { + labelX = barX + width - labelOuterWidth + 2*labelPadding; + } + } else { + labelX = barX + 2*labelPadding; + } + } else { + if (labelOuterWidth <= barX) { + labelX = barX - labelOuterWidth + 2*labelPadding; + } else { + labelX = barX + width + labelPadding; + } } + return ( - {label} + {label} ); }