动态创建图表 [英] Dynamically creating charts
问题描述
我正在尝试在表单内为计算机中的每个驱动器动态创建图表。
I am trying to dynamically create a chart for each drive in the computer, inside a form.
每个图表应为饼图,其中包含以GB为单位的可用空间(绿色)和已用空间(红色)的数量。
Each chart should be a pie chart that contains the amount of free space (colored green) and used space(colored red) in GBs.
但是当我运行以下代码时,我唯一看到的是标题为 C:\, D:\等的空白矩形。
But when I run the following code the only thing I see is blank rectangles with the titles of "C:\", "D:\" and so on.
这是代码:
public static void DrawCharts()
{
Chart[] charts = new Chart[DriveInfo.GetDrives().Length];
DriveInfo[] drives = DriveInfo.GetDrives();
for (int i = 0; i < drives.Length; i++)
{
charts[i] = new Chart();
charts[i].Palette = ChartColorPalette.BrightPastel;
charts[i].Titles.Add(drives[i].Name);
charts[i].Series.Add("Storage");
charts[i].Series[0].ChartType = SeriesChartType.Pie;
charts[i].Location = new System.Drawing.Point(20 + i * 231, 30);
charts[i].Size = new System.Drawing.Size(230, 300);
DataPoint d = new DataPoint();
d.XValue = 1;
double[] p = { (double)drives[i].TotalFreeSpace / 1000000000 };
d.YValues = p;
d.Color = System.Drawing.Color.YellowGreen;
d.Label = "Free Space";
charts[i].Series[0].Points.Add(d);
d.Label = "Used Space";
d.XValue = 2;
double[] a = { (double)((drives[i].TotalSize - drives[i].TotalFreeSpace) / 1000000000) };
d.YValues = a;
d.Color = System.Drawing.Color.Red;
charts[i].Series[0].Points.Add(d);
Form1.tabs.TabPages[1].Controls.Add(charts[i]);
charts[i].Invalidate();
}
}
谢谢。
推荐答案
您快到了。
但是您需要添加到动态创建的图表中的最基本的东西..:
But the most basic thing you need to add to a dynamically created chart..:
charts[i] = new Chart();
..是 ChartArea
:
charts[i].ChartAreas.Add("CA1"); // pick your name!
没有它,则无法显示 Series
。
Without it no Series
can display..
使用它为 TickMarks
, GridLines
或 Label
或设置 Minima
和 Maxima
和时间间隔
。好吧,至少对于大多数其他 ChartTypes
而言; Pies
完全不需要。.
Use it to style the axis with TickMarks
, GridLines
or Labels
or to set Minima
and Maxima
and Intervals
. Well, at least for most other ChartTypes
; Pies
don't need any of this anyway..
请注意,您可以拥有多个 ChartAreas
中的一个 Chart
。
Note that you can have several ChartAreas
in one Chart
.
还要注意,直到至少一个 Series
至少有一个 DataPoint
..
Also note that it still will display nothing until at least one Series
has at least one DataPoint
..
这篇关于动态创建图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!