当前位置:   article > 正文

C# Winform Chart控件用法4之饼状图_c# 饼图

c# 饼图

1、拖chart控件在新建的窗口,命名chart1

 2、源代码

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Windows.Forms.DataVisualization.Charting;
  11. namespace chart饼状图
  12. {
  13. public partial class Form1 : Form
  14. {
  15. public Form1()
  16. {
  17. InitializeComponent();
  18. }
  19. private void Form1_Load(object sender, EventArgs e)
  20. {
  21. string[] x = new string[] { "成都大队", "广东大队", "广西大队", "云南大队", "上海大队", "苏州大队", "深圳大队", "北京大队", "湖北大队", "湖南大队", "重庆大队", "辽宁大队" };
  22. double[] y = new double[] { 589, 598, 445, 654, 884, 457, 941, 574, 745, 854, 684, 257 };
  23. string[] z = new string[] { "", "", "", "", "", "", "", "", "", "", "", "" };
  24. string[] a = new string[] { "成都大队", "广东大队", "广西大队", "云南大队", "上海大队" };
  25. double[] b = new double[] { 541, 574, 345, 854, 257 };
  26. #region 饼状图
  27. //标题
  28. chart1.Titles.Add("饼图数据分析");
  29. chart1.Titles[0].ForeColor = Color.Blue;
  30. chart1.Titles[0].Font = new Font("微软雅黑", 12f, FontStyle.Regular);
  31. chart1.Titles[0].Alignment = ContentAlignment.TopCenter;
  32. chart1.Titles.Add("合计:25412 宗");
  33. chart1.Titles[1].ForeColor = Color.Blue;
  34. chart1.Titles[1].Font = new Font("微软雅黑", 8f, FontStyle.Regular);
  35. chart1.Titles[1].Alignment = ContentAlignment.TopRight;
  36. //控件背景
  37. chart1.BackColor = Color.Transparent;
  38. //图表区背景
  39. chart1.ChartAreas[0].BackColor = Color.Transparent;
  40. chart1.ChartAreas[0].BorderColor = Color.Transparent;
  41. //X轴标签间距
  42. chart1.ChartAreas[0].AxisX.Interval = 1;
  43. chart1.ChartAreas[0].AxisX.LabelStyle.IsStaggered = true;
  44. chart1.ChartAreas[0].AxisX.LabelStyle.Angle = -45;
  45. chart1.ChartAreas[0].AxisX.TitleFont = new Font("微软雅黑", 14f, FontStyle.Regular);
  46. chart1.ChartAreas[0].AxisX.TitleForeColor = Color.Blue;
  47. //X坐标轴颜色
  48. chart1.ChartAreas[0].AxisX.LineColor = ColorTranslator.FromHtml("#38587a"); ;
  49. chart1.ChartAreas[0].AxisX.LabelStyle.ForeColor = Color.Blue;
  50. chart1.ChartAreas[0].AxisX.LabelStyle.Font = new Font("微软雅黑", 10f, FontStyle.Regular);
  51. //X坐标轴标题
  52. chart1.ChartAreas[0].AxisX.Title = "数量(宗)";
  53. chart1.ChartAreas[0].AxisX.TitleFont = new Font("微软雅黑", 10f, FontStyle.Regular);
  54. chart1.ChartAreas[0].AxisX.TitleForeColor = Color.Blue;
  55. chart1.ChartAreas[0].AxisX.TextOrientation = TextOrientation.Horizontal;
  56. chart1.ChartAreas[0].AxisX.ToolTip = "数量(宗)";
  57. //X轴网络线条
  58. chart1.ChartAreas[0].AxisX.MajorGrid.Enabled = true;
  59. chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = ColorTranslator.FromHtml("#2c4c6d");
  60. //Y坐标轴颜色
  61. chart1.ChartAreas[0].AxisY.LineColor = ColorTranslator.FromHtml("#38587a");
  62. chart1.ChartAreas[0].AxisY.LabelStyle.ForeColor = Color.Blue;
  63. chart1.ChartAreas[0].AxisY.LabelStyle.Font = new Font("微软雅黑", 10f, FontStyle.Regular);
  64. //Y坐标轴标题
  65. chart1.ChartAreas[0].AxisY.Title = "数量(宗)";
  66. chart1.ChartAreas[0].AxisY.TitleFont = new Font("微软雅黑", 10f, FontStyle.Regular);
  67. chart1.ChartAreas[0].AxisY.TitleForeColor = Color.Blue;
  68. chart1.ChartAreas[0].AxisY.TextOrientation = TextOrientation.Rotated270;
  69. chart1.ChartAreas[0].AxisY.ToolTip = "数量(宗)";
  70. //Y轴网格线条
  71. chart1.ChartAreas[0].AxisY.MajorGrid.Enabled = true;
  72. chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = ColorTranslator.FromHtml("#2c4c6d");
  73. chart1.ChartAreas[0].AxisY2.LineColor = Color.Transparent;
  74. //背景渐变
  75. chart1.ChartAreas[0].BackGradientStyle = GradientStyle.None;
  76. //图例样式
  77. Legend legend2 = new Legend("#VALX");
  78. legend2.Title = "图例";
  79. legend2.TitleBackColor = Color.Transparent;
  80. legend2.BackColor = Color.Transparent;
  81. legend2.TitleForeColor = Color.Blue;
  82. legend2.TitleFont = new Font("微软雅黑", 10f, FontStyle.Regular);
  83. legend2.Font = new Font("微软雅黑", 8f, FontStyle.Regular);
  84. legend2.ForeColor = Color.Blue;
  85. chart1.Series[0].XValueType = ChartValueType.String; //设置X轴上的值类型
  86. chart1.Series[0].Label = "#VAL"; //设置显示X Y的值
  87. chart1.Series[0].LabelForeColor = Color.Blue;
  88. chart1.Series[0].ToolTip = "#VALX:#VAL(宗)"; //鼠标移动到对应点显示数值
  89. chart1.Series[0].ChartType = SeriesChartType.Pie; //图类型(折线)
  90. chart1.Series[0].Color = Color.Lime;
  91. chart1.Series[0].LegendText = legend2.Name;
  92. chart1.Series[0].IsValueShownAsLabel = true;
  93. chart1.Series[0].LabelForeColor = Color.Blue;
  94. chart1.Series[0].CustomProperties = "DrawingStyle = Cylinder";
  95. chart1.Series[0].CustomProperties = "PieLabelStyle = Outside";
  96. chart1.Legends.Add(legend2);
  97. chart1.Legends[0].Position.Auto = true;
  98. chart1.Series[0].IsValueShownAsLabel = true;
  99. //是否显示图例
  100. chart1.Series[0].IsVisibleInLegend = true;
  101. chart1.Series[0].ShadowOffset = 0;
  102. //饼图折线
  103. chart1.Series[0]["PieLineColor"] = "Blue";
  104. //绑定数据
  105. chart1.Series[0].Points.DataBindXY(x, y);
  106. chart1.Series[0].Points[0].Color = Color.Blue;
  107. //绑定颜色
  108. chart1.Series[0].Palette = ChartColorPalette.BrightPastel;
  109. #endregion
  110. }
  111. }
  112. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/270908
推荐阅读
相关标签
  

闽ICP备14008679号