gosvgchart

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2025 License: MIT Imports: 3 Imported by: 0

README

GoSVGChart

A simple, declarative SVG chart library for Go. This library allows you to create various chart types with a clean, chainable API or a simple markdown-like text format.

Features

  • Multiple ways to create charts:
    • Purely declarative Go API with method chaining
    • Simple markdown-like text format (great for LLM-generated charts)
    • Web server for dynamic chart generation
    • Goldmark extension for embedding charts in markdown documents
  • No external dependencies for core functionality (uses only the Go standard library)
  • Generates SVG output that can be used in web applications or saved to files
  • Supports multiple chart types:
    • Line charts
    • Bar charts
    • Pie/Donut charts
  • Customizable styling and options

Installation

go get github.com/riclib/gosvgchart

Usage (Go API)

Line Chart
import (
    "github.com/riclib/gosvgchart"
    "os"
)

func main() {
    // Create a line chart
    chart := gosvgchart.New().
        SetTitle("Monthly Sales").
        SetData([]float64{120, 250, 180, 310, 270, 390}).
        SetLabels([]string{"Jan", "Feb", "Mar", "Apr", "May", "Jun"}).
        SetColors([]string{"#3498db"}).
        SetSize(600, 400)

    // Render to SVG string
    svg := chart.Render()

    // Save to file
    os.WriteFile("line_chart.svg", []byte(svg), 0644)
}
Bar Chart
// Create a bar chart
chart := gosvgchart.New().
    SetTitle("Quarterly Revenue").
    SetData([]float64{850, 940, 1100, 1200}).
    SetLabels([]string{"Q1", "Q2", "Q3", "Q4"}).
    SetColors([]string{"#2ecc71", "#e74c3c", "#f39c12", "#9b59b6"}).
    SetSize(600, 400)

// Render to SVG string
svg := chart.Render()
Pie Chart
// Create a pie chart
chart := gosvgchart.New().
    SetTitle("Market Share").
    SetData([]float64{35, 25, 20, 15, 5}).
    SetLabels([]string{"Product A", "Product B", "Product C", "Product D", "Others"}).
    SetColors([]string{"#3498db", "#2ecc71", "#e74c3c", "#f39c12", "#9b59b6"}).
    SetSize(600, 500)

// For a donut chart, add:
chart.SetDonutHole(0.6) // 0-0.9, where 0 is a pie chart and 0.9 is a thin donut

// Render to SVG string
svg := chart.Render()

Markdown Chart Format

For an even simpler way to create charts, especially when working with LLMs or in text environments, you can use the markdown-like format:

Line Chart Example
linechart
title: Monthly Sales
width: 600
height: 400
colors: #3498db, #e74c3c

data:
Jan | 120
Feb | 250
Mar | 180
Apr | 310
May | 270
Jun | 390
Bar Chart Example
barchart
title: Quarterly Revenue
width: 600
height: 400
colors: #2ecc71, #e74c3c, #f39c12, #9b59b6

data:
Q1 | 850
Q2 | 940
Q3 | 1100
Q4 | 1200
Pie Chart Example
piechart
title: Market Share
width: 600
height: 500
colors: #3498db, #2ecc71, #e74c3c, #f39c12, #9b59b6

data:
Product A | 35
Product B | 25
Product C | 20
Product D | 15
Others | 5
Side-by-Side Charts Example

When using the Goldmark extension, you can place multiple charts side by side by placing the chart blocks directly adjacent to each other:

barchart
title: 2023 Revenue
width: 450
height: 300
colors: #3498db, #2ecc71

data:
Q1 | 850
Q2 | 940
Q3 | 1100
Q4 | 1200
barchart
title: 2024 Revenue
width: 450
height: 300
colors: #e74c3c, #f39c12

data:
Q1 | 950
Q2 | 1040
Q3 | 1200
Q4 | 1400

Command Line Tools

Convert Markdown to SVG
go run cmd/mdchart/main.go -input chart.md -output chart.svg
Run Web Server
go run cmd/mdchartserver/main.go -port 8080

Then visit:

  • http://localhost:8080 for the web UI
  • POST to http://localhost:8080/chart with markdown content to get SVG
  • GET to http://localhost:8080/charturl?md=linechart_n_title:Test_n_data:_n_A_p_10_n_B_p_20 to get SVG directly via URL

Goldmark Extension

GoSVGChart provides a Goldmark extension that allows you to embed charts directly in your markdown documents:

import (
    "github.com/riclib/gosvgchart/goldmark"
    gm "github.com/yuin/goldmark"
)

// Create a new Goldmark instance with the gosvgchart extension
markdown := gm.New(
    gm.WithExtensions(
        goldmark.New(),
    ),
)

// Convert markdown to HTML with embedded SVG charts
var output []byte
if err := markdown.Convert([]byte(markdownContent), &output); err != nil {
    // Handle error
}

Use the extension in your markdown with fenced code blocks:

```gosvgchart
barchart
title: Quarterly Revenue
width: 600
height: 400
colors: #2ecc71, #e74c3c, #f39c12, #9b59b6

data:
Q1 | 850
Q2 | 940
Q3 | 1100
Q4 | 1200
```

For more details on the Goldmark extension, see the goldmark directory.

Common Methods (Go API)

All chart types share these common methods:

Method Description
SetTitle(title string) Sets the chart title
SetSize(width, height int) Sets the chart dimensions in pixels
SetData(data []float64) Sets the chart data values
SetLabels(labels []string) Sets the chart labels
SetColors(colors []string) Sets the color palette as hex values (e.g., "#ff0000")
Render() Renders the chart to an SVG string

Chart-Specific Methods (Go API)

Line Chart
Method Description
ShowDataPoints(show bool) Shows or hides data points
SetSmooth(smooth bool) Enables smooth curved lines (when true)
Bar Chart
Method Description
SetHorizontal(horizontal bool) Displays bars horizontally (when true)
SetStacked(stacked bool) Stacks multiple data series (when true)
Pie Chart
Method Description
SetDonutHole(percentage float64) Sets the inner circle size (0-0.9)

Design Philosophy

GoSVGChart was designed with these principles in mind:

  1. Simplicity: The API should be intuitive and require minimal code
  2. Zero Dependencies: Only use the Go standard library
  3. Declarative Syntax: Use method chaining for a clean, readable configuration
  4. LLM-Friendly: Easy to explain to and be used by Large Language Models
  5. Multiple Interfaces: Support both code and text-based chart definitions

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BarChart

type BarChart struct {
	BaseChart
	Horizontal bool
	Stacked    bool
}

BarChart implements a bar chart

func NewBarChart

func NewBarChart() *BarChart

NewBarChart creates a new bar chart with default settings

func (*BarChart) Render

func (c *BarChart) Render() string

Render renders the bar chart to an SVG string

func (*BarChart) SetColors

func (c *BarChart) SetColors(colors []string) Chart

SetColors sets the color palette as hex values

func (*BarChart) SetData

func (c *BarChart) SetData(data []float64) Chart

SetData sets the chart data values

func (*BarChart) SetHorizontal

func (c *BarChart) SetHorizontal(horizontal bool) *BarChart

SetHorizontal displays bars horizontally

func (*BarChart) SetLabels

func (c *BarChart) SetLabels(labels []string) Chart

SetLabels sets the chart labels

func (*BarChart) SetSize

func (c *BarChart) SetSize(width, height int) Chart

SetSize sets the chart dimensions in pixels

func (*BarChart) SetStacked

func (c *BarChart) SetStacked(stacked bool) *BarChart

SetStacked stacks multiple data series

func (*BarChart) SetTitle

func (c *BarChart) SetTitle(title string) Chart

SetTitle sets the chart title

type BaseChart

type BaseChart struct {
	ChartType  string
	Title      string
	Width      int
	Height     int
	Data       []float64
	Labels     []string
	Colors     []string
	ShowTitle  bool
	ShowLegend bool
	Margin     struct {
		Top    int
		Right  int
		Bottom int
		Left   int
	}
	BackgroundColor string
}

BaseChart contains common properties and methods for all chart types

type Chart

type Chart interface {
	SetTitle(title string) Chart
	SetSize(width, height int) Chart
	SetData(data []float64) Chart
	SetLabels(labels []string) Chart
	SetColors(colors []string) Chart
	Render() string
}

Chart is the interface that all chart types must implement

func New

func New() Chart

New creates a new chart with default settings

type LineChart

type LineChart struct {
	BaseChart
	ShowPoints bool
	Smooth     bool
}

LineChart implements a line chart

func NewLineChart

func NewLineChart() *LineChart

NewLineChart creates a new line chart with default settings

func (*LineChart) Render

func (c *LineChart) Render() string

Render renders the line chart to an SVG string

func (*LineChart) SetColors

func (c *LineChart) SetColors(colors []string) Chart

SetColors sets the color palette as hex values

func (*LineChart) SetData

func (c *LineChart) SetData(data []float64) Chart

SetData sets the chart data values

func (*LineChart) SetLabels

func (c *LineChart) SetLabels(labels []string) Chart

SetLabels sets the chart labels

func (*LineChart) SetSize

func (c *LineChart) SetSize(width, height int) Chart

SetSize sets the chart dimensions in pixels

func (*LineChart) SetSmooth

func (c *LineChart) SetSmooth(smooth bool) *LineChart

SetSmooth enables smooth curved lines

func (*LineChart) SetTitle

func (c *LineChart) SetTitle(title string) Chart

SetTitle sets the chart title

func (*LineChart) ShowDataPoints

func (c *LineChart) ShowDataPoints(show bool) *LineChart

ShowDataPoints shows or hides data points for line charts

type PieChart

type PieChart struct {
	BaseChart
	DonutHolePercentage float64
}

PieChart implements a pie/donut chart

func NewPieChart

func NewPieChart() *PieChart

NewPieChart creates a new pie chart with default settings

func (*PieChart) Render

func (c *PieChart) Render() string

Render renders the pie chart to an SVG string

func (*PieChart) SetColors

func (c *PieChart) SetColors(colors []string) Chart

SetColors sets the color palette as hex values

func (*PieChart) SetData

func (c *PieChart) SetData(data []float64) Chart

SetData sets the chart data values

func (*PieChart) SetDonutHole

func (c *PieChart) SetDonutHole(percentage float64) *PieChart

SetDonutHole sets the inner circle size for donut charts

func (*PieChart) SetLabels

func (c *PieChart) SetLabels(labels []string) Chart

SetLabels sets the chart labels

func (*PieChart) SetSize

func (c *PieChart) SetSize(width, height int) Chart

SetSize sets the chart dimensions in pixels

func (*PieChart) SetTitle

func (c *PieChart) SetTitle(title string) Chart

SetTitle sets the chart title

Directories

Path Synopsis
cmd
mdchart command
mdchartserver command
examples command

Jump to

Keyboard shortcuts

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