Skip to content

Commit

Permalink
Added a function for horizontal label position to help shortcut some …
Browse files Browse the repository at this point in the history
…things

and work as an example
  • Loading branch information
mtgibbs committed Feb 1, 2016
1 parent 5695b41 commit a29e4a7
Showing 1 changed file with 36 additions and 5 deletions.
41 changes: 36 additions & 5 deletions src/scripts/chartist-plugin-barlabels.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
var barValue = data.value.x === undefined ? data.value.y : data.value.x;
var indexClass = options.includeIndexClass ? ['ct-bar-label-i-', data.seriesIndex, '-', data.index].join('') : '';
var thresholdClass = getThresholdClass(options.thresholdPercentage, options.thresholdOptions, highValue, barValue);
var positionData = handleLabelPosition(options.labelPositionFnc, highValue, barValue);
var positionData = handleLabelPosition(options.labelPositionFnc, highValue, barValue, options.thresholdPercentage);
options = Chartist.extend({}, options, positionData);

if (options.showZeroLabels || (!options.showZeroLabels && barValue != 0)) {
Expand All @@ -88,6 +88,31 @@
};
};

Chartist.plugins.ctBarLabels.InsetLabelsPositionHorizontal = function(data) {

if (data.high && data.value && data.threshold) {
var aboveThreshold = (data.value / data.high * 100 > data.threshold);

if (aboveThreshold) {
return {
labelOffset: {
x: -2,
y: 4
},
textAnchor: 'end'
}
} else {
return {
labelOffset: {
x: 2,
y: 4
},
textAnchor: 'start'
};
}
}
};

}(window, document, Chartist));

return Chartist.plugins.ctBarLabels;
Expand All @@ -110,7 +135,9 @@
var series = chart.data.series;
// check to see if there are multiple series
if (series[0].constructor === Array) {
series = series.reduce(function(prev, curr) { return prev.concat(curr)});
series = series.reduce(function(prev, curr) {
return prev.concat(curr)
});
}

// return the highest value
Expand All @@ -127,13 +154,17 @@
}
}

function handleLabelPosition(lblPositionFnc, highValue, barValue) {
function handleLabelPosition(lblPositionFnc, highValue, barValue, thresholdPercentage) {
if (!lblPositionFnc)
return {};

var positionData = lblPositionFnc({ high: highValue, value: barValue, });
var result = {};
var positionData = lblPositionFnc({
high: highValue,
value: barValue,
threshold: thresholdPercentage
});

var result = {};
// sanitize the object just in case they tried to override other options that will get merged
// TODO: make this terse
if (positionData.labelOffset) {
Expand Down

0 comments on commit a29e4a7

Please sign in to comment.