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

时间:2011-04-05 13:47:50

标签: c# events scrollbar mschart

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

  1. 如何在用户双击系列时实现事件处理程序?我在任何地方都看不到。
  2. 出于某种原因,我的X轴上的滚动条(有趣的是,图表控件似乎认为是Y轴...)似乎部分透明出于某种原因...任何人都可以解释为什么那可能是?
  3. 到目前为止,这是我的代码,我在网上的某个地方发现了一个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();
            }
        }
    

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

图表也有双击事件

private void chart_MouseDoubleClick(object sender, MouseEventArgs e)
相关问题