我试图从数据表生成winform折线图,但UI看起来不太好。
另见我的完整示例代码。我相信我在代码中缺少一些图表用户界面看起来不太好的东西。我不明白白色盒子出现在图表中的原因是什么?
请查看我的完整代码并告诉我在代码中需要添加或纠正的内容。
<h3>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.</h3>
<p style="text-align: justify;">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.</p>
<br>
<h3>Ut wisi enim ad minim veniam</h3>
<p style="text-align: justify;">Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.</p>
由于
答案 0 :(得分:2)
图表附带一些默认元素,如ChartArea,Series和Legend。
如果您想创建自己的ChartArea,为什么不首先清除默认的ChartArea? Chart1.ChartAreas.Clear();
然后(并且只有这样)您的意志实际上将使用Chart1.ChartAreas[0]
引用访问您新创建的那个。
我还建议使用对Series,ChartAreas等的简短和精美命名的引用。:
ChartArea chtArea = Chart1.ChartAreas.Add("chtArea");
Series unitPrice = Chart1.Series.Add("UnitPrice");
这使代码更紧凑,更清晰..
答案 1 :(得分:0)
问题解决了。此行导致问题Chart1.ChartAreas.Add("chtArea");
,所以我删除了这一行。
private void show()
{
DataRow dr = null;
DataTable dt = new DataTable();
dt.Columns.Add("Date", typeof(DateTime));
dt.Columns.Add("data", typeof(Int32));
dt.Columns.Add("CountryCode", typeof(string));
dr = dt.NewRow();
dr[0] = DateTime.Parse("01/01/2017");
dr[1] = 30;
dr[2] = "GB";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = DateTime.Parse("02/01/2017");
dr[1] = 09;
dr[2] = "GB";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = DateTime.Parse("03/01/2017");
dr[1] = 15;
dr[2] = "GB";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = DateTime.Parse("04/01/2017");
dr[1] = 22;
dr[2] = "GB";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = DateTime.Parse("05/01/2017");
dr[1] = 13;
dr[2] = "GB";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = DateTime.Parse("06/01/2017");
dr[1] = 22;
dr[2] = "GB";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = DateTime.Parse("07/01/2017");
dr[1] = 07;
dr[2] = "GB";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = DateTime.Parse("08/01/2017");
dr[1] = 11;
dr[2] = "GB";
dt.Rows.Add(dr);
//Chart1.BorderSkin.SkinStyle = System.Windows.Forms.DataVisualization.Charting.BorderSkinStyle.Emboss;
////Chart1.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;
//Chart1.BorderlineColor = System.Drawing.Color.FromArgb(26, 59, 105);
//Chart1.BorderlineWidth = 3;
//Chart1.BackColor = Color.NavajoWhite;
//Chart1.ChartAreas.Add("chtArea");
Chart1.ChartAreas[0].AxisX.Title = "NPS Dates";
Chart1.ChartAreas[0].AxisX.LabelStyle.Angle = -60;
Chart1.ChartAreas[0].AxisX.TitleFont = new System.Drawing.Font("Verdana", 11, System.Drawing.FontStyle.Bold);
Chart1.ChartAreas[0].AxisY.Title = "NPS Values";
Chart1.ChartAreas[0].AxisY.TitleFont = new System.Drawing.Font("Verdana", 11, System.Drawing.FontStyle.Bold);
Chart1.ChartAreas[0].BorderDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Solid;
Chart1.ChartAreas[0].BorderWidth = 2;
//Chart1.Legends.Add("UnitPrice");
//Chart1.Series.Add("UnitPricexxx");
//Chart1.Series[0].Palette = ChartColorPalette.Bright;
Chart1.Series[0].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
Chart1.Series[0].Points.DataBindXY(dt.DefaultView, "Date", dt.DefaultView, "Data");
//Chart1.Series[0].IsVisibleInLegend = true;
Chart1.Series[0].IsValueShownAsLabel = true;
Chart1.Series[0].ToolTip = "Data Point Y Value: #VALY{G}";
// Setting Line Width
Chart1.Series[0].BorderWidth = 3;
Chart1.Series[0].Color = Color.Red;
// Setting Line Shadow
//Chart1.Series[0].ShadowOffset = 5;
//Legend Properties
Chart1.Legends[0].LegendStyle = System.Windows.Forms.DataVisualization.Charting.LegendStyle.Table;
Chart1.Legends[0].TableStyle = System.Windows.Forms.DataVisualization.Charting.LegendTableStyle.Wide;
Chart1.Legends[0].Docking = System.Windows.Forms.DataVisualization.Charting.Docking.Bottom;
Chart1.Width = 488;
Chart1.Height = 345;
}