Skip to content
This repository has been archived by the owner on Aug 23, 2024. It is now read-only.

Commit

Permalink
Helper API refactor (#40)
Browse files Browse the repository at this point in the history
* api cleaup

* updates

* wtf

* updates

* snapshot.

* tweaks

* snapshot

* api tweaks.

* updates

* updates

* updates

* changes.

* updates

* updates

* sequence => seq

* dont need to use curl, just using wget

* fixing examples
  • Loading branch information
wcharczuk authored May 13, 2017
1 parent 43212f8 commit 03708a9
Show file tree
Hide file tree
Showing 100 changed files with 1,687 additions and 1,055 deletions.
Binary file added .DS_Store
Binary file not shown.
Binary file modified _examples/annotations/output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _examples/axes/output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _examples/axes_labels/output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _examples/bar_chart/output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _examples/basic/output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions _examples/benchmark_line_charts/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,18 @@ func drawLargeChart(res http.ResponseWriter, r *http.Request) {
if err != nil {
numSeriesInt64 = int64(1)
}
if numSeriesInt64 == 0 {
numSeriesInt64 = 1
}
numSeries := int(numSeriesInt64)

numValuesInt64, err := strconv.ParseInt(r.FormValue("values"), 10, 64)
if err != nil {
numValuesInt64 = int64(100)
}
if numValuesInt64 == 0 {
numValuesInt64 = int64(100)
}
numValues := int(numValuesInt64)

series := make([]chart.Series, numSeries)
Expand Down
Binary file added _examples/benchmark_line_charts/output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _examples/custom_formatters/output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 5 additions & 4 deletions _examples/custom_padding/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/wcharczuk/go-chart"
"github.com/wcharczuk/go-chart/drawing"
"github.com/wcharczuk/go-chart/seq"
)

func drawChart(res http.ResponseWriter, req *http.Request) {
Expand All @@ -30,8 +31,8 @@ func drawChart(res http.ResponseWriter, req *http.Request) {
},
Series: []chart.Series{
chart.ContinuousSeries{
XValues: chart.Sequence.Float64(1.0, 100.0),
YValues: chart.Sequence.Random(100.0, 256.0),
XValues: seq.Range(1.0, 100.0),
YValues: seq.RandomValuesWithMax(100, 512),
},
},
}
Expand All @@ -57,8 +58,8 @@ func drawChartDefault(res http.ResponseWriter, req *http.Request) {
},
Series: []chart.Series{
chart.ContinuousSeries{
XValues: chart.Sequence.Float64(1.0, 100.0),
YValues: chart.Sequence.Random(100.0, 256.0),
XValues: seq.Range(1.0, 100.0),
YValues: seq.RandomValuesWithMax(100, 512),
},
},
}
Expand Down
Binary file modified _examples/custom_padding/output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _examples/custom_ranges/output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _examples/custom_ticks/output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added _examples/descending/output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _examples/legend/output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 4 additions & 3 deletions _examples/linear_regression/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@ import (
"net/http"

"github.com/wcharczuk/go-chart"
"github.com/wcharczuk/go-chart/seq"
)

