fix: patchChan loop ending after switching browser tab; feat: improve realtime chart example
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-via/via"
|
"github.com/go-via/via"
|
||||||
|
"github.com/go-via/via-plugin-picocss/picocss"
|
||||||
"github.com/go-via/via/h"
|
"github.com/go-via/via/h"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -16,10 +17,12 @@ func main() {
|
|||||||
v.Config(via.Options{
|
v.Config(via.Options{
|
||||||
LogLvl: via.LogLevelDebug,
|
LogLvl: via.LogLevelDebug,
|
||||||
DevMode: true,
|
DevMode: true,
|
||||||
|
Plugins: []via.Plugin{
|
||||||
|
picocss.Default,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
v.AppendToHead(
|
v.AppendToHead(
|
||||||
h.Link(h.Rel("stylesheet"), h.Href("https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.min.css")),
|
|
||||||
h.Script(h.Src("https://unpkg.com/echarts@6.0.0/dist/echarts.min.js")),
|
h.Script(h.Src("https://unpkg.com/echarts@6.0.0/dist/echarts.min.js")),
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -28,6 +31,16 @@ func main() {
|
|||||||
|
|
||||||
c.View(func() h.H {
|
c.View(func() h.H {
|
||||||
return h.Div(h.Class("container"),
|
return h.Div(h.Class("container"),
|
||||||
|
h.Section(
|
||||||
|
h.Nav(
|
||||||
|
h.Ul(h.Li(h.H3(h.Text("⚡Via")))),
|
||||||
|
h.Ul(
|
||||||
|
h.Li(h.A(h.H5(h.Text("About")), h.Href("https://github.com/go-via/via"))),
|
||||||
|
h.Li(h.A(h.H5(h.Text("Resources")), h.Href("https://github.com/orgs/go-via/repositories"))),
|
||||||
|
h.Li(h.A(h.H5(h.Text("Say hi!")), h.Href("http://github.com/go-via/via/discussions"))),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
chartComp(),
|
chartComp(),
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
@@ -42,8 +55,8 @@ func chartCompFn(c *via.Context) {
|
|||||||
|
|
||||||
isLive := true
|
isLive := true
|
||||||
isLiveSig := c.Signal("on")
|
isLiveSig := c.Signal("on")
|
||||||
refreshRate := c.Signal(1)
|
refreshRate := c.Signal(24)
|
||||||
tkr := time.NewTicker(1000 * time.Millisecond)
|
tkr := time.NewTicker(1000 / time.Duration(refreshRate.Int()) * time.Millisecond)
|
||||||
|
|
||||||
updateRefreshRate := c.Action(func() {
|
updateRefreshRate := c.Action(func() {
|
||||||
tkr.Reset(1000 / time.Duration(refreshRate.Int()) * time.Millisecond)
|
tkr.Reset(1000 / time.Duration(refreshRate.Int()) * time.Millisecond)
|
||||||
@@ -67,7 +80,10 @@ func chartCompFn(c *via.Context) {
|
|||||||
myChart.setOption({
|
myChart.setOption({
|
||||||
xAxis: [{data: %s}],
|
xAxis: [{data: %s}],
|
||||||
series:[{data: %s}]
|
series:[{data: %s}]
|
||||||
},{notMerge:false,lazyUpdate:true});
|
},{
|
||||||
|
notMerge:false,
|
||||||
|
lazyUpdate:true
|
||||||
|
});
|
||||||
`, labelsTxt, dataTxt))
|
`, labelsTxt, dataTxt))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -75,8 +91,7 @@ func chartCompFn(c *via.Context) {
|
|||||||
|
|
||||||
c.View(func() h.H {
|
c.View(func() h.H {
|
||||||
return h.Div(
|
return h.Div(
|
||||||
h.Div(h.ID("chart"), h.Style("width:100%;height:400px;"),
|
h.Div(h.ID("chart"), h.Style("width:100%;height:400px;"), h.Script(h.Raw(`
|
||||||
h.Script(h.Raw(`
|
|
||||||
var prefersDark = window.matchMedia('(prefers-color-scheme: dark)');
|
var prefersDark = window.matchMedia('(prefers-color-scheme: dark)');
|
||||||
var myChart = echarts.init(document.getElementById('chart'), prefersDark.matches ? 'dark' : 'light');
|
var myChart = echarts.init(document.getElementById('chart'), prefersDark.matches ? 'dark' : 'light');
|
||||||
var option = {
|
var option = {
|
||||||
@@ -90,7 +105,7 @@ func chartCompFn(c *via.Context) {
|
|||||||
},
|
},
|
||||||
title: {
|
title: {
|
||||||
left: 'center',
|
left: 'center',
|
||||||
text: 'Large Area Chart'
|
text: '📈 Real-Time Chart Example'
|
||||||
},
|
},
|
||||||
xAxis: {
|
xAxis: {
|
||||||
type: 'category',
|
type: 'category',
|
||||||
@@ -104,7 +119,7 @@ func chartCompFn(c *via.Context) {
|
|||||||
dataZoom: [
|
dataZoom: [
|
||||||
{
|
{
|
||||||
type: 'inside',
|
type: 'inside',
|
||||||
start: 80,
|
start: 90,
|
||||||
end: 100
|
end: 100
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -119,17 +134,18 @@ func chartCompFn(c *via.Context) {
|
|||||||
symbol: 'none',
|
symbol: 'none',
|
||||||
sampling: 'lttb',
|
sampling: 'lttb',
|
||||||
itemStyle: {
|
itemStyle: {
|
||||||
color: '#1488CC'
|
color: '#e8ae01'
|
||||||
},
|
},
|
||||||
|
lineStyle: { color: '#e8ae01'},
|
||||||
areaStyle: {
|
areaStyle: {
|
||||||
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
|
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
|
||||||
{
|
{
|
||||||
offset: 0,
|
offset: 0,
|
||||||
color: '#1488CC'
|
color: '#fecc63'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
offset: 1,
|
offset: 1,
|
||||||
color: '#2B32B2'
|
color: '#c79400'
|
||||||
}
|
}
|
||||||
])
|
])
|
||||||
},
|
},
|
||||||
@@ -138,7 +154,7 @@ func chartCompFn(c *via.Context) {
|
|||||||
]
|
]
|
||||||
};
|
};
|
||||||
option && myChart.setOption(option);
|
option && myChart.setOption(option);
|
||||||
`)),
|
`))),
|
||||||
h.Section(
|
h.Section(
|
||||||
h.Article(
|
h.Article(
|
||||||
h.H5(h.Text("Controls")),
|
h.H5(h.Text("Controls")),
|
||||||
@@ -148,13 +164,12 @@ func chartCompFn(c *via.Context) {
|
|||||||
h.Legend(h.Text("Live Data")),
|
h.Legend(h.Text("Live Data")),
|
||||||
h.Input(h.Type("checkbox"), h.Role("switch"), isLiveSig.Bind(), toggleIsLive.OnChange()),
|
h.Input(h.Type("checkbox"), h.Role("switch"), isLiveSig.Bind(), toggleIsLive.OnChange()),
|
||||||
),
|
),
|
||||||
h.Label(h.Span(h.Text("Refresh Rate (")), h.Span(refreshRate.Text()), h.Span(h.Text(" Hz)")),
|
h.Label(h.Text("Refresh Rate (Hz) ― "), refreshRate.Text(),
|
||||||
h.Input(h.Type("range"), h.Attr("min", "1"), h.Attr("max", "200"), refreshRate.Bind(), updateRefreshRate.OnChange()),
|
h.Input(h.Type("range"), h.Attr("min", "1"), h.Attr("max", "200"), refreshRate.Bind(), updateRefreshRate.OnChange()),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
4
via.go
4
via.go
@@ -398,8 +398,8 @@ func New() *V {
|
|||||||
return
|
return
|
||||||
case patch, ok := <-c.patchChan:
|
case patch, ok := <-c.patchChan:
|
||||||
if !ok {
|
if !ok {
|
||||||
v.logDebug(c, "patchChan closed, exiting handler loop")
|
time.Sleep(100 * time.Millisecond)
|
||||||
return
|
continue
|
||||||
}
|
}
|
||||||
switch patch.typ {
|
switch patch.typ {
|
||||||
case patchTypeElements:
|
case patchTypeElements:
|
||||||
|
|||||||
Reference in New Issue
Block a user