我希望HighChart更新系列,方法是将下一个数据点添加到图表中,以便图表“播放”。 我有一个包含X和Y组件的数据系列。我希望图表能够在本系列的下一个点进行更新(比如每秒)。 我在使用DotNet.HighCharts API的VB.NET 4站点中使用它。我已经跟进了几个演示(例如this一个)并且所有这些都使用了一些随机生成的值。如何让图表使用现有的一组点?这是一个使用随机函数的jsFiddle - http://jsfiddle.net/tQpNN/3/ 我只是不知道如何遍历传入的数据系列(如果是这样做的话)或者让DotNet.HighCharts为我迭代。
使用一些代码编辑:
Dim stfipsList4 = (From r In dt4.AsEnumerable() Select r("areaname")).Distinct().ToList()
Dim SeriesList4 As New List(Of Series)(stfipsList4.Count)
Dim seriesItem4(stfipsList4.Count) As Series
Dim xDate4 As DateTime
Dim fakeDate4 As String
Dim sX4 As Integer
sX4 = 1
For Each state In stfipsList4
Dim data As New Dictionary(Of DateTime, Decimal)
Dim stateVal As String = state.ToString
Dim recCount As Integer = dt4.Rows.Count - 1
Dim seriesPointCount As Integer = dt4.Compute("Count(population)", "areaname = '" + stateVal + "'")
Dim chartData As Object(,) = New Object(seriesPointCount - 1, 1) {}
Dim x As Integer = 0
For i As Integer = 0 To recCount
If dt4.Rows(i)("areaname").ToString = stateVal Then
fakeDate4 = "1/1/" + dt4.Rows(i)("periodyear").ToString
xDate3 = DateTime.Parse(fakeDate4)
chartData.SetValue(xDate4.Date, x, 0)
chartData.SetValue(dt4.Rows(i)("population"), x, 1)
x += 1
End If
Next
seriesItem4(sX4) = New Series With {
.Name = state.ToString, _
.Data = New Helpers.Data(chartData)
}
SeriesList4.Add(seriesItem3(sX4))
sX4 = sX4 + 1
Next
Dim iterateData As String = JsonSerializer.Serialize(New Helpers.Data(New Object() {seriesItem3(1).Data}))
Dim chart4 As Highcharts = New Highcharts("chart4").SetOptions(New Helpers.GlobalOptions() With { _
.[Global] = New [Global]() With { _
.UseUTC = False _
} _
}).InitChart(New Chart() With { _
.DefaultSeriesType = ChartTypes.Spline, _
.MarginRight = 10, _
.Events = New ChartEvents() With { _
.Load = "ChartEventsLoad" _
} _
}).SetTitle(New Title() With { _
.Text = "Live data" _
}).SetXAxis(New XAxis() With { _
.Type = AxisTypes.Datetime, _
.TickPixelInterval = 150 _
}).SetSeries(New Series() With { _
.Data = New Helpers.Data(New Object() {})
}) _
.AddJavascripVariable("counter", "0") _
.AddJavascripVariable("stockData", iterateData) _
.AddJavascripFunction("ChartEventsLoad", "// set up the updating of the chart each second" & vbCr & vbLf & _
" var series = this.series[0];" & vbCr & vbLf & _
" setInterval(function() {" & vbCr & vbLf & _
" var x = stockData[counter].key;" & vbCr & vbLf & _
" var y = stockData[counter].value;" & vbCr & vbLf & _
" series.addPoint([x, y], true, series.points.length > 10);" & vbCr & vbLf & _
" counter++;" & vbCr & vbLf & _
"}, 1000);")
这传递了我的一系列数据(可能是1个或多个不同的个别系列)。我想要做的是通过一次添加一个点来使系列点“播放”(通过缩放x轴但我不想删除任何点)。这个虚拟版本只是在给定时刻添加一个新的随机y值。我不知道如何迭代我现有的数据。
这会将“stockData”javascript var设置为我的数据集。我在FireBug中看到错误说明
stockData [counter]未定义
var counter
在$(document).ready(function)
以及stockdata
和chart4
图表中定义。有关DotNet.Highcharts API吐出的信息,请参阅http://jsfiddle.net/ZvYT6/。
迭代的内联javascript看起来像这样:
function ChartEventsLoad(){
// set up the updating of the chart each second
var series = this.series[0];
setInterval(function() {
var x = stockData[counter].key;
var y = stockData[counter].value;
series.addPoint([x, y], true, series.points.length > 10);
counter++;
}, 1000);
}
});
我真正不确定的部分是stockData[counter]
键/值。这在javascript中被称为什么? stockdata是一个看起来像这样的数据列表:
var stockData = { data: [{ data: [[Date.parse('01/01/1900 00:00:00'), 530000],
....,
[Date.parse('01/01/2010 00:00:00'), 18801310]]
}]
};
编辑v4: 好的,现在我可以使用一些讨厌的javascript来解析我的数据数组。
Dim iterateData As String = JsonSerializer.Serialize(seriesItem3(1))
Dim chart4 As Highcharts = New Highcharts("chart4").SetOptions(New Helpers.GlobalOptions() With { _
.[Global] = New [Global]() With { _
.UseUTC = False _
} _
}).InitChart(New Chart() With { _
.DefaultSeriesType = ChartTypes.Spline, _
.MarginRight = 10, _
.Events = New ChartEvents() With { _
.Load = "ChartEventsLoad" _
} _
}).SetTitle(New Title() With { _
.Text = "Live data" _
}).SetXAxis(New XAxis() With { _
.Type = AxisTypes.Datetime, _
.TickPixelInterval = 150 _
}).SetSeries(New Series() With { _
.Data = New Helpers.Data(New Object() {})
}) _
.AddJavascripVariable("counter", "0") _
.AddJavascripVariable("stockData", iterateData.ToString) _
.AddJavascripFunction("ChartEventsLoad", "// set up the updating of the chart each second" & vbCr & vbLf & _
" var result = stockData.data;" & vbCr & vbLf & _
" var series = this.series[0];" & vbCr & vbLf & _
" setInterval(function() {" & vbCr & vbLf & _
" var p = String(result[counter]);" & vbCr & vbLf & _
" var splitp = p.split("","");" & vbCr & vbLf & _
" var x = splitp[0];" & vbCr & vbLf & _
" var y = splitp[1];" & vbCr & vbLf & _
" series.addPoint([x, y], true, false, true);" & vbCr & vbLf & _
" counter++;" & vbCr & vbLf & _
"}, 1000);")
ltrChart4.Text = chart4.ToHtmlString()
如果我查看firebug中的x和y值,我可以看到这些是预期的值。但是,我的图表只是在x轴上的时间从〜1900小时开始更新,并且每秒增加一个点(按照javascript函数),但是没有y值。另外,y轴似乎从-2.5开始 - 我使用的值都不到1000,更不用说0.我非常接近......
答案 0 :(得分:2)
终于搞定了。这是我的解决方案 - 可能不是最漂亮的但它有效。
重要的是传递iterated
iterateData.ToString()
。从这一点开始,您可以遍历它,然后在每个元素上进行拆分。我添加了一个检查,以确保一旦到达数据集中的最后一个点,就会停止添加点。要做的仍然是允许它处理多个系列并“播放”它们。
Dim stfipsList4 = (From r In dt4.AsEnumerable() Select r("areaname")).Distinct().ToList()
Dim SeriesList4 As New List(Of Series)(stfipsList4.Count)
Dim seriesItem4(stfipsList4.Count) As Series
Dim xDate4 As DateTime
Dim fakeDate4 As String
Dim sX4 As Integer
sX4 = 1
For Each state In stfipsList4
Dim data As New Dictionary(Of DateTime, Decimal)
Dim stateVal As String = state.ToString
Dim recCount As Integer = dt4.Rows.Count - 1
Dim seriesPointCount As Integer = dt4.Compute("Count(population)", "areaname = '" + stateVal + "'")
Dim chartData As Object(,) = New Object(seriesPointCount - 1, 1) {}
Dim x As Integer = 0
For i As Integer = 0 To recCount
If dt4.Rows(i)("areaname").ToString = stateVal Then
fakeDate4 = "1/1/" + dt4.Rows(i)("periodyear").ToString
xDate3 = DateTime.Parse(fakeDate4)
chartData.SetValue(xDate4.Date, x, 0)
chartData.SetValue(dt4.Rows(i)("population"), x, 1)
x += 1
End If
Next
seriesItem4(sX4) = New Series With {
.Name = state.ToString, _
.Data = New Helpers.Data(chartData)
}
SeriesList4.Add(seriesItem3(sX4))
sX4 = sX4 + 1
Next
Dim iterateData As String = JsonSerializer.Serialize(seriesItem3(1))
Dim chart4 As Highcharts = New Highcharts("chart4").SetOptions(New Helpers.GlobalOptions() With { _
.[Global] = New [Global]() With { _
.UseUTC = False _
} _
}).InitChart(New Chart() With { _
.DefaultSeriesType = ChartTypes.Column, _
.MarginRight = 10, _
.Events = New ChartEvents() With { _
.Load = "ChartEventsLoad" _
} _
}).SetTitle(New Title() With { _
.Text = "Live data" _
}).SetXAxis(New XAxis() With { _
.Type = AxisTypes.Datetime, _
.TickPixelInterval = 150 _
}).SetSeries(New Series() With { _
.Data = New Helpers.Data(New Object() {}), _
.Name = seriesItem3(1).Name
}) _
.AddJavascripVariable("iterated", iterateData.ToString) _
.AddJavascripFunction("ChartEventsLoad", "// set up the updating of the chart each second" & vbCr & vbLf & _
" var result = iterated.data;" & vbCr & vbLf & _
" var counter = 0;" & vbCr & vbLf & _
" var series = this.series[0];" & vbCr & vbLf & _
" var loopseries = function() {" & vbCr & vbLf & _
" if (counter <= result.length) {" & vbCr & vbLf & _
" var p = String(result[counter]);" & vbCr & vbLf & _
" var splitp = p.split("","");" & vbCr & vbLf & _
" var x = Number(splitp[0]);" & vbCr & vbLf & _
" var y = Number(splitp[1]);" & vbCr & vbLf & _
" series.addPoint([x, y], true, series.points.length > 10, true);" & vbCr & vbLf & _
" counter++;" & vbCr & vbLf & _
" } else {" & vbCr & vbLf & _
" clearInterval(loopseries);" & vbCr & vbLf & _
" }};" & vbCr & vbLf & _
" setInterval(loopseries, 100);")
ltrChart4.Text = chart4.ToHtmlString()
答案 1 :(得分:1)
您可以在sample project
的DotNet.Highchart project看到“每秒更新样条线”的演示无论如何,这里是一个示例代码,您可以在迭代数据时每秒在图表中添加点数:
Highcharts chart = new Highcharts("chart")
.SetOptions(new GlobalOptions { Global = new Global { UseUTC = false } })
.InitChart(new Chart
{
DefaultSeriesType = ChartTypes.Spline,
MarginRight = 10,
Events = new ChartEvents
{
Load = "ChartEventsLoad"
}
})
.SetTitle(new Title { Text = "Live data" })
.SetXAxis(new XAxis
{
Type = AxisTypes.Datetime,
TickPixelInterval = 150
})
.SetSeries(new Series
{
Name = "Live data",
Data = new Data(new[] { new Point { X = Tools.GetTotalMilliseconds(DateTime.Now), Y = 0 } })
})
.AddJavascripVariable("counter", "0")
.AddJavascripVariable("stockData", JsonSerializer.Serialize(ChartsData.StockData))
.AddJavascripFunction("ChartEventsLoad",
@"// set up the updating of the chart each second
var series = this.series[0];
setInterval(function() {
var x = (new Date()).getTime(); // current time
var y = stockData[counter];
series.addPoint([x, y], true, series.points.length > 10);
counter++;
}, 1000);");
对不起C#代码。我希望这很有用,你可以找到好转换器。