2

Unfortunately, I do not have enough reputation points to post images so here is a link to an example chart.

Does anyone know how I can get the columns in this chart to align with the x-axis properly? Most of the columns on the chart are not even close to their respective x-axis label. If you look at my example, the first two columns should align with the 2005 x-axis, but it doesn't even show up in the chart. Same with the last column. That one doesn't show up in the chart either, however, it should be aligned with the 2015 x-axis.

I have been working on this issue now for quite a while. All of the possible solutions that I have found (including here on SO) haven't worked for me.

Here is the code that I wrote to generate the example output: (Markup)

<div style="width:100%;">
    <div>        
        <asp:Chart ID="chEmpTRI" runat="server" Width="600px">
            <series>
            </series>
            <chartareas>
                <asp:ChartArea Name="ChartArea1">
                </asp:ChartArea>
            </chartareas>
        </asp:Chart>
        <asp:SqlDataSource ID="sdsEmpTRI" runat="server" 
            ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>" 
            SelectCommand="
                SELECT 
                    [Desc], 
                    [Year], 
                    [Value] 
                FROM [vw_EmpTRI] 
                --WHERE [Year] BETWEEN 2005 AND 2007
                ORDER BY [Year], [Desc]">
        </asp:SqlDataSource>        
    </div>
</div>

(Code Behind)

 int? min = 0, max = 0;
protected void Page_Load(object sender, EventArgs e)
{        
    DataView dv = sdsEmpTRI.Select(DataSourceSelectArguments.Empty) as DataView;
    if (dv != null)
    {
        min = dv.Table.Compute("Min(Year)", string.Empty) as int?;
        max = dv.Table.Compute("Max(Year)", string.Empty) as int?;

        if (min.HasValue && max.HasValue)
        {
            for (int i = min.Value; i <= max.Value; i++)
            {
                dv.RowFilter = String.Format("[Year] = {0}", i);
                foreach (DataRowView drv in dv)
                {                                              
                    Decimal? value = drv.Row.Field<Decimal?>("Value");
                    Series s = new Series(String.Format("{0}_{1}", drv.Row.Field<string>("Desc"), i));
                    s.ChartType = SeriesChartType.Column;
                    s.XValueType = ChartValueType.Int32;                       
                    DataPoint p = new DataPoint(s);
                    p.XValue = i;
                    p.YValues = new double[] { value.HasValue ? decimal.ToDouble(value.Value) : double.NaN };
                    s.Points.Add(p);
                    s.SetCustomProperty("PixelPointWidth", "150");
                    s.SetCustomProperty("PointWidth", "1");
                    s.Label = String.Format("{0}", value);
                    chEmpTRI.Series.Add(s);
                }
            }
        }
    }
}

I tried using the custom property s.SetCustomProperty("PointWidth", "1") to try and reduce the spacing between column groups, but that has no affect what so ever. Does anyone out there have any suggestions on how to line this up?

MrBoolJIT
  • 41
  • 1
  • 6

0 Answers0