decision_tree

package
v0.0.27 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 22, 2025 License: MIT Imports: 0 Imported by: 0

Documentation

Overview

Package decision_tree provides functionality to create and render decision trees as SVG.

The package supports: - JSON-based tree definition - Automatic layout calculation - SVG rendering with proper spacing and connections - Custom node styling

Example usage:

tree := &Node{
	ID:    "root",
	Label: "Start",
	Children: []*Node{
		{ID: "child1", Label: "Option 1"},
		{ID: "child2", Label: "Option 2"},
	},
}

renderer := NewRenderer(DefaultConfig())
svg := renderer.RenderTree(tree)

RoadMap: - [ ] Add background color for terminal nodes - [ ] Add extra legend - [ ] Adjust font size for conditions - [ ] Make terminal node size more appropriate - [ ] go-ddt supports rendering decision tree via server(live modification): go-ddt edit decision.dtree.json - [ ] Draw ascii tree - [ ] Serve via http

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Layout configuration
	LevelHeight        float64 // Vertical distance between levels
	NodeSpacing        float64 // Minimum horizontal space between nodes
	NodePadding        float64 // Text padding inside nodes
	BaseNodeWidth      float64 // Minimum node width
	LeafNodeSpacing    float64 // Horizontal spacing between leaf nodes (default: 1.5 * NodeSpacing)
	ParentChildSpacing float64 // Minimum vertical space between parent and child nodes
	VerticalSpanCoeff  float64 // Coefficient for vertical spacing scaling with children span (0.0-1.0)

	// Default styles
	DefaultStyle *NodeStyle
}

Config holds configuration for tree rendering

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns a default configuration

type Node

type Node struct {
	ID         string         `json:"id"`
	Label      string         `json:"label"`
	Conditions map[string]any `json:"conditions,omitempty"`
	Style      *NodeStyle     `json:"style,omitempty"`
	Children   []*Node        `json:"children,omitempty"`
}

Node represents a single node in the decision tree

func (*Node) Clone

func (n *Node) Clone() *Node

Clone creates a deep copy of the node and its children

type NodeStyle

type NodeStyle struct {
	Shape       string `json:"shape,omitempty"`       // rectangle, diamond
	Fill        string `json:"fill,omitempty"`        // CSS color
	Stroke      string `json:"stroke,omitempty"`      // CSS color
	StrokeWidth int    `json:"strokeWidth,omitempty"` // line width
}

NodeStyle defines visual properties for a node

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL