为什么图表堆叠列显示为细线? [英] Why do Chart Stacked Columns show up as thin lines?

查看:204
本文介绍了为什么图表堆叠列显示为细线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  foreach(chartEvents.Series中的系列)
s.Points.Clear

foreach(dataRows中的DataRow dr)
{
string reason =;
double xVal = 0;
double yVal = 0;
double overFlow = 0;
double existsFlow = 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;

//假设已准备好工具提示并设置格式长度

do
{
overFlow = 0;

#region检查x值的持续时间是否超过60分钟

foreach(chartEvents.Series中的系列)
{
if(s。 Points.Count> 0)
{
foreach(DataPoint存在于s.Points中)
{
//如果找到点,则加起来。
if(exists.XValue == xVal)
{
existsFlow + = exist.YValues [0];
}
}
}
}

//如果相加+ new> 60,将当前y设置为60并且为溢出计算
if(existsFlow + yVal> 60)
{
overFlow + = yVal-(60- existsFlow);
yVal = 60 - (existFlow);
}

#endregion

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

#region检查系列类型并添加到系列。

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();
existsFlow = 0;
}

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

解决方案

您可能有错误 X值



请注意,系列只能堆叠 DataPoints 具有相同 X值 DateTimes 包括秒数的时间,所以除非你扭曲它们以达到你的目标,否则它们永远不会叠加。



您的代码在这里很少漏掉:

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

舍入 DateTime / OADate double 到6位不幸的是会修剪时间部分。为此,你可以简单地写 XValue = someDateTimeVariable.Date;



对于其他时间间隔,你必须决定



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

/ p>

  //定义一个合适的格式字符串:
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);

这里有两个结果,所有相同的数据,只有一个以分钟间隔,天。



>



我建议考虑设置 X-Axis X-Values 使用 DateTime datatype!



请忽略我不是真正创建一个令人信服的堆叠柱形图,我根本没有创建好的测试数据。


I am trying to create a stacked column chart with 4 series in it. But somehow, after populating the series and making sure they are aligned, instead of columns, thin lines appears. Code is as below.

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
    }
}

解决方案

You probably have wrong X-Values for the purpose.

Note that your Series can only stack where DataPoints have the same X-Value. DateTimes include times downto fractions of a second, so they will never stack unless you twist them to meet your goal..

You code is closely missing the point here:

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

Rounding a DateTime / OADate double to 6 digits unfortunately will not trim the time portion. For this you would instead simply write XValue = someDateTimeVariable.Date;

For other time intervals you must decide on the time interval, like days, minutes, hours etc.. and trim the data to multiples of that interval.

There are several ways to do that, here is one:

// 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

Now you can use it:

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

Here are two results, all the same data, just one with the minute interval, and the other with days.:

I suggest considering to set the X-Axis and X-Values to use DateTime datatype!

Please ignore the fact that I'm not really creating a convincing stacked column chart, I simply didn't create good test data..

这篇关于为什么图表堆叠列显示为细线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