2016-07-15 48 views
0

陣列我從一個網絡服務器的一些動態的數據格式爲:C#解析字符串成HighCharts

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 

SO系列名稱,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值。

有沒有人有格式化這些數據的算法的知識/技能? :)

回答

0

你需要做一個共同的格式

決定是看你有什麼,我會說的第一件事,你的堅持與

串系列, INT ID, 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() 
     }); 
    } 
} 

注碼是唯一的指導,並沒有經過測試

然後,一旦你有一個項目的列表,你可以在下面建立自己的圖形

0

try代碼。我使用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>>>(); 
     } 
    } 

} 
+0

感謝您的建議。看起來很有希望。雖然存在誤解。你不能依賴'StartsWith(「Series」)',因爲_Series1_,_Series2_和_Series3_僅僅是一個例子。在實際數據中,這些被替換爲圖表的名稱。像「香蕉」,「蘋果」,「橙色」或其他東西(未知) – capcapdk

+0

我總是有一個空白的行之間組?我需要一個確定小組何時結束的具體方法。 – jdweng

+0

更新代碼以查找空白行 – jdweng