سؤال

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 the System.Windows.Forms.RichTextBox class;
  • chart is an instance of the System.Windows.Forms.DataVisualization.Charting.Chart class.

The following method is intended for parsing an array of strings:

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);

نصائح أخرى

I find answer for my question. I change "a litle" one line in code created by Dmitry. Here it is:

Previously code:

private static readonly CultureInfo EnglishCulture = CultureInfo.GetCultureInfo("en-US");

New code (just added System.Globalization. before CultureInfo) and it works on Visual Studio 2012 Ultimate), before change I have error "CultureInfo not found":

private static readonly System.Globalization.CultureInfo EnglishCulture = CultureInfo.GetCultureInfo("en-US");

I dont know why previously code dont work at me .. Maybe its by Visual System version. However all works now fine, and it's funny because now it works on previously code too, and the same with new change. I dont know whats going on, but all is fine.

Thanks Dmitry, for Your help.

PS. VERRY IMPORTANT (at the beginning we must declare this):

using System.Globalization;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top