Chart move the x and y axes

MattLife

Member
Joined
Oct 22, 2019
Messages
7
Programming Experience
1-3
Hi guys, I need your help.
I want to draw graph in c#.
I use windowsform chart to do this.
But I would need to move the x and y axes, and the graph should be drawn accordingly and not limited to a fixed value, and I need to zoom. I tried using LiveCharts, but their codes are too complex for me. And a classic canvas would be very demanding on algorithmization.
I need someone to guide me;
73413202_2863856810293826_6817898486592176128_n.jpg
72920867_2863856746960499_865546892098928640_n.jpg
 
Wouldn't you just need to play with the minimum and maximum values of the axes of the Chart control to effectively move the origin as well give the effect of zooming in and out?

Why don't you share your current code where you are using the WinForms Chart control?
 
Wouldn't you just need to play with the minimum and maximum values of the axes of the Chart control to effectively move the origin as well give the effect of zooming in and out?

Why don't you share your current code where you are using the WinForms Chart control?
I shared what I have, I just add value to chart with cycle because I don't know how to continue.
 
Do not post pictures of code. We can not edit it nor can you expect us to write it out from the picture you've uploaded. If you would like help with your current code, then post your code on the forums, and be sure to enclose your code wrapped in code tags. [CODE=csharp]Your code here[/CODE]
 
Do not post pictures of code. We can not edit it nor can you expect us to write it out from the picture you've uploaded. If you would like help with your current code, then post your code on the forums, and be sure to enclose your code wrapped in code tags. [CODE=csharp]Your code here[/CODE]
Vrchol is Peak

C#:
public void Vykresli()
        {

            graf.Series["Kvadraticka funkce"].Points.Clear();

            if (a > 0)
            {
                double VrcholX = 0 - (b / 2);//
                double VrcholY = a * VrcholX * VrcholX + b * VrcholX + c;



                for (double x = VrcholX - 3; x <= VrcholX + 3; ++x)
                {
                    double y = a * (x * x) + b * x + c;
                    graf.Series["Kvadraticka funkce"].Points.AddXY(x, y);
                }
            }
            else
            {
                double VrcholX = (b / 2); //
                double VrcholY = a * VrcholX * VrcholX + b * VrcholX + c;


                for (double x = VrcholX - 3 ; x <= VrcholX + 3; ++x)
                {
                    double y = a * (x * x) + b * x + c;
                    graf.Series["Kvadraticka funkce"].Points.AddXY(x, y);
                }
            }
        }
 
If you want to move the points, then you will want to use a Timer or a while loop executed from another thread. I'd go with the preceding suggestion for simplicity.

And where is the rest of your code? There is a ton of code missing...

I suggest following this tutorial and reading the documentation on Chart Class (System.Windows.Forms.DataVisualization.Charting).
 
If you want to move the points, then you will want to use a Timer or a while loop executed from another thread. I'd go with the preceding suggestion for simplicity.

And where is the rest of your code? There is a ton of code missing...

I suggest following this tutorial and reading the documentation on Chart Class (System.Windows.Forms.DataVisualization.Charting).

I need to take values from the X-axis and write to autoupdate to render immediately when changing the values on the x-axis.
Form:
C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using LiveCharts;
using LiveCharts.Defaults;
using LiveCharts.WinForms;
using LiveCharts.Wpf;

namespace Rocnikovy_projekt
{

    public partial class Form1 : Form
    {
        

        public Form1()
        {
            InitializeComponent();
        }

        void ChartLoad()
        {
            var chart = chart1.ChartAreas[0];

            chart.AxisX.IntervalType = System.Windows.Forms.DataVisualization.Charting.DateTimeIntervalType.Number;

            chart.AxisX.LabelStyle.Format = "";
            chart.AxisY.LabelStyle.Format = "";
            chart.AxisX.LabelStyle.IsEndLabelVisible = true;

            chart.AxisX.Minimum = 0;
            chart.AxisY.Minimum = 0;

            chart.AxisX.Interval = 0.5;
            chart.AxisY.Interval = 10;
        }
    


