locked
How to insert a lineChart in a C# Project RRS feed

  • Question

  • Hello, Could someone help me with how to insert a line Chart in a C# Project when  the project is  running the project leave the results in an internal  table(matrix n,m )  and I want to show any rows and columns how to Line Chart

    Best Regards.

    Luis E. Guzman


    leg2014

    • Changed type KareninstructorMVP Wednesday, April 8, 2020 2:54 PM This is more inline as a question
    Wednesday, April 8, 2020 2:51 PM

All replies

  • Hi leg2014,

    Please try do this just populate the data table to your data

    LineChartDataTable.Rows.Add("Sun", 43);

    using System.Windows.Forms.DataVisualization.Charting;
    using System.Data;
    using System.Drawing;

    private void button1_Click(object sender, EventArgs e)
            {
                DataTable LineChartDataTable = new DataTable();
                LineChartDataTable.Columns.AddRange(new DataColumn[] { new DataColumn("Week"),new DataColumn("Day",typeof(int))});
    
    
    
                LineChartDataTable.Rows.Add("Sun", 43);
                LineChartDataTable.Rows.Add("Mon", 37);
                LineChartDataTable.Rows.Add("Tues", 53);
                LineChartDataTable.Rows.Add("Wed", 50);
                LineChartDataTable.Rows.Add("Thurs", 57);
                LineChartDataTable.Rows.Add("Fri", 59);
                LineChartDataTable.Rows.Add("Sat", 24);
    
    
                chart1.Titles.Add("Temperatures In NY City");
                chart1.DataSource = LineChartDataTable;
                chart1.ChartAreas[0].AxisX.Interval = 1;
                chart1.Series.Clear();
    
                
    
    
                for (int i = 1; i < LineChartDataTable.Columns.Count; i++)
                {
                    Series seriesValues = new Series();
    
                    seriesValues.XValueMember = LineChartDataTable.Columns[0].ColumnName;
    
                    seriesValues.YValueMembers = LineChartDataTable.Columns[i].ColumnName;
    
    
    
                    //type of the chart
                    seriesValues.ChartType = SeriesChartType.Line;
    
    
    
                    seriesValues.IsVisibleInLegend = true;
                    seriesValues.IsValueShownAsLabel = true;
                    seriesValues.BorderWidth = 4;
                    seriesValues.Color = Color.Red;
                    seriesValues.LabelForeColor = Color.White;
                    seriesValues.LabelBackColor = Color.Green;
                    seriesValues.LegendText = LineChartDataTable.Columns[i].ColumnName;
                    chart1.Series.Add(seriesValues);
                }
            }

    The result


    Please remember to mark the replies as answers if they helped you :) ~

    Wednesday, April 8, 2020 3:59 PM
  • Hi leg2014,

    Thank you for posting here.

    This is the simplest example of using a line chart, it looks relatively simple.

            private void Form1_Load(object sender, EventArgs e)
            {
                var series = new Series();
    
                series.Points.DataBindXY(new[] { 2001, 2002, 2003, 2004 }, new[] { 100, 200, 90, 150 });
                chart1.Series.Add(series);
                series.ChartType = SeriesChartType.Line;
    
            }

    For more information about line charts, please see the documentation.

    Chart Class

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 9, 2020 5:27 AM
  • Hi,

    Has your issue been resolved?

    If so, please click "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 5, 2020 9:08 AM