Giter Site home page Giter Site logo

go-chart's Introduction

go-chart

Build Status

Package chart is a very simple golang native charting library that supports timeseries and continuous line charts.

The v1.0 release has been tagged so things should be more or less stable, if something changes please log an issue.

Installation

To install chart run the following:

> go get -u github.com/wcharczuk/go-chart

Most of the components are interchangeable so feel free to crib whatever you want.

Usage

The chart code to produce the above is as follows:

// note this assumes that xvalues and yvalues
// have been pulled from a pricing service.
graph := chart.Chart{
    Width:  1024,
    Height: 400,
    YAxis: chart.YAxis {
        Style: chart.Style{
            Show: true,
        },
    },
    XAxis: chart.XAxis {
        Style: chart.Style{
            Show: true,
        },
    },
    Series: []chart.Series{
        chart.TimeSeries{
            XValues: xvalues,
            YValues: yvalues,
            Style: chart.Style {
                FillColor: chart.DefaultSeriesStrokeColors[0].WithAlpha(64),
            },
        },
        chart.AnnotationSeries{
            Name: "Last Value",
            Style: chart.Style{
                Show:        true,
                StrokeColor: chart.DefaultSeriesStrokeColors[0],
            },
            Annotations: []chart.Annotation{
                chart.Annotation{
                    X:     chart.TimeToFloat64(xvalues[len(xvalues)-1]),
                    Y:     yvalues[len(yvalues)-1],
                    Label: chart.FloatValueFormatter(yvalues[len(yvalues)-1]),
                },
            },
        },
    },
}
graph.Render(chart.PNG, buffer) //thats it!

The key areas to note are that we have to explicitly turn on two features, the axes and add the last value label annotation series. When calling .Render(..) we add a parameter, chart.PNG that tells the renderer to use a raster renderer. Another option is to use chart.SVG which will use the vector renderer and create an svg representation of the chart.

Alternate Usage

You can alternately leave a bunch of features turned off and constrain the proportions to something like a spark line:

The code to produce the above would be:

// note this assumes that xvalues and yvalues
// have been pulled from a pricing service.
graph := chart.Chart{
    Width:  1024,
    Height: 100,
    Series: []chart.Series{
        chart.TimeSeries{
            XValues: xvalues,
            YValues: yvalues,
        },
    },
}
graph.Render(chart.PNG, buffer)

2 Y-Axis Charts

It is also possible to draw series against 2 separate y-axis with their own ranges (usually good for comparison charts). In order to map the series to an alternate axis make sure to set the YAxis property of the series to YAxisSecondary.

graph := chart.Chart{
    Title: stock.Name,
    TitleStyle: chart.Style{
        Show: false,
    },
    Width:  width,
    Height: height,
    XAxis: chart.XAxis{
        Style: chart.Style{
            Show: true,
        },
    },
    YAxis: chart.YAxis{
        Style: chart.Style{
            Show: true,
        },
    },
    Series: []chart.Series{
        chart.TimeSeries{
            Name:    "vea",
            XValues: vx,
            YValues: vy,
            Style: chart.Style{
                Show: true,
                StrokeColor: chart.GetDefaultSeriesStrokeColor(0),
                FillColor:   chart.GetDefaultSeriesStrokeColor(0).WithAlpha(64),
            },
        },
        chart.TimeSeries{
            Name:    "spy",
            XValues: cx,
            YValues: cy,
            YAxis:   chart.YAxisSecondary,  // key (!)
            Style: chart.Style{
                Show: true,
                StrokeColor: chart.GetDefaultSeriesStrokeColor(1),
                FillColor:   chart.GetDefaultSeriesStrokeColor(1).WithAlpha(64),
            },
        },
        chart.AnnotationSeries{
            Name: fmt.Sprintf("%s - Last Value", "vea"),
            Style: chart.Style{
                Show:        true,
                StrokeColor: chart.GetDefaultSeriesStrokeColor(0),
            },
            Annotations: []chart.Annotation{
                chart.Annotation{
                    X:     float64(vx[len(vx)-1].Unix()),
                    Y:     vy[len(vy)-1],
                    Label: fmt.Sprintf("%s - %s", "vea", chart.FloatValueFormatter(vy[len(vy)-1])),
                },
            },
        },
        chart.AnnotationSeries{
            Name: fmt.Sprintf("%s - Last Value", "goog"),
            Style: chart.Style{
                Show:        true,
                StrokeColor: chart.GetDefaultSeriesStrokeColor(1),
            },
            YAxis: chart.YAxisSecondary, // key (!)
            Annotations: []chart.Annotation{
                chart.Annotation{
                    X:     float64(cx[len(cx)-1].Unix()),
                    Y:     cy[len(cy)-1],
                    Label: fmt.Sprintf("%s - %s", "goog", chart.FloatValueFormatter(cy[len(cy)-1])),
                },
            },
        },
    },
}
graph.Render(chart.PNG, buffer)

Moving Averages

You can now also graph a moving average of a series using a special MovingAverageSeries that takes an InnerSeries as a required argument.

There is a helper method, GetLastValue on the MovingAverageSeries to aid in creating a last value annotation for the series.

Design Philosophy

I wanted to make a charting library that used only native golang, that could be stood up on a server (i.e. it had built in fonts).

The goal with the API itself is to have the "zero value be useful", and to require the user to not code more than they absolutely needed.

Contributions

This library is super early but contributions are welcome.

go-chart's People

Contributors

wcharczuk avatar

Watchers

 avatar  avatar

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.