我试图用列的名称标记我的饼图(在这种情况下是机器名称,以及旁边的百分比。这很难C#中的默认值。我正在从MySQL数据库中获取数据并根据列"机器"绘制它。下面是我当前拥有的代码,它仅显示百分比:
string DatabaseCountMachines = "SELECT Machine, COUNT(*) total FROM completed GROUP BY Machine";
MySqlConnection conDataBase = new MySqlConnection(constring);
MySqlCommand cmdDataBase = new MySqlCommand(DatabaseCountMachines, conDataBase);
MySqlDataReader StatsbyMachine;
try
{
conDataBase.Open();
StatsbyMachine = cmdDataBase.ExecuteReader();
while (StatsbyMachine.Read())
{
this.chartMachine.Series["Series1"].Points.AddXY(StatsbyMachine.GetString("Machine"), StatsbyMachine.GetString("total"));
this.chartMachine.Series["Series1"].CustomProperties = "PieLabelStyle=Outside";
this.chartMachine.Series["Series1"].Label = "#LEGENDTEXT" + "#PERCENT";
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
在饼图中 x-values
请勿应用,因此它们不显示...您不妨使用AddY
。
只需将两个数据添加到每个DataPoint
的Label
!
更改
chartMachine.Series["Series1"].Points
.AddXY(StatsbyMachine.GetString("Machine"), StatsbyMachine.GetString("total"));
to
int index = chartMachine.Series["Series1"].Points.AddY(StatsbyMachine.GetString("total"));
chartMachine.Series["Series1"].Points[index].Label =
StatsbyMachine.GetString("Machine") + " : " + StatsbyMachine.GetString("total");
请注意,尽管您添加的字符串是y值会转换为数字,即使您使用使用X值的图表类型,也不适用于X值。
某些图表数据不需要数字X值,例如名称,城市甚至ID,因此即使在可能的位置也不会自动转换。
y-values otoh始终需要数字,否则图表的整个想法毫无意义。因此,如果可能的话,您的string total
将转换为数字。转换失败的地方,值将为0
..