        private void KvaButton1_Click(object sender, EventArgs e)
        {
            
            double x1, x2 = 0;
            KvadratickáFunkce KvaFun = new KvadratickáFunkce(double.Parse(KvaTextBox1.Text), double.Parse(KvaTextBox2.Text), double.Parse(KvaTextBox3.Text), chart1);
            KvaFun.Vykresli();
            x1 = KvaFun.Koreny(out x2);
            if (x1 != x2)
            {
                label1.Text = "Kořen x1 = " + x1;
                label2.Text = "Kořen x2 = " + x2;
            }
            else label1.Text = "Kořen x1 a x2 = " + x1;
            label3.Text = "Vrchol v bodě: " +  KvaFun.VrcholXY();
            label4.Text = "Definiční obor: " + KvaFun.Definicni_obor();
            label5.Text = KvaFun.MaxMin();
            
            
            
        }

        private void KvaTextBox1_TextChanged(object sender, EventArgs e)
        {
            label1.Text = "";
            label2.Text = "";
            label3.Text = "";
            label4.Text = "";
            label5.Text = "";
        }
    }

}
class
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace Rocnikovy_projekt
{
    class KvadratickáFunkce
    {
        private double a;
        private double b;
        private double c;
        private Chart graf;
        private bool maximum = false;

        private double D, x1, x2;


        public KvadratickáFunkce(double a, double b, double c, Chart graf)
        {
            this.a = a;
            this.b = b;
            this.c = c;
            this.graf = graf;
        }
        //****************************************************************
   

     
        public void Vykresli()
        {

            graf.Series["Kvadraticka funkce"].Points.Clear();

            if (a > 0)
            {
                double VrcholX = 0 - (b / 2);//
                double VrcholY = a * VrcholX * VrcholX + b * VrcholX + c;



                for (double x = VrcholX - 3; x <= VrcholX + 3; ++x)
                {
                    double y = a * (x * x) + b * x + c;
                    graf.Series["Kvadraticka funkce"].Points.AddXY(x, y);
                }
            }
            else
            {
                double VrcholX = (b / 2); //
                double VrcholY = a * VrcholX * VrcholX + b * VrcholX + c;


                for (double x = VrcholX - 3 ; x <= VrcholX + 3; ++x)
                {
                    double y = a * (x * x) + b * x + c;
                    graf.Series["Kvadraticka funkce"].Points.AddXY(x, y);
                }
            }
        }
        //************************************************************************
        public double Koreny(out double x2)
        {
            x2 = 0;

            D = b * b - 4 * a * c;
            if (D == 0)
            {
                x1 = -b / (2 * a);
                x2 = x1;

            }
            else if (D > 0)
            {
                x1 = (-b + Math.Sqrt(D)) / (2 * a);
                x2 = (-b - Math.Sqrt(D)) / (2 * a);
            }

            return x1;
        }
        //**************************************************************************
        public string VrcholXY()
        {
            bool maximum = false;
            double VrcholX = 0;
            double VrcholY = 0;
            if (a > 0)
            {
                VrcholX = 0 - (b / 2);//
                VrcholY = a * VrcholX * VrcholX + b * VrcholX + c;
                return "[" + VrcholX + ";" + VrcholY + "]";
                maximum = true;
             
            }

            else
            {
                VrcholX = (b / 2); //
                VrcholY = a * VrcholX * VrcholX + b * VrcholX + c;
                return "[" + VrcholX + ";" + VrcholY + "]";
            }
        }
        //*****************************************************************************
        public string Definicni_obor()
        {
            if(a>0)
            {
                return "<" + (c - ((b * b / (4 * a)))) + ";∞)";
            }
            else
            {
                return "(-∞;" + (c - ((b * b / (4 * a)))) + ">";
            }
        }
        //****************************************************************************
        public string MaxMin()
        {
            if (maximum) return "V bodě x = " + (-(b / 2 / a) + " má funkce minimum \nFunkce nemá maximum");
            else return "V bodě x = " + (-(b / 2 / a) + " má funkce maximum \nFunkce nemá minimum");
        }
    }
}
 
Last edited:
So this isn't helpful? Chart move the x and y axes

If you can't read documentation, then you should attend more schooling for reading classes.

If you can't follow a tutorial (which isn't half bad), you probably should consider a different career path other than programming.

The code you provided does not draw any of the xy points on a chart. But what I linked you does. Maybe you should try that before saying I can't find anything that I understand.
 
Ooooook.....

So, when you want to drive you car, you jump into the car without lessons and you're an expert on the whole shebang without any training, right?
Wrong, you take lessons, get instructed by an instructor, then you do a test.

You're here because you didn't take lessons, and you didn't get instructed, which is why you came here to us. And you never did any test, because you're not listening to what I am telling you, for you to understand enough to even begin testing; you need to read what I provided. If you were paying attention, you would find that them docs provide the methods to update your charts in real time just as you want to do. If you invalidate, you can call update(). And if you looked at the tutorial, you would know how to add the x/y points to the chart. Combined with the docs, you would have found how chart1.Refresh() forces a refresh of the control causing the new x/y points to be updated. But I can only conclude from your rather lazy inept reply, that you are looking for a handout.

How about doing the relevant research, and educate yourself with it instead of looking for someone to give you all the answers coordinately.
 
Ooooook.....

So, when you want to drive you car, you jump into the car without lessons and you're an expert on the whole shebang without any training, right?
Wrong, you take lessons, get instructed by an instructor, then you do a test.

You're here because you didn't take lessons, and you didn't get instructed, which is why you came here to us. And you never did any test, because you're not listening to what I am telling you, for you to understand enough to even begin testing; you need to read what I provided. If you were paying attention, you would find that them docs provide the methods to update your charts in real time just as you want to do. If you invalidate, you can call update(). And if you looked at the tutorial, you would know how to add the x/y points to the chart. Combined with the docs, you would have found how chart1.Refresh() forces a refresh of the control causing the new x/y points to be updated. But I can only conclude from your rather lazy inept reply, that you are looking for a handout.

How about doing the relevant research, and educate yourself with it instead of looking for someone to give you all the answers coordinately.
C#:
public void Vykresli()
        {

            graf.Series["Kvadraticka funkce"].Points.Clear();

            if (a > 0)
            {
                double VrcholX = 0 - (b / 2);//
                double VrcholY = a * VrcholX * VrcholX + b * VrcholX + c;



                for (double x = RozA; x <= RozB; ++x)
                {
                    double y = a * (x * x) + b * x + c;
                    graf.Series["Kvadraticka funkce"].Points.AddXY(x, y);
                }
            }
            else
            {
                double VrcholX = (b / 2); //
                double VrcholY = a * VrcholX * VrcholX + b * VrcholX + c;


                for (double x = RozA; x <= RozB; ++x)
                {
                    double y = a * (x * x) + b * x + c;
                    graf.Series["Kvadraticka funkce"].Points.AddXY(x, y);
                }
            }
        }
C#:
I didn't mean to be rude.

But I don't need to know how to add a point to the chart. All I needed was to take points from the X-axis and put them into a variable and then plot the graph according to the points that are available. If the graph is moved or zoomed (when the x-axis is changed), it would be redrawn.

Like this: (I had to use the fixed points.)
In button 1 is bool if I clicked on the button;

C#:
private void KvaTextBox4_TextChanged(object sender, EventArgs e)
        {
           
            bool spravne = double.TryParse(KvaTextBox4.Text, out double cislo);

            if (spravne&&buttonclick)
            {
                KvaFun.RozA = cislo;
                KvaFun.Vykresli();
            }
        }

        private void KvaTextBox5_TextChanged(object sender, EventArgs e)
        {
            bool spravne = double.TryParse(KvaTextBox5.Text, out double cislo);

            if(spravne&&buttonclick)
            {
                KvaFun.RozB = cislo;
                KvaFun.Vykresli();
            }
        }
 
Back
Top Bottom