Code Monkey home page Code Monkey logo

margaid's Introduction

Margaid โ‡„ diagraM

The world surely doesn't need another plotting library. But I did, and that's why Margaid was born.

Margaid is a small, no dependencies Golang library for plotting 2D data to SVG images. The idea is to create nice charts with a few lines of code and not having to bring in heavy machinery.

"Margaid" is an old name meaning "pearl", which seemed fitting for something shiny and small. It's also the word "diagraM" spelled backwards.

Features

Margaid plots series of data to an SVG image. Series can be capped by size or time to simplify realtime data collection.

Plots are drawn using straight lines, smooth lines or bars.

Each axis has a fixed or automatic range, linear or log projection, configurable labels and optional grid lines.

Plot colors are automatically picked for each new plot, trying to spread them in hue and saturation to get a good mix.

There is no clever layout or layering going on. Each new command draws on top of the results from previous commands.

Getting started

Minimal example

Minimal plot

These are the minimal steps needed to create a Margaid plot:

  • Import the library
import "github.com/erkkah/margaid"
  • Create a series and add some values
series := margaid.NewSeries()
series.Add(margaid.MakeValue(10, 3.14), margaid.MakeValue(90, 93.8))
// et.c.
  • Create the diagram:
diagram := margaid.New(800, 600)
  • Plot the series
diagram.Line(series)
  • Add a frame and X axis
diagram.Frame()
diagram.Axis(series, margaid.XAxis, diagram.ValueTicker('f', 2, 10), false, "Values")
  • Render to stdout
diagram.Render(os.Stdout)

Example showing more features

Example plot

To generate the diagram above from the code shown below:

> go run -tags example ./example > example.svg
// example/example.go
package main

import (
    "math/rand"
    "os"
    "time"

    m "github.com/erkkah/margaid"
)

func main() {

    randomSeries := m.NewSeries()
    rand.Seed(time.Now().Unix())
    for i := float64(0); i < 10; i++ {
        randomSeries.Add(m.MakeValue(i+1, 200*rand.Float64()))
    }

    testSeries := m.NewSeries()
    multiplier := 2.1
    v := 0.33
    for i := float64(0); i < 10; i++ {
        v *= multiplier
        testSeries.Add(m.MakeValue(i+1, v))
    }

    diagram := m.New(800, 600,
        m.WithAutorange(m.XAxis, testSeries),
        m.WithAutorange(m.YAxis, testSeries),
        m.WithAutorange(m.Y2Axis, testSeries),
        m.WithProjection(m.YAxis, m.Log),
        m.WithInset(70),
        m.WithPadding(2),
        m.WithColorScheme(90),
    )

    diagram.Line(testSeries, m.UsingAxes(m.XAxis, m.YAxis), m.UsingMarker("square"), m.UsingStrokeWidth(1))
    diagram.Smooth(testSeries, m.UsingAxes(m.XAxis, m.Y2Axis), m.UsingStrokeWidth(3.14))
    diagram.Smooth(randomSeries, m.UsingAxes(m.XAxis, m.YAxis), m.UsingMarker("filled-circle"))
    diagram.Axis(testSeries, m.XAxis, diagram.ValueTicker('f', 0, 10), false, "X")
    diagram.Axis(testSeries, m.YAxis, diagram.ValueTicker('f', 1, 2), true, "Y")

    diagram.Frame()
    diagram.Title("A diagram of sorts ๐Ÿ“Š ๐Ÿ“ˆ")

    diagram.Render(os.Stdout)
}

Documentation

For more details, check the reference documentation.

margaid's People

Contributors

erkkah avatar janpfeifer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

margaid's Issues

Option to set a solid background ?

hi @erkkah,

Just a detail, but the examples in the README.md are not very visible if in dark mode. I was wondering if there is an easy way to add an option to set a solid background (default to white instead of transparent?) in the generated SVG ?

cheers, and thanks for the library! :)

set series title

I just discovered margaid and like it very much.
However, I cannot see how to set a series title apart from at creation time.
I need to set the series title once values have been accumulated and calculation done on these values. is there a way to achieve that?

pseudo code to illustrate:

for i,d:=range data{
   ...
 series[i] = m.NewSeries()
avg=0  
 for j:=0;j<len(data.x);j++{
      somevalue= f(data.y[i])
     avg+=somevalue
     series[i].Add(m.MakeValue(data.x[i],somevalue))
    }
avg:=avg/float64(len(data.x))

series[i].SetTitle("seriesname "+ strconv.FormatFloat(avg, ...)  //how do I achieve this?
}

stroke width

how can I change stroke width from default 3px to 1px? Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.