addNode generates a graph node in DOT format.
(node *Node, nodeID int, maxFlat float64)
| 139 | |
| 140 | // addNode generates a graph node in DOT format. |
| 141 | func (b *builder) addNode(node *Node, nodeID int, maxFlat float64) { |
| 142 | flat, cum := node.FlatValue(), node.CumValue() |
| 143 | attrs := b.attributes.Nodes[node] |
| 144 | |
| 145 | // Populate label for node. |
| 146 | var label string |
| 147 | if attrs != nil && attrs.Formatter != nil { |
| 148 | label = attrs.Formatter(&node.Info) |
| 149 | } else { |
| 150 | label = multilinePrintableName(&node.Info) |
| 151 | } |
| 152 | |
| 153 | flatValue := b.config.FormatValue(flat) |
| 154 | if flat != 0 { |
| 155 | label = label + fmt.Sprintf(`%s (%s)`, |
| 156 | flatValue, |
| 157 | strings.TrimSpace(measurement.Percentage(flat, b.config.Total))) |
| 158 | } else { |
| 159 | label = label + "0" |
| 160 | } |
| 161 | cumValue := flatValue |
| 162 | if cum != flat { |
| 163 | if flat != 0 { |
| 164 | label = label + `\n` |
| 165 | } else { |
| 166 | label = label + " " |
| 167 | } |
| 168 | cumValue = b.config.FormatValue(cum) |
| 169 | label = label + fmt.Sprintf(`of %s (%s)`, |
| 170 | cumValue, |
| 171 | strings.TrimSpace(measurement.Percentage(cum, b.config.Total))) |
| 172 | } |
| 173 | |
| 174 | // Scale font sizes from 8 to 24 based on percentage of flat frequency. |
| 175 | // Use non linear growth to emphasize the size difference. |
| 176 | baseFontSize, maxFontGrowth := 8, 16.0 |
| 177 | fontSize := baseFontSize |
| 178 | if maxFlat != 0 && flat != 0 && float64(abs64(flat)) <= maxFlat { |
| 179 | fontSize += int(math.Ceil(maxFontGrowth * math.Sqrt(float64(abs64(flat))/maxFlat))) |
| 180 | } |
| 181 | |
| 182 | // Determine node shape. |
| 183 | shape := "box" |
| 184 | if attrs != nil && attrs.Shape != "" { |
| 185 | shape = attrs.Shape |
| 186 | } |
| 187 | |
| 188 | // Create DOT attribute for node. |
| 189 | attr := fmt.Sprintf(`label="%s" id="node%d" fontsize=%d shape=%s tooltip="%s (%s)" color="%s" fillcolor="%s"`, |
| 190 | label, nodeID, fontSize, shape, escapeForDot(node.Info.PrintableName()), cumValue, |
| 191 | dotColor(float64(node.CumValue())/float64(abs64(b.config.Total)), false), |
| 192 | dotColor(float64(node.CumValue())/float64(abs64(b.config.Total)), true)) |
| 193 | |
| 194 | // Add on extra attributes if provided. |
| 195 | if attrs != nil { |
| 196 | // Make bold if specified. |
| 197 | if attrs.Bold { |
| 198 | attr += ` style="bold,filled"` |
no test coverage detected