问题描述
是否可以绘制从richTextBox收集的数据图?例如我在richTextBox中有以下文本:
$ GPRMC,152908.00,A,5307.0794359,N,02308.8918827,E, 188.6,280612,0.0,E,D * 3A
$ GPRMC,152909.00,A,5307.0794307,N,02308.8918792,E,0.081,193.3,280612,0.0,E,D * 3A
$ GPRMC, 152910.00,A,5307.0794343,N,02308.8918811,E,0.034,42.9,280612,0.0,E,D * 0F
$ GPRMC,152911.00,A,5307.0794410,N,02308.8918827,E,0.099,1.3,280612, 0.0,E,D * 30
$ GPRMC,152912.00,A,5307.0794461,N,02308.8918808,E,0.055,331.9,280612,0.0,E,D * 32
我想要绘制图表,例如纬度(时间):
5307.0794359 - > 152908.00
5307.0794307 - > 152909.00
5307.0794343 - > 152910.00
5307.0794410 - > 152911.00
5307.0794461 - > 152912.00
我不知道如何写一个函数, ,4,5]和时间[1,2,3,4,5]。然后绘制图表。
我需要一个通用函数,因为我可以有100行或400行等数据。
任何人帮助我?计数我任何帮助(代码,示例,提示或链接)。
解决方案假设:
-
richTextBox
是System.Windows.Forms.RichTextBox
类的一个实例; -
图表
是System.Windows.Forms.DataVisualization.Charting.Chart
类的实例。
以下方法用于解析 string
s的数组:
private static readonly CultureInfo EnglishCulture = CultureInfo.GetCultureInfo(en-US);
private static Tuple< double,double> [] GetData(string [] lines)
{
return Array.ConvertAll(lines,line =>
{
string [] elems = line.Split(',');
return new Tuple< double,double>(double.Parse(elems [3],EnglishCulture),double.Parse ],EnglishCulture));
});
}
用法:
var data = GetData(richTextBox.Lines);
现在你只需要绑定这个 data
数组作为图表的 DataSource
或手动将它们添加到系列,如下所示:
chart.Series.Clear();
Series series = new Series(sample){ChartType = SeriesChartType.Line,BorderWidth = 2,MarkerSize = 5,MarkerStyle = MarkerStyle.Square};
foreach(var p in data)
series.Points.Add(p.Item1,p.Item2);
chart.Series.Add(series);
is it possible to plot graphs of data that are collected from a richTextBox? For example I have following text in richTextBox:
$GPRMC,152908.00,A,5307.0794359,N,02308.8918827,E,0.049,188.6,280612,0.0,E,D*3A
$GPRMC,152909.00,A,5307.0794307,N,02308.8918792,E,0.081,193.3,280612,0.0,E,D*3A
$GPRMC,152910.00,A,5307.0794343,N,02308.8918811,E,0.034,42.9,280612,0.0,E,D*0F
$GPRMC,152911.00,A,5307.0794410,N,02308.8918827,E,0.099,1.3,280612,0.0,E,D*30
$GPRMC,152912.00,A,5307.0794461,N,02308.8918808,E,0.055,331.9,280612,0.0,E,D*32
And I want plot chart for example Latitude(time):
5307.0794359 -> 152908.00
5307.0794307 -> 152909.00
5307.0794343 -> 152910.00
5307.0794410 -> 152911.00
5307.0794461 -> 152912.00
I dont how to write a function, that will retrieve Latitude from lines[1,2,3,4,5] and time from lines [1,2,3,4,5]. And then plot a chart.
I need a universal function, because I can have data which 100 lines or 400 lines etc.
Can anyone help me? Counts for me any help (code, example, tips or links).
Assuming that:
richTextBox
is an instance of theSystem.Windows.Forms.RichTextBox
class;chart
is an instance of theSystem.Windows.Forms.DataVisualization.Charting.Chart
class.
The following method is intended for parsing an array of string
s:
private static readonly CultureInfo EnglishCulture = CultureInfo.GetCultureInfo("en-US");
private static Tuple<double, double>[] GetData(string[] lines)
{
return Array.ConvertAll(lines, line =>
{
string[] elems = line.Split(',');
return new Tuple<double, double>(double.Parse(elems[3], EnglishCulture), double.Parse(elems[1], EnglishCulture));
});
}
Usage:
var data = GetData(richTextBox.Lines);
Now you only need to bind this data
array as a chart's DataSource
or manually add them to the series as shown below:
chart.Series.Clear();
Series series = new Series("sample") { ChartType = SeriesChartType.Line, BorderWidth = 2, MarkerSize = 5, MarkerStyle = MarkerStyle.Square };
foreach (var p in data)
series.Points.Add(p.Item1, p.Item2);
chart.Series.Add(series);
这篇关于使用来自richTextBox的值绘制图C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!