2

I've a txt file with a 360 numbers, I must read all of these and draw a kind of Disc made of FillPie eachone colored in scale of grey due to the value of the list. Until here everything is quite simple.I made a class with the data(value in the txt and degree) of one single fillpie with a Paint method that draw it of the correct color. this is the code of the class:

class DatoDisco
{
    int valoreSpicchio;
    int gradi;

    public DatoDisco(int valoreTastatore, int gradiLettura)
    {
        valoreSpicchio = valoreTastatore;
        gradi = gradiLettura;
    }

    public void Clear()
    {
        valoreSpicchio = 0;
        gradi = 0;
    }

    private int ScalaGrigi()
    {
        int grigio = 0;
        if (valoreSpicchio <= 0)
        {
            grigio = 125 + (valoreSpicchio / 10);
            if (grigio < 0)
                grigio = 0;
        }
        if (valoreSpicchio > 0)
        {
            grigio = 125 + (valoreSpicchio / 10);
            if (grigio > 230)
                grigio = 230;
        }
        return grigio;
    }

    public void Paint (Graphics grafica)
    {
        try
        {
            Brush penna = new SolidBrush(Color.FromArgb(255, ScalaGrigi(), ScalaGrigi(), ScalaGrigi()));
            grafica.FillPie(penna, 0, 0, 400, 400, gradi, 1.0f);
        }
        catch
        {
        }
    }

    public int ValoreSpicchio
    {
        get
        {
            return valoreSpicchio;
        }
    }

    public int Gradi
    {
        get
        {
            return gradi;
        }
    } 
}

here is where I draw everything:

public partial class Samac : Form
{
    libnodave.daveOSserialType fds;
    libnodave.daveInterface di;
    libnodave.daveConnection dc;
    int rack = 0;
    int slot = 2;
    int scalaGrigi = 0;
    int angolatura = 0;
    List<int> valoriY = new List<int>();
    //Disco disco = new Disco();
    List<DatoDisco> disco = new List<DatoDisco>();

