最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

winforms - Chart won't display data for Stock Chart - Stack Overflow

programmeradmin3浏览0评论

I'm developing a WinForms app for displaying Stock price data using C# Chart and ChartType Stock. The code creates a few data samples but the code displays an empty form instead of a chart. What is missing?

I am developing the app for my own personal use only.

Here is the code:

New info: I have stepped thru the code to verify chart1.DataSource is indeed populated with the data from FillData(). I also added

 chart1.ChartAreas["ChartArea1"].Visible = true;

but still does not display data.

Also added code:

 chart1.Titles.Add("MyStock"); 

Which does show the title "MyStock" on the form, but still not the data.

private void InitializeComponent()
    {
        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();
        System.Windows.Forms.DataVisualization.Charting.Series series1 = new 
        System.Windows.Forms.DataVisualization.Charting.Series();
        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);
        legend1.Name = "Legend1";
        this.chart1.Legends.Add(legend1);
        this.chart1.Location = new System.Drawing.Point(12, 12);
        this.chart1.Name = "chart1";
        series1.ChartArea = "ChartArea1";
        series1.Legend = "Legend1";
        series1.Name = "Series1";
        series1.YValuesPerPoint = 4;
        this.chart1.Series.Add(series1);
        this.chart1.Size = new System.Drawing.Size(776, 416);
        this.chart1.TabIndex = 0;
        this.chart1.Text = "chart1";

        // 
        // StockChart
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(800, 450);
        this.Controls.Add(this.chart1);
        this.Name = "StockChart";
        this.Text = "Stock Chart";
        ((System.ComponentModel.ISupportInitialize)(this.chart1)).EndInit();
        this.ResumeLayout(false);

    }

    private 
    System.Windows.Forms.DataVisualization.Charting.Chart chart1;

Here is the form used to display stock prices

public partial class StockChart : Form
{
    public StockChart()
    {
        InitializeComponent();
        this.Load += StockChar_Load;
    }

    private void StockChar_Load(object sender, System.EventArgs e)
    {          
        chart1.Series["Series1"].ChartType = 
        System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Stock;
        chart1.Series["Series1"].CustomProperties = "PriceDownColor=Red, 
        PriceUpColor=Green";
        chart1.DataSource = FillData();
        chart1.DataBind();
        chart1.Visible = true;
    }


    private DataTable FillData()
    {
        DataTable stockData = new DataTable();
        stockData.Clear();
        stockData.Columns.Add("date");            
        stockData.Columns.Add("open");
        stockData.Columns.Add("high");
        stockData.Columns.Add("low");
        stockData.Columns.Add("close");

        DataRow row1 = stockData.NewRow();
        row1["date"] = "01/10/2025";         
        row1["open"] = 99.50;
        row1["high"] = 102.34;
        row1["low"] = 97.75;
        row1["close"] = 101;
        stockData.Rows.Add(row1);

        DataRow row2 = stockData.NewRow();
        row2["date"] = "01/11/2025";
        row2["open"] = 101.50;
        row2["high"] = 103.34;
        row2["low"] = 99.75;
        row2["close"] = 102;
        stockData.Rows.Add(row2);

        DataRow row3 = stockData.NewRow();
        row3["date"] = "01/12/2025";
        row3["open"] = 101.50;
        row3["high"] = 102.34;
        row3["low"] = 99.75;
        row3["close"] = 100;
        stockData.Rows.Add(row3);

        return stockData;
    }
}
发布评论

评论列表(0)

  1. 暂无评论