以下是一些关于饼图的显示代码:
MyChart.Series.Clear();
//饼状图
MyChart.DataSource = list;
string[] xValues = new string[list.Count];//X轴数据,即显示文字
decimal[] yValues = new decimal[list.Count];//Y轴数据,即金额
for (int i = 0; i < list.Count; i++)
{
xValues[i] = list[i].ProductName;
yValues[i] = list[i].productMoney;
}
var series = new Series();
series.ChartType = SeriesChartType.Pie;
series.CustomProperties = "PieDrawingStyle=SoftEdge,PieLabelStyle=Enabled,DoughnutRadius=60";
series["PieLabelStyle"] = "Outside"; //设置文字显示在外
series["PieLineColor"] = "Black";
series.Points.DataBindXY(xValues, yValues);
series.Label = "#VALX:[#PERCENT{p1}]"; //设置图例显示
series.LegendText = "#VALX:#VAL";
MyChart.Series.Add(series);
//标题
var title = new Title();
title.Text = "销售概况";
title.Alignment = ContentAlignment.MiddleCenter;
title.Font = new System.Drawing.Font("Trebuchet MS", 14F, FontStyle.Bold);
MyChart.Titles.Add(title);
//ChartArea1
MyChart.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = true;//3D模式
MyChart.ChartAreas[0].AxisX.Interval = 1;
//背景色
MyChart.Legends["Legends1"].BackColor = Color.FromArgb(235, 235, 235);
//斜線背景
MyChart.Legends["Legends1"].BackHatchStyle = ChartHatchStyle.DarkDownwardDiagonal;
MyChart.Legends["Legends1"].BorderWidth = 1;
MyChart.Legends["Legends1"].BorderColor = Color.FromArgb(200, 200, 200);
//字體設定
//MyChart.Series["Series1"].Font = new Font("Trebuchet MS",10,System.Drawing.FontStyle.Bold);
MyChart.Series["Series1"].Points.FindMaxByValue().LabelForeColor = Color.Red;
MyChart.Series["Series1"].BorderColor = Color.FromArgb(255, 101, 101, 101);
//MyChart.Series["Series1"]["PieDrawingStyle"] = "Default";
//初始化柱子颜色
Color[] colorValues = new Color[] {Color.IndianRed, Color.DarkTurquoise, Color.Red, Color.SlateGray, Color.Gold, Color.Green, Color.Indigo, Color.MediumOrchid};
for (int i = 0; i < MyChart.Series[0].Points.Count; i++)
{
MyChart.Series[0].Points[i].Color = colorValues[i];
}
效果图: