简体   繁体   English

如何将一个图表分配给另一C#

[英]How to assign one chart to another C#

I'm attempting to open a chart in another window form, however the classes used for the data in the chart is in the first form. 我试图以其他窗口形式打开图表,但是用于图表中数据的类位于第一种形式。 My goal here is to have a chart be able to open many times in a modeless window. 我的目标是使图表能够在无模式窗口中多次打开。

in form1.cs I build my chart: 在form1.cs中,我建立了图表:

Chart chart = new Chart();
Series price = new Series("Price"); //create new series
chart.Series.Add(price);

chart.Series["Price"].ChartType = SeriesChartType.Candlestick;

chart.Series["Price"]["OpenCloseStyle"] = "Candlestick";

chart.Series["Price"]["ShowOpenClose"] = "Both";


chart.Series["Price"]["PriceUpColor"] = "Green"; //Price increase = green
chart.Series["Price"]["PriceDownColor"] = "red"; //price decrease = red

for (int i = 0; i < data.Count; i++)
{
    chart.Series["Price"].Points.AddXY(data[i].getDate(), data[i].getHigh()); //Adds date and high value
    chart.Series["Price"].Points[i].YValues[1] = System.Convert.ToDouble(data[i].getLow()); //Low value added to chart
    chart.Series["Price"].Points[i].YValues[2] = System.Convert.ToDouble(data[i].getOpen()); //open value added to chart
    chart.Series["Price"].Points[i].YValues[3] = System.Convert.ToDouble(data[i].getClose()); //close value added to chart
}

Form2.cs: Form2.cs:

public void DisplayChart(Chart newChart)
{
   chart1 = newChart;
   chart1.Show();
}

It is best to make the Chart is a separate class and call or create it from whichever form you need. 最好使Chart是一个单独的类,然后从所需的任何形式调用或创建它。 This follows object-oriented principles so you can reuse code and also use it for multiple purposes/views. 这遵循面向对象的原则,因此您可以重用代码并将其用于多个目的/视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM