1

I have an asp:Chart that I'm populating using c#. See below:

    protected void populateBarChart()
    {
        int userDeptId = int.Parse(Session["userDept"].ToString());
        DateTime? selectedDate = null;

        string query = "SELECT * FROM [dbo].sf_MonthEndChartDepartment(null , " + userDeptId + ") order by xAxisValue";
        if (Session["selectedMonth"] != null)
        {
            selectedDate = DateTime.Parse(Session["selectedMonth"].ToString());
            string sqlFormattedDate = ((DateTime)selectedDate).ToString("yyyy-MM-dd HH:mm:ss.fff");
            query = "SELECT * FROM [dbo].sf_MonthEndChartDepartment('" + sqlFormattedDate + "' , " + userDeptId + ") order by xAxisValue";
        }

        if (cn.State != ConnectionState.Open)
            cn.Open();
        cmd = new SqlCommand(query, cn);

        try
        {
            SqlDataReader dreader = cmd.ExecuteReader();
            while (dreader.Read())
            {
                int projectTypId = Int32.Parse(dreader["ProjectTypeId"].ToString());
                string xAxisValueMon = dreader["xAxisValueMon"].ToString();
                double xAxisValue = double.Parse(dreader["xAxisValue"].ToString());
                double yAxisValue = double.Parse(dreader["yAxisValue"].ToString());

                // NOTE: Series are zero based, where database records are 1 based.
                // Therefore subtract 1 from the projectTypeId to get correct series
                if (projectTypId == CodHelpus.PROJECT_TYPE_SAVING)
                {
                    chart1.Series[projectTypId - 1].Points.Add(new DataPoint(xAxisValue, yAxisValue));
                    chart1.Series[projectTypId - 1].Label = "Cost Saving";
                }
                else if (projectTypId == CodHelpus.PROJECT_TYPE_REVENUE)
                {
                    chart1.Series[projectTypId - 1].Points.Add(new DataPoint(xAxisValue, yAxisValue));
                    chart1.Series[projectTypId - 1].Label = "Revenue (OZ)";
                }
                else if (projectTypId == CodHelpus.PROJECT_TYPE_RISK)
                {
                    chart1.Series[projectTypId - 1].Points.Add(new DataPoint(xAxisValue, yAxisValue));
                    chart1.Series[projectTypId - 1].Label = "Risk";
                }
                else if (projectTypId == CodHelpus.PROJECT_TYPE_LTO)
                {
                    chart1.Series[projectTypId - 1].Points.Add(new DataPoint(xAxisValue, yAxisValue));
                    chart1.Series[projectTypId - 1].Label = "LTO";
                }
                else if (projectTypId == CodHelpus.PROJECT_TYPE_SAFETY)
                {
                    chart1.Series[projectTypId - 1].Points.Add(new DataPoint(xAxisValue, yAxisValue));
                    chart1.Series[projectTypId - 1].Label = "Safety";
                }
                else if (projectTypId == CodHelpus.PROJECT_TYPE_CAPITAL)
                {
                    chart1.Series[projectTypId - 1].Points.Add(new DataPoint(xAxisValue, yAxisValue));
                    chart1.Series[projectTypId - 1].Label = "Capital";
                }
                else if (projectTypId == CodHelpus.PROJECT_TYPE_NA)
                {
                    chart1.Series[projectTypId - 1].Points.Add(new DataPoint(xAxisValue, yAxisValue));
                    chart1.Series[projectTypId - 1].Label = "N/A";
                }
            }


            foreach (Series cs in chart1.Series)
                cs.ChartType = SeriesChartType.StackedColumn;

        }
        catch (SqlException ex)
        {

        }
        finally
        {
            cmd.Dispose();
            cn.Close();
        }
        //chart1.SaveXml(@"C:\NetworkDatatest\chart.xml"); //test the output
}

Here is the asp code:

    <div class="col-lg-6 nopadding">
        <asp:Chart ID="chart1" runat="server" Width="500px" RightToLeft="No">
            <Series>
                <asp:Series ChartArea="ChartArea1" Name="Saving"    LabelForeColor="Transparent" Legend="Legend1" />
                <asp:Series ChartArea="ChartArea1" Name="Revenue"    LabelForeColor="Transparent" Legend="Legend1" />
                <asp:Series ChartArea="ChartArea1" Name="Risk"       LabelForeColor="Transparent" Legend="Legend1" />
                <asp:Series ChartArea="ChartArea1" Name="LTO"        LabelForeColor="Transparent" Legend="Legend1" />
                <asp:Series ChartArea="ChartArea1" Name="Safety"     LabelForeColor="Transparent" Legend="Legend1" />
                <asp:Series ChartArea="ChartArea1" Name="Capital"    LabelForeColor="Transparent" Legend="Legend1" />
                <asp:Series ChartArea="ChartArea1" Name="NA"         LabelForeColor="Transparent" Legend="Legend1" />
            </Series>
            <ChartAreas>
                <asp:ChartArea Name="ChartArea1"></asp:ChartArea>
            </ChartAreas>
            <legends>
                <asp:Legend Name="Legend1" Docking="Bottom">
                </asp:Legend>
            </legends>
        </asp:Chart>
    </div>

Here's a link to the results returned by the query:

https://www.dropbox.com/s/iqdih3ae6iu5s0h/dataset_results.csv?dl=0

It populates as I would expect, except some of the data series are "floaters" above the other stacked bars, and there's those weird arrow-like horizontal line things. See below:

Does anyone know what those lines are and how to remove them, and does anyone know why some of the stacked bars are floaters?

This is what the output looked like before help from jstreet:

https://www.dropbox.com/s/ijmbf93sb1tf6f4/Capture.jpg?dl=0

And this is the output after ensuring that there are values for all series points (but still has the ghost data bars):

https://www.dropbox.com/s/aan8ne7twmqd14f/Capture2.jpg?dl=0

Aiden Dipple
  • 134
  • 2
  • 15
  • 1
    In a stacked bar chart you can't have "missing" data. In case data is missing for one particular series in one particular point, you have to add that explicitly as 0(zero) value. – jsanalytics Dec 20 '16 at 03:18
  • Nice one jstreet! I'll check that and update with the results. – Aiden Dipple Dec 20 '16 at 03:32
  • @jstreet Thanks and having a result set that had zero values for null results has fixed the "floating" bars perfectly. I'm still getting those weird marker-like horizontal lines. Any idea? Thanks. See https://www.dropbox.com/s/aan8ne7twmqd14f/Capture2.jpg?dl=0 – Aiden Dipple Dec 20 '16 at 22:43
  • I suspect you may have "ghost" series in your chart. Your code does not show where and how your series are being created. – jsanalytics Dec 20 '16 at 23:00
  • Please read this: [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – jsanalytics Dec 20 '16 at 23:05
  • Hi @jstreet thanks for the input .... I will endeavor to have a better example in future. I suspect it might be a ghost series (I'm assuming that is a series where every value is 0?). Thanks again, that gives me a new avenue to purrsue. – Aiden Dipple Dec 21 '16 at 00:07
  • By "ghost" series i mean some series that is hanging in there with some "junk" content. If you post specific code, people can offer specific help. Otherwise is all guess work...:O) – jsanalytics Dec 21 '16 at 00:14
  • I'm not sure if that's adequate for the example (it only includes the query code - but I put a link to the results returned). Cheers – Aiden Dipple Dec 21 '16 at 00:55
  • That is being caused by repeatedly adding labels to the series, like this `chart1.Series[projectTypId - 1].Label = "Revenue (OZ)";` – jsanalytics Dec 21 '16 at 02:12
  • @jstreet .... You are a champion! :) – Aiden Dipple Dec 21 '16 at 03:39
  • Thank you, glad you fixed it! – jsanalytics Dec 21 '16 at 03:41

0 Answers0