2
$\begingroup$

This Q is related to this one but haven't found a way to extend it to the use of VertexLabels in panels.

Given this graph data:

CCIFigure = { {"CCI Groups Dictionary", {0, 1}, 
     a}, {"Computational CCI Groups", {0, 1/2}, 
     b}, {"Scraped VASD Text/Codes", {-1, 0}, 
     a}, {"Extracted UCSD Codes", {1, 0}, 
     a}, {"Normalized ICD9", {0, 0}, b}} // associationIndex //    Dataset

This graph has combinations of directed and undirected edges:

Graph[{1 \[DirectedEdge] 2, 2 \[UndirectedEdge] 5, 
  3 \[DirectedEdge] 5, 4 \[DirectedEdge] 5},
 VertexCoordinates -> Normal@Normal[CCIFigure [All, 2]],
 VertexLabels -> Normal@Normal[CCIFigure [All, 1]]]

enter image description here

However, using VertexLabels in panels as per reference pages:

Graph[{1 \[DirectedEdge] 2, 2 \[UndirectedEdge] 5, 
  3 \[DirectedEdge] 5, 4 \[DirectedEdge] 5},
 VertexCoordinates -> Normal@Normal[CCIFigure [All, 2]],
 VertexLabels -> 
  Normal@Normal[CCIFigure [All, 1 /*  (Placed[#, Center, panel] &)]]]

results in arrowheads occluded by the panel:

enter image description here

Is there some combination of options or workaround to fix without knowing in advance the length of the text or panel margins?

Graph layout would have to be aware of bounding box, but from WTC2017 there doesn't seem to be a user-friendly solution to this.

helper functions:

panel[lbl_] := 
 Panel[lbl, FrameMargins -> 0, Background -> Lighter[Yellow, 0.7]]

and

associationIndex[l_List]:=Query[MapIndexed[First[#2]->#1&]/*Association][l]
$\endgroup$
  • $\begingroup$ Did you already try using Placed[#, {After, Above}, panel] &? $\endgroup$ – J. M.'s technical difficulties Mar 17 '18 at 23:54
  • $\begingroup$ @J.M., I see {After,Above} puts panels besides the vertices which are rendered as circles - it's an option I will consider but it's not as clean as the panels themselves as the vertices. $\endgroup$ – alancalvitti Mar 18 '18 at 1:58
2
$\begingroup$
f = Normal @ Normal[CCIFigure[All, 1 /* panel]] /. Rule[a_, b_] :> Rule[a, Inset[b, #]&];

Graph[{1 -> 2, 2 <-> 5,  3 -> 5, 4 -> 5}, 
 VertexShapeFunction -> f, 
 VertexCoordinates -> Normal @ Normal[CCIFigure [All, 2]], 
 PerformanceGoal -> "Quality"]

enter image description here

As @Szabolcs noted in a comment:

The key points here are:
1. use PerformanceGoal -> "Quality" to auto-offset the arrows
2. the offsetting is done for the vertex shape, not the vertex label.

Update: A more cumbersome alternative is using VertexSize:

g = Graph[ {1 -> 2, 2 <-> 5, 3 -> 5, 4 -> 5}, 
  VertexCoordinates -> Normal@Normal[CCIFigure [All, 2]],
  VertexLabels -> Normal@Normal[CCIFigure [All, 1 /*  (Placed[#, Center, panel] &)]], 
  ImageSize -> {600, 400}]

enter image description here

labelsizes = Normal[Values[ImageDimensions /@ Rasterize /@ panel /@ CCIFigure[[All, 1]]]];
vsizes = MapIndexed[#2[[1]] -> # &, 
   ImageScaled /@ N@Transpose[Transpose[labelsizes]/ImageDimensions[Image@g]]];
SetProperty[g, {VertexShapeFunction -> None, VertexSize -> vsizes, 
  PerformanceGoal -> "Quality", ImagePadding -> 100}]

enter image description here

|improve this answer|||||
$\endgroup$
  • 1
    $\begingroup$ +1, but in an era of Siri and "deep learning" why do we have to manually do all this? $\endgroup$ – alancalvitti Mar 18 '18 at 1:59
  • $\begingroup$ @alan, we won't have to in version 11.4 i hope:) $\endgroup$ – kglr Mar 18 '18 at 2:01
  • $\begingroup$ @kglr Do you know anything about 11.4? $\endgroup$ – Szabolcs Mar 18 '18 at 10:27
  • 1
    $\begingroup$ @alancalvitti The key points here are: 1. use PerformanceGoal -> "Quality" to auto-offset the arrows 2. the offsetting is done for the vertex shape, not the vertex label. Originally you simply overlaid labels that were larger than the vertices. $\endgroup$ – Szabolcs Mar 18 '18 at 10:38
  • 2
    $\begingroup$ @alancalvitti I addressed that in point 2: offsetting is done for the vertex shape, not the label. You just added labels. $\endgroup$ – Szabolcs Mar 18 '18 at 16:41

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.