Reshma
Reshma

Reputation: 1430

How to bind asp.net chart?

I want to bind asp.net bar chart control with some values. Given below code contains some variables with values. I want show those values in chart as given below:

Code:

protected void btnSubmit_Click(object sender, EventArgs e)
{
    int cnt = Convert.ToInt32(txtCount.Text);
    DateTime start = Convert.ToDateTime(txtStart.Text);
    DateTime end = Convert.ToDateTime(txtEnd.Text);
    var date1 = start.ToShortDateString();
    var date2 = end.ToShortDateString();

    TimeSpan datedifference = end.Subtract(start);
    int dateCount = datedifference.Days;
    float maxUpload = dateCount * 288;  
    float remainingUpload = maxUpload - cnt;
    float averageUpload = remainingUpload / (dateCount * 288) * 100;
    float missdUpload = 100 - averageUpload;
    string siteid = ddlSiteID.SelectedValue;

    lblTotalDays.Text = Convert.ToString(dateCount);
    lblTotaldays2.Text = Convert.ToString(dateCount);
    lblTotaldays3.Text = Convert.ToString(dateCount);
    lblTotaldays4.Text = Convert.ToString(dateCount);
    lblMaximum.Text = Convert.ToString(maxUpload);
    lblUploaded.Text = Convert.ToString(remainingUpload);
    lblMissed.Text = Convert.ToString(cnt);
    lblPercentage.Text = Convert.ToString(averageUpload);
    lblSiteid.Text = Convert.ToString(ddlSiteID.SelectedValue);
    lblMissdPer.Text = Convert.ToString(missdUpload);
    lblStart.Text = Convert.ToString(date1);
    lblEnd.Text = Convert.ToString(date2);

    chartBind();
}

private void chartBind()
{
    Series ser = Chart2.Series["Series1"];
    ser.Points.AddXY(lblTotalDays.Text, lblPercentage.Text);
}

Aspx:

enter image description here

Current Out Put :

enter image description here

Expected Chart :

enter image description here

Now I am getting uploaded percentage in my chart. I need show the missed percentage also. Help me to find a proper solution.

Upvotes: 0

Views: 518

Answers (1)

nickson104
nickson104

Reputation: 580

Your problem is that you are only generating one series point:

private void chartBind()
{
    Series ser = Chart2.Series["Series1"];
    ser.Points.AddXY(lblTotalDays.Text, lblPercentage.Text);
}

You need to add a new point for each point you want to plot, there are many approaches to this however.

My personal approach is to generate the series by plotting points:

First I create the point class:

    public class ChartPoint
    {
        public Double PointValue { get; set; }
        public String AxisXText { get; set; }
    }

Which can then be instantiated as a list:

    public static List<ChartPoint> MethodName()
    {
        List<ChartPoint> points = new List<ChartPoint>();

          //Get Your Measured Variables, in a list so you can loop through

          points.Add(new ChartPoint
          {
               PointValue = measured variable,
               AxisXText = "Text you want to display"
          });

       return points;
   }            

This list of points can then be bound to a chart as a datasource; i.e.

chrtWeeklyAverage.DataSource = ChartHelper.RenderWeeklyAverageChart(myclass, start, end);

In your case its probably better to do the points.add step twice and just hard-code the values in: point value being uploaded %, Axis text being 'uploaded/missed'.

This is only one way to approach the issue, there are likely far better solutions.

Upvotes: 2

Related Questions