c# MS图表控制-两个问题

本文关键字:-两 问题 控制 MS | 更新日期: 2023-09-27 17:48:56

所以我用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();
        }
    }

c# MS图表控制-两个问题

没有特定的事件能够处理数据点点击,但您可以使用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...
        }
    }
}

图表还有一个双击事件

private void chart_MouseDoubleClick(object sender, MouseEventArgs e)