.NET条形图未正确绘制

时间:2013-03-20 18:45:39

标签: c# .net sharepoint-2010

当我绘制条形图时,有些项目没有在X轴上正确绘制,而等效的Y轴是正常的。

我的用户控件有以下代码,

<asp:Chart ID="Chart1" runat="server" Width="850px" Height="600px">
    <Series>
        <asp:series Name="Series1"></asp:series>
    </Series>
    <ChartAreas>
        <asp:ChartArea Name="ChartArea1">
        </asp:ChartArea>
    </ChartAreas>
</asp:Chart>

填充数据表的代码,

protected DataTable GetGroupExperienceData(PickerEntity pickerEntity)
{
    DataTable table;
    table = new DataTable();

    table.Columns.Add("Participant", typeof(string));
    table.Columns.Add("Experience", typeof(Int32));
    table.Columns.Add("Question", typeof(string));

    DataRow row;                    

    if (collListItems2.Count > 0 && collListItems2 != null)
    {
        for (int i = 0; i < collListItems2.Count; i++)
        {
            row = table.NewRow();

            row["Participant"] = user.Name;

            SPFieldLookupValue lpkFieldQName = new SPFieldLookupValue(itemRep["SurveyQuestion"].ToString());
            string lkpQNameVal = lpkFieldQName.LookupValue;

            row["Question"] = lkpQNameVal;

            SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("(Error)", TraceSeverity.Unexpected, EventSeverity.Error), TraceSeverity.Unexpected, ">" + row["Question"].ToString(), "");

            if (itemRep["Experience"] !=null)
            {
                row["Experience"] = itemRep["Experience"];
            }
            else
            {
                row["Experience"] = Convert.ToInt32("0");
            }

            table.Rows.Add(row);
        }
    }

    return dt;
}

最后图表绘图代码,

protected void DrawExperienceChart(DataTable dt)
{
    Chart1.Series["Series1"].ChartType = SeriesChartType.Bar;
    Chart1.Series["Series1"]["DrawingStyle"] = "Emboss";
    Chart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = false;
    Chart1.Series["Series1"].IsValueShownAsLabel = true;
    Chart1.DataSource = dt;
    Chart1.Series["Series1"].XValueMember = "Question";
    Chart1.Series["Series1"].YValueMembers = "Experience";
    Chart1.DataBind();
}

所有这些功能都是通过按钮点击来调用的,

protected void btnGetReport_Click(object sender, EventArgs e)
{
    DataTable dt =  GetGroupExperienceData(pickerEntity);

    DrawExperienceChart(dt);   
}

这是输出,X asix上缺少许多“问题”数据,但在Y轴上绘制了相同的“经验”。

enter image description here

我认为数据可能不会进入数据表,但数据表中包含我可以使用SPDiagnosticsService.Local.WriteTrace()看到的数据,

enter image description here

1 个答案:

答案 0 :(得分:0)

很简单,有一个设置必须启用才能查看所有标签,否则图表控件将隐藏大部分标签。

Chart1.ChartAreas["ChartArea1"].AxisX.Interval = 1;