簡體   English   中英

C#創建自定義圖表類

[英]C# Creating Custom Chart Class

自從我做了這樣的事情已經有一段時間了,但是我試圖創建一個從DataVisualization.Chart類派生的自定義圖表類,我有以下內容

public class clsCustomChart:System.Windows.Forms.DataVisualization.Charting.Chart
{

    public clsCustomChart(string strChartTitle, double[] dblX, double[] dblY)
    {
        //  Create the chart

        //  Create the chart
        Chart chartReturn = new Chart();
        chartReturn.BackColor = Color.FromArgb(50, Color.DarkGray);
        chartReturn.BorderlineDashStyle = ChartDashStyle.Solid;
        chartReturn.BorderlineColor = Color.Black;
        chartReturn.Width = 300;
        chartReturn.Height = 300;

        //  Create the legend
        Legend l = new Legend("Legend");
        l.Docking = Docking.Bottom;
        l.BackColor = Color.Transparent;
        chartReturn.Legends.Add(l);

        //  Create the chart area
        ChartArea a = new ChartArea("ChartArea1");
        a.Area3DStyle.Enable3D = false;
        a.Area3DStyle.WallWidth = 0;
        a.BackColor = Color.FromArgb(100, Color.Black);

        chartReturn.ChartAreas.Add(a);

        //  Create the axis
        a.AxisX.LineColor = Color.Silver;
        a.AxisX.MajorGrid.Enabled = true;
        a.AxisX.MinorGrid.Enabled = false;
        a.AxisX.MajorGrid.LineColor = Color.FromArgb(50, Color.Black);
        a.AxisX.LabelStyle.Font = new System.Drawing.Font("Arial", 8F);

        a.AxisY.LineColor = Color.Silver;
        a.AxisY.MajorGrid.Enabled = true;
        a.AxisY.MinorGrid.Enabled = false;
        a.AxisY.MajorGrid.LineColor = Color.FromArgb(50, Color.Black);
        a.AxisY.LabelStyle.Font = new System.Drawing.Font("Arial", 8F);

        //  Chart title
        chartReturn.Titles.Add(new Title(strChartTitle));

        //  Add the data
        //  Create the data series
        Series s = new Series("IN");
        s.ChartType = SeriesChartType.Line;

        dblX.ToList<double>().ForEach(x => { s.Points.Add(x); });
        s.Color = Color.FromArgb(200, Color.Red);
        s.BorderWidth = 3;

        Series s2 = new Series("OUT");
        s2.ChartType = SeriesChartType.Line;

        dblY.ToList<double>().ForEach(x => { s2.Points.Add(x); });
        s2.Color = Color.FromArgb(200, Color.Green);
        s2.BorderWidth = 3;

        chartReturn.Series.Add(s);
        chartReturn.Series.Add(s2);

        chartReturn.SaveImage("c:/test/" + DateTime.Now.ToString("ddMMyyyy_HHmmss") + ".jpeg", ChartImageFormat.Jpeg);

    }

}

作為圖表對象創建時,自定義圖表中的代碼都經過測試並且可以正常工作,而自定義類將圖表另存為圖像。

但是,當我嘗試以一種形式

Chart C = (Chart)new clsCustomChart("TEST",x,y);

this.Controls.Add(C);

我沒有圖表...任何人都可以建議.....

TIA

//  Create the chart
Chart chartReturn = new Chart(); 

這將創建一個圖表,然后對其進行樣式設置並丟棄。

將其刪除並替換chartReturnthis

另外,如果您想通過設計器將其放置在窗體上,則可能需要提供無參數的構造函數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM