8
$\begingroup$

This is a follow-up question to: Unable to export a formatted graph?

I trying to layout a graph so that if you zoom in on a vertex, things look nice. To see what I mean by nice, let me just describe how I would like to make the edges connect to the vertices with a few examples:

Here's my starting point, with a simple graph using a typical layout:

CompleteGraph[5, 
 VertexShape -> 
  Table[n -> 
    Graphics[
     Inset[Style[n, FontFamily -> "Source Sans Pro", FontSize -> 25, 
       Background -> None]]], {n, 5}], 
 EdgeStyle -> Directive[AbsoluteThickness[5], CapForm["Round"]], 
 VertexSize -> 0.25, PerformanceGoal -> "Quality", ImageSize -> 200]

enter image description here

This looks great, but if you zoom in on the vertices, we can start to see some issues. The minimum distances from the glyph to the edges vary a lot, moreover the curve through the edge endpoints isn't short and smooth.

enter image description here

So we don't simply want our edge endpoints equidistant from the vertex center coordinate. Instead we want edges sensitive to some bounding oval of the vertex label's glyph, like this:

enter image description here

Here's another more detailed example for vertex 3, which shows on the left what we start with, and and the right what I want.

enter image description here

Perhaps there's a simple way to do this, RegionNearest comes to mind, but may be overkill:

r = RegionNearest[Disk[{0, 0}, {3, 4}], {5, 5}];
Graphics[{{LightGray, Disk[{0, 0}, {3, 4}]}, 
  Inset[Style["3", FontSize -> 250, FontColor -> Black], {0, 
    0}], {PointSize[Large], {Red, Point[{5, 5}]}, {Blue, Point[r]}, 
   Line[{{5, 5}, r}]}}]

enter image description here

$\endgroup$
6
$\begingroup$
cg = CompleteGraph[7, 
 VertexLabels -> Thread[Range[7] -> (Placed[Style[#, 25], Center] & /@ Range[7])], 
 VertexLabelStyle -> Directive[FontFamily -> "Source Sans Pro"], 
 EdgeStyle -> Directive[AbsoluteThickness[5], CapForm["Round"]], 
 VertexSize -> Scaled[0.01], PerformanceGoal -> "Quality", 
 ImageSize -> 200, 
 VertexShapeFunction -> ({{Opacity[0], EdgeForm[], Disk[#, Scaled@{3/40, .1}]}} &)]

enter image description here

Use EdgeForm[Gray] to get

enter image description here

ImageCrop[Rasterize@Magnify[%, 5], {300, 300}, {1, -.7}]

enter image description here

Update from Comments:

To fix the misaligned labels (they don't look "centered"), @kglr suggested:

graphicsNumbers = 
  Graphics[First[
      First[ImportString[
        ExportString[
         Style[#, FontSize -> Scaled[.1], 
          FontFamily -> "Source Sans Pro"], "PDF"], "PDF", 
        "TextMode" -> "Outlines"]]]] & /@ Range[7];
cg = CompleteGraph[7, 
  VertexLabels -> 
   Thread[Range[
      7] -> (Placed[
         Graphics[Inset[#, {1/2, 1/2}, Automatic, Scaled[.1]]], 
         Center] & /@ graphicsNumbers)], 
  EdgeStyle -> Directive[AbsoluteThickness[5], CapForm["Round"]], 
  VertexSize -> Scaled[0.01], PerformanceGoal -> "Quality", 
  ImageSize -> 200, 
  VertexShapeFunction -> ({{Opacity[0], EdgeForm[], 
       Disk[#, Scaled@{3/40, .1}]}} &), Background -> None]

But this produces the same graph as above.

|improve this answer|||||
$\endgroup$
  • $\begingroup$ I noticed, If you look at the 7, 6, 4, 3, or 2, it’s a little bit off. Can we enter the ellipse at the centroid of the number? $\endgroup$ – M.R. Jan 22 '18 at 13:31
  • $\begingroup$ @M.R., I think it is related to the fonts rendering off- center issue: see this and this. Using Graphics versions of fonts should make it better. $\endgroup$ – kglr Jan 22 '18 at 18:33
  • $\begingroup$ by graphics version of fonts do you mean using Inset or something else? $\endgroup$ – M.R. Jan 22 '18 at 21:05
  • $\begingroup$ @M.R. I meant using Inset; e.g., something like graphicsNumbers = Graphics[First[ First[ImportString[ ExportString[ Style[#, FontSize -> Scaled[.1], FontFamily -> "Source Sans Pro"], "PDF"], "PDF", "TextMode" -> "Outlines"]]]] & /@ Range[7]; and changing the vertex labels option to VertexLabels -> Thread[Range[ 7] -> (Placed[ Graphics[Inset[#, {1/2, 1/2}, Automatic, Scaled[.1]]], Center] & /@ graphicsNumbers)]. $\endgroup$ – kglr Jan 22 '18 at 21:12
  • $\begingroup$ I added that to your answer, but I zoomed in and saw that it has no effect $\endgroup$ – M.R. Jan 22 '18 at 21:51
3
$\begingroup$

Could use VertexSize -> {a, b}:

CompleteGraph[5, 
 VertexLabelStyle -> 
  Directive[FontFamily -> "Source Sans Pro", FontSize -> 25], 
 VertexLabels -> Placed["Name", Center], VertexShapeFunction -> None, 
 EdgeStyle -> Directive[AbsoluteThickness[5], CapForm["Round"]], 
 VertexSize -> {.15, .2}, PerformanceGoal -> "Quality", 
 ImageSize -> 200]
|improve this answer|||||
$\endgroup$
  • $\begingroup$ Do you have any tips for this one? $\endgroup$ – Szabolcs Jan 28 '18 at 13:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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