Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feat) reafactored handlers by moving printer logic to pkg classes #11

Merged
merged 3 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,24 +104,21 @@ https://instachart.coveritup.app/line?title=Line+Chart+Simple&subtitle=Sleeping+
</p>





## `GET /bar`


| `data` | Required | Description | Example |
| :----- | :------- | :--------------- | :--------------------------- |
| `x` | ◯ | []Array (string) | `"x": [["Mon","Tue", "Wed"]` |
| `y` | ◯ | []Array (int) | `"y": [[1,2,3]]` |
| `data` | Required | Description | Example |
| :----- | :------- | :------------- | :-------------------------- |
| `x` | ◯ | Array (string) | `"x": ["Mon","Tue", "Wed"]` |
| `y` | ◯ | []Array (int) | `"y": [[1,2,3]]` |

<details>
<summary><b>REQUEST URL</b></summary>

```sh
https://instachart.coveritup.app/bar?title=Bar+Chart&subtitle=Sleeping+hours&data={
"x": ["Monday", "Friday", "Sunday"],
"y": [[8, 2 ,14]]
"y": [[8,2,14]]
}
```

Expand All @@ -130,8 +127,8 @@ https://instachart.coveritup.app/bar?title=Bar+Chart&subtitle=Sleeping+hours&dat
<br>

<p align="center">
<a href="https://instachart.coveritup.app/bar?title=Bar+Chart&width=1024&height=620&subtitle=Sleeping+hours&metric=h&data={%20%22x%22:%20[%22Monday%22,%20%22Friday%22,%20%22Sunday%22],%20%22y%22:%20[[8,%202%20,14]]%20}">
<img alt="bar chart" src='https://instachart.coveritup.app/bar?title=Bar+Chart&width=1024&height=620&subtitle=Sleeping+hours&metric=h&data={%20%22x%22:%20[%22Monday%22,%20%22Friday%22,%20%22Sunday%22],%20%22y%22:%20[[8,%202%20,14]]%20}'>
<a href="https://instachart.coveritup.app/bar?title=Bar+Chart&subtitle=Sleeping+hours&data={%20%22x%22:%20[%22Monday%22,%20%22Friday%22,%20%22Sunday%22],%20%22y%22:%20[[8,%202%20,14]]%20}">
<img alt="bar chart" src='https://instachart.coveritup.app/bar?title=Bar+Chart&subtitle=Sleeping+hours&data={%20%22x%22:%20[%22Monday%22,%20%22Friday%22,%20%22Sunday%22],%20%22y%22:%20[[8,%202,14]]%20}'>
</a>
</p>

Expand Down
85 changes: 85 additions & 0 deletions pkg/bar_chart.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package pkg

import (
"fmt"
"net/http"

"github.com/labstack/echo/v4"
charts "github.com/vicanso/go-charts/v2"
)

type BarChart struct {
chart *Chart
}
Expand All @@ -9,3 +17,80 @@ func NewBarChart() *BarChart {
chart: NewChart(),
}
}

func (c *BarChart) GetVertical(xData []string, yData [][]float64, names []string, req *ChartRequest) ([]byte, error) {
p, err := charts.BarRender(
yData,
charts.TitleOptionFunc(charts.TitleOption{
Text: req.ChartTitle,
Subtext: req.ChartSubtitle,
SubtextFontSize: DEFAULT_SUBTITLE_FONT_SIZE,
Left: charts.PositionCenter,
}),
charts.HeightOptionFunc(req.Height),
charts.WidthOptionFunc(req.Width),
charts.XAxisDataOptionFunc(xData),
charts.LegendOptionFunc(charts.LegendOption{
Orient: charts.OrientVertical,
Data: names,
Left: charts.PositionLeft,
}),
charts.MarkLineOptionFunc(0, charts.SeriesMarkDataTypeAverage),
charts.MarkPointOptionFunc(0, charts.SeriesMarkDataTypeMax,
charts.SeriesMarkDataTypeMin),
func(opt *charts.ChartOption) {
opt.Theme = req.Theme
opt.ValueFormatter = func(f float64) string {
return fmt.Sprintf("%.0f%s", f, req.Metric)
}
idx := len(opt.SeriesList) - 1
if len(opt.SeriesList) > 1 {
idx = 1
}
opt.SeriesList[idx].MarkPoint = charts.NewMarkPoint(
charts.SeriesMarkDataTypeMax,
charts.SeriesMarkDataTypeMin,
)
opt.SeriesList[idx].MarkLine = charts.NewMarkLine(
charts.SeriesMarkDataTypeAverage,
)
},
)
if err != nil {
return nil, err
}

buf, err := p.Bytes()
if err != nil {
return nil, err
}
return buf, err
}

