1

I am trying to implement scroll bar along x-axis, but i failed , can any one help please......This is my code:

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        Chart chart2 = new Chart();
        Chart chart3 = new Chart();
        int[] rand;
        int[] rand1;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
    {

        chart2.Width = 1000 ;
        chart2.Height = 200;
        this.Controls.Add(chart2);
        this.Controls.Add(chart3);

        chart2.MouseMove += new MouseEventHandler(chart2_MouseMove);
       //chart2.GetToolTipText += new EventHandler<ToolTipEventArgs>(chart2_GetToolTipText);

        chart2.ChartAreas.Add("Area");




        chart2.ChartAreas[0].CursorX.AutoScroll = true;
        chart2.ChartAreas[0].CursorX.Interval = 1;

        chart2.ChartAreas[0].AxisX.LabelAutoFitStyle = LabelAutoFitStyles.None;
        chart2.ChartAreas[0].AxisX.IsStartedFromZero = true;
        chart2.ChartAreas[0].AxisX.Minimum = 0;

        chart2.ChartAreas[0].AxisX.ScaleView.SizeType = DateTimeIntervalType.Number;
        chart2.ChartAreas[0].AxisX.MajorTickMark.Interval = .5;
        chart2.ChartAreas[0].AxisX.ScrollBar.IsPositionedInside = true;
        chart2.ChartAreas[0].AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.SmallScroll;
        chart2.ChartAreas[0].AxisX.ScaleView.SmallScrollSize = 100;
        chart2.ChartAreas[0].AxisX.ScrollBar.Enabled = true;


        chart2.ChartAreas[0].AxisY.IsStartedFromZero = true;

        int[] X = new int[100];
        for (int i = 0; i < 50; i++)
        {
             X[i] = i+1;
        }
        int[] Y = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        chart2.Series.Add("Alpha");
        chart2.Series.Add("Beta");
        chart2.Series["Alpha"].ChartType = SeriesChartType.Line;
        chart2.Series["Beta"].ChartType = SeriesChartType.Line;

        chart2.Series["Alpha"].Color = Color.Blue;
        chart2.Series["Beta"].Color = Color.DarkGreen;
        chart2.Series["Alpha"].XValueType = ChartValueType.Double;
        chart2.Series["Beta"].XValueType = ChartValueType.Double;
        //chart2.Series[0].


        chart2.Series["Alpha"].YValueType = ChartValueType.Double;
        chart2.Series["Beta"].YValueType = ChartValueType.Double;


        Legend leg = new Legend();
        chart2.Legends.Add(leg);
        chart2.Legends[0].Alignment = StringAlignment.Near;



        Random _r = new Random();
        rand = new int[1000];
        rand1 = new int[1000];
        for (int i = 0; i < X.Length; i++)
        {
            int n = _r.Next(0, 100);
            rand[i] = n;
            chart2.Series["Alpha"].Points.AddY(n);
        }

        for (int i = 0; i < X.Length; i++)
        {
            int n = _r.Next(0, 100);
            rand1[i] = n;
            chart2.Series["Beta"].Points.AddY(n);
        }
        chart2.Series[0].ToolTip = "X = #VALX, Y= #VALY";
        chart2.Series[1].ToolTip = "X = #VALX, Y= #VALY";

    }

    private void chart2_Click(object sender, EventArgs e)
    {

    }

    private void chart2_MouseMove(object sender, MouseEventArgs e)
    {
        HitTestResult htr = this.chart2.HitTest (e.X, e.Y);
        Point p = new Point(e.X, e.Y);
        int K = Convert.ToInt32(p.X);
        chart2.ChartAreas[0].CursorX.Interval = 0;
        chart2.ChartAreas[0].CursorX.SetCursorPixelPosition(p,true);
        //chart2.ChartAreas[0].CursorY.SetCursorPixelPosition(p, true);
        //Label Xlbldisplay = new Label();
        //Xlbldisplay.Text = "X";

        //Label Ylbldisplay = new Label();
        //Ylbldisplay.Text = "Y";
        chart2.Series[0].ToolTip = "X = #VALX, Y= #VALY";
        //string p = chart2.GetToolTipText;
        if (Convert.ToInt32(chart2.ChartAreas[0].CursorX.Position) <= 1000)
        {
            aXdisplay.Text = Convert.ToString(Convert.ToInt32(chart2.ChartAreas[0].CursorX.Position));

            aYdisplay.Text = Convert.ToString(rand[Convert.ToInt32(aXdisplay.Text)]);
            bXdisplay.Text = aXdisplay.Text;
            bYdisplay.Text = Convert.ToString(rand1[Convert.ToInt32(aXdisplay.Text)]);
        }



    }

    //private void chart2_GetToolTipText(object sender, ToolTipEventArgs e)
    //{
    //    HitTestResult rt = chart2.HitTest(e.X, e.Y);
    //    if(rt.ChartElementType == ChartElementType.DataPoint)
    //    {
    //        Xdisplay.Text = chart2.Series[0].Points[rt.PointIndex].XValue.ToString();

    //        Ydisplay.Text = chart2.Series[0].Points[rt.PointIndex].YValues[0].ToString();
    //    }
    //    //Ydisplay.Text = e.Y.ToString();
    //}

    //private void chart2_CursorPositionChanged(object sender, CursorEventArgs e)
    //{
    //    //HitTestResult htr = this.chart2.HitTest(e.X, e.Y);
    //    //Point p = new Point(e.X, e.Y);

    //    //chart2.ChartAreas[0].CursorX.Interval = 0;
    //    //chart2.ChartAreas[0].CursorX.SetCursorPixelPosition(p, true);
    //    //chart2.ChartAreas[0].CursorY.SetCursorPixelPosition(p, true);
    //}







}

}

CodeMonkey
  • 11,196
  • 30
  • 112
  • 203
dkt
  • 11
  • 2
  • You need to tag this question for the platform (OS and/or language and/or libraries) you are using; this will increase the chances of it being found by someone who can help. The [scroll] tag may not help much here. – PJTraill Jun 13 '15 at 10:16
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – Bernhard Barker Jun 13 '15 at 11:58
  • Wow That is strange. Perhaps narrowing it down by starting with this answer: https://stackoverflow.com/questions/5141126/adding-a-scroll-bar-to-ms-chart-control-c-sharp and then going from there. – SteveFerg Jun 13 '15 at 23:57

0 Answers0