简体   繁体   English

如何在ms-图中显示X轴的两个级别

[英]How to show two level of X-Axis in ms-Chart

I have a report that show some info about cities.I want to show the state name below all cities along with a state. 我有一个报告,其中显示有关城市的信息。我想在所有城市下方显示州名以及州。

such this image: 这样的图像:

在此处输入图片说明

how I can do this similar to image (not Exactly) with ms chart ? 我如何使用ms图表与图像(不是完全一样)进行此操作?

thanks 谢谢

You can add 2 rows of CustomLabels, eg: 您可以添加2行CustomLabel,例如:

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

Result is pretty similar: 结果非常相似:

带有2行标签的图表

In Visual studio RDLC this can be easily achieved by 在Visual Studio RDLC中,可以通过以下方式轻松实现

  1. create a dataset and add a datatable with fields; 创建数据集并添加带有字段的数据表; city,state,value 市,州,值 看图片

  2. create a blank RDLC report and insert a chart of your choice from the tool box selecting your datatable as the datasource 创建一个空白的RDLC报告并从工具框中插入您选择的图表,然后选择您的数据表作为数据源

  3. click on the chart area to get "Chart data" dialog 单击图表区域以获取“图表数据”对话框

  4. For Vlues add "your value", for category groups add "State" and "City" 为Vlues添加“您的值”,为类别组添加“州”和“城市” 看图片

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM