2012-04-16 12 views
6

मैं गतिशील रूप से चार्ट के बाद wtih ZedGraph बनाना चाहता हूं: enter image description here ज़ेडग्राफ अक्ष रिवर्स और टाइम अक्ष कैसे बनाएं? धन्यवादज़ेडग्राफ कस्टम ग्राफ

युपीडी 1: मैं निम्नलिखित कोड के साथ की कोशिश करने के लिए है:

GraphPane myPane = zg1.GraphPane; 
      myPane.YAxis.Type = AxisType.Date; 
      myPane.YAxis.Scale.MajorUnit = DateUnit.Minute; 
      myPane.YAxis.Scale.MinorUnit = DateUnit.Second; 
      myPane.XAxis.IsVisible = false; 
      myPane.X2Axis.IsVisible = true; 
      myPane.X2Axis.MajorGrid.IsVisible = true; 
      myPane.X2Axis.Scale.Min = 0; 
      myPane.X2Axis.Scale.Max = 600; 
      myPane.YAxis.Scale.Format = "HH:mm:ss"; 
      PointPairList list = new PointPairList(); 
      PointPairList list2 = new PointPairList(); 
      for (int i = 0; i < 36; i++) 
      { 
       double x = (double)i * 5.0; 
       double y = (double)new XDate(DateTime.Now.AddSeconds(i)); 
       list.Add(y, x); 
       //list2.Add(y2, x); 
       listBox1.Items.Add("x = " + x + " y = " + y); 
      } 

      // Generate a red curve with diamond symbols, and "Alpha" in the legend 
      LineItem myCurve = myPane.AddCurve("Alpha", 
       list, Color.Red, SymbolType.None); 
      // Fill the symbols with white 
      myCurve.Symbol.Fill = new Fill(Color.White); 
         myPane.Y2Axis.MajorGrid.IsVisible = true; 
      // Align the Y2 axis labels so they are flush to the axis 
      myPane.Y2Axis.Scale.Align = AlignP.Inside; 

      // Fill the axis background with a gradient 
      myPane.Chart.Fill = new Fill(Color.White, Color.LightGray, 45.0f); 
      zg1.IsShowPointValues = true; 
      zg1.AxisChange(); 
      // Make sure the Graph gets redrawn 
      zg1.Invalidate(); 

लेकिन मैंने कुछ गलत ले: enter image description here

UPD2:

मैं के लिए कोड है मल्टीथ्रेडिंग:

private TimerCallback ReadTimerCallback; 
     private LineItem myCurve; 
     private Random rnd = new Random(500); 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      GraphPane myPane = zg1.GraphPane; 

      myPane.XAxis.IsVisible = false; 

      myPane.X2Axis.IsVisible = true; 
      myPane.X2Axis.MajorGrid.IsVisible = true; 
      myPane.X2Axis.Scale.Min = 0; 
      myPane.X2Axis.Scale.Max = 600; 

      myPane.YAxis.IsVisible = false; 

      myPane.Y2Axis.IsVisible = true; 
      myPane.Y2Axis.Scale.MajorUnit = DateUnit.Minute; 
      myPane.Y2Axis.Scale.MinorUnit = DateUnit.Second; 
      myPane.Y2Axis.Scale.Format = "HH:mm:ss"; 
      myPane.Y2Axis.Type = AxisType.DateAsOrdinal; 

      // As we get more data we want to add it on to the end of the curve 
      // and we also want to get the scale so that we can shift it along 
      double? oringinalLastDate; 
      XDate firstDate; 
      if (myPane.CurveList.Count == 0) 
      { 
       myCurve = myPane.AddCurve("Alpha", 
              new PointPairList(), 
              Color.Red, 
              SymbolType.None); 
       firstDate = new XDate(DateTime.Now); 
       oringinalLastDate = null; 
      } 
      else 
      { 
       myCurve = (LineItem)myPane.CurveList[0]; 
       firstDate = myCurve.Points[myCurve.Points.Count - 1].Y; 
       oringinalLastDate = myPane.Y2Axis.Scale.Max; 
      } 

      /*for (int i = 0; i < 36; i++) 
      { 
       double x = i * 5.0; 
       firstDate.AddSeconds(i); 

       myCurve.AddPoint(x, firstDate); 

       //listBox1.Items.Add("x = " + x + " y = " + firstDate); 
      }*/ 

      myCurve.Symbol.Fill = new Fill(Color.White); 
      myCurve.IsX2Axis = true; 
      myCurve.IsY2Axis = true; 
      //myPane.Y2Axis.Scale.IsReverse = true; 

      myPane.Chart.Fill = new Fill(Color.White, Color.LightGray, 45.0f); 
      zg1.IsShowPointValues = true; 

      // Now make the minimum of the scale, the maximum that it was so 
      // the graph shifts 
      if (oringinalLastDate.HasValue) 
       myPane.Y2Axis.Scale.Min = oringinalLastDate.Value; 

      zg1.AxisChange(); 
      zg1.Invalidate(); 
      AutoResetEvent ReadautoEvent = new AutoResetEvent(false); 
      ReadTimerCallback = new TimerCallback(this.ShowData); 
      System.Threading.Timer timer = new System.Threading.Timer(ReadTimerCallback, ReadautoEvent, 100, 1000); 

     } 

     private void ShowData (object Object) 
     { 
      this.myCurve.AddPoint(rnd.Next(500, 600), new XDate(DateTime.Now)); 
     } 