func drawChart(res http.ResponseWriter, req *http.Request) {

/*
In this example we add a new type of series, a `SimpleMovingAverageSeries` that takes another series as a required argument.
InnerSeries only needs to implement `ValueProvider`, so really you could chain `SimpleMovingAverageSeries` together if you wanted.
InnerSeries only needs to implement `ValuesProvider`, so really you could chain `SimpleMovingAverageSeries` together if you wanted.
*/

mainSeries := chart.ContinuousSeries{
Name: "A test series",
XValues: chart.Sequence.Float64(1.0, 100.0), //generates a []float64 from 1.0 to 100.0 in 1.0 step increments, or 100 elements.
YValues: chart.Sequence.Random(100, 100), //generates a []float64 randomly from 0 to 100 with 100 elements.
XValues: seq.Range(1.0, 100.0), //generates a []float64 from 1.0 to 100.0 in 1.0 step increments, or 100 elements.
YValues: seq.RandomValuesWithAverage(100, 100), //generates a []float64 randomly from 0 to 100 with 100 elements.
}

// note we create a LinearRegressionSeries series by assignin the inner series.
Expand Down
16 changes: 9 additions & 7 deletions _examples/market_hours/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,25 @@ import (
"net/http"

"github.com/wcharczuk/go-chart"
"github.com/wcharczuk/go-chart/seq"
"github.com/wcharczuk/go-chart/util"
)

func drawChart(res http.ResponseWriter, req *http.Request) {
start := chart.Date.Date(2016, 7, 01, chart.Date.Eastern())
end := chart.Date.Date(2016, 07, 21, chart.Date.Eastern())
xv := chart.Sequence.MarketHours(start, end, chart.NYSEOpen, chart.NYSEClose, chart.Date.IsNYSEHoliday)
yv := chart.Sequence.RandomWithAverage(len(xv), 200, 10)
start := util.Date.Date(2016, 7, 01, util.Date.Eastern())
end := util.Date.Date(2016, 07, 21, util.Date.Eastern())
xv := seq.Time.MarketHours(start, end, util.NYSEOpen(), util.NYSEClose(), util.Date.IsNYSEHoliday)
yv := seq.New(seq.NewRandom().WithLen(len(xv)).WithAverage(200).WithScale(10)).Array()

graph := chart.Chart{
XAxis: chart.XAxis{
Style: chart.StyleShow(),
TickPosition: chart.TickPositionBetweenTicks,
ValueFormatter: chart.TimeHourValueFormatter,
Range: &chart.MarketHoursRange{
MarketOpen: chart.NYSEOpen,
MarketClose: chart.NYSEClose,
HolidayProvider: chart.Date.IsNYSEHoliday,
MarketOpen: util.NYSEOpen(),
MarketClose: util.NYSEClose(),
HolidayProvider: util.Date.IsNYSEHoliday,
},
},
YAxis: chart.YAxis{
Expand Down
5 changes: 3 additions & 2 deletions _examples/min_max/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"net/http"

"github.com/wcharczuk/go-chart"
"github.com/wcharczuk/go-chart/seq"
)

func drawChart(res http.ResponseWriter, req *http.Request) {
mainSeries := chart.ContinuousSeries{
Name: "A test series",
XValues: chart.Sequence.Float64(1.0, 100.0),
YValues: chart.Sequence.RandomWithAverage(100, 100, 50),
XValues: seq.Range(1.0, 100.0),
YValues: seq.New(seq.NewRandom().WithLen(100).WithAverage(100).WithScale(50)).Array(),
}

minSeries := &chart.MinSeries{
Expand Down
Binary file modified _examples/pie_chart/output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 4 additions & 3 deletions _examples/poly_regression/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@ import (
"net/http"

"github.com/wcharczuk/go-chart"
"github.com/wcharczuk/go-chart/seq"
)

func drawChart(res http.ResponseWriter, req *http.Request) {

/*
In this example we add a new type of series, a `PolynomialRegressionSeries` that takes another series as a required argument.
InnerSeries only needs to implement `ValueProvider`, so really you could chain `PolynomialRegressionSeries` together if you wanted.
InnerSeries only needs to implement `ValuesProvider`, so really you could chain `PolynomialRegressionSeries` together if you wanted.
*/

mainSeries := chart.ContinuousSeries{
Name: "A test series",
XValues: chart.Sequence.Float64(1.0, 100.0), //generates a []float64 from 1.0 to 100.0 in 1.0 step increments, or 100 elements.
YValues: chart.Sequence.Random(100, 100), //generates a []float64 randomly from 0 to 100 with 100 elements.
XValues: seq.Range(1.0, 100.0), //generates a []float64 from 1.0 to 100.0 in 1.0 step increments, or 100 elements.
YValues: seq.RandomValuesWithAverage(100, 100), //generates a []float64 randomly from 0 to 100 with 100 elements.
}

polyRegSeries := &chart.PolynomialRegressionSeries{
Expand Down
Binary file added _examples/poly_regression/output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 11 additions & 9 deletions _examples/request_timings/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/wcharczuk/go-chart"
util "github.com/wcharczuk/go-chart/util"
)

func parseInt(str string) int {
Expand All @@ -23,7 +24,7 @@ func parseFloat64(str string) float64 {
func readData() ([]time.Time, []float64) {
var xvalues []time.Time
var yvalues []float64
err := chart.File.ReadByLines("requests.csv", func(line string) {
err := util.File.ReadByLines("requests.csv", func(line string) error {
parts := strings.Split(line, ",")
year := parseInt(parts[0])
month := parseInt(parts[1])
Expand All @@ -32,6 +33,7 @@ func readData() ([]time.Time, []float64) {
elapsedMillis := parseFloat64(parts[4])
xvalues = append(xvalues, time.Date(year, time.Month(month), day, hour, 0, 0, 0, time.UTC))
yvalues = append(yvalues, elapsedMillis)
return nil
})
if err != nil {
fmt.Println(err.Error())
Expand All @@ -41,12 +43,12 @@ func readData() ([]time.Time, []float64) {

func releases() []chart.GridLine {
return []chart.GridLine{
{Value: chart.Time.ToFloat64(time.Date(2016, 8, 1, 9, 30, 0, 0, time.UTC))},
{Value: chart.Time.ToFloat64(time.Date(2016, 8, 2, 9, 30, 0, 0, time.UTC))},
{Value: chart.Time.ToFloat64(time.Date(2016, 8, 2, 15, 30, 0, 0, time.UTC))},
{Value: chart.Time.ToFloat64(time.Date(2016, 8, 4, 9, 30, 0, 0, time.UTC))},
{Value: chart.Time.ToFloat64(time.Date(2016, 8, 5, 9, 30, 0, 0, time.UTC))},
{Value: chart.Time.ToFloat64(time.Date(2016, 8, 6, 9, 30, 0, 0, time.UTC))},
{Value: util.Time.ToFloat64(time.Date(2016, 8, 1, 9, 30, 0, 0, time.UTC))},
{Value: util.Time.ToFloat64(time.Date(2016, 8, 2, 9, 30, 0, 0, time.UTC))},
{Value: util.Time.ToFloat64(time.Date(2016, 8, 2, 15, 30, 0, 0, time.UTC))},
{Value: util.Time.ToFloat64(time.Date(2016, 8, 4, 9, 30, 0, 0, time.UTC))},
{Value: util.Time.ToFloat64(time.Date(2016, 8, 5, 9, 30, 0, 0, time.UTC))},
{Value: util.Time.ToFloat64(time.Date(2016, 8, 6, 9, 30, 0, 0, time.UTC))},
}
}

Expand Down Expand Up @@ -125,8 +127,8 @@ func drawChart(res http.ResponseWriter, req *http.Request) {

graph.Elements = []chart.Renderable{chart.LegendThin(&graph)}

res.Header().Set("Content-Type", chart.ContentTypeSVG)
graph.Render(chart.SVG, res)
res.Header().Set("Content-Type", chart.ContentTypePNG)
graph.Render(chart.PNG, res)
}

func main() {
Expand Down
9 changes: 5 additions & 4 deletions _examples/scatter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/wcharczuk/go-chart"
"github.com/wcharczuk/go-chart/drawing"
"github.com/wcharczuk/go-chart/seq"
)

func drawChart(res http.ResponseWriter, req *http.Request) {
Expand All @@ -25,8 +26,8 @@ func drawChart(res http.ResponseWriter, req *http.Request) {
DotWidth: 5,
DotColorProvider: viridisByY,
},
XValues: chart.Sequence.Random(128, 1024),
YValues: chart.Sequence.Random(128, 1024),
XValues: seq.Range(0, 127),
YValues: seq.New(seq.NewRandom().WithLen(128).WithMax(1024)).Array(),
},
},
}
Expand All @@ -50,8 +51,8 @@ func unit(res http.ResponseWriter, req *http.Request) {
},
Series: []chart.Series{
chart.ContinuousSeries{
XValues: chart.Sequence.Float64(0, 4, 1),
YValues: chart.Sequence.Float64(0, 4, 1),
XValues: seq.RangeWithStep(0, 4, 1),
YValues: seq.RangeWithStep(0, 4, 1),
},
},
}
Expand Down
Binary file modified _examples/scatter/output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 3 additions & 7 deletions _examples/simple_moving_average/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,15 @@ import (
"net/http"

"github.com/wcharczuk/go-chart"
"github.com/wcharczuk/go-chart/seq"
)

func drawChart(res http.ResponseWriter, req *http.Request) {

/*
In this example we add a new type of series, a `SimpleMovingAverageSeries` that takes another series as a required argument.
InnerSeries only needs to implement `ValueProvider`, so really you could chain `SimpleMovingAverageSeries` together if you wanted.
*/

mainSeries := chart.ContinuousSeries{
Name: "A test series",
XValues: chart.Sequence.Float64(1.0, 100.0), //generates a []float64 from 1.0 to 100.0 in 1.0 step increments, or 100 elements.
YValues: chart.Sequence.Random(100, 100), //generates a []float64 randomly from 0 to 100 with 100 elements.
XValues: seq.Range(1.0, 100.0), //generates a []float64 from 1.0 to 100.0 in 1.0 step increments, or 100 elements.
YValues: seq.RandomValuesWithMax(100, 100), //generates a []float64 randomly from 0 to 100 with 100 elements.
}

// note we create a SimpleMovingAverage series by assignin the inner series.
Expand Down
Binary file modified _examples/stacked_bar/output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added _examples/text_rotation/output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _examples/timeseries/output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion _examples/twoaxis/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"

"github.com/wcharczuk/go-chart"
util "github.com/wcharczuk/go-chart/util"
)

func drawChart(res http.ResponseWriter, req *http.Request) {
Expand All @@ -23,7 +24,7 @@ func drawChart(res http.ResponseWriter, req *http.Request) {
TickPosition: chart.TickPositionBetweenTicks,
ValueFormatter: func(v interface{}) string {
typed := v.(float64)
typedDate := chart.Time.FromFloat64(typed)
typedDate := util.Time.FromFloat64(typed)
return fmt.Sprintf("%d-%d\n%d", typedDate.Month(), typedDate.Day(), typedDate.Year())
},
},
Expand Down
Binary file modified _examples/twoaxis/output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 2 additions & 4 deletions _examples/twopoint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"bytes"
"log"
"os"
//"time"
"github.com/wcharczuk/go-chart" //exposes "chart"

"github.com/wcharczuk/go-chart"
)

func main() {
Expand All @@ -18,8 +18,6 @@ func main() {
Style: chart.Style{
Show: true,
},

//XValues: []time.Time{time.Unix(3*b,0),time.Unix(4*b,0),time.Unix(5*b,0),time.Unix(6*b,0),time.Unix(7*b,0),time.Unix(8*b,0),time.Unix(9*b,0),time.Unix(10*b,0)},
XValues: []float64{10 * b, 20 * b, 30 * b, 40 * b, 50 * b, 60 * b, 70 * b, 80 * b},
YValues: []float64{1.0, 2.0, 30.0, 4.0, 50.0, 6.0, 7.0, 88.0},
}
Expand Down
10 changes: 6 additions & 4 deletions annotation_series.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package chart
import (
"fmt"
"math"

util "github.com/wcharczuk/go-chart/util"
)

// AnnotationSeries is a series of labels on the chart.
Expand Down Expand Up @@ -55,10 +57,10 @@ func (as AnnotationSeries) Measure(r Renderer, canvasBox Box, xrange, yrange Ran
lx := canvasBox.Left + xrange.Translate(a.XValue)
ly := canvasBox.Bottom - yrange.Translate(a.YValue)
ab := Draw.MeasureAnnotation(r, canvasBox, style, lx, ly, a.Label)
box.Top = Math.MinInt(box.Top, ab.Top)
box.Left = Math.MinInt(box.Left, ab.Left)
box.Right = Math.MaxInt(box.Right, ab.Right)
box.Bottom = Math.MaxInt(box.Bottom, ab.Bottom)
box.Top = util.Math.MinInt(box.Top, ab.Top)
box.Left = util.Math.MinInt(box.Left, ab.Left)
box.Right = util.Math.MaxInt(box.Right, ab.Right)
box.Bottom = util.Math.MaxInt(box.Bottom, ab.Bottom)
}
}
return box
Expand Down
5 changes: 3 additions & 2 deletions bar_chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"math"

"github.com/golang/freetype/truetype"
util "github.com/wcharczuk/go-chart/util"
)

// BarChart is a chart that draws bars on a range.
Expand Down Expand Up @@ -368,7 +369,7 @@ func (bc BarChart) getAdjustedCanvasBox(r Renderer, canvasBox Box, yrange Range,
lines := Text.WrapFit(r, bar.Label, barLabelBox.Width(), axisStyle)
linesBox := Text.MeasureLines(r, lines, axisStyle)

xaxisHeight = Math.MinInt(linesBox.Height()+(2*DefaultXAxisMargin), xaxisHeight)
xaxisHeight = util.Math.MinInt(linesBox.Height()+(2*DefaultXAxisMargin), xaxisHeight)
}
}

Expand Down Expand Up @@ -435,7 +436,7 @@ func (bc BarChart) styleDefaultsTitle() Style {
}

func (bc BarChart) getTitleFontSize() float64 {
effectiveDimension := Math.MinInt(bc.GetWidth(), bc.GetHeight())
effectiveDimension := util.Math.MinInt(bc.GetWidth(), bc.GetHeight())
if effectiveDimension >= 2048 {
return 48
} else if effectiveDimension >= 1024 {
Expand Down
Loading

0 comments on commit 03708a9

Please sign in to comment.