private bool HandlePointValues(PointF mousePt, ZedGraphControl sender) { int iPt; GraphPane pane; object nearestObj; using (Graphics g = this.CreateGraphics()) { if (sender.MasterPane.FindNearestPaneObject(mousePt, g, out pane, out nearestObj, out iPt)) { if (nearestObj is CurveItem && iPt >= 0) { CurveItem curve = (CurveItem)nearestObj; // Provide Callback for User to customize the tooltips PointPair pt = curve.Points[iPt]; if (pt.Tag is string) { this.toolTip1.SetToolTip(this, (string)pt.Tag); } else { double xVal, yVal, lowVal; ValueHandler valueHandler = new ValueHandler(pane, false); if ((curve is BarItem || curve is ErrorBarItem || curve is HiLowBarItem) && pane.BarSettings.Base != BarBase.X) { valueHandler.GetValues(curve, iPt, out yVal, out lowVal, out xVal); } else { valueHandler.GetValues(curve, iPt, out xVal, out lowVal, out yVal); } string xStr = MakeValueLabel(curve.GetXAxis(pane), xVal, iPt, curve.IsOverrideOrdinal); string yStr = MakeValueLabel(curve.GetYAxis(pane), yVal, iPt, curve.IsOverrideOrdinal); this.toolTip1.SetToolTip(sender, xStr + "\n" + yStr); this.toolTip1.ForeColor = curve.Color; this.toolTip1.BackColor = Color.Black; toolTip1.Active = true; //this.pointToolTip.SetToolTip( this, // curve.Points[iPt].ToString( this.pointValueFormat ) ); } } else { toolTip1.Active = false; } } else { toolTip1.Active = false; } //g.Dispose(); } return(true); }
protected override void DrawSingleBar(Graphics g, GraphPane pane, CurveItem curve, int index, int pos, Axis baseAxis, Axis valueAxis, float barWidth, float scaleFactor) { base.DrawSingleBar(g, pane, curve, index, pos, baseAxis, valueAxis, barWidth, scaleFactor); PointPair pointPair = curve.Points[index]; MiddleErrorTag middleError = pointPair.Tag as MiddleErrorTag; if (pointPair.IsInvalid || middleError == null) { return; } double curBase, curLowVal, curHiVal; ValueHandler valueHandler = new ValueHandler(pane, false); valueHandler.GetValues(curve, index, out curBase, out curLowVal, out curHiVal); double middleValue = middleError.Middle; float pixBase = baseAxis.Scale.Transform(curve.IsOverrideOrdinal, index, curBase); float pixLowBound = valueAxis.Scale.Transform(curLowVal) - 1; float pixHiBound = valueAxis.Scale.Transform(curHiVal); float pixError = (float)Math.Abs((pixLowBound - pixHiBound) / (curLowVal - curHiVal) * middleError.Error); float clusterWidth = pane.BarSettings.GetClusterWidth(); //float barWidth = curve.GetBarWidth( pane ); float clusterGap = pane.BarSettings.MinClusterGap * barWidth; float barGap = barWidth * pane.BarSettings.MinBarGap; // Calculate the pixel location for the side of the bar (on the base axis) float pixSide = pixBase - clusterWidth / 2.0F + clusterGap / 2.0F + pos * (barWidth + barGap); float pixMiddleValue = valueAxis.Scale.Transform(curve.IsOverrideOrdinal, index, middleValue); // Draw the bar if (pane.BarSettings.Base == BarBase.X) { if (barWidth >= 3 && middleError.Error > 0) { // Draw whiskers float pixLowError = Math.Min(pixLowBound, pixMiddleValue + pixError / 2); float pixHiError = Math.Max(pixHiBound, pixLowError - pixError); pixLowError = Math.Min(pixLowBound, pixHiError + pixError); float pixMidX = (float)Math.Round(pixSide + barWidth / 2); // Line g.DrawLine(MiddlePen, pixMidX, pixHiError, pixMidX, pixLowError); if (barWidth >= PIX_TERM_WIDTH) { // Ends float pixLeft = pixMidX - (float)Math.Round(PIX_TERM_WIDTH / 2); float pixRight = pixLeft + PIX_TERM_WIDTH - 1; g.DrawLine(MiddlePen, pixLeft, pixHiError, pixRight, pixHiError); g.DrawLine(MiddlePen, pixLeft, pixLowError, pixRight, pixLowError); } } g.DrawLine(MiddlePen, pixSide, pixMiddleValue, pixSide + barWidth, pixMiddleValue); } else { if (barWidth >= 3 && middleError.Error > 0) { // Draw whiskers float pixHiError = Math.Min(pixHiBound, pixMiddleValue + pixError / 2); float pixLowError = Math.Max(pixLowBound, pixHiError - pixError); pixHiError = Math.Min(pixHiBound, pixLowError + pixError); float pixMidY = (float)Math.Round(pixSide + barWidth / 2); // Line g.DrawLine(MiddlePen, pixLowError, pixMidY, pixHiError, pixMidY); if (barWidth >= PIX_TERM_WIDTH) { // Ends float pixTop = pixMidY - (float)Math.Round(PIX_TERM_WIDTH / 2); float pixBottom = pixTop + PIX_TERM_WIDTH - 1; g.DrawLine(MiddlePen, pixHiError, pixTop, pixHiError, pixBottom); g.DrawLine(MiddlePen, pixLowError, pixTop, pixLowError, pixBottom); } } g.DrawLine(MiddlePen, pixMiddleValue, pixSide, pixMiddleValue, pixSide + barWidth); } }
private Point HandlePointValues(Point mousePt) { GraphPane pane; using (Graphics g = CreateGraphics()) { int iPt; object nearestObj; if (_masterPane.FindNearestPaneObject(mousePt, g, out pane, out nearestObj, out iPt)) { if (nearestObj is CurveItem && iPt >= 0) { CurveItem curve = (CurveItem) nearestObj; // Provide Callback for User to customize the tooltips if (PointValueEvent != null) { string label = PointValueEvent(this, pane, curve, iPt); if (!string.IsNullOrEmpty(label)) { pointToolTip.SetToolTip(this, label); pointToolTip.Active = true; } else pointToolTip.Active = false; } else { if (curve is PieItem) { pointToolTip.SetToolTip(this, ((PieItem) curve).Value.ToString(_pointValueFormat)); } // else if ( curve is OHLCBarItem || curve is JapaneseCandleStickItem ) // { // StockPt spt = (StockPt)curve.Points[iPt]; // this.pointToolTip.SetToolTip( this, ( (XDate) spt.Date ).ToString( "MM/dd/yyyy" ) + "\nOpen: $" + // spt.Open.ToString( "N2" ) + // "\nHigh: $" + // spt.High.ToString( "N2" ) + "\nLow: $" + // spt.Low.ToString( "N2" ) + "\nClose: $" + // spt.Close.ToString // ( "N2" ) ); // } else { PointPair pt = curve.Points[iPt]; if (pt.Tag is string) pointToolTip.SetToolTip(this, (string) pt.Tag); else { double xVal, yVal, lowVal; ValueHandler valueHandler = new ValueHandler(pane, false); if ((curve is BarItem || curve is ErrorBarItem || curve is HiLowBarItem) && pane.BarSettings.Base != BarBase.X) valueHandler.GetValues(curve, iPt, out yVal, out lowVal, out xVal); else valueHandler.GetValues(curve, iPt, out xVal, out lowVal, out yVal); string xStr = MakeValueLabel(curve.GetXAxis(pane), xVal, iPt, curve.IsOverrideOrdinal); string yStr = MakeValueLabel(curve.GetYAxis(pane), yVal, iPt, curve.IsOverrideOrdinal); pointToolTip.SetToolTip(this, "( " + xStr + ", " + yStr + " )"); //this.pointToolTip.SetToolTip( this, // curve.Points[iPt].ToString( this.pointValueFormat ) ); } } pointToolTip.Active = true; } } else pointToolTip.Active = false; } else pointToolTip.Active = false; //g.Dispose(); } return mousePt; }
/// <summary> /// Create a TextLabel for each bar in the GraphPane. /// Call this method only after calling AxisChange() /// </summary> /// <remarks> /// This method will go through the bars, create a label that corresponds to the bar value, /// and place it on the graph depending on user preferences. This works for horizontal or /// vertical bars in clusters or stacks.</remarks> /// <param name="pane">The GraphPane in which to place the text labels.</param> /// <param name="isBarCenter">true to center the labels inside the bars, false to /// place the labels just above the top of the bar.</param> /// <param name="valueFormat">The double.ToString string format to use for creating /// the labels /// </param> private void CreateBarLabels(GraphPane pane, bool isBarCenter, string valueFormat) { bool isVertical = pane.BarSettings.Base == BarBase.X; // Make the gap between the bars and the labels = 2% of the axis range float labelOffset; if (isVertical) { labelOffset = (float)(pane.YAxis.Scale.Max - pane.YAxis.Scale.Min) * 0.02f; } else { labelOffset = (float)(pane.XAxis.Scale.Max - pane.XAxis.Scale.Min) * 0.02f; } // keep a count of the number of BarItems int curveIndex = 0; // Get a valuehandler to do some calculations for us ValueHandler valueHandler = new ValueHandler(pane, true); // Loop through each curve in the list foreach (CurveItem curve in pane.CurveList) { // work with BarItems only BarItem bar = curve as BarItem; if (bar != null) { IPointList points = curve.Points; // Loop through each point in the BarItem for (int i = 0; i < points.Count; i++) { // Get the high, low and base values for the current bar // note that this method will automatically calculate the "effective" // values if the bar is stacked double baseVal, lowVal, hiVal; valueHandler.GetValues(curve, i, out baseVal, out lowVal, out hiVal); // Get the value that corresponds to the center of the bar base // This method figures out how the bars are positioned within a cluster float centerVal = (float)valueHandler.BarCenterValue(bar, bar.GetBarWidth(pane), i, baseVal, curveIndex); // Create a text label -- note that we have to go back to the original point // data for this, since hiVal and lowVal could be "effective" values from a bar stack string barLabelText = (isVertical ? points[i].Y : points[i].X).ToString(valueFormat); // Calculate the position of the label -- this is either the X or the Y coordinate // depending on whether they are horizontal or vertical bars, respectively float position; if (isBarCenter) { position = (float)(hiVal + lowVal) / 2.0f; } else { position = (float)hiVal + labelOffset; } // Create the new TextObj TextObj label; if (isVertical) { label = new TextObj(barLabelText, centerVal, position); } else { label = new TextObj(barLabelText, position, centerVal); } // Configure the TextObj label.Location.CoordinateFrame = CoordType.AxisXYScale; label.FontSpec.Size = 12; label.FontSpec.FontColor = Color.Black; label.FontSpec.Angle = isVertical ? 90 : 0; label.Location.AlignH = isBarCenter ? AlignH.Center : AlignH.Left; label.Location.AlignV = AlignV.Center; label.FontSpec.Border.IsVisible = false; label.FontSpec.Fill.IsVisible = false; // Add the TextObj to the GraphPane pane.GraphObjList.Add(label); } } curveIndex++; } }