【问题标题】:C# Parse string into arrays for HighChartsC# 将字符串解析为 HighCharts 的数组
【发布时间】:2016-07-15 10:55:55
【问题描述】:

我从网络服务器获取一些动态数据,格式如下:

Series1
7   2       
8   11      
9   5       
10  4

¤Series2
6   0,1     
7   2,2     
8   10,4    
9   6,9     
10  5,1     
11  2,7     
12  3,9     
13  3,6     
14  4       
15  2,3     
16  0,3     
17  0       
18  0       
21  0       
22  0

¤Series3
6   0,2     
7   2,1     
8   9,4     
9   6,4     
10  4,6     
11  2,2     
12  3       
13  3,2     
14  4,3     
15  2,2     
16  0,6     
17  0,1     
18  0       
21  0       
22  0,1

所以系列名称、x 轴和 y 轴数据用 '¤' 分隔。

我需要将这些数据放入 HighChart 图表中,格式如下:

chart.SetXAxis(new XAxis
{
     Categories = new[] { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }
});

 chart.SetSeries(new[]
 {
       new Series {
                        Name = "Series1",
                        Type = DotNet.Highcharts.Enums.ChartTypes.Column,
                        Data = new Data
                    (new object[] { 29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4 }) },
                    new Series {
                        Name = "Series2",
                        Data = new Data
                    (new object[]{ 129.9, 171.5, 10.4, 29.2, 44.0, 76.0, 35.6, 48.5, 21.4, 19.1, 9.56, 94.4 }) }
                }
                );

我已经尝试了很多方法来格式化这些数据,包括大量的数组拆分和创建字典......但我一直失败。一个困难的部分是,这个例子中的 series1 只有 7、8、9、10(XAxis)的数据,而另一个系列有 6 到 22 的数据(例子)所以我不能指望所有的系列都有相同 x 值的数据。

是否有人具备算法来格式化此数据的知识/技能? :)

【问题讨论】:

    标签: c# highcharts format


    【解决方案1】:

    您需要做的第一件事是确定一个通用格式

    看看你有什么我会说你坚持

    字符串系列, 整数标识, int[] 绳子,

    须藤代码

    seriesString = OriginalString.SplitBySeries
    seriesRow = seriesString.SplitByNewLine
    
    series = seriesRow.First
    for each row that isn't first
        col = row.SplitByWhitespace
        id = col.first
        cord = col.Last.SplitbyComma
    

    在 C# 中看起来像

    struct DataItem
    {
        string Series{get;set;}
        int ID{get;set;}
        int[] Cord{get;set;}
    }
    
    string seriesSeperator = <Seperator string>
    string colSeperator = <Seperator string>
    List<DataItem> items
    
    foreach(string series in OriginalString.Split(seriesSeperator)
    {
        var rows = serive.Split(Environment.Newline);
        var name = series[0];
        for(int i = 1;i<series.Lenth;i++)
        {
            var col = row.Split(colSeperator );
            items.Add( new DataIten
            {
                Series = name,
                ID = int.Parse(col[0]),
                cord = col[1].Split(",").Select(c=>int.Parse(c)).ToArray()
            });
        }
    }
    

    注释代码仅供参考,未经测试

    然后,一旦您有了项目列表,您就可以构建图表

    【讨论】:

      【解决方案2】:

      试试下面的代码。我使用 StreamReader 从文件中读取,但您可以使用 StringReader 从字符串中读取。

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.IO;
      
      
      namespace ConsoleApplication3
      {
          class Program
          {
              const string FILENAME = @"c:\temp\test.txt";
              public enum State
              {
                  FIND_GROUP,
                  GET_GROUP
              }
      
              static void Main(string[] args)
              {
                  StreamReader reader = new StreamReader(FILENAME);
                  string inputLine = "";
                  Series series = null;
                  State state = State.FIND_GROUP;
                  while ((inputLine = reader.ReadLine()) != null)
                  {
                      inputLine = inputLine.Trim();
                      if (inputLine.Length > 0)
                      {
                          switch (state)
                          {
                              case State.FIND_GROUP:
                                  series = new Series();
                                  series.name = inputLine.Replace("�", "");
                                  Series.series.Add(series);
                                  state = State.GET_GROUP;
                                  break;
                              case State.GET_GROUP:
                                  string[] numbers = inputLine.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                                  series.values.Add(new KeyValuePair<int, List<int>>(
                                      int.Parse(numbers[0]),
                                      numbers[1].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(x => int.Parse(x)).ToList()
                                  ));
                                  break;
                          }
                      }
                      else
                      {
                          state = State.FIND_GROUP;
                      }
                  }
      
              }
           }
          public class Series
          {
              public string name { get; set; }
              public static List<Series> series = new List<Series>();
              public List<KeyValuePair<int, List<int>>> values { get; set; }
              public Series()
              {
                  values = new List<KeyValuePair<int, List<int>>>();
              }
           }
      
      }
      

      【讨论】:

      • 感谢您的建议。看起来很有希望。不过也有误会。您不能依赖StartsWith("Series"),因为Series1Series2Series3 只是示例。在实际数据中,这些被替换为图表的名称。比如“香蕉”、“苹果”、“橙子”或其他东西(未知)
      • 组之间总是有一个空白行?我需要一种特定的方法来确定组何时结束。
      猜你喜欢
      • 1970-01-01
      • 2012-11-23
      • 2011-12-06
      • 2017-02-02
      • 1970-01-01
      • 2013-03-30
      • 2022-06-24
      • 2010-10-03
      • 1970-01-01
      相关资源
      最近更新 更多