Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide gauge labels when value is hidden #34171

Merged
merged 4 commits into from
Apr 1, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 30 additions & 25 deletions src/legacy/ui/public/vislib/visualizations/gauges/meter.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,34 @@ export function MeterGaugeProvider() {
const smallContainer = svg.node().getBBox().height < 70;
let hiddenLabels = smallContainer;

// If the value label is hidden we later want to hide also all other labels
// since they don't make sense as long as the actual value is hidden.
let valueLabelHidden = false;

gauges
.append('text')
.attr('class', 'chart-label')
.attr('y', -5)
.text(d => {
if (this.gaugeConfig.percentageMode) {
const percentage = Math.round(100 * (d.y - min) / (max - min));
return `${percentage}%`;
}
return data.yAxisFormatter(d.y);
})
.attr('style', 'dominant-baseline: central;')
.style('text-anchor', 'middle')
.style('font-size', '2em')
.style('display', function () {
const textLength = this.getBBox().width;
const textTooLong = textLength > maxRadius;
if (textTooLong) {
hiddenLabels = true;
valueLabelHidden = true;
}
return textTooLong ? 'none' : 'initial';
});

if (this.gaugeConfig.labels.show) {
svg
.append('text')
Expand All @@ -254,7 +282,7 @@ export function MeterGaugeProvider() {
if (textTooLong) {
hiddenLabels = true;
}
return smallContainer || textTooLong ? 'none' : 'initial';
return valueLabelHidden || smallContainer || textTooLong ? 'none' : 'initial';
});

svg
Expand All @@ -269,33 +297,10 @@ export function MeterGaugeProvider() {
if (textTooLong) {
hiddenLabels = true;
}
return smallContainer || textTooLong ? 'none' : 'initial';
return valueLabelHidden || smallContainer || textTooLong ? 'none' : 'initial';
});
}

gauges
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This (value label) has just been moved above the other two labels, so we know whether or not that's shown.

.append('text')
.attr('class', 'chart-label')
.attr('y', -5)
.text(d => {
if (this.gaugeConfig.percentageMode) {
const percentage = Math.round(100 * (d.y - min) / (max - min));
return `${percentage}%`;
}
return data.yAxisFormatter(d.y);
})
.attr('style', 'dominant-baseline: central;')
.style('text-anchor', 'middle')
.style('font-size', '2em')
.style('display', function () {
const textLength = this.getBBox().width;
const textTooLong = textLength > maxRadius;
if (textTooLong) {
hiddenLabels = true;
}
return textTooLong ? 'none' : 'initial';
});

if (this.gaugeConfig.scale.show) {
this.drawScale(svg, radius(1), angle);
}
Expand Down