C#编程实现饼图控件PieChart详细代码示例

需积分: 48 28 下载量 66 浏览量 更新于2024-09-10 1 收藏 1KB TXT 举报
"这篇文章主要介绍了如何在C#中利用PieChart控件开发饼图的代码实现。通过在窗体上添加Panel控件,并在其中放置PieChart,可以创建出具有不同颜色、工具提示以及交互效果的饼图。" 在C#编程中,饼图是一种常用的数据可视化方式,它能直观地展示数据的比例关系。在这个示例中,我们使用了一个名为PieChart的控件来创建饼图。首先,实例化了一个PieChart对象`PieChart1`: ```csharp PieChartPieChart1 = new PieChart(); ``` 接着,为PieChart控件添加了一个事件处理程序,当用户点击图表中的某个扇区时,将触发`PieChart1_ItemClicked`方法: ```csharp PieChart1.ItemClicked += new Nexus.Windows.Forms.PieChartItemEventHandler(this.PieChart1_ItemClicked); ``` 然后,向PieChart1中添加了多个`PieChartItem`,每个项代表饼图的一个扇区,包含了其值、颜色、标签和工具提示信息: ```csharp PieChart1.Items.Add(new PieChartItem(10, Color.BurlyWood, "Tan", "Tantooltip", 0)); PieChart1.Items.Add(new PieChartItem(10, Color.Gold, "Gold", "Goldtooltip", 0)); PieChart1.Items.Add(new PieChartItem(10, Color.Chocolate, "Brown", "Browntooltip", 50)); PieChart1.Items.Add(new PieChartItem(20, Color.DarkRed, "Red", "Redtooltip", 0)); ``` 这些项的值(如10、20)决定了扇区的大小,颜色定义了显示的颜色,标签是显示在图例中的文字,而工具提示则在鼠标悬停时显示。 接下来,我们设置了饼图的一些样式属性,如表面透明度、高亮亮度、旋转角度、厚度和半径: ```csharp PieChart1.ItemStyle.SurfaceAlphaTransparency = 0.75F; PieChart1.FocusedItemStyle.SurfaceAlphaTransparency = 0.75F; PieChart1.FocusedItemStyle.SurfaceBrightnessFactor = 0.3F; PieChart1.Rotation = (float)(3 * Math.PI / 180); PieChart1.Thickness = 20; PieChart1.Radius = 104; ``` 此外,还设置了位置、大小和背景色: ```csharp PieChart1.Location = new System.Drawing.Point(567, 287); PieChart1.BackColor = Color.White; PieChart1.Width = 450; PieChart1.Height = 280; ``` 最后,将PieChart1添加到Panel1的控件集合中,以便在窗体上显示: ```csharp panel1.Controls.Add(PieChart1); ``` 通过以上步骤,我们就成功地在C#窗体应用中创建了一个交互式的饼图。这个饼图可以根据需要进行定制,包括调整颜色、尺寸、样式等,以适应不同的数据可视化需求。
229 浏览量
web页上绘制饼图曲线图等组件(c#) 例子: 生成饼图表******************************************** private void InitializeComponent() { this.myBarGraph.RenderGraph += new ZedGraph.Web.ZedGraphWebControlEventHandler(this.OnRenderGraphBar); this.myLineGraph.RenderGraph += new ZedGraph.Web.ZedGraphWebControlEventHandler(this.OnRenderGraphLine); } private void OnRenderGraphBar(ZedGraphWeb zgw, System.Drawing.Graphics g, ZedGraph.MasterPane masterPane) { myBarMethod(zgw, g, masterPane, strYear, strMonth, compareType); } private void OnRenderGraphLine(ZedGraphWeb zgw, System.Drawing.Graphics g, ZedGraph.MasterPane masterPane) { myLineMethod(zgw, g, masterPane, douYear, douMonth); } private void myBarMethod(ZedGraphWeb zgw, System.Drawing.Graphics g, ZedGraph.MasterPane masterPane, string[] strYear, string[] strMonth, string compareType) { GraphPane myPane = masterPane[0]; myPane.Title.Text = title + "柱状图分析"; myPane.XAxis.Title.Text = "时间(月/年)"; if (compareType == "AddValue") { myPane.YAxis.Title.Text = "增加值"; } else { myPane.YAxis.Title.Text = "增加比例(%)"; } List<PointPairList> ListPointParitList = new List<PointPairList>(); for (int i = 0; i < strYear.Length; i++) { ListPointParitList.Add(new PointPairList()); } int n = 0; for (double x = 0; x < strMonth.Length; x += 1.0) { for (int i = 0; i < strYear.Length; i++) { //ListPointParitList[i].Add(x, randNum[n++] * multiplyValue); ListPointParitList[i].Add(x, GetFXData(strYear[i], strMonth[(int)x])); } } List<BarItem> ListBarItem = new List<BarItem>(); List<Color> ListColor = GetColor(); for (int i = 0; i < strYear.Length; i++) { ListBarItem.Add(new BarItem(strYear[i], ListPointParitList[i], ListColor[i])); ListBarItem[i] = myPane.AddBar(strYear[i], ListPointParitList[i], ListColor[i]); } myPane.XAxis.MajorTic.IsBetweenLabels = true; myPane.XAxis.Scale.TextLabels = strMonth; myPane.XAxis.Type = AxisType.Text; myPane.Fill = new Fill(Color.White, Color.FromArgb(200, 200, 255), 45.0f); myPane.Chart.Fill = new Fill(Color.White, Color.LightGoldenrodYellow, 45.0f); masterPane.AxisChange(g); }