Skip to content

Commit

Permalink
quickfix: added bar highlight feature
Browse files Browse the repository at this point in the history
  • Loading branch information
ernest-okot committed Aug 23, 2017
1 parent 466648a commit 36de99c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
3 changes: 2 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,8 @@ <h2>Devinit Charts</h2>
},
interactions: {
enable: true,
}
},
highlight: ['Jan']
},
data: [
{"Rainfall": 100, "Months": "Jan", color: 'red1'},
Expand Down
29 changes: 27 additions & 2 deletions src/factories/createBarChart.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ export default (element, plot, config) => {
const chart = createLinearChart({element, plot, config});

if (interactions.enable) {
plot.onAnchor(createBarInteraction(config.orientation, config.labeling));
plot.onAnchor(plot => {
setTimeout(() => {
createBarInteraction(config.orientation, config.labeling, config.highlight)(plot)
}, 500)
});
}

return chart;
}

const createBarInteraction = (orientation = 'vertical', labeling = {}) => {
const createBarInteraction = (orientation = 'vertical', labeling = {}, highlight = []) => {

const {
prefix = '',
Expand All @@ -37,6 +41,26 @@ const createBarInteraction = (orientation = 'vertical', labeling = {}) => {
};

return plot => {
// QUICKFIX: Highlight some bars in a bar chart
if (highlight.length) {
plot.entities()
.filter(entity => highlight.indexOf(entity.datum.label) > -1)
.forEach(entity => {
const { x, y, width, height } = entity.selection.node().getBBox();

plot
.background()
.append('text')
.attr('class', 'plot-label hover-label')
.attr('x', orientation === 'vertical' ? x + ( width / 2 ) : width + x + 5)
.attr('y', orientation === 'horizontal' ? y + ( height / 2 ) : y - 10)
.attr('text-anchor', orientation === 'vertical' ? 'middle' : 'start')
.attr('style', `fill: ${entity.selection.attr("fill")}`)
.text(`${prefix}${approximate(entity.datum.value)}${suffix}`);
})
}


const interaction = new Plottable.Interactions.Pointer()
.onPointerExit(() => {
reset(plot);
Expand All @@ -59,6 +83,7 @@ const createBarInteraction = (orientation = 'vertical', labeling = {}) => {
.attr('x', orientation === 'vertical' ? x + ( width / 2 ) : width + x + 5)
.attr('y', orientation === 'horizontal' ? y + ( height / 2 ) : y - 10)
.attr('text-anchor', orientation === 'vertical' ? 'middle' : 'start')
.attr('style', `fill: ${entity.selection.attr("fill")}`)
.text(`${prefix}${approximate(entity.datum.value)}${suffix}`);

const fill = color(entity.selection.attr('initial-fill')).darker(0.7);
Expand Down

0 comments on commit 36de99c

Please sign in to comment.