WinForm控件之【Chart】
基本介绍
图表控件,图表对象属性比较丰富再此不做详细描述解读,通过下列例子相信初学者很快就能上手。
常设置属性
Titles:图表标题;
ChartAreas:图表区,对图表区域的XY轴、网格、数据展示格式类型等的各属性;
Series:图表序列,显示图表类型、数据点个属性、轴标签、调色板等设置;
Legends:图表图例,一般用于解释图表区域部分色调指向或数据分析;
Name:指示代码中用来标识该对象的名称;
事例举例
相关代码
//加载 每日实时票房 private void btn_dailyBoxOffice_Click(object sender, EventArgs e) { //不显示图例 foreach (System.Windows.Forms.DataVisualization.Charting.Legend item in chart1.Legends) { item.Enabled = false; } //设置背景为白色 chart1.ChartAreas[0].BackColor = Color.White; //标题 if (chart1.Titles.Count > 0) chart1.Titles.Clear(); System.Windows.Forms.DataVisualization.Charting.Title title = new System.Windows.Forms.DataVisualization.Charting.Title("实时票房", System.Windows.Forms.DataVisualization.Charting.Docking.Top, new System.Drawing.Font("宋体", 16F), Color.Black); //title.Alignment = ContentAlignment.MiddleCenter; chart1.Titles.Add(title); //图表设置 chart1.ChartAreas[0].Area3DStyle.Enable3D = false; chart1.ChartAreas[0].AxisY.Interval = 170;//设置刻度间隔为170 chart1.ChartAreas[0].AxisY.LabelStyle.Format = ""; chart1.ChartAreas[0].AxisX.Minimum = 0; chart1.ChartAreas[0].AxisX.IsMarginVisible = true; chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = Color.Silver; chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.Silver; chart1.ChartAreas[0].AxisX.MajorGrid.Enabled = false;//不显示X主要网格线 chart1.ChartAreas[0].AxisY.MajorGrid.Enabled = true;//显示Y主要网格线 chart1.ChartAreas[0].AxisX.MajorTickMark.Enabled = false;//不显示X主要刻度线 chart1.ChartAreas[0].AxisY.MajorTickMark.Enabled = false;//不显示Y主要刻度线 //标签上不显示数据点的值 chart1.Series[0].IsValueShownAsLabel = false; //设置调色板 chart1.Series[0].Palette = System.Windows.Forms.DataVisualization.Charting.ChartColorPalette.Pastel; //设置纵向条形图类型 chart1.Series[0].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Bar; chart1.Series[0].CustomProperties = ""; if (chart1.Series[0].Points.Count > 0) chart1.Series[0].Points.Clear(); //添加数据 chart1.Series[0].Points.AddXY("其他", 90); chart1.Series[0].Points.AddXY("爱宠大机密2", 50); chart1.Series[0].Points.AddXY("猪八戒.传说", 60); chart1.Series[0].Points.AddXY("蜘蛛侠:英雄远征", 100); chart1.Series[0].Points.AddXY("扫毒2:天地对决", 250); chart1.Series[0].Points.AddXY("狮子王", 500); chart1.Series[0].Points.AddXY("银河补习班", 800); lbl_unit.Visible = true; } //加载 影片排片占比 private void btn_filmRatio_Click(object sender, EventArgs e) { chart1.ChartAreas[0].BackColor = Color.White;//设置背景为白色 //标题 if (chart1.Titles.Count > 0) chart1.Titles.Clear(); System.Windows.Forms.DataVisualization.Charting.Title title = new System.Windows.Forms.DataVisualization.Charting.Title("排片占比", System.Windows.Forms.DataVisualization.Charting.Docking.Top, new System.Drawing.Font("宋体", 16F), Color.Black); chart1.Titles.Add(title); //设置3D效果 chart1.ChartAreas[0].Area3DStyle.Enable3D = true; chart1.ChartAreas[0].Area3DStyle.PointDepth = 50; chart1.ChartAreas[0].Area3DStyle.PointGapDepth = 50; chart1.ChartAreas[0].Area3DStyle.WallWidth = 0;//设置墙的宽度为0 //图表设置 chart1.ChartAreas[0].AxisY.LabelStyle.Format = "0%"; chart1.ChartAreas[0].AxisY.Interval = 0.05;//设置刻度间隔为5% chart1.ChartAreas[0].AxisX.Minimum = 0.1; chart1.ChartAreas[0].AxisX.MajorGrid.Enabled = false;//不显示X网格线 chart1.ChartAreas[0].AxisY.MajorGrid.Enabled = false;//不显示Y网格线 chart1.ChartAreas[0].AxisX.MajorTickMark.Enabled = false;//不显示X主要刻度线 chart1.ChartAreas[0].AxisY.MajorTickMark.Enabled = false;//不显示Y主要刻度线 chart1.Series[0].IsValueShownAsLabel = true;//显示标签 chart1.Series[0].Label = "#VAL{P}";//设置数据点标签的文本格式 //设置数据点展示形式为圆柱形 chart1.Series[0].CustomProperties = "DrawingStyle=Cylinder, PointWidth=1"; //设置调色板 chart1.Series[0].Palette = System.Windows.Forms.DataVisualization.Charting.ChartColorPalette.Pastel; //设置图表类型 chart1.Series[0].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Column; if (chart1.Series[0].Points.Count > 0) chart1.Series[0].Points.Clear(); //数据 chart1.Series[0].Points.AddXY("银河补习班", 0.3437); chart1.Series[0].Points.AddXY("狮子王", 0.2127); chart1.Series[0].Points.AddXY("扫毒2:天地对决", 0.1845); chart1.Series[0].Points.AddXY("蜘蛛侠:英雄远征", 0.0691); chart1.Series[0].Points.AddXY("猪八戒.传说", 0.0069); chart1.Series[0].Points.AddXY("爱宠大机密2", 0.0221); chart1.Series[0].Points.AddXY("其他", 0.161); //多次加载时清理图例 chart1.Legends[0].Enabled = false; if (chart1.Legends.Count > 1) chart1.Legends.RemoveAt(1); //创建影片图例 System.Windows.Forms.DataVisualization.Charting.Legend legend = new System.Windows.Forms.DataVisualization.Charting.Legend(); legend.Title = "影片名称"; Dictionary<string, Color> dic = new Dictionary<string, Color>(); dic.Add("银河补习班", Color.Red); dic.Add("狮子王", Color.Pink); dic.Add("扫毒2:天地对决", Color.Blue); dic.Add("蜘蛛侠:英雄远征", Color.SkyBlue); dic.Add("猪八戒.传说", Color.GreenYellow); dic.Add("爱宠大机密2", Color.Orange); dic.Add("其他", Color.MediumPurple); //加载图例列 foreach (System.Windows.Forms.DataVisualization.Charting.DataPoint item in chart1.Series[0].Points) { item.IsVisibleInLegend = true; item.LegendText = item.AxisLabel; item.Color = dic[item.AxisLabel]; System.Windows.Forms.DataVisualization.Charting.LegendItem legendItem = new System.Windows.Forms.DataVisualization.Charting.LegendItem(); legendItem.Color = dic[item.AxisLabel]; legendItem.Name = item.AxisLabel; legend.CustomItems.Add(legendItem); } chart1.Legends.Add(legend); lbl_unit.Visible = false; }