UPD3: मैं वाई अक्ष को नीचे ले जाना चाहता हूं, इस धुरी को स्केल नहीं करना चाहता हूं। और मैं अद्यतन किया है चार्ट केवल 1 मिनट: enter image description here

उत्तर

3

मुझे लगता है कि आप DateAsOrdinal चाहते हैं (दिनांक के बजाय), तो आप बेहतर तारीख अभ्यावेदन देने के लिए (हालांकि शायद नहीं करता है, तो आप इससे खुश हैं) और आपके IsX2Axis निर्धारित करने की आवश्यकता और वक्र पर IsY2 एक्सिस गुण सत्य के लिए।

यहां आपके कोड का एक अद्यतन संस्करण है जो दिखाता है कि मेरा क्या मतलब है - क्या आपको इसकी आवश्यकता है? (यह एक वैवी लाइन नहीं होगी जैसे आपने डेटा मान दिए हैं और एक्स अक्ष स्केल आपके ड्रॉइंग में 100 से 100 पर शुरू होता है, लेकिन ऐसा इसलिए है क्योंकि आपका कोड स्पष्ट रूप से 0 पर सेट हो गया था, इसलिए मुझे लगता है कि यह है कि आप चाहते हैं)।

यह हर बार जब भी इसे कॉल किया जाता है तो यह अधिक डेटा जोड़ देगा (मुझे लगता है कि आप इसे अपने बटन 1 से कॉल कर रहे हैं) और यह धुरी के लिए न्यूनतम मान को स्थानांतरित करेगा ताकि आप केवल हाल ही में डेटा दिखा रहे हों - यदि आप न्यूनतम मान निर्धारित नहीं करते हैं तो आप एक wobbly लाइन देखेंगे क्योंकि यह सभी डेटा दिखा रहा है।

GraphPane myPane = zg1.GraphPane;    

myPane.XAxis.IsVisible = false; 

myPane.X2Axis.IsVisible = true; 
myPane.X2Axis.MajorGrid.IsVisible = true; 
myPane.X2Axis.Scale.Min = 0; 
myPane.X2Axis.Scale.Max = 600; 

myPane.YAxis.IsVisible = false; 

myPane.Y2Axis.IsVisible = true; 
myPane.Y2Axis.Scale.MajorUnit = DateUnit.Minute; 
myPane.Y2Axis.Scale.MinorUnit = DateUnit.Second; 
myPane.Y2Axis.Scale.Format = "HH:mm:ss"; 
myPane.Y2Axis.Type = AxisType.DateAsOrdinal; 

// As we get more data we want to add it on to the end of the curve 
// and we also want to get the scale so that we can shift it along 
double? oringinalLastDate; 
XDate firstDate; 
LineItem myCurve; 
if(myPane.CurveList.Count == 0) 
{ 
    myCurve = myPane.AddCurve("Alpha", 
           new PointPairList(), 
           Color.Red, 
           SymbolType.None); 
    firstDate = new XDate(DateTime.Now); 
    oringinalLastDate = null; 
} 
else 
{ 
    myCurve = (LineItem)myPane.CurveList[0]; 
    firstDate = myCurve.Points[myCurve.Points.Count - 1].Y; 
    oringinalLastDate = myPane.Y2Axis.Scale.Max; 
}    

for (int i = 0; i < 36; i++) 
{ 
    double x = i * 5.0; 
    firstDate.AddSeconds(i); 

    myCurve.AddPoint(x, firstDate); 

    listBox1.Items.Add("x = " + x + " y = " + firstDate); 
} 

