Windows窗体DataVisualization图表重叠系列?

时间:2016-03-02 16:27:39

标签: c# winforms charts data-visualization

我为Windows窗体项目创建了一个图表,并尝试使用一个图表来跟踪两个系列 - 基本上是收集的值和预算值。我希望的目标是让两个系列在外观上相互重叠 - 所以如果收集的数量超过了预算值,你甚至不会看到代表该特定值的预算的条形图。不幸的是,我似乎无法做到这一点。如果我将图表类型设置为stackbar,则添加的第二个系列似乎是偏移的,所以无论两个数字的差异有多大,我仍然会看到两个条形,如果我将条形类型设置为条形,那么两条条只是并排出现。

有没有办法实现我的目标?我是否以正确的方式思考这个问题?我已经搜索了关于图表的教程,但是我偶然发现的那些是非常基础的。提前感谢您提供的任何帮助或指导。

创建图表的相关代码:

        string receiptQry = "<sql query text>";

        DataTable dt = new DataTable();
        SqlConnectionStringBuilder csb = new SqlConnectionStringBuilder();
        csb.DataSource = "mysqlserver";
        csb.InitialCatalog = "mydatabase";
        csb.IntegratedSecurity = true;
        string connString = csb.ToString();
        dt.Columns.Add("AttyCode", typeof(string));
        dt.Columns.Add("Collected Receipts", typeof(string));
        dt.Columns.Add("Budgeted Receipts", typeof(string));
        dt.PrimaryKey = new DataColumn[] { dt.Columns["AttyCode"] };

        List<string> attyList = new List<string>();
        List<string> rcptList = new List<string>();
        using (SqlConnection connection = new SqlConnection(connString))
        {
            using (SqlCommand command = connection.CreateCommand())
            {
                command.CommandText = receiptQry;

                connection.Open();

                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        attyList.Add(reader["REF_CODE"].ToString());
                        rcptList.Add(reader["Collected Receipts"].ToString());
                    }
                }
                connection.Close();
            }
        }

        int i = 0;
        foreach (string s in attyList)
        {
            if (rcptList[i].ToString() != "0.00")
            {
                string budgRecp = get_year_budget_rcpts(s.Trim()).ToString();
                AddRow3(dt, s, rcptList[i].ToString(), budgRecp);
            }
            i++;
        }

        chart1.DataSource = dt;

        chart1.Series.Add("Budgeted");
        chart1.Series.Add("Receipts");

        chart1.Series["Receipts"].XValueMember = "AttyCode";
        chart1.Series["Budgeted"].XValueMember = "AttyCode";
        chart1.Series["Receipts"].YValueMembers = "Collected Receipts";
        chart1.Series["Budgeted"].YValueMembers = "Budgeted Receipts";
        chart1.Series["Budgeted"].ChartType = SeriesChartType.StackedBar;
        chart1.Series["Budgeted"].IsValueShownAsLabel = true;
        chart1.Series["Budgeted"].IsXValueIndexed = true;
        chart1.Series["Budgeted"].YValuesPerPoint = 1;
        chart1.Series["Receipts"].ChartType = SeriesChartType.StackedBar;
        chart1.Series["Receipts"].IsValueShownAsLabel = true;
        chart1.Series["Receipts"].IsXValueIndexed = true;
        chart1.Series["Receipts"].YValuesPerPoint = 1;
        chart1.Series["Budgeted"].SmartLabelStyle.Enabled = false;
        chart1.Dock = DockStyle.Fill;
        chart1.DataBind();

        chart1.ChartAreas[0].AxisX.Interval = 1;
        chart1.ChartAreas[0].AxisX.Title = "Attorney Code";
        chart1.ChartAreas[0].AxisY.Title = "Collected Receipts [YTD]";
        chart1.Series["Receipts"].SmartLabelStyle.Enabled = false;

        chart1.Series["Receipts"].Color = Color.Green;
        chart1.Series["Budgeted"].Color = Color.Red;
        chart1.Series["Receipts"].BackHatchStyle = ChartHatchStyle.None;
        chart1.ChartAreas[0].AxisX.MajorGrid.LineWidth = 0;
        chart1.ChartAreas[0].AxisY.MajorGrid.LineWidth = 0;
        chart1.ChartAreas[0].Area3DStyle.Enable3D = true;
        chart1.Dock = DockStyle.Fill;
        chart1.ChartAreas[0].Area3DStyle.WallWidth = 20;

0 个答案:

没有答案