c# - 图表动态创建。在 .net 中,C#

标签 c# .net winforms charts

有人有在 .NET 中使用图表的经验吗?特别是我想以编程方式创建它们。

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

我正在尝试使用 .NET(.net 4、Visual Studio 2010) chart ,但随机生成的数据集没有出现。图表仍然空​​白。我搜索了示例,但只找到了类似 this 的示例,并且,是的,使用手动“拖动”方法它可以工作。我不知道为什么我以编程方式生成的数据没有出现。

最佳答案

是的。

// 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());
        }
    }
}

用户界面:

enter image description here

关于c# - 图表动态创建。在 .net 中,C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10622674/

相关文章:

c# - 获得有关计算机的独特之处以创建免费试用版

c# - 如何更新标签中的文本

c# - 返回唯一值和 Sum LINQ

c# - if 条件与异常处理程序

c# - 如何在 WebBrowser 中以编程方式提交没有提交按钮的表单

.net - 使VB输入框中的文本框变大

c# - 使用 Angular 时保持 API 共享 secret 的安全

c# - 如何在新的 mongo C# 驱动程序中执行 findAll 并使其同步

c# - OpenMappedExeConfiguration 与 OpenExeConfiguration

c# - & 热键不会在运行时消失