3

I am building an Asp.net Stacked Column chart.

Here is how it looks :

Chart Image

Here is how it should look :

Goal Chart Image

Ignore the numbers on the chart but look at the X axis - Why does it give me 1148,1153, 1163 when they do not appear in the data.

Here is my Data :

Data Image

Here is the code:

   Dim chart As New Chart
                chart.ID = "Chart1"

                Dim chartareas As New ChartArea
                chart.ChartAreas.Add(chartareas)

      chart.DataBindCrossTable(DtFinalRecords.DefaultView, "OutcomeScore", "TermID", "RecordsPerGroup", "Label=RecordsPerGroup")




                chart.ChartAreas(0).AxisX.MajorGrid.Enabled = False
                chart.ChartAreas(0).AxisY.MajorGrid.Enabled = False


                For Each cs As Series In chart.Series
                    cs.ChartType = SeriesChartType.StackedColumn
                Next

                pnlcharts.Controls.Add(chart)

Any help would be appreciated. Thank you!

jsanalytics
  • 13,058
  • 4
  • 22
  • 43
dee
  • 75
  • 6

1 Answers1

1

DataBindCrossTable does the best job it can with minimal coding effort required from you. But if you are not happy with the default behavior then you have to explicitly customize it. In your particular case, you want to assign custom labels to your data points:

protected void Page_Load(object sender, EventArgs e)
{
    Chart1.Palette = ChartColorPalette.None;
    Chart1.PaletteCustomColors = new Color[] { ColorTranslator.FromHtml("#DF5B59"), ColorTranslator.FromHtml("#E0D773 "), ColorTranslator.FromHtml("#8AAC53"), ColorTranslator.FromHtml("#6A843F") };

    Chart1.ChartAreas[0].AxisY.MajorGrid.Enabled = false;
    Chart1.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
    Chart1.ChartAreas[0].AxisX.Interval = 1;

    var rows = from row in dt.AsEnumerable() select row.Field<int>("OutcomeScore");

    Chart1.Series.Clear();

    foreach (int i in rows.Distinct())
        Chart1.Series.Add(new Series { Name = i.ToString(), ChartType = SeriesChartType.StackedColumn });

    foreach (DataRow dr in dt.Rows)
    {
        DataPoint dp = new DataPoint();
        dp.AxisLabel = dr["TermID"].ToString();
        dp.Label = dr["RecordsPerGroup"].ToString();
        dp.XValue = (int)dr["TermID"];
        dp.YValues[0] = (int)dr["RecordsPerGroup"];

        string name = dr["OutcomeScore"].ToString();
        Chart1.Series[name].Points.Add(dp);
    }
}

enter image description here

jsanalytics
  • 13,058
  • 4
  • 22
  • 43
  • Thank you very much!! This worked. I upvoted as well! – dee Apr 05 '17 at 00:23
  • Any idea on how I can add markers or highlight a particular data point on stacked bar/column charts ? That is my last problem with these charts and I will be done - http://stackoverflow.com/questions/43216347/asp-net-stackedbar-chart-markers – dee Apr 05 '17 at 00:26