kimliv
kimliv

Reputation: 427

C# StripLine (chart) drawing by click onto the chart

community,

this is my first question here. to all the other problems i had there was often a answer on this side. for this i did not find one. here is my problem:

i have a chart and i want to add a vertical starpline at the point i click the mouse on the chart. this i working so far with that code:

chartSettlingCurve.ChartAreas[0].CursorX.SetCursorPixelPosition(new Point(e.X, e.Y), true);
double pX = chartSettlingCurve.ChartAreas[0].CursorX.Position; //X Axis coordinate Mouse
            DataPoint dataPoint = chartSettlingCurve.Series[0].Points.FindByValue(nearestPreceedingValue(nearestPreceedingValue(pX)), "X");
            labelTab3Test.Text = Convert.ToString(dataPoint);

            DataPoint maxValuePoint = chartSettlingCurve.Series[0].Points.FindMaxByValue();

            StripLine stripLineEnd = new StripLine();
            stripLineEnd.BorderColor = Color.Blue;
            stripLineEnd.IntervalOffset = dataPoint.XValue;
            stripLineEnd.Text = "End";

this code generates a stripline at every point i click on the graph but i want just one line. so if ther still is a strip line it shopud be replaced by the new one at a other position.

woud be nice if you can help me. if you dont get my problem pleas ask.

thanks

Upvotes: 0

Views: 3815

Answers (2)

srkn sltrk
srkn sltrk

Reputation: 418

You can remove all striplines using code below;

        while (chartSettlingCurve.ChartAreas[0].AxisX.StripLines.Count > 0)
        {
            chartSettlingCurve.ChartAreas[0].AxisX.StripLines.Remove(hartSettlingCurve.ChartAreas[0].AxisX.StripLines.Last());   
        }

than you can add some code.

Upvotes: 0

kimliv
kimliv

Reputation: 427

a working solution is:

            for (int j = 0; j < chartSettlingCurve.ChartAreas[0].AxisX.StripLines.Count; j++)
            {
                if (chartSettlingCurve.ChartAreas[0].AxisX.StripLines[j].Tag.ToString() == "end")
                {
                    chartSettlingCurve.ChartAreas[0].AxisX.StripLines.RemoveAt(j);
                    j--;
                }
            }

or as an alternative:

            bool loop = true;
            while (loop)
            {
                loop = false;
                foreach (var element in chartSettlingCurve.ChartAreas[0].AxisX.StripLines)
                {
                    if (element.Tag.ToString() == "end")
                    {
                        chartSettlingCurve.ChartAreas[0].AxisX.StripLines.Remove(element);
                        loop = true;
                        break;
                    }
                }
            }

Upvotes: 1

Related Questions