C#MS图表控件 - 两个问题 [英] C# MS Chart Control - two questions

查看:186
本文介绍了C#MS图表控件 - 两个问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我做了这个范围条形图与MS图表控件。我有两个问题:

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


  1. 如何在用户双击一个系列的时候实现一个事件处理程序?我不能在任何地方看到。

  2. 由于某种原因,我的X轴上的滚动条(有趣的是,图表控件似乎认为是Y轴...)似乎是部分透明的某些原因...任何人都可以说明为什么会这样吗?

这是我的代码到目前为止,混蛋从我在网上找到的一个PDF(是的,我知道它很乱,需要整理):

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


推荐答案

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

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

这篇关于C#MS图表控件 - 两个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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