Search code examples
c#chartsmschartseries

change the position of X axis Labels with 2 series


I have chart with column type (2 series) :
- one series for Male.
- one series for Female.
everything is good but I'm trying to set X1,X2 Labels on bottom (on X axis) :

enter image description here

I want to put [Units #3333 and Units #0099] in bottom with [Units #1111 and Units #555]

Is it Possible ?

Here's the Code (cs file):

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.DataVisualization.Charting;

namespace PCMD.AssignTransferSystem.web.Managers
{
    public partial class ColumnChart : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Chart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = true;
            Chart1.ChartAreas["ChartArea1"].Area3DStyle.IsClustered = true;
            Chart1.ChartAreas["ChartArea1"].AxisX.IsLabelAutoFit = false;
            Chart1.ChartAreas["ChartArea1"].Area3DStyle.PointDepth = 20;
            Chart1.ChartAreas["ChartArea1"].Area3DStyle.Perspective = 0;
            SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=AssignTransferDB;Integrated Security=True");
            conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;


        string cmdstr = "select * from ReqUnitsByGender"; // View in DB
        cmd.CommandText = cmdstr;
        SqlDataReader Dr = cmd.ExecuteReader();

        Chart1.ChartAreas["ChartArea1"].AxisX.Interval = 1;
        Chart1.ChartAreas["ChartArea1"].AxisX2.Interval = 1;
        //Chart1.ChartAreas["ChartArea1"].AxisX.LabelStyle.Font = new Font("Arial", 16);
        //Chart1.ChartAreas["ChartArea1"].AxisX2.LabelStyle.Font = new Font("Arial", 16);
        //Chart1.Series["male"].IsValueShownAsLabel = true;
        //Chart1.Series["female"].IsValueShownAsLabel = true;
        //Chart1.Series["male"]["LabelStyle"] = "Bottom";
        //Chart1.Series["female"]["LabelStyle"] = "Bottom";
        //Chart1.Series["male"]["BarLabelStyle"] = "Bottom";
        //Chart1.Series["female"]["BarLabelStyle"] = "Bottom";
        while (Dr.Read())
        {
            if (Dr["Gender"].ToString()=="True")
            {
                Chart1.Series["male"].Points.AddXY(Dr["UnitName"].ToString(), Dr["CountRequest"].ToString());
                Chart1.Series["male"].XAxisType = AxisType.Primary;
            }
            if (Dr["Gender"].ToString() == "False")
            {
                Chart1.Series["female"].Points.AddXY(Dr["UnitName"].ToString(), Dr["CountRequest"].ToString());
                Chart1.Series["female"].XAxisType = AxisType.Secondary;            
            }  
        }
        conn.Close();
    }
}
}

Solution

  • You're using the secondary x-axis for your "female" series. Change it to

    Chart1.Series["female"].XAxisType = AxisType.Primary;
    

    and you should be all set. If the two series don't have the same x-values, however, the bars will likely have space between them.