2012-05-16 26 views
24

Hat jemand Erfahrung in der Arbeit mit Diagrammen in .NET? Speziell möchte ich sie programmatisch erstellen.Diagramm dynamisch erstellen. in .net, C#

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Windows.Forms.DataVisualization.Charting; 
using System.Diagnostics; 

namespace WindowsFormsApplication6 
{ 
    public partial class Form1 : Form 
    { 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     Random rnd = new Random(); 
     Chart mych = new Chart(); 
     mych.Series.Add("duck"); 

     mych.Series["duck"].SetDefault(true); 
     mych.Series["duck"].Enabled = true; 
     mych.Visible = true; 

     for (int q = 0; q < 10; q++) 
     { 
      int first = rnd.Next(0,10); 
      int second = rnd.Next(0,10); 
      mych.Series["duck"].Points.AddXY(first, second); 
      Debug.WriteLine(first + " " + second); 
     } 
     mych.Show(); 
     Controls.Add(mych); 
     mych.Show(); 
    } 
    } 
} 

Ich versuche .NET (.net 4, Visual Studio 2010)chart, aber die zufällig generierte Datensatz zu verwenden, nicht angezeigt. Das Diagramm blieb leer. Ich suchte nach Beispielen und fand nur solche wie this, und ja, mit manueller "Drag" -Methode funktioniert es. Ich habe keine Ahnung, warum die Daten, die ich programmatisch erzeuge, nicht erscheinen.

+0

Ich mag diese https: // Github .com/beto-rodriguez/Live-Charts –

Antwort

44

Ja.

// FakeChart.cs 
// ------------------------------------------------------------------ 
// 
// A Winforms app that produces a contrived chart using 
// DataVisualization (MSChart). Requires .net 4.0. 
// 
// Author: Dino 
// 
// ------------------------------------------------------------------ 
// 
// compile: \net4.0\csc.exe /t:winexe /debug+ /R:\net4.0\System.Windows.Forms.DataVisualization.dll FakeChart.cs 
// 

using System; 
using System.Windows.Forms; 
using System.Windows.Forms.DataVisualization.Charting; 


namespace Dino.Tools.WebMonitor 
{ 
    public class FakeChartForm1 : Form 
    { 
     private System.ComponentModel.IContainer components = null; 
     System.Windows.Forms.DataVisualization.Charting.Chart chart1; 

     public FakeChartForm1() 
     { 
      InitializeComponent(); 
     } 

     private double f(int i) 
     { 
      var f1 = 59894 - (8128 * i) + (262 * i * i) - (1.6 * i * i * i); 
      return f1; 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      chart1.Series.Clear(); 
      var series1 = new System.Windows.Forms.DataVisualization.Charting.Series 
      { 
       Name = "Series1", 
       Color = System.Drawing.Color.Green, 
       IsVisibleInLegend = false, 
       IsXValueIndexed = true, 
       ChartType = SeriesChartType.Line 
      }; 

      this.chart1.Series.Add(series1); 

      for (int i=0; i < 100; i++) 
      { 
       series1.Points.AddXY(i, f(i)); 
      } 
      chart1.Invalidate(); 
     } 

     protected override void Dispose(bool disposing) 
     { 
      if (disposing && (components != null)) 
      { 
       components.Dispose(); 
      } 
      base.Dispose(disposing); 
     } 

     private void InitializeComponent() 
     { 
      this.components = new System.ComponentModel.Container(); 
      System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea(); 
      System.Windows.Forms.DataVisualization.Charting.Legend legend1 = new System.Windows.Forms.DataVisualization.Charting.Legend(); 
      this.chart1 = new System.Windows.Forms.DataVisualization.Charting.Chart(); 
      ((System.ComponentModel.ISupportInitialize)(this.chart1)).BeginInit(); 
      this.SuspendLayout(); 
      // 
      // chart1 
      // 
      chartArea1.Name = "ChartArea1"; 
      this.chart1.ChartAreas.Add(chartArea1); 
      this.chart1.Dock = System.Windows.Forms.DockStyle.Fill; 
      legend1.Name = "Legend1"; 
      this.chart1.Legends.Add(legend1); 
      this.chart1.Location = new System.Drawing.Point(0, 50); 
      this.chart1.Name = "chart1"; 
      // this.chart1.Size = new System.Drawing.Size(284, 212); 
      this.chart1.TabIndex = 0; 
      this.chart1.Text = "chart1"; 
      // 
      // Form1 
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
      this.ClientSize = new System.Drawing.Size(284, 262); 
      this.Controls.Add(this.chart1); 
      this.Name = "Form1"; 
      this.Text = "FakeChart"; 
      this.Load += new System.EventHandler(this.Form1_Load); 
      ((System.ComponentModel.ISupportInitialize)(this.chart1)).EndInit(); 
      this.ResumeLayout(false); 
     } 

     /// <summary> 
     /// The main entry point for the application. 
     /// </summary> 
     [STAThread] 
     static void Main() 
     { 
      Application.EnableVisualStyles(); 
      Application.SetCompatibleTextRenderingDefault(false); 
      Application.Run(new FakeChartForm1()); 
     } 
    } 
} 

UI:

enter image description here

+1

Vielen Dank! Ich versuche es herauszufinden, Hexe hatte Probleme mit meinem Code. Ist toll, dass ich endlich ein funktionierendes Beispiel habe. Und google, findet im Grunde nichts, so. – KilyenOrs

+0

So entfernen Sie Achsengitter? – user

0

Sie müssen die Form1_Load Handler zum Load Ereignis befestigen:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Windows.Forms.DataVisualization.Charting; 
using System.Diagnostics; 

namespace WindowsFormsApplication6 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
      Load += Form1_Load; 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      Random rnd = new Random(); 
      Chart mych = new Chart(); 
      mych.Height = 100; 
      mych.Width = 100; 
      mych.BackColor = SystemColors.Highlight; 
      mych.Series.Add("duck"); 

      mych.Series["duck"].SetDefault(true); 
      mych.Series["duck"].Enabled = true; 
      mych.Visible = true; 

      for (int q = 0; q < 10; q++) 
      { 
       int first = rnd.Next(0, 10); 
       int second = rnd.Next(0, 10); 
       mych.Series["duck"].Points.AddXY(first, second); 
       Debug.WriteLine(first + " " + second); 
      } 

      Controls.Add(mych); 
     } 
    } 
} 
+0

wahrscheinlich bin ich der Nob, aber nur ein leeres Diagramm erscheint ohne Daten (mit dem genauen Code, den Sie geschrieben haben) Es ist möglich, dass dies einer meiner Fehler ist, aber nicht der einzige sicher. Danke, dass du trotzdem geholfen hast. – KilyenOrs

+0

@KilyenOrs 'mych.Invalidate()' ist der Schlüssel. –

6

Microsoft hat eine schöne Grafik Kontrolle. Laden Sie es herunter here. Tolles Video zu diesem here. Beispielcode ist here. Glückliche Kodierung!

+2

Ein guter Link. Beachten Sie, dass es für .NET 3.5 ist. .NET 4+ verfügt über die neue Diagrammklasse in [System.Windows.Forms.DataVisualization.Charting] (http://msdn.microsoft.com/en-us/library/system.windows.forms.datavisualization.charting.chart. aspx). – MickyD

4

Fügen Sie einen Verweis auf System.Windows.Form.DataVisualization, dann fügen Sie die entsprechende Anweisung using:

using System.Windows.Forms.DataVisualization.Charting; 

private void CreateChart() 
{ 
    var series = new Series("Finance"); 

    // Frist parameter is X-Axis and Second is Collection of Y- Axis 
    series.Points.DataBindXY(new[] { 2001, 2002, 2003, 2004 }, new[] { 100, 200, 90, 150 }); 
    chart1.Series.Add(series); 

} 
1

Versuchen Sie, diese Zeilen auf Ihrem Code, nach mych.Visible = true;:

ChartArea chA = new ChartArea(); 
mych.ChartAreas.Add(chA); 
Verwandte Themen