I am trying to form a Legend on my Observable Plot, and I have run into a couple of problems. I understand that from the documentation that I can choose a scale, set e.g., color { legend: true }
and then use stroke: "NAME"
where NAME is a column in the data set for the legend. The named column values become category values in the legend.
The first problem I encountered is when a categorical value matches an predefined color name. In my example I have categories named for locations on a map, but one of the locations is named for a color. If I use the color name as the value of the named “stroke” column, then the line will render in the explicit color listed but the legend will not include the corresponding category. This was easy to work around by slightly changing category name formatting to avoid the conflict.
The second problem is specific to multi-line plots. My application uses multiple timeseries of water quality data, and I have several numeric columns per plot (e.g., same parameter at two depths, same parameter at two locations). My goal is for the location name/position to be included in a legend. However, there is no specific column with a “stroke” value in this data, instead each column is a separate line.
Therefore, I want a static way to declare each line’s representation in a legend, but I would prefer if Observable would choose the color palette using its default logic. Thus for each lineY, I simply want to give the corresponding legend name, Observable should control the colors.
I haven’t been able to make this work. The site is published here: https://noyo-harbor-blue-economy.observablehq.cloud/feasibility-study/
FWIW, I am generating this markdown from a template, using a system I’ve written for this task. The template shows the lineY
expressions described above, which include manual color assignments (like , stroke: schemeObservable10[5]}
), but I wish I could easily generate a legend.
Looking for help, thanks!