myCurve.Symbol.Fill = new Fill(Color.White); 
myCurve.IsX2Axis = true; 
myCurve.IsY2Axis = true; 

myPane.Chart.Fill = new Fill(Color.White, Color.LightGray, 45.0f); 
zg1.IsShowPointValues = true; 

// Now make the minimum of the scale, the maximum that it was so 
// the graph shifts 
if (oringinalLastDate.HasValue) 
    myPane.Y2Axis.Scale.Min = oringinalLastDate.Value; 

zg1.AxisChange();    
zg1.Invalidate(); 

अद्यतन

आप चार्ट अद्यतन करने के लिए y अक्ष पर 500 से 600 के बीच एक यादृच्छिक संख्या इस यह करना चाहिए (के बाद हर 3 अंक जोड़ रहे हैं, पैमाने के साथ एक बार एक दूसरे चाहते हैं साथ चलता है):

private int pointCount; 
private double? scaleMin = null; 
private static readonly Random rnd = new Random(); 

private void button1_Click(object sender, EventArgs e) 
{ 
    GraphPane myPane = zg1.GraphPane; 

    myPane.XAxis.IsVisible = false; 

    myPane.X2Axis.IsVisible = true; 
    myPane.X2Axis.MajorGrid.IsVisible = true; 
    myPane.X2Axis.Scale.Min = 0; 
    myPane.X2Axis.Scale.Max = 600; 

    myPane.YAxis.IsVisible = false; 

    myPane.Y2Axis.IsVisible = true; 
    myPane.Y2Axis.Scale.MajorUnit = DateUnit.Minute; 
    myPane.Y2Axis.Scale.MinorUnit = DateUnit.Second; 
    myPane.Y2Axis.Scale.Format = "HH:mm:ss"; 
    myPane.Y2Axis.Type = AxisType.DateAsOrdinal; 

    LineItem myCurve = myPane.AddCurve("Alpha", 
            new PointPairList(), 
            Color.Red, 
            SymbolType.None); 

    myCurve.Symbol.Fill = new Fill(Color.White); 
    myCurve.IsX2Axis = true; 
    myCurve.IsY2Axis = true; 

    myPane.Chart.Fill = new Fill(Color.White, Color.LightGray, 45.0f); 
    zg1.IsShowPointValues = true; 

    pointCount = 0; 

    var t = new System.Windows.Forms.Timer(); 
    t.Interval = 1000; 
    t.Tick += ShowData; 

    Thread.Sleep(100); 

    t.Start(); 
} 

private void ShowData(object sender, EventArgs e) 
{ 
    var t = (System.Windows.Forms.Timer) sender; 
    t.Enabled = false; 

    pointCount++; 

    int x = rnd.Next(500, 600); 
    var y = new XDate(DateTime.Now); 

    GraphPane myPane = zg1.GraphPane; 

    if (scaleMin == null) scaleMin = myPane.Y2Axis.Scale.Max; 

    LineItem myCurve = (LineItem)myPane.CurveList[0];    

    myCurve.AddPoint(x, y); 

    // After 3 points are added move the scale along 
    if (pointCount > 3) 
    { 
     myPane.Y2Axis.Scale.Min = scaleMin.Value; 
     scaleMin = myPane.Y2Axis.Scale.Max; 
    } 

    zg1.AxisChange(); 
    zg1.Invalidate(); 

    t.Enabled = true; 
} 
+0

धन्यवाद kmp! मुझे यही चाहिए, लेकिन मैं वास्तविक समय डेटा को देखने के लिए गति में समय अक्ष बनाना चाहता हूं। इसे कैसे बनाया जाए? – amaranth

+0

मैंने उत्तर अपडेट किया है - उम्मीद है कि मैंने आपको समझा है - डेटा को हर बार कोड कहा जाता है और न्यूनतम अक्ष बिंदु समायोजित किया जाता है ताकि आप डेटा के अंतिम 35 सेकंड देख सकें। – kmp

+0

1. मुझे myCurve.AddPoint (x, firstDate) के साथ नए डेटा जोड़ना होगा और myPane.Y2Axis.Scale.Min = oringinalLastDate.Value द्वारा न्यूनतम स्केल सेट करना होगा? 2. क्या मैं अपने कर्व को दूसरे धागे से जोड़ सकता हूं? – amaranth

संबंधित मुद्दे