我正在嘗試創建一組圖表系列,將數據添加到系列中,然後通過顯示系列中的一些來顯示系列。數據發生變化,用戶可以選擇要查看的系列。我認爲這種方法會比用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;
}
謝謝TaW - 在你的幫助下,我得到了工作的代碼! – Zeus