0

I've got a function to draw a line graph as shown below. The data points are obtained from a MySQL table. However, how can I number each data point in the x axis 1,2,3,4...?

In the example below, the query returns two results and the graph displays the two points so the graph should have 1 and 2 marked on the axis.

EDIT: Initial problem solved. However, for the example above, there are 2 data points but the max x value is 3. Is there a way of setting the maximum x value to equal the number of data points?

line graph

protected void chart(int moduleID)
{
    string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
    MySqlConnection conn = new MySqlConnection(connStr);

    string comm = "SELECT * FROM scores WHERE test_id=0 AND module_id=@ModuleID AND user_id=@UserID";
    MySqlCommand mySqlCommand = new MySqlCommand(comm, conn);
    mySqlCommand.Parameters.Add(new MySqlParameter("@ModuleID", moduleID));
    mySqlCommand.Parameters.Add(new MySqlParameter("@UserID", Session["UserID"]));

    MySqlDataAdapter dataAdapter = new MySqlDataAdapter(mySqlCommand);
    DataTable ds = new DataTable();

    Chart1.ChartAreas["ChartArea1"].AxisX.MajorGrid.Enabled = false;
    Chart1.ChartAreas["ChartArea1"].AxisY.MajorGrid.Enabled = false;
    Chart1.ChartAreas["ChartArea1"].AxisX.Minimum = 1;
    Chart1.ChartAreas["ChartArea1"].AxisX.LabelStyle.Enabled = false;
    Chart1.ChartAreas["ChartArea1"].AxisX.Title = "attempt no.";
    Chart1.ChartAreas["ChartArea1"].AxisY.Minimum = 0;
    Chart1.ChartAreas["ChartArea1"].AxisY.Maximum = 100;
    Chart1.ChartAreas["ChartArea1"].AxisY.Title = "%";
    Chart1.ChartAreas["ChartArea1"].AxisY.TextOrientation = TextOrientation.Horizontal;

    try
    {
        conn.Open();
        dataAdapter.Fill(ds);

        if (ds.Rows.Count > 0)
        {
            Chart1.DataSource = ds;
            Chart1.Series["Series1"].YValueMembers = "score";
            Chart1.DataBind();
        }
        else
        {
            Chart1.Visible = false;
            lblError2.Text = "No results found.";
        }
    }
    catch
    {
        lblError.Text = "Database connection error. Unable to obtain data at the moment.";
    }
    finally
    {
        conn.Close();
    }
}

1 Answer 1

1

There is problem with:

Chart1.ChartAreas["ChartArea1"].AxisX.LabelStyle.Enabled = false;

You have disbled AxisX labels

http://msdn.microsoft.com/en-us/library/system.windows.forms.datavisualization.charting.labelstyle.enabled%28v=vs.110%29.aspx

Sign up to request clarification or add additional context in comments.

2 Comments

Ah great, thanks. For the example above, there are 2 data points but the max x value is 3. Is there a way of setting the maximum x value to equal the number of data points?
If you set AxisX.Maximum = Double.NaN; it will be auto-scale. Also you can set it to a specific value. To find your max value you can use AxisX.Maximum = Chart1.Series["Series1"].Points.FindMaxByValue("X").XValue;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.