Setting the “Enable3D” property of “ChartArea” in Code behind for StackedColumn charts
質問
I created a StackedColumn chart using the built in .NET 4.0 controls. The charts are coming out properly. I want to make it look 3-D though. I am able to do that by using XAML. But I am unable to do the same using code behind.
Please see my code behind. I have a Chart, and a ChartArea. I saw that the code to enable 3-D is as follows:
Chart1.ChartAreas["Default"].Area3DStyle.Enable3D = true;
But I get an error at Runtime as follows: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
I also tried using the following options (where cArea is the instance of ChartArea) :
cArea.Area3DStyle.Enable3D = true;
Chart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = true;
Chart1.ChartAreas[0].Area3DStyle.Enable3D = true;
Is there any difference in where I set these properties. I tried all the 3 above statements before I add the Chart1 element.
The code behind I use is :
Chart1.Series.Clear();
Chart1.ChartAreas.Clear();
ChartArea cArea = new ChartArea("Default");
Chart1.ChartAreas.Add(cArea);
// ABLE TO USE cArea to change properties.... !!!
cArea.AxisX.LabelStyle.Angle = +80;
cArea.AxisX.LabelStyle.Interval = 1;
cArea.BackColor = Color.Beige;
foreach (var g in groups)
{
Series s1 = new Series(g.Key);
Chart1.Series.Add(s1);
s1.ChartType = SeriesChartType.StackedColumn;
s1["PointWidth"] = "0.7";
foreach (var m in g)
{
s1.Points.AddXY(m.Category, m.Count);
}
s1.IsValueShownAsLabel = true;
s1.ToolTip = "#VALY";
}
this.Controls.Add(Chart1);
The XAML is :
<asp:Chart ID="Chart1" runat="server" ImageLocation="~/FolderLoc/Chart_#SEQ(1000,0)" ImageStorageMode="UseImageLocation" ImageType="Png" >
</asp:Chart>
I am however able to use the cArea to change other properties as part of my chart as shown in the code:
// ABLE TO USE cArea to change properties.... !!!
cArea.AxisX.LabelStyle.Angle = +80;
cArea.AxisX.LabelStyle.Interval = 1;
cArea.BackColor = Color.Beige;
When using Static data, I am able to set the "Enable3D" to TRUE with the XAML as :
<chartareas>
<asp:ChartArea Name="Default">
<Area3DStyle Enable3D="True" />
</asp:ChartArea>
</chartareas>
Can you please tell me how to make the chart 3-D enabled?
解決
Two things to try:
Chart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = true;
Chart1.ChartAreas[0].Area3DStyle.Enable3D = true;
他のヒント
You have empty data in the X axis. Use AlignDataPointsByAxis to fix this.