Skip to content

Commit

Permalink
(feat) add color option on line
Browse files Browse the repository at this point in the history
  • Loading branch information
kevincobain2000 committed Dec 25, 2023
1 parent 7dc7ca2 commit 5618781
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
| `y_label` | | string | |
| `height` | | int | |
| `width` | | int | |
| `color` | | string | `blue`,`red`,`green`,`yellow`,`cyan`,`orange` |


| `data` | Required | Description | Example |
Expand Down
27 changes: 26 additions & 1 deletion pkg/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,35 @@ func (c *Chart) GetBackground() chart.Style {
}
}

func (c *Chart) GetChartStroke(variant int) chart.Style {
func (c *Chart) GetChartStroke(variant int, color string) chart.Style {
var alpha uint8 = 80
strokeColor := chart.ColorBlue
fillColor := chart.ColorBlue.WithAlpha(alpha)
if color != "" {
switch color {
case "red":
strokeColor = chart.ColorRed
fillColor = chart.ColorRed.WithAlpha(alpha)
case "green":
strokeColor = chart.ColorGreen
fillColor = chart.ColorGreen.WithAlpha(alpha)
case "yellow":
strokeColor = chart.ColorYellow
fillColor = chart.ColorYellow.WithAlpha(alpha)
case "black":
strokeColor = chart.ColorBlack
fillColor = chart.ColorBlack.WithAlpha(alpha)
case "cyan":
strokeColor = chart.ColorCyan
fillColor = chart.ColorCyan.WithAlpha(alpha)
case "orange":
strokeColor = chart.ColorOrange
fillColor = chart.ColorOrange.WithAlpha(alpha)
case "blue":
strokeColor = chart.ColorBlue
fillColor = chart.ColorBlue.WithAlpha(alpha)
}
}
switch variant {
case 1:
strokeColor = chart.ColorRed
Expand Down
10 changes: 5 additions & 5 deletions pkg/line_chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ func NewLineChart() *LineChart {
func (c *LineChart) GetBackground() chart.Style {
return c.chart.GetBackground()
}
func (c *LineChart) GetChartStroke(variant int) chart.Style {
return c.chart.GetChartStroke(variant)
func (c *LineChart) GetChartStroke(variant int, color string) chart.Style {
return c.chart.GetChartStroke(variant, color)
}

func (c *LineChart) GetXAxis(label string) chart.XAxis {
Expand All @@ -43,7 +43,7 @@ func (c *LineChart) GetYValues(data []float64) []float64 {
return yValues
}

func (c *LineChart) GetSeries(xData [][]string, yData [][]float64, names []string) []chart.Series {
func (c *LineChart) GetSeries(xData [][]string, yData [][]float64, names []string, color string) []chart.Series {
var series []chart.Series
isTimeSeries := c.IsTimeseries(xData[0][0])
for i := 0; i < len(xData); i++ {
Expand All @@ -55,14 +55,14 @@ func (c *LineChart) GetSeries(xData [][]string, yData [][]float64, names []strin
if isTimeSeries {
series = append(series, chart.TimeSeries{
Name: name,
Style: c.GetChartStroke(i),
Style: c.GetChartStroke(i, color),
XValues: c.GetXValuesAsDates(xData[i]),
YValues: c.GetYValues(yData[i]),
})
} else {
series = append(series, chart.ContinuousSeries{
Name: name,
Style: c.GetChartStroke(i),
Style: c.GetChartStroke(i, color),
XValues: c.GetXValuesAsFloat(xData[i]),
YValues: c.GetYValues(yData[i]),
})
Expand Down
3 changes: 2 additions & 1 deletion pkg/line_chart_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type LineChartRequest struct {
XAxisLabel string `json:"x_label" query:"x_label" form:"x_label"`
YAxisLabel string `json:"y_label" query:"y_label" form:"y_label"`
ChartTitle string `json:"title" query:"title" form:"title"`
Color string `json:"color" query:"color" form:"color"`
Height int `json:"height" query:"height" form:"height"`
Width int `json:"width" query:"width" form:"width"`
}
Expand Down Expand Up @@ -56,7 +57,7 @@ func (h *LineChartHandler) Get(c echo.Context) ([]byte, error) {
Background: h.chart.GetBackground(),
XAxis: h.chart.GetXAxis(req.XAxisLabel),
YAxis: h.chart.GetYAxis(req.YAxisLabel),
Series: h.chart.GetSeries(data.XData, data.YData, data.Names),
Series: h.chart.GetSeries(data.XData, data.YData, data.Names, req.Color),
}
graph.Elements = []chart.Renderable{
chart.Legend(&graph),
Expand Down

0 comments on commit 5618781

Please sign in to comment.