func (c *BarChart) GetHorizontal(xData []string, yData [][]float64, names []string, req *ChartRequest) ([]byte, error) {
p, err := charts.HorizontalBarRender(
yData,
charts.TitleOptionFunc(charts.TitleOption{
Text: req.ChartTitle,
Subtext: req.ChartSubtitle,
SubtextFontSize: DEFAULT_SUBTITLE_FONT_SIZE,
Left: charts.PositionCenter,
}),
charts.HeightOptionFunc(req.Height),
charts.WidthOptionFunc(req.Width),
charts.YAxisDataOptionFunc(xData),
func(opt *charts.ChartOption) {
opt.Theme = req.Theme
opt.ValueFormatter = func(f float64) string {
return fmt.Sprintf("%.0f%s", f, req.Metric)
}
},
)

if err != nil {
return nil, echo.NewHTTPError(http.StatusUnprocessableEntity, err.Error())
}

buf, err := p.Bytes()
return buf, err
}
76 changes: 4 additions & 72 deletions pkg/bar_chart_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package pkg

import (
"encoding/json"
"fmt"
"net/http"

"github.com/labstack/echo/v4"
charts "github.com/vicanso/go-charts/v2"
)

type BarChartHandler struct {
Expand Down Expand Up @@ -45,76 +43,10 @@ func (h *BarChartHandler) Get(c echo.Context) ([]byte, error) {
}
return nil, echo.NewHTTPError(http.StatusUnprocessableEntity, msgs)
}

if !req.Horizontal {
p, err := charts.BarRender(
data.YData,
charts.TitleOptionFunc(charts.TitleOption{
Text: req.ChartTitle,
Subtext: req.ChartSubtitle,
SubtextFontSize: 9,
Left: charts.PositionCenter,
}),
charts.HeightOptionFunc(req.Height),
charts.WidthOptionFunc(req.Width),
charts.XAxisDataOptionFunc(data.XData),
charts.LegendOptionFunc(charts.LegendOption{
Orient: charts.OrientVertical,
Data: data.Names,
Left: charts.PositionLeft,
}),
charts.MarkLineOptionFunc(0, charts.SeriesMarkDataTypeAverage),
charts.MarkPointOptionFunc(0, charts.SeriesMarkDataTypeMax,
charts.SeriesMarkDataTypeMin),
func(opt *charts.ChartOption) {
opt.Theme = req.Theme
opt.ValueFormatter = func(f float64) string {
return fmt.Sprintf("%.0f%s", f, req.Metric)
}
idx := len(opt.SeriesList) - 1
if len(opt.SeriesList) > 1 {
idx = 1
}
opt.SeriesList[idx].MarkPoint = charts.NewMarkPoint(
charts.SeriesMarkDataTypeMax,
charts.SeriesMarkDataTypeMin,
)
opt.SeriesList[idx].MarkLine = charts.NewMarkLine(
charts.SeriesMarkDataTypeAverage,
)
},
)
if err != nil {
return nil, echo.NewHTTPError(http.StatusUnprocessableEntity, err.Error())
}
buf, err := p.Bytes()
return buf, err
}

p, err := charts.HorizontalBarRender(
data.YData,
charts.TitleOptionFunc(charts.TitleOption{
Text: req.ChartTitle,
Subtext: req.ChartSubtitle,
SubtextFontSize: 9,
Left: charts.PositionCenter,
}),
charts.HeightOptionFunc(req.Height),
charts.WidthOptionFunc(req.Width),
charts.YAxisDataOptionFunc(data.XData),
func(opt *charts.ChartOption) {
opt.Theme = req.Theme
opt.ValueFormatter = func(f float64) string {
return fmt.Sprintf("%.0f%s", f, req.Metric)
}
},
)

if err != nil {
return nil, echo.NewHTTPError(http.StatusUnprocessableEntity, err.Error())
SetHeaders(c.Response().Header())
if req.Horizontal {
return h.chart.GetHorizontal(data.XData, data.YData, data.Names, req)
}

buf, err := p.Bytes()
SetHeaders(c.Response().Header())
return buf, err
return h.chart.GetVertical(data.XData, data.YData, data.Names, req)
}
8 changes: 8 additions & 0 deletions pkg/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ import (
"net/http"
)

