whispervis/widgets/chart.go

52 lines
1.0 KiB
Go
Raw Normal View History

2018-10-23 21:10:42 +02:00
package widgets
import (
2018-10-23 22:30:57 +02:00
"strings"
2018-10-23 21:10:42 +02:00
charts "github.com/cnguy/gopherjs-frappe-charts"
"github.com/gopherjs/vecty"
"github.com/gopherjs/vecty/elem"
"github.com/gopherjs/vecty/prop"
)
// Chart represents a wrapper for Frapper Charts library.
type Chart struct {
vecty.Core
2018-10-23 22:30:57 +02:00
id string
name string
2018-10-23 21:10:42 +02:00
data *charts.ChartData
}
2018-10-23 22:30:57 +02:00
func NewChart(name string, data *charts.ChartData) *Chart {
id := strings.ToLower(name) // TODO(divan): make it unique
2018-10-23 21:10:42 +02:00
return &Chart{
2018-10-23 22:30:57 +02:00
id: id,
name: name,
2018-10-23 21:10:42 +02:00
data: data,
}
}
// Render implements the vecty.Component interface for Chart.
func (c *Chart) Render() vecty.ComponentOrHTML {
return elem.Div(
vecty.Markup(
2018-10-23 22:30:57 +02:00
prop.ID(c.id),
2018-10-23 21:10:42 +02:00
),
)
}
// Mount implements the vecty.Mounter interface for Chart. Triggers when DOM has been created for the component.
func (c *Chart) Mount() {
2018-10-23 22:30:57 +02:00
charts.NewLineChart("#"+c.id, c.data).
WithTitle(c.name).
2018-10-23 21:10:42 +02:00
WithColors([]string{"blue"}).
SetShowDots(false).
SetHeatline(true).
SetRegionFill(true).
SetXAxisMode("tick").
SetYAxisMode("span").
SetIsSeries(true).
Render()
}