【问题标题】:How to show two level of X-Axis in ms-Chart如何在 ms-Chart 中显示两级 X 轴
【发布时间】:2011-08-23 06:31:58
【问题描述】:

我有一份报告显示有关城市的一些信息。我想在所有城市下方显示州名以及州名。

这样的图片:

我怎样才能做到这一点类似于图像(不完全)与 ms 图表?

谢谢

【问题讨论】:

    标签: c# c#-4.0 mschart


    【解决方案1】:

    您可以添加 2 行 CustomLabels,例如:

    private void FillChart()
    {
        // fill the data table with values
        var dt = new DataTable();
        dt.Columns.Add("Id", typeof(int));
        dt.Columns.Add("City", typeof(string));
        dt.Columns.Add("State", typeof(string));
        dt.Columns.Add("Population", typeof(int));
    
        dt.Rows.Add(0, "City1", "State1", 100);
        dt.Rows.Add(1, "City2", "State1", 30);
        dt.Rows.Add(2, "City3", "State1", 40);
        dt.Rows.Add(3, "City1", "State2", 80);
        dt.Rows.Add(4, "City2", "State2", 70);
    
        // bind the data table to chart
        this.chart1.Series.Clear();
    
        var series = this.chart1.Series.Add("Series 1");
        series.XValueMember = "Id";
        series.YValueMembers = "Population";
    
        series.ChartType = SeriesChartType.Column;
        this.chart1.DataSource = dt;
        this.chart1.DataBind();
    
        // custom labels 
        foreach (var g in dt.AsEnumerable().GroupBy(x => x.Field<string>("State")))
        {
            string state = g.Key;
            var cities = g.Select(r => new { Id = r.Field<int>("Id"), City = r.Field<string>("City") });
            // find min-max
            int min = cities.Min(y => y.Id);
            int max = cities.Max(y => y.Id);
            // city labels
            foreach (var city in cities)
            {
                var label = new CustomLabel(city.Id - 1, city.Id + 1, city.City, 0, LabelMarkStyle.None);
                this.chart1.ChartAreas[0].AxisX.CustomLabels.Add(label);
            }
            // city states
            var statelabel = new CustomLabel(min, max, state, 1, LabelMarkStyle.LineSideMark);
            this.chart1.ChartAreas[0].AxisX.CustomLabels.Add(statelabel);
        }
    }
    

    结果非常相似:

    【讨论】:

      【解决方案2】:

      在 Visual Studio RDLC 中,这可以通过以下方式轻松实现

      1. 创建数据集并添加带有字段的数据表;城市,州,价值

      2. 创建一个空白 RDL​​C 报告并从工具框中插入您选择的图表,选择您的数据表作为数据源

      3. 点击图表区域进入“图表数据”对话框

      4. 对于 Vlues 添加“您的价值”,对于类别组添加“州”和“城市”

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多