The particular problem is one that have been asked before. I’ve got an asp:chart control and it’s not showing all my labels on the X axis. Same question as:
Why do labels disappear in ASP.NET charting?
However the answer (interval=1 on X axis) does nothing on my control. And furthermore, I’m wondering if I’ve got some wrong settings, because a lot of things does nothing when I try to change them, for example LabelAutoFitStyle setting doesn’t effect anything on my chart.
Here is my code:
<asp:Chart ID="SalesChart" Width="500" Height="400" ImageStorageMode="UseHttpHandler" EnableViewState="False" runat="server" ImageType="Png">
<Series>
<asp:Series ChartType="Column" Name="StatsSeries" XAxisType="Secondary" ToolTip="#VALX: #VALY{C0}" label="#VALY{C0}" LabelBorderColor="Gray" LabelBackColor="#FFF6AA">
<Points>
</Points>
</asp:Series>
</Series>
<ChartAreas>
<asp:ChartArea Area3DStyle-Enable3D="true" Name="StatsChartArea" Area3DStyle-PointGapDepth="0" Area3DStyle-Rotation="20" Area3DStyle-PointDepth="60" Area3DStyle-Inclination="20">
<AxisX LabelAutoFitMinFontSize="10" Interval="1">
<MajorGrid Interval="1" />
<MinorGrid />
<LabelStyle Interval="1" />
</AxisX>
<AxisY>
<LabelStyle Format='{C0}' />
<MinorGrid Enabled="True" LineColor="Gray" />
</AxisY>
</asp:ChartArea>
</ChartAreas>
</asp:Chart>
code behind (I’ve hidden a lot of code here that is not relevant):
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Here I get chart data
//Set everything up
if (statsDataTable.Rows.Count > 0)
{
SetChartData(statsDataTable);
SetChartGridResolution();
SetChartBarColors();
}
}
}
private void SetChartData(DataTable statsDataTable)
{
ArrayList xValues = new ArrayList();
ArrayList yValues = new ArrayList();
// I populate the lists with values, and then bind it below
SalesChart.Series[0].Points.DataBindXY(xValues, yValues);
}
/// <summary>
/// Set the intervals on the grid, based on the highest value.
/// </summary>
private void SetChartGridResolution()
{
if (SalesChart.Series[0].Points.FindMaxByValue().YValues[0] > 2000000)
{
SalesChart.ChartAreas[0].AxisY.LabelStyle.Interval = 1000000;
SalesChart.ChartAreas[0].AxisY.MajorGrid.Interval = 1000000;
SalesChart.ChartAreas[0].AxisY.MajorTickMark.Interval = 1000000;
SalesChart.ChartAreas[0].AxisY.MinorGrid.Interval = 500000;
}
else if (SalesChart.Series[0].Points.FindMaxByValue().YValues[0] > 1000000)
{
SalesChart.ChartAreas[0].AxisY.LabelStyle.Interval = 200000;
SalesChart.ChartAreas[0].AxisY.MajorGrid.Interval = 200000;
SalesChart.ChartAreas[0].AxisY.MajorTickMark.Interval = 200000;
SalesChart.ChartAreas[0].AxisY.MinorGrid.Interval = 100000;
}
else
{
SalesChart.ChartAreas[0].AxisY.LabelStyle.Interval = 100000;
SalesChart.ChartAreas[0].AxisY.MajorGrid.Interval = 100000;
SalesChart.ChartAreas[0].AxisY.MajorTickMark.Interval = 100000;
SalesChart.ChartAreas[0].AxisY.MinorGrid.Interval = 50000;
}
}
private void SetChartBarColors()
{
System.Drawing.Color color1 = System.Drawing.ColorTranslator.FromHtml("#007dc3");
System.Drawing.Color color2 = System.Drawing.ColorTranslator.FromHtml("#539dc8");
for (int i = 0; i < SalesChart.Series[0].Points.Count; i++)
{
if (i % 2 == 0)
SalesChart.Series[0].Points[i].Color = color1;
else
SalesChart.Series[0].Points[i].Color = color2;
}
}
I’d be great if you could give me a clue!
Regards Jacob
Ok, I solved it! This problem has cost me several hours, trying to solve various problems. I hope someone can be spared that by reading this.
It was all because of XAxisType=”Secondary”, when this is selected (to use the upper x-line instead of the lower) all configuration on the x-axis must be set on instead of . This was causing a lot of things to “not work” for me…