const (
DEFAULT_PADDING_TOP = 20
DEFAULT_PADDING_RIGHT = 20
DEFAULT_PADDING_BOTTOM = 20
DEFAULT_PADDING_LEFT = 20
DEFAULT_SUBTITLE_FONT_SIZE = 9
)

type Chart struct {
}

Expand Down
20 changes: 17 additions & 3 deletions pkg/donut_chart.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package pkg

import "github.com/wcharczuk/go-chart/v2"
import (
"bytes"

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

type DonutChart struct {
chart *Chart
Expand All @@ -11,13 +15,23 @@ func NewDonutChart() *DonutChart {
chart: NewChart(),
}
}
func (c *DonutChart) GetValues(names []string, values []float64) []chart.Value {

func (c *DonutChart) Get(values []float64, names []string, req *ChartRequest) ([]byte, error) {
var chartValues []chart.Value
for i := 0; i < len(names); i++ {
chartValues = append(chartValues, chart.Value{
Value: values[i],
Label: names[i],
})
}
return chartValues
graph := chart.DonutChart{
Title: req.ChartTitle,
Height: req.Height,
Width: req.Width,
Values: chartValues,
}

buffer := bytes.NewBuffer([]byte{})
err := graph.Render(chart.PNG, buffer)
return buffer.Bytes(), err
}
16 changes: 1 addition & 15 deletions pkg/donut_chart_handler.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package pkg

import (
"bytes"
"encoding/json"
"net/http"

"github.com/labstack/echo/v4"
"github.com/wcharczuk/go-chart/v2"
)

type DonutChartHandler struct {
Expand Down Expand Up @@ -45,18 +43,6 @@ func (h *DonutChartHandler) Get(c echo.Context) ([]byte, error) {
return nil, echo.NewHTTPError(http.StatusUnprocessableEntity, msgs)
}

graph := chart.DonutChart{
Title: req.ChartTitle,
Height: req.Height,
Width: req.Width,
Values: h.chart.GetValues(data.Names, data.Values),
}

buffer := bytes.NewBuffer([]byte{})
err := graph.Render(chart.PNG, buffer)
if err != nil {
return nil, echo.NewHTTPError(http.StatusUnprocessableEntity, err.Error())
}
SetHeaders(c.Response().Header())
return buffer.Bytes(), err
return h.chart.Get(data.Values, data.Names, req)
}
58 changes: 58 additions & 0 deletions pkg/line_chart.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package pkg

import (
"fmt"

charts "github.com/vicanso/go-charts/v2"
)

type LineChart struct {
chart *Chart
}
Expand All @@ -9,3 +15,55 @@ func NewLineChart() *LineChart {
chart: NewChart(),
}
}

func (c *LineChart) Get(xData [][]string, yData [][]float64, names []string, req *ChartRequest) ([]byte, error) {
p, err := charts.LineRender(
yData,
charts.HeightOptionFunc(req.Height),
charts.WidthOptionFunc(req.Width),
charts.TitleOptionFunc(charts.TitleOption{
Text: req.ChartTitle,
Subtext: req.ChartSubtitle,
SubtextFontSize: DEFAULT_SUBTITLE_FONT_SIZE,
Left: charts.PositionCenter,
}),
charts.XAxisDataOptionFunc(xData[0]),
charts.LegendOptionFunc(charts.LegendOption{
Orient: charts.OrientVertical,
Data: names,
Left: charts.PositionLeft,
}),
func(opt *charts.ChartOption) {
opt.Theme = req.Theme
opt.Legend.Padding = charts.Box{
Top: DEFAULT_PADDING_TOP,
Bottom: DEFAULT_PADDING_BOTTOM,
}
opt.ValueFormatter = func(f float64) string {
return fmt.Sprintf("%.0f%s", f, req.Metric)
}
opt.FillArea = req.Fill

idx := len(opt.SeriesList) - 1
if len(opt.SeriesList) > 1 {
idx = 1
}
opt.SeriesList[idx].MarkPoint = charts.NewMarkPoint(
charts.SeriesMarkDataTypeMax,
charts.SeriesMarkDataTypeMin,
)
opt.SeriesList[idx].MarkLine = charts.NewMarkLine(
charts.SeriesMarkDataTypeAverage,
)
},
)
if err != nil {
return nil, err
}

buf, err := p.Bytes()
if err != nil {
return nil, err
}
return buf, err
}
Loading