/// <summary> /// Chart Paint event handler. /// </summary> private void Chart_PostPaint(object sender, Dundas.Charting.WebControl.ChartPaintEventArgs e) { if (sender is ChartArea) { ChartArea area = (ChartArea)sender; // call the paint method. if (ChartAreas.IndexOf(area.Name) >= 0 && enabled) { PaintDataTable(sender, e); } } }
protected void Chart1_PostPaint(object sender, Dundas.Charting.WebControl.ChartPaintEventArgs e) { if (sender is ChartArea) { Series series = Chart1.Series["Tasks"]; // Take Graphics object from chart Graphics graph = e.ChartGraphics.Graphics; if (_dataTable == null) { return; } for (int i = 0; i < _dataTable.Rows.Count; i++) { DataRow row = _dataTable.Rows[i]; DataPoint chdObj = GetSeriesPoint(row["TASK_REF_ID"]); DataPoint obj = GetSeriesPoint(row["AFTER_TASK_REF_ID"]); if (obj != null && chdObj != null) { double p1X, p2X, p1Y, p2Y; p1X = obj.XValue; p2X = chdObj.XValue; p1Y = obj.YValues[1]; p2Y = chdObj.YValues[0]; if ((i + 1) <= series.Points.Count && i != 0) { float pixelX1 = (float)e.ChartGraphics.GetPositionFromAxis("Default", AxisName.Y, p1Y); float pixelY1 = (float)e.ChartGraphics.GetPositionFromAxis("Default", AxisName.X, p1X); float pixelX2 = (float)e.ChartGraphics.GetPositionFromAxis("Default", AxisName.Y, p2Y); float pixelY2 = (float)e.ChartGraphics.GetPositionFromAxis("Default", AxisName.X, p2X); PointF point1 = PointF.Empty; PointF point2 = PointF.Empty; PointF point3 = PointF.Empty; point1.X = pixelX1; point1.Y = pixelY1; point2.X = pixelX2; point2.Y = pixelY2; // make the middle right-angle point point3.X = pixelX2; point3.Y = pixelY1; // Convert relative coordinates to absolute coordinates. point1 = e.ChartGraphics.GetAbsolutePoint(point1); point2 = e.ChartGraphics.GetAbsolutePoint(point2); point3 = e.ChartGraphics.GetAbsolutePoint(point3); // Draw connection line graph.DrawLine(new Pen(Color.Black, 1), point1, point3); graph.DrawLine(new Pen(Color.Black, 1), point3, point2); DrawArrow(graph, Color.Black, point2, point3, 20.5); } } } } }
/// <summary> /// This method does all the work for the painting of the data table. /// </summary> private void PaintDataTable(object sender, Dundas.Charting.WebControl.ChartPaintEventArgs e) { ChartArea area = (ChartArea)sender; // get the rect of the chart area RectangleF rect = e.ChartGraphics.GetAbsoluteRectangle(area.Position.ToRectangleF()); // get the inner plot position ElementPosition elemPos = area.InnerPlotPosition; // find the coordinates of the inner plot position float x = rect.X + (rect.Width / 100 * elemPos.X); float y = rect.Y + (rect.Height / 100 * elemPos.Y); float ChartAreaBottomY = rect.Y + rect.Height; float width = (rect.Width / 100 * elemPos.Width); float height = (rect.Height / 100 * elemPos.Height); // find the height of the font that will be used Font axisFont = area.AxisX.LabelStyle.Font; string testString = "ForFontHeight"; SizeF axisFontSize = e.ChartGraphics.MeasureString(testString, axisFont); // find the height of the font that will be used Font titleFont = area.AxisX.TitleFont; testString = area.AxisX.Title; SizeF titleFontSize = e.ChartGraphics.MeasureString(testString, titleFont); int seriesCount = 0; // for each series that is attached to the chart area, // draw some boxes around the labels in the color provided for (int i = e.Chart.Series.Count - 1; i >= 0; i--) { if (area.Name == e.Chart.Series[i].ChartArea) { seriesCount++; } } // now, if a box was actually drawn, then draw // the verticle lines to separate the columns of the table. if (seriesCount > 0) { for (int i = 0; i < e.Chart.Series.Count; i++) { if (area.Name == e.Chart.Series[i].ChartArea) { double min = area.AxisX.Minimum; double max = area.AxisX.Maximum; // modify the min value for the current axis view if (area.AxisX.View.Position - 1 > min) { min = area.AxisX.View.Position - 1; } // modify the max value for the currect axis view if ((area.AxisX.View.Position + area.AxisX.View.Size + 0.5) < max) { max = area.AxisX.View.Position + area.AxisX.View.Size + 0.5; } // find the starting point that will be display. // this is dependent on the current axis view. // this sample assumes the same number of points in each // series so always take from the zeroth series int pointIndex = 0; foreach (DataPoint pt in ChartObj.Series[0].Points) { if (pt.XValue > min) { break; } pointIndex++; } bool TableLegendDrawn = false; for (double AxisValue = min; AxisValue < max; AxisValue++) { float pixelX = (float)e.ChartGraphics.GetPositionFromAxis("Default", AxisName.X, AxisValue); float nextPixelX = (float)e.ChartGraphics.GetPositionFromAxis("Default", AxisName.X, AxisValue + 1); float pixelY = ChartAreaBottomY - titleFontSize.Height - (seriesCount * axisFontSize.Height); PointF point1 = PointF.Empty; PointF point2 = PointF.Empty; // Set Maximum and minimum points point1.X = pixelX; point1.Y = 0; // Convert relative coordinates to absolute coordinates. point1 = e.ChartGraphics.GetAbsolutePoint(point1); point2.X = point1.X; point2.Y = ChartAreaBottomY - titleFontSize.Height; point1.Y = pixelY; // Draw connection line e.ChartGraphics.DrawLine(new Pen(borderColor), point1, point2); point2.X = nextPixelX; point2.Y = 0; point2 = e.ChartGraphics.GetAbsolutePoint(point2); StringFormat format = new StringFormat(); format.Alignment = StringAlignment.Center; format.LineAlignment = StringAlignment.Center; // for each series draw one value in the column int row = 0; foreach (Series ser in ChartObj.Series) { if (area.Name == ser.ChartArea) { if (!TableLegendDrawn) { // draw the series color box e.ChartGraphics.FillRectangle(new SolidBrush(ser.Color), x - 10, row * (axisFont.Height) + (point1.Y), 10, axisFontSize.Height); e.ChartGraphics.DrawRectangle(new Pen(borderColor), x - 10, row * (axisFont.Height) + (point1.Y), 10, axisFontSize.Height); e.ChartGraphics.FillRectangle(new SolidBrush(tableColor), x, row * (axisFont.Height) + (point1.Y), width, axisFontSize.Height); e.ChartGraphics.DrawRectangle(new Pen(borderColor), x, row * (axisFont.Height) + (point1.Y), width, axisFontSize.Height); } if (pointIndex < ser.Points.Count) { string label = ser.Points[pointIndex].YValues[0].ToString(); RectangleF textRect = new RectangleF(point1.X, row * (axisFont.Height) + (point1.Y + 1), point2.X - point1.X, axisFont.Height); e.ChartGraphics.DrawString(label, axisFont, new SolidBrush(area.AxisX.LabelStyle.FontColor), textRect, format); } row++; } } TableLegendDrawn = true; pointIndex++; } // do this only once so break! break; } } } }