-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
"text-color": "{color}" not working #1873
Comments
How about if you set color to rgba(255, 255, 0, 1)? |
Oh, I just realised you probably can't do it like that. As far as I know You will have to accomplish your dynamic coloring using filters (https://www.mapbox.com/mapbox-gl-style-spec/#types-filter), which, however, provides a little less flexibility. |
@averas is 100% correct -- you'll need to wait for data-driven styling to be implemented for full flexibility, and use multiple filtered layers in the meantime. |
Just to make sure I understand it correctly. If I want to achieve the following using filters. Suppose every marker represents a car that can be red green orange. I have to create 3 different layers for every car using filters and then create code to manage all those layers going through the geojson properties? |
If you only need three colors you only need three layers. Your data can still be a single source (for example GeoJSON). If you add a property to your data set such as "color" which can be "green", "orange" and "red" a filter on one of these layers might looks like: ... paint: { fill-color: "#00ff00" } ... "filter": [ "==", "color", "green" ] ... |
Three layers multiplied by every single car on the map right? (Let say it represents the fuel of a car as example) |
If the fuel level is normalised to a scale of "red", "orange", "green" you will only need to see to it that your application code maintains that property on your data set and you will, again, get away with three layers -- one for each color. |
Ok I think I understand now thanks. So 1 layer can represent all the red cars on the map for example. 2 layer all the orange, and third layer all the green cars. |
Correct, and you only need one source data set. |
Has data-driven styling been implemented yet? |
@ericjames yes, data-driven styling is available for text-color. You can see SDK support tables detailing which version of any Mapbox GL SDK first supported data-driven styling for any given paint property on the style spec page: |
It's worth noting that the So what actually works is: Hopefully this will save someone some time. |
Well, actually you can use color from the property field, by using the Example:
javascript code:
Works great! See the same for drawing a line, example: |
You know what also works?... 👍 This! // RGB mapping
var redColor = [200, 86, 100];
var greenColor = [70, 180, 50];
var blueColor = [57, 74, 210];
...
'text-color': [
"rgb",
["at", ['get', 'type'], ["literal", redColor]],
["at", ['get', 'type'], ["literal", greenColor]],
["at", ['get', 'type'], ["literal", blueColor]]
] Or something similar, but in this case directly use a hex (#) value: // HTML Hex color array
var colors = ["#9A3F46", "#7AE8A7", "#D056AE"];
...
'text-color': ["to-string", ['at', ['get', 'type'], ["literal", colors]]] I think Mapbox should really put some examples likes these to their website! |
Uncaught TypeError: Cannot read property '0' of null
color is set to
#ffff00
I can check it by doing "text-field": "{color}" and i can see #ffff00 text on the map
The text was updated successfully, but these errors were encountered: