C# MS Chart Control - 两个问题

发布于 2024-10-29 17:50:25 字数 5266 浏览 2 评论 0原文

所以我用 MS Chart Control 制作了这个范围条形图。我有两个问题:

  1. 当用户双击一个系列时,如何实现事件处理程序?我在任何地方都看不到一个人。
  2. 由于某种原因,我的 X 轴上的滚动条(有趣的是,图表控件似乎认为是 Y 轴...)似乎由于某种原因部分透明...任何人都可以解释为什么会这样?

到目前为止,这是我的代码,是从我在网上找到的 PDF 中修改而来的(是的,我知道,它很乱,需要整理):

    private void PopulateGantt()
    {
        foreach (Job jobThis in lstJobs)
        {
            if ((jobThis.HireFrom != null) && (jobThis.HireTo != null))
            {
                string xlabel = string.Empty;
                double xordinal = 0;
                double yplot1 = 0;
                double yplot2 = 0;
                yplot1 = (double)((DateTime)jobThis.HireFrom).ToOADate();
                yplot2 = (double)((DateTime)jobThis.HireTo).ToOADate()+1;
                // Use a different series for each datapoint
                Series seriesInstance = new Series();
                // For Gantt charts, we want a RangeBar graph type
                seriesInstance.ChartType = SeriesChartType.RangeBar;
                // Have a start and end date so plotting 2 points on the y-axis
                seriesInstance.YValuesPerPoint = 2;
                // We want to draw datapoint side by side (night is day?)
                seriesInstance.CustomProperties = "DrawSideBySide=false";
                // Add the datapoint to the series, specifying tooltiptext, colorand label

                xordinal = lstJobs.IndexOf(jobThis); //(double)itemIndex;
                seriesInstance.Points.AddXY(xordinal, yplot1, yplot2);

                //seriesInstance.Points[0].Color = resourceColor;
                seriesInstance.Points[0].AxisLabel = xlabel;
                seriesInstance.Label = jobThis.Number + jobThis.Type + " - " + jobThis.ClientCompanyName;
                seriesInstance.Points[0].ToolTip = jobThis.Number + jobThis.Type +
                                "\r\n\r\n" + jobThis.ClientCompanyName +
                                "\r\n\r\n" + jobThis.BriefDescription;

                seriesList.Add(seriesInstance);
            }
            chtHC.Series.Clear();
            foreach (Series plotSeries in seriesList)
            {
                chtHC.Series.Add(plotSeries);
            }
            // Force x-axis to show each task or resource
            chtHC.ChartAreas[0].AxisX.Interval = 1;
            // Set y-axis to show each day of the month
            chtHC.ChartAreas[0].AxisY.Interval = 1;
            // Set x-axis to show in reversed order so dates are displayed leftto-right
            //chtHC.ChartAreas[0].AxisY.IsReversed = true;
            //chtHC.ChartAreas[0].AxisX
            // Set other y-axis properties
            chtHC.ChartAreas[0].AxisY.IsStartedFromZero = false;
            chtHC.ChartAreas[0].AxisY.IsMarginVisible = false;
            chtHC.ChartAreas[0].AxisY.IntervalType = DateTimeIntervalType.Days;
            // Set the y-axis labels
            DateTime? datFirst = null;// = new DateTime();
            DateTime? datLast = null; //= new DateTime();
            //datFirst = (DateTime)lstJobs[0].HireFrom;
            foreach (Job jobFirst in lstJobs)
            {
                if (jobFirst.HireFrom != null)
                {
                    if (datFirst == null)
                    {
                        datFirst = (DateTime)jobFirst.HireFrom;
                    }
                    else
                    {
                        if (jobFirst.HireFrom < datFirst)
                        {
                            datFirst = (DateTime)jobFirst.HireFrom;
                        }
                    }
                }

                if (jobFirst.HireTo != null)
                {
                    if (datLast == null)
                    {
                        datLast = (DateTime)jobFirst.HireTo;
                    }
                    else
                    {
                        if (jobFirst.HireTo > datLast)
                        {
                            datLast = (DateTime)jobFirst.HireTo;
                        }
                    }
                }

            }

            if ((datFirst != null))
            {
                //datLast = ((DateTime)datFirst).AddDays(21);
                chtHC.ChartAreas[0].AxisY.Minimum = ((DateTime)datFirst).AddDays(-1).ToOADate();
                chtHC.ChartAreas[0].AxisY.Maximum = ((DateTime)datLast).AddDays(+1).ToOADate();
            }
            chtHC.ChartAreas[0].CursorY.AutoScroll = true;
            chtHC.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
            chtHC.ChartAreas[0].AxisY.ScaleView.SizeType = DateTimeIntervalType.Days;

            //chtHC.ChartAreas[0].AxisY.LabelStyle.Format = "MMM dd ddd";
            //chtHC.ChartAreas[0].AxisY.LabelStyle.Format = "ddd MMM dd";
            chtHC.ChartAreas[0].AxisY.LabelStyle.Format = "ddd dd/MM/yyyy";
            //chtHC.ChartAreas[0].AxisX.// Force redraw of chart

            chtHC.ChartAreas[0].AxisY.ScaleView.Zoom(chtHC.ChartAreas[0].AxisY.Minimum, chtHC.ChartAreas[0].AxisY.Minimum+21);
            chtHC.ChartAreas[0].AxisY.ScrollBar.ButtonStyle = ScrollBarButtonStyles.SmallScroll;
            chtHC.ChartAreas[0].AxisY.ScaleView.SmallScrollSize = 1;

            chtHC.Update();
        }
    }

