locked
cusomize x series in Chart RRS feed

  • Question

  • User-1812955944 posted

    Hi

    I wrote this code for customize X series of Chart by date:

    string connStr = @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source = D:\UserData\UserData.accdb";
                string query = "select weightNow, dateNow from WeightControl WHERE IDCo = '" + txtIdCo.Text + "' ";
                using (OleDbConnection conn = new OleDbConnection(connStr))
                {
                    using (OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn))
                    {
    
                        DateTime minDate = DateTime.Parse(txtFromDate.Text.ToString());
                        DateTime maxDate = DateTime.Parse(txtTodate.Text.ToString());
                        chart1.ChartAreas["ChartArea1"].AxisX.Minimum = minDate.ToOADate();
                        chart1.ChartAreas["ChartArea1"].AxisX.Maximum = maxDate.ToOADate();
                        chart1.Series["Lose Weight"].YValueMembers = "weightNow";
                        chart1.DataBind();
    
                    }

    Chart have customized by date and the dates have shown in X vector ....

    but the curve of chart did not show. Can anyone help to solve this problem?

    Friday, August 12, 2016 4:11 PM

All replies

  • User283571144 posted

    Hi Mr.Torbati,

    Chart have customized by date and the dates have shown in X vector ....

    but the curve of chart did not show. Can anyone help to solve this problem?

    According to your description and codes, I find you don't set chart1's DataSource and  Chart1.Series.XValueMember property.

    So chart did not show now.

    I suggest you could use 'OleDbDataAdapter.Fill'' method to fill the data into a datatable, then set chart1's DataSource property to 'datatable'.

    At last set Chart1.Series.XValueMember property to the right 'datatable' column.

    More details, you could refer to follow codes:

    Aspx:

        <form id="form1" runat="server">
        <div>
            <asp:Chart ID="Chart1" runat="server" Width="836px">
                <Series>
                    <asp:Series Name="Series1"></asp:Series>
                </Series>
                <ChartAreas>
                    <asp:ChartArea Name="ChartArea1"></asp:ChartArea>
                </ChartAreas>
            </asp:Chart>
        </div>
        </form>

    Code-behind:

         protected void Page_Load(object sender, EventArgs e)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("weightNow", typeof(Int32));
                dt.Columns.Add("dateNow", typeof(DateTime));
                DateTime departure = new DateTime(2016, 5, 12);
                DateTime arrival = new DateTime(2016, 6, 13);
                DateTime arrival2 = new DateTime(2016, 7, 14);
                DateTime departure1 = new DateTime(2016, 4, 12);
                DateTime arriva1l = new DateTime(2016, 3, 13);
                DateTime arrival22 = new DateTime(2016, 2, 14);
                dt.Rows.Add(30, departure);
                dt.Rows.Add(42, arrival);
                dt.Rows.Add(54, arrival2);
                dt.Rows.Add(74, departure1);
                dt.Rows.Add(24, arriva1l);
                dt.Rows.Add(64, arrival22);
    
                Chart1.Series["Series1"].ChartType = SeriesChartType.Spline;
                Chart1.Series["Series1"]["DrawingStyle"] = "Emboss";
                Chart1.DataSource = dt;
                Chart1.Series["Series1"].YValueMembers = "weightNow";
                Chart1.Series["Series1"].XValueMember = "dateNow";
                Chart1.Series["Series1"].XValueType = ChartValueType.DateTime;
                //Chart1.ChartAreas["ChartArea1"].AxisX.LabelStyle.Format = "yyyy-MM-dd";
                //Chart1.ChartAreas["ChartArea1"].AxisX.IntervalType = DateTimeIntervalType.Months;
                DateTime minDate = new DateTime(2016, 3, 13).AddSeconds(-1);
                DateTime maxDate = new DateTime(2016, 6, 13); // or DateTime.Now;
                Chart1.ChartAreas["ChartArea1"].AxisX.Minimum = minDate.ToOADate();
                Chart1.ChartAreas["ChartArea1"].AxisX.Maximum = maxDate.ToOADate();
                Chart1.DataBind();
            }

    Best Regards,

    Brando

    Sunday, August 14, 2016 5:43 AM
  • User-1812955944 posted

    thanks

    My chart has bind to DataGridview  as below: 

     string connStr = @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source = D:\UserData\UserData.accdb";
                string query = "select weightNow, dateNow from WeightControl WHERE IDCo = '" + txtIdCo.Text + "' ";
                using (OleDbConnection conn = new OleDbConnection(connStr))
                {
                    using (OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn))
                    {
                        DataSet ds = new DataSet();
    
                        adapter.Fill(ds);
                        dataGridView7.DataSource = ds.Tables[0];
                        chart2.DataSource = dataGridView7.DataSource;
    
    
                        chart2.Series["serires1"].YValueMembers = "weightNow";
                        chart2.Series["series1"].XValueMember = "dateNow";
                        DateTime minDate = DateTime.Parse(txtFromDate.Text.ToString());
                        DateTime maxDate = DateTime.Parse(txtTodate.Text.ToString());
                        chart2.ChartAreas["ChartArea1"].AxisX.Minimum = minDate.ToOADate();
                        chart2.ChartAreas["ChartArea1"].AxisX.Maximum = maxDate.ToOADate();
                        chart2.DataBind();
    
                        
                    }
                }

    But don't show any curve in chart!

    Sunday, August 14, 2016 8:23 AM
  • User283571144 posted

    Hi Mr.Torbati,

    chart2.Series["serires1"].YValueMembers = "weightNow";

    chart2.Series["series1"].XValueMember = "dateNow";

    According to your codes, I find you set two Series, one is 'serires1', the other one is 'series1'.

    But According to your previous codes, your chart Series is "Lose Weight".

    I suggest you could check your codes, and make sure the correct Series you want to show in the chart.

    Best Regards,

    Brando

    Sunday, August 14, 2016 8:59 AM
  • User-1812955944 posted

    Thanks for responding ..........

    In-fact series are "loseWeight" and in my code they are correct. 

    Sunday, August 14, 2016 2:56 PM
  • User283571144 posted

    Hi Mr.Torbati,

    Thanks for responding ..........

    In-fact series are "loseWeight" and in my code they are correct. 

    Could you please post more relevant codes about your chart?

    Is the chart show now or it's type is column not curve?

    Besides, I suggest you could use VS debugger to check your codes make sure the "chart2.DataSource", "minDate", "maxDate" has value.

    Best Regards,

    Brando

    Tuesday, August 16, 2016 1:21 AM
  • User-1812955944 posted

    Hi

    Thanks a lot for tracking this problem. 

    My codes are as blew:

    private void btnCustomChartShow_Click(object sender, EventArgs e)
            {
    
                string connStr = @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source = D:\UserData\UserData.accdb";
                string query = "select weightNow, dateNow from WeightControl WHERE IDCo = '" + txtIdCo.Text + "' ";
                using (OleDbConnection conn = new OleDbConnection(connStr))
                {
                    using (OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn))
                    {
                        DataSet ds = new DataSet();
    
                        adapter.Fill(ds);
                        dataGridView7.DataSource = ds.Tables[0];
                        chart2.DataSource = dataGridView7.DataSource;
    
    
                        chart2.Series["series1"].YValueMembers = "weightNow";
                        chart2.Series["series1"].XValueMember = "dateNow";
                        DateTime minDate = DateTime.Parse(txtFromDate.Text.ToString());
                        DateTime maxDate = DateTime.Parse(txtTodate.Text.ToString());
                        chart2.ChartAreas["ChartArea1"].AxisX.Minimum = minDate.ToOADate();
                        chart2.ChartAreas["ChartArea1"].AxisX.Maximum = maxDate.ToOADate();
                        chart2.DataBind();
    
                        
                    }
                }
            }

    Also VS debugger don't show any problem. When I test the codes,  X series value (date) had shown customized but cure of chart hadn't shown.

    Tuesday, August 16, 2016 3:06 PM
  • User-2057865890 posted

    Hi Mr.Torbati,

    Try

    chart2.Series["series1"].ChartType = SeriesChartType.Spline;

    Best Regards,

    Chris

    Saturday, August 20, 2016 9:27 AM