    float[] valoriTastatore = new float[360];
    public Samac()
    {
        InitializeComponent();
        StreamReader dataStream = new StreamReader("save.txt");

        textBox1.Text = dataStream.ReadLine();
        dataStream.Dispose();
        for (int i = 0; i <= 360; i++ )
            chart1.Series["Series2"].Points.Add(0);
        //AggiornaGrafico(textBox1.Text, chart1);
        SetStyle(ControlStyles.SupportsTransparentBackColor, true);
    }
    string indirizzoIpPlc
    {
        get
        {
            FileIniDataParser parser = new FileIniDataParser();
            IniData settings = parser.LoadFile("config.ini");
            return settings["PLC_CONNECTION"]["PLC_IP"];
        }
    }
    private void AggiornaGrafico(string nomeFile, Chart grafico, bool online)
    {
        int max = 0;
        int min = 0;
        grafico.Series["Series1"].Points.Clear();
        grafico.Series["Series2"].Points.Clear();
        grafico.Series["Series3"].Points.Clear();
        grafico.Series["Series4"].Points.Clear();
        grafico.ChartAreas[0].AxisX.Maximum = 360;
        grafico.ChartAreas[0].AxisX.Minimum = 0;
        grafico.ChartAreas[0].AxisY.Maximum = 500;
        grafico.ChartAreas[0].AxisY.Minimum = -500;
        String file = nomeFile;
        valoriY.Clear();
        disco.Clear();

        if (online == false)
        {
            System.IO.File.WriteAllText("save.txt", nomeFile);
        }

            StreamReader dataStreamGrafico = new StreamReader(file);
            StreamReader dataStreamScheda = new StreamReader("Scheda.sch");

            string datasample;
            string[] scheda = new string[56];
            for (int i = 0; i < 56; i++)
            {
                scheda[i] = dataStreamScheda.ReadLine();
            }
            dataStreamScheda.Close();
        int gradi = 1;
            while ((datasample = dataStreamGrafico.ReadLine()) != null)
                { 
                    grafico.Series["Series2"].Points.Add(0);
                    grafico.Series["Series2"].Color = Color.Red;
                    grafico.Series["Series2"].BorderWidth = 3;
                    grafico.Series["Series3"].Points.Add(Convert.ToInt32(float.Parse(scheda[5])));
                    grafico.Series["Series3"].Color = Color.Green;
                    grafico.Series["Series3"].BorderWidth = 3;
                    grafico.Series["Series4"].Points.Add(Convert.ToInt32(-float.Parse(scheda[5])));
                    grafico.Series["Series4"].Color = Color.Green;
                    grafico.Series["Series4"].BorderWidth = 3;
                    grafico.Series["Series1"].Points.Add(int.Parse(datasample));
                    grafico.Series["Series1"].BorderWidth = 5; 
                    valoriY.Add(int.Parse(datasample));
                    //disco.Add(int.Parse(datasample));
                    disco.Add(new DatoDisco(int.Parse(datasample), gradi));
                    gradi++;
                }
            dataStreamGrafico.Close();
            max = Convert.ToInt32(chart1.Series["Series1"].Points.FindMaxByValue().YValues[0]);
            min = Convert.ToInt32(chart1.Series["Series1"].Points.FindMinByValue().YValues[0]);
            lblCampanatura.Text = (((float)max + min) / 2000.0).ToString();
            lblSbandieramento.Text = (((float)max - min) / 1000.0).ToString();

        if ((Math.Abs(max) > 800) || (Math.Abs(min) > 800))
        {
            if (Math.Abs(max) >= Math.Abs(min))
            {
                chart1.ChartAreas[0].AxisY.Maximum = max + 200;
                chart1.ChartAreas[0].AxisY.Minimum = -(max + 200);
            }
            else
            {
                chart1.ChartAreas[0].AxisY.Maximum = min + 200;
                chart1.ChartAreas[0].AxisY.Minimum = -(min + 200);
            }
        }
        else
        {
            chart1.ChartAreas[0].AxisY.Maximum = 800;
            chart1.ChartAreas[0].AxisY.Minimum = -800;
        }
            boxGraficaDisco.Refresh();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        DialogResult result = openFileDialog1.ShowDialog();
          textBox1.Text = openFileDialog1.FileName;
          if (result == DialogResult.OK)
          {
              AggiornaGrafico(textBox1.Text, chart1, timer1.Enabled);     
          }

    }

    ToolTip tooltip = new ToolTip();
    private int lastX;
    private int lastY;


