0

I am wondering if a chart can be made to display its default data set rather than a blank page after the app has been run.

When a chart is created in visual studio it displays some default data:

enter image description here

When the app is run however, the resulting chart is blank:

enter image description here

I understand that the app acts this way because there is no data assigned to the chart. I am wondering though, if it is possible to somehow change the behavior of the chart to display the default data. I tried searching through chart properties for this feature, but couldn't find it. Might have missed it though, since there are so many of them.

As an alternative solution I could create my own default dummy data as it is suggested here: c# chart axis not displaying when form is loaded.

Code example with a blank chart. (requires reference to System.Windows.Forms.DataVisualization):

using System;
using System.Windows.Forms;

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

    public class Chart: Form
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        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.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
            series1.Legend = "Legend1";
            series1.Name = "Series1";
            this.chart1.Series.Add(series1);
            this.chart1.Size = new System.Drawing.Size(776, 426);
            this.chart1.TabIndex = 0;
            this.chart1.Text = "chart1";
            // 
            // Chart
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(800, 450);
            this.Controls.Add(this.chart1);
            this.Name = "Chart";
            this.Text = "Form1";
            ((System.ComponentModel.ISupportInitialize)(this.chart1)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

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

        public Chart()
        {
            InitializeComponent();
        }
    }
}
  • I believe you need to create your own dummy – BugFinder Jun 17 '19 at 10:38
  • There are no 'default data'. You need to either create some or, as a workaround, you could load an image as ChartAreas]0}.BackImage or, if you make the chartarea.BackColor transparent as chart.BackImage. Also, for a similar demand, [see here!](https://stackoverflow.com/questions/55621622/how-to-show-a-message-if-chart-data-is-empty/55621939?r=SearchResults&s=2|52.3718#5562193) – TaW Jun 17 '19 at 10:42

0 Answers0