Draw a chart from a datatable

AMER SAID 396 Reputation points
2022-10-23T17:32:21.317+00:00

hi

Use the charts inside Visual Studio.
database ms access .

My problem now is if the years are not specified and repeated inside the years column.
And the sales column for each year gives more than one value within the year because the calculation is based on certain periods, months, or extended periods, and not on the basis of the year as a whole.

253315-database.png

What I want is to create chart and dynamic Series,StackedColumn for the years that will not be repeated, as the years 2010-2012-2013
The addition of columns is appropriate for all sales within one year, where 2010 there are two sales, so they are visible in the column for the year 2010
. 2012 There are four sales that are displayed within the same column in a dynamic manner according to different sales

I designed this chart and it worked for me.

database
253352-webexplain1.png

chart

253328-webexplain.png

code

  OleDbConnection conn = new OleDbConnection(ConfigurationManager.ConnectionStrings["Test"].ToString());  
            conn.Open();  
            string query = "SELECT * FROM mobile_tb ";  
            OleDbCommand cmd = new OleDbCommand(query, conn);  
  
            DataTable dt = new DataTable();  
            dt.Load(cmd.ExecuteReader());  
            conn.Close();  
  
  
            if (dt != null && dt.Rows.Count > 0)  
            {  
  
                for (int i = 0; i < dt.Rows.Count; i++)  
                {  
                   
                    chart1.Series["Apple"].Points.Add(new DataPoint(i, dt.Rows[i]["Apple"].ToString().Trim()));  
                    chart1.Series["Nokia"].Points.Add(new DataPoint(i, dt.Rows[i]["Nokia"].ToString().Trim()));  
                    chart1.Series["Samsung"].Points.Add(new DataPoint(i, dt.Rows[i]["Samsung"].ToString().Trim()));  
                    chart1.Series[0].Points[i].AxisLabel = dt.Rows[i]["year"].ToString().Trim();  
                    
                }  
  
            }  

my

Developer technologies C#
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2022-10-25T08:03:23.503+00:00

    @AMER SAID ,

    Based on your description, you want to pivot the table and show it on charts, you could Create PivotTable or PivotChart views in an Access desktop database

    Here is the related sql code snippet.

    TRANSFORM sum(mobile_tb.sales) AS sales  
    SELECT mobile_tb.year  
    FROM mobile_tb  
    GROUP BY mobile_tb.year  
    PIVOT mobile_tb.brand;  
    

    For more information, please refer to: Pivot Query in MS Access

    Original table

    253806-image.png

    Pivoted

    253807-image.png

    Best regards,
    Zhanglong

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.