    private void chart1_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.X != this.lastX || e.Y != this.lastY)
        {
            try
        {
                int cursorX = Convert.ToInt32(chart1.ChartAreas[0].AxisX.PixelPositionToValue(e.Location.X));

                tooltip.Show("X:" + cursorX.ToString("0.00") + "Y:" + Convert.ToInt32(chart1.Series[0].Points[cursorX].YValues[0]).ToString(), this.chart1, e.Location.X + 20, e.Location.Y + 20);                    
        }

        catch { }
        }
        this.lastX = e.X;
        this.lastY = e.Y;
    }

    private void button1_Click_1(object sender, EventArgs e)
    {
        int indice = ((int)Char.GetNumericValue(textBox1.Text[textBox1.Text.Length - 5]))+1;
        if (File.Exists(textBox1.Text.Substring(0, textBox1.Text.Length - 5) + indice + ".txt"))
        {
            textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 5) + indice + ".txt";
            try
            {
                AggiornaGrafico(textBox1.Text, chart1, timer1.Enabled);
            }
            catch
            {
                MessageBox.Show("Il File non esiste");
            }
        }
        else
        {
            MessageBox.Show("Il File non esiste");
        }
    }

    private void btnGrafMeno_Click(object sender, EventArgs e)
    {
        int indice = ((int)Char.GetNumericValue(textBox1.Text[textBox1.Text.Length - 5])) - 1;
        if (indice >= 0)
        {
            textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 5) + indice + ".txt";
            try
            {
                AggiornaGrafico(textBox1.Text, chart1, timer1.Enabled);
            }
            catch
            {
                MessageBox.Show("Il File non esiste");
            }
        }
        else
        {
            MessageBox.Show("Prima lettura disco");
        }
    }

    private void btnConnetti_Click(object sender, EventArgs e)
    {
        fds.rfd = libnodave.openSocket(102, indirizzoIpPlc);
        fds.wfd = fds.rfd;
        if (fds.rfd > 0)
        {
            di = new libnodave.daveInterface(fds, "IF1", 0, libnodave.daveProtoISOTCP, libnodave.daveSpeed187k);
            di.setTimeout(1000000);
            dc = new libnodave.daveConnection(di, 0, rack, slot);
            int res = dc.connectPLC();
            timer1.Start();
           // AggiornaGrafico("Disco.csv", chart1, timer1.Enabled);
        }
        else
        {
            MessageBox.Show("Impossibile connettersi");
        }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        if (timer1.Enabled == true)
        {
                int res;
                res = dc.readBytes(libnodave.daveDB, 21, 40, 1, null);
                if (res == 0)
                {
                    var letturaDati = (dc.getS8At(0) & (1 << 0)) != 0;
                    if (letturaDati == true)
                    {
                        int puntatore = 30;
                        StreamWriter datiDisco = new StreamWriter("DatiDaPlc.csv");
                        datiDisco.WriteLine("X;" + "C;" + "Z;");
                        while (puntatore <= 10838)
                        {
                            res = dc.readBytes(libnodave.daveDB, 3, puntatore, 192, null);
                            if (res == 0)
                            {
                                for (int i = 0; dc.getU32At(i) != 0; i = i + 12)
                                {
                                    datiDisco.Write(dc.getU32At(i).ToString() + ";");
                                    datiDisco.Write(dc.getU32At(i + 4).ToString() + ";");
                                    datiDisco.WriteLine(dc.getFloatAt(i + 8).ToString() + ";");
                                }
                            }
                            puntatore = puntatore + 192;
                        }
                        datiDisco.Close();
                        StreamReader lettura = new StreamReader("DatiDaPlc.csv");
                        StreamWriter scritt = new StreamWriter("Disco.csv");
                        var titolo = lettura.ReadLine();
                        var posizioneLettura = lettura.ReadLine();
                        var posX = posizioneLettura.Split(';');
                        int minX = Convert.ToInt32(posX[0]) - 5;
                        int maxX = Convert.ToInt32(posX[0]) + 5;
                        int contatore = 0;
                        while (!lettura.EndOfStream)
                        {
                            var line = lettura.ReadLine();
                            var values = line.Split(';');

                            if ((Convert.ToInt32(values[1]) >= contatore && Convert.ToInt32(values[1]) < contatore + 1000) && (Convert.ToInt32(values[0]) > minX && Convert.ToInt32(values[0]) <= maxX))
                            {
                                scritt.WriteLine(Convert.ToInt32(float.Parse(values[2]) * 1000).ToString());
                                contatore += 1000;
                            }
                        }
                        lettura.Close();
                        scritt.Close();
                        AggiornaGrafico("Disco.csv", chart1, timer1.Enabled);
                    } 
                }

                else
                {
                    timer1.Stop();
                    MessageBox.Show("Disconnesso");
                    dc.disconnectPLC();
                    di.disconnectAdapter();
                    fds.rfd = libnodave.closeSocket(102);
                    fds.wfd = fds.rfd;
                }
        }
    }

    private void btnDisconnetti_Click(object sender, EventArgs e)
    {
        if (timer1.Enabled == true)
        {
            dc.disconnectPLC();
            di.disconnectAdapter();
            fds.rfd = libnodave.closeSocket(102);
            fds.wfd = fds.rfd;
            timer1.Stop();
            AggiornaGrafico(textBox1.Text, chart1, timer1.Enabled);
        }
    }

    private void Samac_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (timer1.Enabled == true)
        {
            dc.disconnectPLC();
            di.disconnectAdapter();
            libnodave.closeSocket(102);
            timer1.Stop();
        }
    }

    private void button1_Click_2(object sender, EventArgs e)
    {
        if (timer1.Enabled == true)
        {
            AggiornaGrafico("Disco.csv", chart1, timer1.Enabled);
        }
        else
        {
            AggiornaGrafico(textBox1.Text, chart1, timer1.Enabled);
        }
    }

    private void chart2_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.X != this.lastX || e.Y != this.lastY)
        {
            try
            {
                int cursorX = Convert.ToInt32(chart2.ChartAreas[0].AxisX.PixelPositionToValue(e.Location.X));
                int cursorY = Convert.ToInt32(chart2.ChartAreas[0].AxisY.PixelPositionToValue(e.Location.Y));
                //tooltip.Show("X:" + chart2.Series[0].Points[cursorX].XValue.ToString() + "Y:" + chart2.Series[0].Points[cursorX].YValues[0].ToString(), this.chart2, e.Location.X + 20, e.Location.Y + 20);
                tooltip.Show("X:" + cursorX.ToString() + "Y:#VALY" , this.chart2, e.Location.X + 20, e.Location.Y + 20);
                //chart2.Series[0].ToolTip="#VALY";
            }

            catch { }
        }
        this.lastX = e.X;
        this.lastY = e.Y;
    }

    private void boxGraficaDisco_Paint(object sender, PaintEventArgs e)
    {
        Graphics grafica = e.Graphics;
        //disco.Paint(grafica);
        foreach (DatoDisco d in disco)
        {
            d.Paint(grafica);
        }
    }

    private void boxGraficaDisco_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.X != this.lastX || e.Y != this.lastY)
        {
            try
            {
                foreach (DatoDisco d in disco)
                {

                }
            }

            catch { }
        }
        this.lastX = e.X;
        this.lastY = e.Y;
    }
}