So I've made this range bar chart with the MS Chart Control. I have two questions:

  1. How can I implement an event handler for when the user double clicks a series? I can't see one anywhere.
  2. For some reason the scrollbar on my X Axis (which, amusingly, the Chart Control seems to think is the Y Axis...) seems to be partially transparent for some reason...can anyone shed any light as to why that might be?

Here's my code so far, bastardised from a PDF I found somewhere on the net (yeah, I know, it's messy, needs tidying up):

    private void PopulateGantt()
    {
        foreach (Job jobThis in lstJobs)
        {
            if ((jobThis.HireFrom != null) && (jobThis.HireTo != null))
            {
                string xlabel = string.Empty;
                double xordinal = 0;
                double yplot1 = 0;
                double yplot2 = 0;
                yplot1 = (double)((DateTime)jobThis.HireFrom).ToOADate();
                yplot2 = (double)((DateTime)jobThis.HireTo).ToOADate()+1;
                // Use a different series for each datapoint
                Series seriesInstance = new Series();
                // For Gantt charts, we want a RangeBar graph type
                seriesInstance.ChartType = SeriesChartType.RangeBar;
                // Have a start and end date so plotting 2 points on the y-axis
                seriesInstance.YValuesPerPoint = 2;
                // We want to draw datapoint side by side (night is day?)
                seriesInstance.CustomProperties = "DrawSideBySide=false";
                // Add the datapoint to the series, specifying tooltiptext, colorand label

                xordinal = lstJobs.IndexOf(jobThis); //(double)itemIndex;
                seriesInstance.Points.AddXY(xordinal, yplot1, yplot2);

                //seriesInstance.Points[0].Color = resourceColor;
                seriesInstance.Points[0].AxisLabel = xlabel;
                seriesInstance.Label = jobThis.Number + jobThis.Type + " - " + jobThis.ClientCompanyName;
                seriesInstance.Points[0].ToolTip = jobThis.Number + jobThis.Type +
                                "\r\n\r\n" + jobThis.ClientCompanyName +
                                "\r\n\r\n" + jobThis.BriefDescription;

                seriesList.Add(seriesInstance);
            }
            chtHC.Series.Clear();
            foreach (Series plotSeries in seriesList)
            {
                chtHC.Series.Add(plotSeries);
            }
            // Force x-axis to show each task or resource
            chtHC.ChartAreas[0].AxisX.Interval = 1;
            // Set y-axis to show each day of the month
            chtHC.ChartAreas[0].AxisY.Interval = 1;
            // Set x-axis to show in reversed order so dates are displayed leftto-right
            //chtHC.ChartAreas[0].AxisY.IsReversed = true;
            //chtHC.ChartAreas[0].AxisX
            // Set other y-axis properties
            chtHC.ChartAreas[0].AxisY.IsStartedFromZero = false;
            chtHC.ChartAreas[0].AxisY.IsMarginVisible = false;
            chtHC.ChartAreas[0].AxisY.IntervalType = DateTimeIntervalType.Days;
            // Set the y-axis labels
            DateTime? datFirst = null;// = new DateTime();
            DateTime? datLast = null; //= new DateTime();
            //datFirst = (DateTime)lstJobs[0].HireFrom;
            foreach (Job jobFirst in lstJobs)
            {
                if (jobFirst.HireFrom != null)
                {
                    if (datFirst == null)
                    {
                        datFirst = (DateTime)jobFirst.HireFrom;
                    }
                    else
                    {
                        if (jobFirst.HireFrom < datFirst)
                        {
                            datFirst = (DateTime)jobFirst.HireFrom;
                        }
                    }
                }

                if (jobFirst.HireTo != null)
                {
                    if (datLast == null)
                    {
                        datLast = (DateTime)jobFirst.HireTo;
                    }
                    else
                    {
                        if (jobFirst.HireTo > datLast)
                        {
                            datLast = (DateTime)jobFirst.HireTo;
                        }
                    }
                }

            }

            if ((datFirst != null))
            {
                //datLast = ((DateTime)datFirst).AddDays(21);
                chtHC.ChartAreas[0].AxisY.Minimum = ((DateTime)datFirst).AddDays(-1).ToOADate();
                chtHC.ChartAreas[0].AxisY.Maximum = ((DateTime)datLast).AddDays(+1).ToOADate();
            }
            chtHC.ChartAreas[0].CursorY.AutoScroll = true;
            chtHC.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
            chtHC.ChartAreas[0].AxisY.ScaleView.SizeType = DateTimeIntervalType.Days;

            //chtHC.ChartAreas[0].AxisY.LabelStyle.Format = "MMM dd ddd";
            //chtHC.ChartAreas[0].AxisY.LabelStyle.Format = "ddd MMM dd";
            chtHC.ChartAreas[0].AxisY.LabelStyle.Format = "ddd dd/MM/yyyy";
            //chtHC.ChartAreas[0].AxisX.// Force redraw of chart

            chtHC.ChartAreas[0].AxisY.ScaleView.Zoom(chtHC.ChartAreas[0].AxisY.Minimum, chtHC.ChartAreas[0].AxisY.Minimum+21);
            chtHC.ChartAreas[0].AxisY.ScrollBar.ButtonStyle = ScrollBarButtonStyles.SmallScroll;
            chtHC.ChartAreas[0].AxisY.ScaleView.SmallScrollSize = 1;

            chtHC.Update();
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

水染的天色ゝ 2024-11-05 17:50:25

没有能够处理数据点点击的特定事件,但您可以使用 MouseClick 事件加上 HitTest 方法,例如:

void chart1_MouseClick(object sender, MouseEventArgs e)
{
    var pos = e.Location;
    var results = chart1.HitTest(pos.X, pos.Y,false, ChartElementType.DataPoint);
    foreach (var result in results)
    {
        if (result.ChartElementType == ChartElementType.DataPoint)
        {
            // use result.Series etc...
        }
    }
}

There's no specific event able to handle datapoint clicks, but you can use MouseClick event plus HitTest method, e.g.:

void chart1_MouseClick(object sender, MouseEventArgs e)
{
    var pos = e.Location;
    var results = chart1.HitTest(pos.X, pos.Y,false, ChartElementType.DataPoint);
    foreach (var result in results)
    {
        if (result.ChartElementType == ChartElementType.DataPoint)
        {
            // use result.Series etc...
        }
    }
}
淑女气质 2024-11-05 17:50:25

该图表还有双击事件

private void chart_MouseDoubleClick(object sender, MouseEventArgs e)

the chart has also a double click event

private void chart_MouseDoubleClick(object sender, MouseEventArgs e)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文