为什么图表堆积列显示为细线?

时间:2015-06-13 07:55:20

标签: c# charts series stacked-chart

我正在尝试创建一个包含4个系列的堆积柱形图。但不知何故,在填充系列并确保它们对齐后,而不是列,会出现细线。代码如下。

foreach (Series s in chartEvents.Series)
    s.Points.Clear();

foreach (DataRow dr in data.Rows)
{
    string reason = "";
    double xVal = 0;
    double yVal = 0;
    double overFlow = 0;
    double existFlow = 0;

    try
    {
        reason = dr["reasonID"].ToString();
        xVal = Math.Round(Convert.ToDateTime(dr["XValue"].ToString()).ToOADate(), 6);
        yVal = Math.Round(Convert.ToDouble(dr["duration"].ToString()) / 60, 3);
        overFlow = 0;

        // assume tooltip prepared and format length here

        do
        {
            overFlow = 0;

            #region check if duration at x value will exceed 60 mins

            foreach (Series s in chartEvents.Series)
            {
                if (s.Points.Count > 0)
                {
                    foreach (DataPoint exist in s.Points)
                    {
                        // if point found, add up.
                        if (exist.XValue == xVal)
                        {
                            existFlow += exist.YValues[0];
                        }
                    }
                }
            }

            // if added up + new  > 60, set current y to 60 and calculate for overflow
            if (existFlow + yVal > 60)
            {
                overFlow += yVal - (60 - existFlow);
                yVal = 60 - (existFlow);
            }

            #endregion

            DataPoint dp = new DataPoint(xVal, yVal);
            DataPoint dpEmpty = new DataPoint(xVal, 0);
            dpEmpty.IsEmpty = true;

            #region Check series type and add to series.

            if (reason.Contains("|"))
            {
                if (reason.Split('|')[3] == "SCHEDULED DOWN")
                {
                    if (reason.Split('|')[4].Contains("SETUP"))
                    {
                        dp.ToolTip = Convert.ToDateTime(dr["dateStart"].ToString()).ToString("yyyy-MM-dd HH:mm:ss").PadRight(25) + "SETUP:  " + actionDone;
                        dp.Color = Color.Goldenrod;

                        chartEvents.Series["SETUP"].Points.Add(dp);
                        chartEvents.Series["CLEAR"].Points.Add(dpEmpty);
                        chartEvents.Series["DOWN"].Points.Add(dpEmpty);
                        chartEvents.Series["OTHERS"].Points.Add(dpEmpty);

                    }
                    else
                    {
                        dp.ToolTip = Convert.ToDateTime(dr["dateStart"].ToString()).ToString("yyyy-MM-dd HH:mm:ss").PadRight(25) + "OTHERS:  " + actionDone;
                        dp.Color = Color.Orange;

                        chartEvents.Series["OTHERS"].Points.Add(dp);
                        chartEvents.Series["CLEAR"].Points.Add(dpEmpty);
                        chartEvents.Series["DOWN"].Points.Add(dpEmpty);
                        chartEvents.Series["SETUP"].Points.Add(dpEmpty);
                    }
                }

                else if (reason.Split('|')[3] == "UNSCHEDULED DOWN")
                {
                    dp.ToolTip = Convert.ToDateTime(dr["dateStart"].ToString()).ToString("yyyy-MM-dd HH:mm:ss").PadRight(25) + "DOWN:  " + actionDone;
                    dp.Color = Color.Red;

                    chartEvents.Series["DOWN"].Points.Add(dp);
                    chartEvents.Series["CLEAR"].Points.Add(dpEmpty);
                    chartEvents.Series["OTHERS"].Points.Add(dpEmpty);
                    chartEvents.Series["SETUP"].Points.Add(dpEmpty);
                }

                else
                {
            // do something
                }
            }
            else
            {
                dp.ToolTip = actionDone;
                dp.Color = Color.Orange;

                chartEvents.Series["CLEAR"].Points.Add(dp);
                chartEvents.Series["DOWN"].Points.Add(dpEmpty);
                chartEvents.Series["OTHERS"].Points.Add(dpEmpty);
                chartEvents.Series["SETUP"].Points.Add(dpEmpty);
            }



            #endregion

            if (overFlow > 0)
            {
                yVal = overFlow;
                xVal = DateTime.FromOADate(xVal).AddHours(1).ToOADate();
                existFlow = 0;
            }

        } while (overFlow != 0);
    }
    catch (Exception ex)
    {
    // do something
    }
}

1 个答案:

答案 0 :(得分:2)

出于此目的,您可能错误X-Values

请注意,Series只能在DataPoints具有相同 X-Value的位置进行堆叠。 DateTimes包括次数的一小部分,所以它们永远不会叠加,除非你扭曲它们以达到你的目标..

您的代码非常缺少这一点:

xVal = Math.Round(Convert.ToDateTime(dr["XValue"].ToString()).ToOADate(), 6);

不幸的是,将DateTime / OADate double舍入为6位将修剪时间部分。为此,您只需编写XValue = someDateTimeVariable.Date;

即可

对于其他时间间隔,您必须确定时间间隔,例如天,分钟,小时等。并将数据修剪为该间隔的倍数。

有几种方法可以做到这一点,这里有一个:

// define a suitable format string:
string myIntervalMonthFormat = "yyyy-MM-01 00:00:00";
string myIntervalDayFormat = "yyyy-MM-dd 00:00:00";
string myIntervalHourFormat = "yyyy-MM-dd hh:00:00";
string myIntervalMinuteFormat = "yyyy-MM-dd hh:mm:00";
// etc

现在你可以使用它了:

string so = ((DateTime) dr["XValue"]).ToString(yourFormat );
xVal = Math.Round(Convert.ToDateTime(so).ToOADate(), 6);  

以下是两个结果,所有相同的数据,只有一个具有分钟间隔,另一个带有天数。:

enter image description here enter image description here

我建议您考虑将X-AxisX-Values设置为使用DateTime数据类型!

请忽略这样一个事实:我并没有真正创建一个令人信服的堆积柱形图,我只是没有创建好的测试数据..