【问题标题】:change the position of X axis Labels with 2 series用2个系列改变X轴标签的位置
【发布时间】:2014-10-02 10:07:43
【问题描述】:

我有列类型的图表(2 系列):
- 一个男性系列。
- 一个女性系列。
一切都很好,但我正在尝试在底部(在 X 轴上)设置 X1、X2 标签:

我想将 [Units #3333 和 Units #0099] 与 [Units #1111 和 Units #555] 放在底部

有可能吗?

这是代码(cs文件):

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();
    }
}
}

【问题讨论】:

  • 发布您如何设置图表的代码。
  • 你可以在答案部分看到我的代码@mmathis
  • 谢谢。但是,您的代码不是答案,因此请编辑您的问题并将代码发布到那里,然后删除包含您的代码的答案。

标签: c# charts mschart series


【解决方案1】:

您正在为“女性”系列使用辅助 x 轴。改成

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

你应该准备好了。但是,如果这两个系列的 x 值不同,则条形之间可能会有空间。

【讨论】:

  • 我把一个系列[] 主要的另一个是次要的 例如(如果我把两个系列都放在主要的):系列[“女性"] 覆盖第一个标签
  • 我不太确定你在描述什么......你能发布一个你希望情节看起来像什么的模型吗?您可以尝试弄乱辅助 x 轴的 AxisCrossing 属性,但是您可能会得到重叠的标签。一般来说,主要的 x 轴位于绘图的底部,次要位于顶部 - 这些也是相关标签所在的位置。如果你想要底部的标签,你应该使用主轴。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-12
  • 2012-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-04
相关资源
最近更新 更多