【发布时间】:2017-02-22 18:51:37
【问题描述】:
我正在尝试创建一组图表系列,将数据添加到系列中,然后通过使它们可见来显示一些系列。数据发生变化,用户可以选择查看哪个系列。我认为这种方法会比使用chart.Series.Clear(); 清除所有系列,然后以相同的方法重新创建系列要好。
例如,拼车中具有随机里程的汽车列表,然后选择要显示的汽车。
下面的代码不起作用(我已经评论了哪里)。该系列不是公开的,我认为需要将它们添加到像 SeriesCollection 这样的公共收藏中,但我不确定如何。
感谢您的帮助。
// create new chart series and add to a chartarea
ChartArea TestChartArea = new ChartArea();
public void CreateChartSeries()
{
List<string> lstCars = new List<string> { "Mazda", "Tesla", "Honda", "Jaguar", "Ford", "Toyota" };
foreach (string Car in lstCars)
{
// car series created correctly?
var Srs = new Series(Car);
Srs.ChartArea = TestChart.Name;
Srs.YAxisType = AxisType.Primary;
Srs.Color = Color.Red;
Srs.ChartType = SeriesChartType.Line;
TestChart.Series.Add(Srs);
}
}
// add data to chart series
public void SeriesData()
{
List<string> lstCars = new List<string> { "Mazda", "Tesla", "Honda", "Jaguar", "Ford", "Toyota" };
int[] Xseries = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int[] Milage = new int[10];
Random Random = new Random();
foreach (string Car in lstCars)
{
for (int M = 0; M < 10; M++)
Milage[M] = Random.Next(150, 15000);
// not sure how to call and add data to each series
Srs.Points.DataBindXY(Xseries, Milage);
}
}
// plot series - some visible
public void PlotCreatedSeries()
{
// not sure how to refer to each series
Mazda.Enabled = true;
Tesla.Enabled = false;
Honda.Enabled = true;
Jaguar.Enabled = false;
Ford.Enabled = true;
Toyota.Enabled = false;
}
【问题讨论】: