/// <summary> /// Find nearest column of existing columns for the defined point /// </summary> /// <param name="point">The point that we want nearest column</param> /// <returns>Index of column ( -1 for not found)</returns> public int NearestColumnIndex(Point point) { int colIndex = -1; if (pointLocation.Count != 0) { ItemPointInfo l = pointLocation[0]; ItemPointInfo g = pointLocation[pointLocation.Count - 1]; float xl = l.Center.X; float xg = g.Center.X; bool bR2L = Parent.Chart.IsRTL(); if (bR2L) { ItemPointInfo e = l; l = g; g = e; float xe = xl; xl = xg; xg = xe; } foreach (ItemPointInfo ipi in pointLocation) { float x = ipi.Center.X; if (x > xl && x <= point.X) { l = ipi; xl = x; } if (x < xg && x >= point.X) { g = ipi; xg = x; } } if (xl == xg) { colIndex = l.ColIndex; } else { float x1 = Math.Abs(xl - point.X); float x2 = Math.Abs(xg - point.X); if (x1 > x2) { colIndex = g.ColIndex; } else { colIndex = l.ColIndex; } } } return(colIndex); }
public Line() { //Initalize variables nWidthColumn = 20; nWidthLine = 2F; nDiagonalPoint = 2; layoutMode = LayoutMode.LineAndPoint; pointType = PointType.Circle; bMouseEnterPoint = false; pointLocation = new List <ItemPointInfo>(); //For not throw exception,If mouse move event fire itemLastPointEnter = new ItemPointInfo(); verticalGrid = HorizontalGridMode.None; bColorOverTransparency = false; }
private object EstimateByNearestColumns(Point point, IBindingData bd) { NearestColumnsIndex(point, out int befor, out int after); if (befor == -1 && after == -1) { return(null); } if (befor == -1) { return(bd.ColumnValue(pointLocation[after].ColIndex)); } if (after == -1) { return(bd.ColumnValue(pointLocation[befor].ColIndex)); } if (after == befor) { return(bd.ColumnValue(pointLocation[after].ColIndex)); } ItemPointInfo l = pointLocation[befor]; ItemPointInfo g = pointLocation[after]; double x1 = l.Center.X; double x2 = g.Center.X; double y1 = bd.ColumnValue(l.ColIndex); double y2 = bd.ColumnValue(g.ColIndex); double x = point.X; double y = 0; if (x1 == x2) //Not equal but for .... { y = y1; } else { double m = (y1 - y2) / (x1 - x2); y = m * (x - x1) + y1; } return(bd.CalculateColumnValue(y)); }
protected override void InternalPaintFinish(Graphics grChart, IBindingData data) { //TODO : active VerticalGrid in BackOfData if (this.VerticalGrid != HorizontalGridMode.None) { Pen penGrid = new Pen(base.GridColor, 1); AxileBase.SetDashStyle(penGrid, this.GridDashStyle); int wBegin = (int)this.DrawArea.Top; int wEnd = (int)this.DrawArea.Bottom; foreach (AxileLabelText dt in LabelsX) { ItemPointInfo ipi = new ItemPointInfo { ColIndex = -1 }; foreach (ItemPointInfo ipi2 in pointLocation) { if (ipi2.ColIndex == dt.ColumnIndex) { ipi = ipi2; break; } } if (ipi.ColIndex != -1) { int x = (int)(ipi.Center.X); grChart.DrawLine(penGrid, x, wBegin, x, wEnd); } else { grChart.FillRectangle(Brushes.DarkKhaki, dt.Rectangle); } } penGrid.Dispose(); } }
private void Chart_MouseMove(object sender, MouseEventArgs e) { if (rcChartDrawArea.Contains(e.Location)) { //Line enter exit if (bMouseEnterPoint) { if (!itemLastPointEnter.Rect.Contains(e.Location)) { bMouseEnterPoint = false; OnMouseLeavePoint(itemLastPointEnter.ColIndex, itemLastPointEnter.RowIndex); itemLastPointEnter.ColIndex = itemLastPointEnter.RowIndex = -1; } } if (!bMouseEnterPoint) { foreach (ItemPointInfo item in pointLocation) { if (item.Rect.Contains(e.Location)) { bMouseEnterPoint = true; itemLastPointEnter = item; OnMouseEnterPoint(itemLastPointEnter.ColIndex, itemLastPointEnter.RowIndex); break; } } } } else { if (bMouseEnterPoint) { bMouseEnterPoint = false; OnMouseLeavePoint(itemLastPointEnter.ColIndex, itemLastPointEnter.RowIndex); } } }
/// <summary> /// Find nearest columns of existing columns for the defined point /// </summary> /// <param name="point">The point that we want nearest columns</param> /// <param name="columnBefor">Column befor of the point</param> /// <param name="columnAfter">Column after of the point</param> public void NearestColumnsIndex(Point point, out int columnBefor, out int columnAfter) { columnBefor = -1; columnAfter = -1; if (pointLocation.Count != 0) { ItemPointInfo l = pointLocation[0]; ItemPointInfo g = pointLocation[pointLocation.Count - 1]; float xl = l.Center.X; float xg = g.Center.X; bool bR2L = Parent.Chart.IsRTL(); if (bR2L) { ItemPointInfo e = l; l = g; g = e; float xe = xl; xl = xg; xg = xe; } if (l.Center.X == point.X) { columnBefor = l.ColIndex; columnAfter = l.ColIndex; } else if (g.Center.X == point.X) { columnBefor = g.ColIndex; columnAfter = g.ColIndex; } else if (l.Center.X > point.X) { columnBefor = -1; columnAfter = l.ColIndex; } else if (g.Center.X < point.X) { columnBefor = g.ColIndex; columnAfter = -1; } else { foreach (ItemPointInfo ipi in pointLocation) { float x = ipi.Center.X; if (x > xl && x <= point.X) { l = ipi; xl = x; } if (x < xg && x >= point.X) { g = ipi; xg = x; } } if (xl == xg) { columnBefor = columnAfter = l.ColIndex; } else { columnBefor = l.ColIndex; columnAfter = g.ColIndex; if (bR2L) { int ebf = columnBefor; columnBefor = columnAfter; columnAfter = ebf; } } } } }
protected override void InternalPaintChart(Graphics grChart, IBindingData data, Rectangle rcArea, ref double xAxisBetweenTextSpace, ref double xAxisTextWidth ) { bool bR2L = Parent.Chart.IsRTL(); //Draw lines and points if ((int)rcChartDrawArea.Height > 0 && (int)rcChartDrawArea.Width > 0) { double maxminHeight = (double)(rcChartDrawArea.Height / (MaxInTopOfChartArea - MinInBottomOfChartArea)); double maxOfValueInX = data.ColumnValue(data.ColumnCount - 1); //Use in ColumnScaling mode double xScale = 1; if (!ColumnScaling) { if (SizingModeLabel == SizingModeLabel.Fit || SizingModeLabel == SizingModeLabel.FitIfLarge) { double x = 0; x += data.ColumnCount * nWidthColumn; xScale = rcChartDrawArea.Width / x; } if (SizingModeLabel == SizingModeLabel.FitIfLarge && xScale > 1) { xScale = 1; } xAxisBetweenTextSpace = 0; xAxisTextWidth = nWidthColumn * xScale; } else { double x = 0; x = maxOfValueInX * nWidthColumn; xScale = rcChartDrawArea.Width / x; xAxisBetweenTextSpace = 0; xAxisTextWidth = nWidthColumn * xScale; } Color[] colLineInner = new Color[Parent.Chart.Colors.Length]; for (int i = 0; i < colLineInner.Length; i++) { colLineInner[i] = Parent.Chart.Colors[i]; } if (ColorOverTransparency && data.RowCount > 1) { for (int i = 0; i < colLineInner.Length && i < data.RowCount; i++) { int minTransparency = 128; if (data.RowCount > 3) { minTransparency = 64; } double def = 255 - minTransparency; def /= (data.RowCount - 1); double ri = 255 - def * i; colLineInner[i] = Color.FromArgb((byte)ri, colLineInner[i].R, colLineInner[i].G, colLineInner[i].B); } } Brush[] brPoint = new Brush[colLineInner.Length]; Pen[] penLine = new Pen[colLineInner.Length]; for (int i = 0; i < colLineInner.Length; i++) { brPoint[i] = new SolidBrush(colLineInner[i]); penLine[i] = new Pen(colLineInner[i], nWidthLine) { LineJoin = LineJoin.Bevel //If use default (Miter) may be infringe }; } double nWidthColumnWithScale = (nWidthColumn * xScale); RectangleF rcChartDrawArea2 = new RectangleF( rcChartDrawArea.Left - nDiagonalPoint, rcChartDrawArea.Top - nDiagonalPoint, rcChartDrawArea.Width + nDiagonalPoint * 2, rcChartDrawArea.Height + nDiagonalPoint * 2 ); for (int rowIndex = 0; rowIndex < data.RowCount; rowIndex++) { drawAllDataField = true; double xDraw = 0; int curConntOfPointLocation = pointLocation.Count; List <PointF> lst = new List <PointF>(); List <List <PointF> > lstLines = new List <List <PointF> >(); int columnCount = data.ColumnCount; int columnCount2 = columnCount / 2; for (int colIndex = 0; colIndex < columnCount; colIndex++) { if (!drawAllDataField) { break; } #region Calculate points if (ColumnScaling) { xDraw = data.ColumnValue(colIndex) * nWidthColumnWithScale + xAxisBetweenTextSpace; } double d = data.ValueDouble(rowIndex, colIndex); bool isOverflow = false; double yDrawCenterOfPoint = ((d - MinInBottomOfChartArea) * maxminHeight); if (d > MaxInTopOfChartArea) { isOverflow = true; d = MaxInTopOfChartArea; yDrawCenterOfPoint = ((d - MinInBottomOfChartArea) * maxminHeight); } if (d < MinInBottomOfChartArea) { isOverflow = true; d = MinInBottomOfChartArea; yDrawCenterOfPoint = ((d - MinInBottomOfChartArea) * maxminHeight); } yDrawCenterOfPoint = rcChartDrawArea.Bottom - yDrawCenterOfPoint; RectangleF rcPoint; double xDrawCenterOfPoint = 0; if (bR2L) { xDrawCenterOfPoint = rcChartDrawArea.Right - xDraw; if (!ColumnScaling)//In ColumnScaling is ignore { xDrawCenterOfPoint -= nWidthColumnWithScale / 2; } } else { xDrawCenterOfPoint = xDraw + rcChartDrawArea.Left; if (!ColumnScaling)//In ColumnScaling is ignore { xDrawCenterOfPoint += nWidthColumnWithScale / 2; } } rcPoint = new RectangleF( (float)xDrawCenterOfPoint - nDiagonalPoint, (float)yDrawCenterOfPoint - nDiagonalPoint, nDiagonalPoint * 2, nDiagonalPoint * 2); PointF center = new PointF( (float)xDrawCenterOfPoint, (float)yDrawCenterOfPoint); if (rcChartDrawArea2.Contains(center)) { ItemPointInfo item = new ItemPointInfo { Rect = rcPoint, Center = center }; bool bValidValue = data.Valid(rowIndex, colIndex); if (bValidValue) { lst.Add(item.Center); } else { if (lst.Count > 0) { lstLines.Add(lst); lst = new List <PointF>(); } } item.IsOverflow = isOverflow; item.ColIndex = colIndex; item.RowIndex = rowIndex; pointLocation.Add(item); } else { if (pointLocation.Count > 0 && curConntOfPointLocation != pointLocation.Count) {//If Point is Out,Draw line that is seen ItemPointInfo item = pointLocation[pointLocation.Count - 1]; bool bValidValue = data.Valid(rowIndex, colIndex); if (bValidValue) { PointF centerBeginPaint = new PointF(); float m = (center.X - item.Center.X); if (m == 0f) { centerBeginPaint.X = center.X; //Exactly : center.Y != item.center.Y if (center.Y < item.Center.Y) { centerBeginPaint.Y = rcChartDrawArea2.Bottom; } else { centerBeginPaint.Y = rcChartDrawArea2.Top; } } else { float x = (bR2L ? rcChartDrawArea2.Left : rcChartDrawArea2.Right); m = (center.Y - item.Center.Y) / m; centerBeginPaint.X = x; centerBeginPaint.Y = m * (x - center.X) + center.Y; } lst.Add(centerBeginPaint); } } drawAllDataField = false; break; } if (!ColumnScaling) { xDraw += nWidthColumnWithScale; } #endregion } if (lst.Count > 0) { lstLines.Add(lst); lst = null; } Graphics gr = grChart; //Draw Lines if (layoutMode != LayoutMode.Point) { foreach (List <PointF> lst2 in lstLines) { if (lst2.Count > 1) { gr.DrawLines(penLine[rowIndex % colLineInner.Length], lst2.ToArray()); } } } //Draw Points if (layoutMode != LayoutMode.Line) { for (int i = curConntOfPointLocation; i < pointLocation.Count; i++) { ItemPointInfo item = pointLocation[i]; bool bValidValue = data.Valid(item.RowIndex, item.ColIndex); if (!bValidValue) { continue; //No need Draw } int selectedLineColor = item.RowIndex % colLineInner.Length; if (pointType == PointType.Square) { gr.FillRectangle(brPoint[selectedLineColor], item.Rect); } else if (pointType == PointType.Circle) { gr.FillEllipse(brPoint[selectedLineColor], item.Rect); } } } } for (int i = 0; i < colLineInner.Length; i++) { brPoint[i].Dispose(); penLine[i].Dispose(); } } }