Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

Problem to draw a vertical line to chart

$
0
0

Hello, 

I am using the chart component and tries to add a vertical line.

1. I first call "createchart()" to create 3 horizontal lines, which works fine.

2. I then call "createverticalline()" in order to Add a vertical line to the chart. No vertical line is shown in the chart which is the problem.

However, if I ONLY call "createverticalline()", then a vertical line is shown in the chart.

So the functions do work but I can't understand why the vertical line is not shown after I have"createchart()" ?

        private void button6_Click(object sender, EventArgs e)
        {
            createchart();
            createverticalline(); //Run ONLY this and a vertical line will be shown?
        }
        void createchart()
        {
            chart1.ChartAreas["ChartArea1"].AxisX.MajorGrid.LineColor = Color.Gainsboro;
            chart1.ChartAreas["ChartArea1"].AxisY.MajorGrid.LineColor = Color.Gainsboro;

            Series series = new Series(); int countlines = 0; DateTime dt = DateTime.Now;
            chart1.Series.Clear();
            for (int i = 1; i < 4; i++) //3 different lines
            {
                countlines++;
                series = new Series();
                series.Name = "Series" + countlines.ToString();
                series.Color = System.Drawing.Color.DarkBlue;

                series.IsVisibleInLegend = false;
                series.IsXValueIndexed = false;
                series.ChartType = SeriesChartType.Line;
                this.chart1.Series.Add(series);

                dt = DateTime.Now; dt = dt.AddDays(-1);
                for (int i2 = 0; i2 < 10; i2++)
                {
                    dt = dt.AddDays(1);
                    series.Points.AddXY(dt, (50 * i));
                }
            }

            DateTime minDate = DateTime.Now;
            DateTime maxDate = minDate.AddDays(8);
            chart1.ChartAreas[0].AxisY.Maximum = 200;
            chart1.ChartAreas[0].AxisY.Minimum = 0;
            chart1.ChartAreas[0].AxisX.Minimum = 0;
            chart1.Invalidate();
            chart1.ChartAreas[0].AxisY.RoundAxisValues();
            chart1.ChartAreas[0].AxisX.Title = "Date";
            chart1.ChartAreas[0].AxisY.Title = Foundcurrency;
            chart1.Series[0].XValueType = ChartValueType.DateTime; chart1.ChartAreas[0].AxisX.LabelStyle.Format = "MM-dd";
            chart1.ChartAreas[0].AxisX.Interval = 1;
            chart1.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Days;
            chart1.ChartAreas[0].AxisX.IntervalOffset = 1;
            chart1.Series[0].XValueType = ChartValueType.DateTime;
            chart1.ChartAreas[0].AxisX.Minimum = minDate.ToOADate();
            chart1.ChartAreas[0].AxisX.Maximum = maxDate.ToOADate();
        }
        void createverticalline()
        {
            // Note that directly after adding points this will return NaN:
            double maxDataPoint = chart1.ChartAreas[0].AxisY.Maximum;
            double minDataPoint = chart1.ChartAreas[0].AxisY.Minimum;

            //the vertical line
            VerticalLineAnnotation annotation2 = new VerticalLineAnnotation();
            annotation2.IsSizeAlwaysRelative = false;
            annotation2.AllowMoving = true;
            annotation2.IsInfinitive = true;
            annotation2.Name = "myLine";
            annotation2.X = 0;
            annotation2.AxisX = chart1.ChartAreas[0].AxisX;
            annotation2.AxisY = chart1.ChartAreas[0].AxisY;
            annotation2.AnchorY = minDataPoint;
            annotation2.Height = maxDataPoint - minDataPoint;
            annotation2.Width = 2;
            annotation2.LineWidth = 2;
            annotation2.StartCap = LineAnchorCapStyle.None;
            annotation2.EndCap = LineAnchorCapStyle.None;
            annotation2.AnchorX = 1;  // <- your point
            annotation2.LineColor = Color.Red; // <- your color
            chart1.Annotations.Add(annotation2);
            chart1.Update();
        }



Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>