Now I need that when i go with the mouse over the drawn disc, a tooltip show me the data of the fillPie(degree and value of txt) but i can't figure out how. Anyone can help me? this is an image of the disc enter image description here

  • man you have to do serious refactoring... i can't even read that code if i had written it myself... can you? – Florian Schmidinger Apr 14 '15 at 08:50
  • 1
    related: http://stackoverflow.com/questions/6270785/how-to-determine-whether-a-point-x-y-is-contained-within-an-arc-section-of-a-c – Florian Schmidinger Apr 14 '15 at 09:01
  • Handle mouse move of the container on which you are drawing the disc and if the mouse is on the disc (using event args coordinates) show text wither by using DrawText or by creating a label. – danish Apr 14 '15 at 09:27
  • If all you need is the DataPoint values the mouse is over you can try to add a ToolTip to each DataPoint. – TaW Apr 14 '15 at 09:42
  • Thanks for the answers, I managed to make appear the Tooltip if the mouse is over the Discbut I don't know how to get the angle between the mouse position and the center of the disc. – Salvatore Rollo Apr 14 '15 at 12:20

1 Answers1

1

Eventually it looks as if all you want is a function to get the angle between the mouse position and the center of the disc..

Here is a function to calculate an angle given two points:

double AngleFromPoints(Point pt1, Point pt2)
{
    Point P = new Point(pt1.X - pt2.X, pt1.Y - pt2.Y);
    double alpha = 0d;
    if (P.Y == 0) alpha = P.X > 0 ? 0d : 180d;
    else
    {
        double f = 1d * P.X / (Math.Sqrt(P.X * P.X + P.Y * P.Y));
        alpha = Math.Acos(f) * 180d / Math.PI; 
        if (P.Y > 0) alpha = 360d - alpha;
    }
    return alpha;
}
TaW
  • 53,122
  • 8
  • 69
  • 111