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

How to display label data ONLY if the value is negative? #3947

Closed
Phil353556 opened this issue Feb 10, 2025 · 7 comments
Closed

How to display label data ONLY if the value is negative? #3947

Phil353556 opened this issue Feb 10, 2025 · 7 comments
Labels

Comments

@Phil353556
Copy link

Hello,

I look at the example and modify to have a negative value and to make the color red if <0
https://naver.github.io/billboard.js/demo/#Data.DataLabelColors
Image

It works, of course, however I should like to display the label ONLY for negative values otherwise, it can be unreadable.
as we can see on this screenshot
Image

Is it possible to do that and how to modify these lines?

labels: {
      colors: function(color, d) { return d.value <0 ? "red" : color }
    }

Thank you so much
Phil

@Phil353556
Copy link
Author

Phil353556 commented Feb 10, 2025

I found this workaround, however is there another better solution?


  labels: {
      colors: function(color, d) { return d.value < 0 ? "red" : 'white' ; }
    }

however it display in white color, I should want nothing or transparent color

Image

What about this?

labels: {
      colors: function(color, d) { return d.value < 0 ? "red" : "transparent"; }
    }

Image

@netil netil added the question label Feb 11, 2025
@netil
Copy link
Member

netil commented Feb 11, 2025

@Phil353556, if want to show only negative value, use data.labels.format instead.

bb.generate({
	data: {
		columns: [
			["data1", 30, -200, -100, 400, 150, 250],
			["data2", -50, 150, -150, 150, -50, -150]
		],
		labels: {
			format: v => v < 0 ? v : null
		}
	}
})

@Phil353556
Copy link
Author

Hi @netil,
Thank you so much for your answer.

Another question, how to add a comment on a graph?
I found something like below but I'm sure there is a better way to do.

<style> .annotation { position: absolute; top: 54%; left: 50%; transform: translate(-50%, -50%); background-color: red; padding: 10px; border: 1px solid black; z-index: 10; } </style>
comment comment comment comment

Phil

@netil
Copy link
Member

netil commented Feb 11, 2025

If you're finding solution from the option, you can use title option. Otherwise, you need to add your own customizing dom with styles.

@Phil353556
Copy link
Author

Hello @netil

Of course this is working:
labels: {
format: v => v < 0 ? v : null
}

however the value is not written in red color but with the color of the line.
Is it possible to display negative values in red color ?
Thank you
Phil

@netil
Copy link
Member

netil commented Feb 11, 2025

for color, need to specify data.lables.colors option. So you need to combine options.

bb.generate({
	data: {
		columns: [
			["data1", 30, -200, -100, 400, 150, 250],
			["data2", -50, 150, -150, 150, -50, -150]
		],
		labels: {
			format: v => v < 0 ? v : null,
			colors: "red"
		}
	}
});

@Phil353556
Copy link
Author

That's perfect, thank you so much.
Have a good day
Phil

@netil netil closed this as completed Feb 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants