2
$\begingroup$

I want to place a legend in a graph constructed with graphics primitives. Because some other elements use DropShadowing[{6,-6}] and RoundingRadius->x I want to use that in the frame of the legend, but I do not know how the LegendFuntion->"Frame" would allow customization. (I also want to control the Thickness of the frame.)

My effort to illustrate the problem is a Rectangle with DropShadowing and a legend that is a frame:

Legended[Graphics[{[email protected], FaceForm@None, EdgeForm@Thick,
   Rectangle[corner = {8, ySize = 5} (sclg = .8), -corner], 
   FaceForm[White], DropShadowing[{6, -6}], 
   Rectangle[{-2, 0}, {0, 3}, RoundingRadius -> .1]}
  , ImageSize -> 1667], 
 Placed[PointLegend[{Red, Blue, Red, Blue, Red, Blue, Red, Blue, Red, 
    Blue}, {"One", "Two", "One", "Two", "One", "Two", "One", "Two", 
    "One", "Two"}, LegendMarkers -> Graphics@Rectangle[], 
   LegendMarkerSize -> 15, LegendFunction -> "Frame", 
   LegendLabel -> "Legend", 
   LegendLayout -> "Row"], {.15, .81}]]

Thanks for your thoughts! (I shall grant you that the documentation contains a sample function for creating a drop-shadow legend, but that seems not to automatically adjust size as the legend changes and uses neither DropShadowing nor RoundingRadius which would likely simplify things.)

$\endgroup$

1 Answer 1

7
$\begingroup$
ClearAll[dropShadow]
Options[dropShadow] = 
  {RoundingRadius -> 5, frameBackground -> White,  FrameStyle -> Gray};

dropShadow[args___, opts : OptionsPattern[{dropShadow, Graphics}]] := 
  Module[{frameOptions = Thread[{RoundingRadius, Background, FrameStyle} -> 
        OptionValue[{RoundingRadius, frameBackground, FrameStyle}]]}, 
    Graphics[{DropShadowing[args], Inset[Framed[#, frameOptions]]}, 
     ImageSize -> Automatic -> Rasterize[#, "RasterSize"], 
     FilterRules[{opts}, Except[Options[dropShadow]]]]] &;

Examples:

colors = {Red, Green, Blue, Orange};
labels = {"label1", "label2", "label3", "label4"};

SwatchLegend[colors, labels, LegendFunction -> dropShadow[]]

enter image description here

SwatchLegend[colors, labels, LegendFunction -> dropShadow[{6, -6}]]

enter image description here

SwatchLegend[colors, labels, LegendMargins -> 10, 
 LegendMarkerSize -> 45, LabelStyle -> 32, 
 LegendFunction -> dropShadow[{20, -25}, 10, Green]]

enter image description here

SwatchLegend[colors, labels, LegendLayout -> "Row", 
 LegendMargins -> 10, LegendMarkers -> "Bubble", 
 LegendMarkerSize -> 45, LabelStyle -> 14, 
 LegendFunction -> 
  dropShadow[{10, -10}, frameBackground -> LightGray, 
   Background -> LightBlue, ImagePadding -> 10]]

enter image description here

Row[Append[#[colors, labels, LegendFunction -> 
    dropShadow[{6, -6}, frameBackground -> LightBlue]] & /@ 
      {PointLegend, LineLegend}, 
  BarLegend[{"TemperatureMap", {0, 10}}, LegendLayout -> "Row", 
   LegendFunction -> 
     dropShadow[{6, -6}, frameBackground -> LightBlue, ImagePadding -> 30]]], 
  Spacer[10]]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.