c# - c#winforms应用程序运行时如何在图表中显示默认虚拟数据集
问题描述
我想知道在应用程序运行后是否可以制作图表来显示其默认数据集而不是空白页面。
在 Visual Studio 中创建图表时,它会显示一些默认数据:
但是,当应用程序运行时,结果图表为空白:
我了解该应用程序以这种方式运行,因为没有为图表分配数据。不过我想知道,是否有可能以某种方式更改图表的行为以显示默认数据。我尝试在图表属性中搜索此功能,但找不到。不过可能错过了,因为它们太多了。
作为替代解决方案,我可以按照此处的建议创建自己的默认虚拟数据:c# chart axis not displayed when form is loaded。
带有空白图表的代码示例。(需要参考 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();
}
}
}
解决方案
推荐阅读
- reactjs - REACT 上下文 API 提供程序返回未定义
- qt - 从 QWebSocketServer 实时向客户端发送一系列消息
- rust - Rust 的内置目标规范在哪里定义?
- python - 尝试使用 Flask-SQLAlchemy 创建数据库
- python - ValueError:无法将 NumPy 数组转换为张量(不支持的对象类型 numpy.ndarray)。当给出数组数组时
- javascript - 如何在字符串中找到匹配单词的确切数量?
- c# - 使用 2d 数组来跟踪“d”在该位置的时间
- c - 在用户输入我的 if else if 循环后,什么都不做(CS50 pset1)
- python - 使用 Pexpect.spawn() 执行 shell 脚本
- c# - 具有自定义格式的 DateTime.ParseExact 失败