using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.IO;

namespace meteo
{
    public partial class grafy : Form
    {
        Bitmap image1;
        private Graphics graX;
        Bitmap snih = new Bitmap("pozadi3.png");
        bool klik = false;

        int lengthArea = 0; //400;
        int lengthArea2 = 0; //400;
        int lengthArea3 = 0; //400;
        int heightArea = 0;//250;
        int topX = 55;
        int topX2 = 78;
        int topY = 30;

        
        public grafy()
        {
            InitializeComponent();
            
        }

        

        private void DrawLineChart(double[] data, int[] mesic, int[] den, int[] hod, int[] min, double[] data2)
        {
            DateTime Mycas = new DateTime();

            graX.Clear(SystemColors.Control);
            graX.DrawImage(snih, 0, 0);
            int count = 1;
            int[] alWeight, alWeight2;

            graX.DrawRectangle(new Pen(Color.Red), topX, topY, lengthArea, heightArea);
            alWeight = new int[data.Length - 1];
            alWeight2 = new int[data2.Length - 1];
            for (int i = 0; i < data.Length - 1; i++)
            {
                alWeight[i] = (int)(10 * data[i]);
                alWeight2[i] = (int)(10 * data2[i]);
            }

            int numberOfSections = data.Length;

            int maxWeight = alWeight.Max() + 1;
            int minWeight = alWeight.Min() - 1;
            int maxWeight2 = alWeight2.Max();
            int minWeight2 = alWeight2.Min();
            minWeight2 = 0;   // prava strana bude v roysahu 0 - 100
            maxWeight2 = 1000;

            int meritko = heightArea / 15;         // meritko  
            int prvni = (maxWeight * heightArea) / (maxWeight - minWeight);    // 15.1   
            int druhy = heightArea - prvni;
            int prvni2 = (maxWeight2 * heightArea) / (maxWeight2 - minWeight2);
            int druhy2 = heightArea - prvni2;
            Pen pen = new Pen(Color.SaddleBrown);
            Pen pen2 = new Pen(Color.Tomato);
            pen.Width = 2;
            pen2.Width = 2;
            Font drawFont1 = new Font("Arial", 8, FontStyle.Bold);
            SolidBrush drawBrush2 = new SolidBrush(Color.Black);
            graX.DrawLine(new Pen(Color.SaddleBrown, 2), topX, 440, topX + 20, 440);  //legenda
            graX.DrawString("Teplota [°C]", drawFont1, drawBrush2, topX + 25, 435);
            graX.DrawString("Vlhkost [%]", drawFont1, drawBrush2, topX + 25, 450);
            graX.DrawLine(new Pen(Color.Tomato, 2), topX, 455, topX + 20, 455);  //legenda
            Rectangle rec = new Rectangle(topX, topY, lengthArea, heightArea);
            graX.DrawRectangle(new Pen(Color.Black), rec);
            int smallX = topX;
            int smallX2 = topX;

            decimal smallLength = (lengthArea / ((decimal)(numberOfSections - 2)));
            int smallHeight = 0;
            int smallHeight2 = 0;

            decimal b1x = 0, b11x = 0, b2x = 0, b22x = 0;

            Point p1 = new Point();
            Point p2 = new Point();
            Point p11 = new Point();
            Point p22 = new Point();

            graX.DrawString("Vývoj teploty t [°C] a relativní vlhkosti r [%] za dané období", new Font("Arial", 12, FontStyle.Bold), new SolidBrush(Color.Black), 160, 7, new StringFormat());  // popis

            if (numberOfSections >= lengthArea) { count = 2; }    // kdyz je vice bodu nez pixelu
            else { count = 1; }
            if (smallLength < 1) { smallLength = 1; }

            //label1.Text = (numberOfSections - 1).ToString();
            graX.DrawString("Celkem položek: " + (numberOfSections - 1).ToString(), new Font("Arial", 8, FontStyle.Bold), new SolidBrush(Color.Black), 300, 450, new StringFormat());  // popis

            // ------------- legenda ------------------
            int krok2;
            decimal kolik = ((decimal)prvni) / ((decimal)meritko);     // 16.1
            decimal kolik2 = ((decimal)druhy) / ((decimal)meritko);      //16.1
            decimal kolik3 = prvni2 / (meritko);
            int krok = (int)(prvni / (kolik));
            if (kolik2 != 0) { krok2 = (int)(druhy / kolik2); }
            else krok2 = 0;
            int krok3 = (int)(prvni2 / (kolik3));
            int krok4 = druhy2 / 10;

            if (minWeight >= 0)
            {
                for (int i = 0; i < (16); i++)
                {
                    graX.DrawLine(new Pen(Color.Black), topX, ((int)-i * meritko) + topY + heightArea, lengthArea + topX, ((int)-i * meritko) + heightArea + topY);
                    graX.DrawString((((((maxWeight - minWeight) / 15.0) * i) + minWeight) / 10.0).ToString("f1") + " °C", drawFont1, drawBrush2, 5, ((int)-i * meritko) + heightArea + meritko - 5, new StringFormat());   //leva +
                }    // 16.1    /15.0

            }
            else
            {
                if (maxWeight < 0)
                {
                    for (int i = 0; i < (16); i++)
                    {
                        graX.DrawLine(new Pen(Color.Black), topX, ((int)-i * meritko) + topY + heightArea, lengthArea + topX, ((int)-i * meritko) + heightArea + topY);
                        graX.DrawString((((((maxWeight - minWeight) / 15.0) * i) + minWeight) / 10.0).ToString("f1") + " °C", drawFont1, drawBrush2, 5, ((int)-i * meritko) + heightArea + meritko, new StringFormat());   //leva +
                    }
                }
                else
                {
                    // leva Y 
                    for (int i = 0; i < (kolik); i++)   //16.1
                    {
                        graX.DrawString((maxWeight / (kolik * 10) * i).ToString("f1") + " °C", drawFont1, drawBrush2, 5, ((int)-i * krok) + prvni + krok - 5, new StringFormat());   //leva +
                        graX.DrawLine(new Pen(Color.Black), topX, ((int)-i * krok) + prvni + topY, lengthArea + topX, ((int)-i * krok) + prvni + topY);  //oprava
                    }

                    //leva Y -
                    for (int i = 1; i < (kolik2); i++)
                    {
                        graX.DrawString((minWeight / (kolik2 * 10) * i).ToString("f1") + " °C", drawFont1, drawBrush2, 0, ((int)+i * krok2) + prvni + krok - 5, new StringFormat());  // leva -
                        graX.DrawLine(new Pen(Color.Black), topX, ((int)+i * krok) + prvni + topY, lengthArea + topX, ((int)+i * krok) + prvni + topY); //oprava
                    }
                }
            }

            //prava Y
            for (int i = 0; i < (kolik3 + 1); i++)
            {
                graX.DrawString((maxWeight2 / (kolik3 * 10) * i).ToString("f1") + " %", drawFont1, drawBrush2, lengthArea + topX + 4, ((int)-i * krok3) + prvni2 + krok3 - 5, new StringFormat());    //prava osa

            }
            // ------// legenda-------------

            for (int i = 0; i < numberOfSections - 1; i = i + count)
            {
                b1x = b2x;
                b11x = b22x;

                p1 = p2;
                p11 = p22;


                if (i == 0)
                {
                    b2x = b2x + topX;
                    b22x = b22x + topX;
                }
                else
                {
                    b2x = b2x + smallLength;
                    b22x = b22x + smallLength;
                }

                smallHeight = prvni - ((alWeight[i] * prvni) / (maxWeight));
                smallHeight2 = prvni2 - ((alWeight2[i] * prvni2) / (maxWeight2));
                p2.Y = topY + smallHeight;
                p22.Y = topY + smallHeight2;

                if (b1x != 0 && p1.Y != 0)
                {
                    graX.DrawLine(pen, (int)b1x, p1.Y, (int)b2x, p2.Y);
                }

                if (b11x != 0 && p11.Y != 0)
                {
                    graX.DrawLine(pen2, (int)b11x, p11.Y, (int)b22x, p22.Y);
                }

                Mycas = new DateTime(2010, mesic[i], den[i], hod[i], min[i], 00);

                int nasobek = 0;
                if (numberOfSections < 144) { nasobek = 12; }
                if ((numberOfSections >= 144) && (numberOfSections < 288)) { nasobek = 24; }
                if ((numberOfSections >= 288) && (numberOfSections < 432)) { nasobek = 36; }
                if ((numberOfSections >= 432) && (numberOfSections < 576)) { nasobek = 72; }
                if (nasobek == 0) { nasobek = 84; }

                if ((i % nasobek) == 0)
                {
                    graX.DrawString(Mycas.ToString("HH:mm"), drawFont1, new SolidBrush(Color.Black), (int)b2x - 15, heightArea + topY + 3, new StringFormat());   // hodiny
                    graX.DrawString(Mycas.ToString("dd/MM"), drawFont1, new SolidBrush(Color.Black), (int)b2x - 15, heightArea + topY + 15, new StringFormat());//dny
                }

                // x osa
                if ((i % 3) == 0)  // 3 x zapis do 1hodiny 
                {
                    graX.DrawLine(new Pen(Color.Green), (int)b2x, heightArea + topY, (int)b2x, heightArea + topY - 5);
                }
                // x osa
                if ((i % 72) == 0) // 3 * 24 = 72   1den
                {
                    graX.DrawLine(new Pen(Color.Red), (int)b2x, heightArea + topY, (int)b2x, heightArea + topY - 10);
                }
            }
            p.Image = image1;
        }

        private void DrawLineChart3(double[] data, int[] mesic, int[] den, int[] hod, int[] min, double[] data2)
        {
            DateTime Mycas = new DateTime();

            graX.Clear(SystemColors.Control);
            graX.DrawImage(snih, 0, 0);
            int count = 1;
            int[] alWeight, alWeight2;

            graX.DrawRectangle(new Pen(Color.Red), topX2, topY, lengthArea2, heightArea);
            alWeight = new int[data.Length - 1];
            alWeight2 = new int[data2.Length - 1];
            for (int i = 0; i < data.Length - 1; i++)
            {
                alWeight[i] = (int)(10 * data[i]);
                alWeight2[i] = (int)(10 * data2[i]);
            }

            int numberOfSections = data.Length;

            int maxWeight = alWeight.Max() + 10;
            int minWeight = alWeight.Min() - 10;
            int maxWeight2 = alWeight2.Max() + 1;
            int minWeight2 = alWeight2.Min();
            minWeight2 = 0;   // prava strana bude v roysahu 0 - 100
            //maxWeight2 = 1000;

            int meritko = heightArea / 15;         // meritko  
            int prvni = (maxWeight * heightArea) / (maxWeight - minWeight);    // 15.1   
            int druhy = heightArea - prvni;
            int prvni2 = (maxWeight2 * heightArea) / (maxWeight2 - minWeight2);
            int druhy2 = heightArea - prvni2;
            Pen pen = new Pen(Color.SaddleBrown);
            Pen pen2 = new Pen(Color.Tomato);
            pen.Width = 2;
            pen2.Width = 2;
            Font drawFont1 = new Font("Arial", 8, FontStyle.Bold);
            SolidBrush drawBrush2 = new SolidBrush(Color.Black);
            graX.DrawLine(new Pen(Color.SaddleBrown, 2), topX2, 440, topX2 + 20, 440);  //legenda
            graX.DrawString("Atmosférický tlak [hPa]", drawFont1, drawBrush2, topX2 + 25, 435);
            graX.DrawString("Rychlost větru [m/s]", drawFont1, drawBrush2, topX2 + 25, 450);
            graX.DrawLine(new Pen(Color.Tomato, 2), topX2, 455, topX2 + 20, 455);  //legenda
            Rectangle rec = new Rectangle(topX2, topY, lengthArea2, heightArea);
            graX.DrawRectangle(new Pen(Color.Black), rec);
            int smallX = topX2;
            int smallX2 = topX2;

            decimal smallLength = (lengthArea2 / ((decimal)(numberOfSections - 2)));
            int smallHeight = 0;
            int smallHeight2 = 0;

            decimal b1x = 0, b11x = 0, b2x = 0, b22x = 0;

            Point p1 = new Point();
            Point p2 = new Point();
            Point p11 = new Point();
            Point p22 = new Point();

            graX.DrawString("Vývoj atmosférického tlaku p [hPa] a rychlosti větru v [m/s] za dané období", new Font("Arial", 12, FontStyle.Bold), new SolidBrush(Color.Black), 120, 7, new StringFormat());  // popis

            if (numberOfSections >= lengthArea2) { count = 2; }    // kdyz je vice bodu nez pixelu
            else { count = 1; }
            if (smallLength < 1) { smallLength = 1; }

            //label1.Text = (numberOfSections - 1).ToString();
            graX.DrawString("Celkem položek: " + (numberOfSections - 1).ToString(), new Font("Arial", 8, FontStyle.Bold), new SolidBrush(Color.Black), 300, 450, new StringFormat());  // popis
            
            // ------------- legenda ------------------
            int krok2;
            decimal kolik = ((decimal)prvni) / ((decimal)meritko);     // 16.1
            decimal kolik2 = ((decimal)druhy) / ((decimal)meritko);      //16.1
            decimal kolik3 = prvni2 / (meritko);
            int krok = (int)(prvni / (kolik));
            if (kolik2 != 0) { krok2 = (int)(druhy / kolik2); }
            else krok2 = 0;
            int krok3 = (int)(prvni2 / (kolik3));
            int krok4 = druhy2 / 10;

            if (minWeight >= 0)
            {
                for (int i = 0; i < (16); i++)
                {
                    graX.DrawLine(new Pen(Color.Black), topX2, ((int)-i * meritko) + topY + heightArea, lengthArea2 + topX2, ((int)-i * meritko) + heightArea + topY);
                    graX.DrawString((((((maxWeight - minWeight) / 15.0) * i) + minWeight) / 10.0).ToString("f1") + " hPa", drawFont1, drawBrush2, 5, ((int)-i * meritko) + heightArea + meritko - 5, new StringFormat());   //leva +
                }    // 16.1    /15.0

            }
            else
            {
                if (maxWeight < 0)
                {
                    for (int i = 0; i < (16); i++)
                    {
                        graX.DrawLine(new Pen(Color.Black), topX2, ((int)-i * meritko) + topY + heightArea, lengthArea2 + topX2, ((int)-i * meritko) + heightArea + topY);
                        graX.DrawString((((((maxWeight - minWeight) / 15.0) * i) + minWeight) / 10.0).ToString("f1") + " hPa", drawFont1, drawBrush2, 5, ((int)-i * meritko) + heightArea + meritko, new StringFormat());   //leva +
                    }
                }
                else
                {
                    // leva Y 
                    for (int i = 0; i < (kolik); i++)   //16.1
                    {
                        graX.DrawString((maxWeight / (kolik * 10) * i).ToString("f1") + " hPa", drawFont1, drawBrush2, 5, ((int)-i * krok) + prvni + krok - 5, new StringFormat());   //leva +
                        graX.DrawLine(new Pen(Color.Black), topX2, ((int)-i * krok) + prvni + topY, lengthArea2 + topX2, ((int)-i * krok) + prvni + topY);  //oprava
                    }

                    //leva Y -
                    for (int i = 1; i < (kolik2); i++)
                    {
                        graX.DrawString((minWeight / (kolik2 * 10) * i).ToString("f1") + " hPa", drawFont1, drawBrush2, 0, ((int)+i * krok2) + prvni + krok - 5, new StringFormat());  // leva -
                        graX.DrawLine(new Pen(Color.Black), topX2, ((int)+i * krok) + prvni + topY, lengthArea2 + topX2, ((int)+i * krok) + prvni + topY); //oprava
                    }
                }
            }

            //prava Y
            for (int i = 0; i < (kolik3 + 1); i++)
            {
                graX.DrawString((maxWeight2 / (kolik3 * 10) * i).ToString("f1") + " m/s", drawFont1, drawBrush2, lengthArea2 + topX2 + 4, ((int)-i * krok3) + prvni2 + krok3 - 5, new StringFormat());    //prava osa

            }
            // ------// legenda-------------

            for (int i = 0; i < numberOfSections - 1; i = i + count)
            {
                b1x = b2x;
                b11x = b22x;

                p1 = p2;
                p11 = p22;


                if (i == 0)
                {
                    b2x = b2x + topX2;
                    b22x = b22x + topX2;
                }
                else
                {
                    b2x = b2x + smallLength;
                    b22x = b22x + smallLength;
                }

                smallHeight = prvni - ((alWeight[i] * prvni) / (maxWeight));
                smallHeight2 = prvni2 - ((alWeight2[i] * prvni2) / (maxWeight2));
                p2.Y = topY + smallHeight;
                p22.Y = topY + smallHeight2;

                if (b1x != 0 && p1.Y != 0)
                {
                    graX.DrawLine(pen, (int)b1x, p1.Y, (int)b2x, p2.Y);
                }

                if (b11x != 0 && p11.Y != 0)
                {
                    graX.DrawLine(pen2, (int)b11x, p11.Y, (int)b22x, p22.Y);
                }

                Mycas = new DateTime(2010, mesic[i], den[i], hod[i], min[i], 00);

                int nasobek = 0;
                if (numberOfSections < 144) { nasobek = 12; }
                if ((numberOfSections >= 144) && (numberOfSections < 288)) { nasobek = 24; }
                if ((numberOfSections >= 288) && (numberOfSections < 432)) { nasobek = 36; }
                if ((numberOfSections >= 432) && (numberOfSections < 576)) { nasobek = 72; }
                if (nasobek == 0) { nasobek = 84; }

                if ((i % nasobek) == 0)
                {
                    graX.DrawString(Mycas.ToString("HH:mm"), drawFont1, new SolidBrush(Color.Black), (int)b2x - 15, heightArea + topY + 3, new StringFormat());   // hodiny
                    graX.DrawString(Mycas.ToString("dd/MM"), drawFont1, new SolidBrush(Color.Black), (int)b2x - 15, heightArea + topY + 15, new StringFormat());//dny
                }

                // x osa
                if ((i % 3) == 0)  // 3 x zapis do 1hodiny 
                {
                    graX.DrawLine(new Pen(Color.Green), (int)b2x, heightArea + topY, (int)b2x, heightArea + topY - 5);
                }
                // x osa
                if ((i % 72) == 0) // 3 * 24 = 72   1den
                {
                    graX.DrawLine(new Pen(Color.Red), (int)b2x, heightArea + topY, (int)b2x, heightArea + topY - 10);
                }
            }
            p.Image = image1;
        }

        void nacti_data()
        {

            //string tempFile = Path.GetTempFileName();
            object[] row;

            string datum = null, teplota = null, vlhkost = null;
            double[] arrteplota, arrvlhkost, arrdatum;
            int[] arrhod, arrmin, arrden, arrmesic, arrrok;

            string path = @"Windows/www/wwwpub/amline/data.txt";

            try
            {
                if (File.Exists(path))
                {

                    using (var sr = new StreamReader(@"Windows/www/wwwpub/amline/data.txt", true))
                    {

                        string line;

                        while ((line = sr.ReadLine()) != null)
                        {
                            if (line != "")
                            {


                                //else datum += myTable.Rows[i][0].ToString();

                                row = line.Split(';'); // nacte datum
                                datum += row[0].ToString() + ';';
                                teplota += row[1].ToString() + ';';
                                vlhkost += row[2].ToString() + ';';
                            }
                        }

                        string[] arrString = datum.Split(new char[] { ';' });//you can define your own way to split the string
                        string[] arrString1 = teplota.Split(new char[] { ';' });//you can define your own way to split the string
                        string[] arrString2 = vlhkost.Split(new char[] { ';' });//you can define your own way to split the string

                        arrdatum = new double[arrString.Length];
                        arrteplota = new double[arrString1.Length];
                        arrvlhkost = new double[arrString2.Length];
                        arrhod = new int[arrString.Length];
                        arrmin = new int[arrString.Length];

                        arrrok = new int[arrString.Length];
                        arrmesic = new int[arrString.Length];
                        arrden = new int[arrString.Length];

                        for (int i = 0; i < arrString.Length - 1; i++)   // -1 pridano
                        {
                            arrteplota[i] = double.Parse(arrString1[i], CultureInfo.InvariantCulture);
                            //arrtlak[i] = double.Parse(arrString3[i], CultureInfo.CurrentCulture);
                            arrvlhkost[i] = double.Parse(arrString2[i], CultureInfo.InvariantCulture);
                            //arrrosny[i] = double.Parse(arrString6[i], CultureInfo.CurrentCulture);

                            DateTime dt = Convert.ToDateTime(arrString[i]);
                            arrhod[i] = dt.Hour;
                            arrmin[i] = dt.Minute;
                            arrrok[i] = dt.Year;
                            arrmesic[i] = dt.Month;
                            arrden[i] = dt.Day;
                        }
                        DrawLineChart(arrteplota, arrmesic, arrden, arrhod, /*arr*/ arrmin, arrvlhkost);

                    }
                }//if
            }//try
            catch { MessageBox.Show("problem s nactenim data.txt"); }
        }
        void nacti_data2()
        {

            object[] row = null;
            string datum = null, tlak = null, vitr = null; ;
            double[] arrtlak = null, arrdatum = null, arrvitr = null; ;
            int[] arrhod = null, arrmin = null, arrden = null, arrmesic = null, arrrok = null;

            string path = @"Windows/www/wwwpub/amline/data2.txt";

            try
            {
                if (File.Exists(path))
                {

                    using (var sr = new StreamReader(@"Windows/www/wwwpub/amline/data2.txt", true))
                    {

                        string line;

                        while ((line = sr.ReadLine()) != null)
                        {
                            if (line != "")
                            {


                                //else datum += myTable.Rows[i][0].ToString();

                                row = line.Split(';'); // nacte datum
                                datum += row[0].ToString() + ';';
                                tlak += row[1].ToString() + ';';
                                vitr += row[2].ToString() + ';';
                                //vlhkost += row[2].ToString() + ';';
                                //DateTime dt1 = Convert.ToDateTime(row[0].ToString());
                                //if (dt1 >= thisWeekEnd) sw.WriteLine(line);  //pokud je mladsi nez 7dni, tak ho ponecha
                            }
                        }

                        string[] arrString = datum.Split(new char[] { ';' });//you can define your own way to split the string
                        string[] arrString1 = tlak.Split(new char[] { ';' });//you can define your own way to split the string
                        string[] arrString2 = vitr.Split(new char[] { ';' });//you can define your own way to split the string
                        //string[] arrString2 = vlhkost.Split(new char[] { ';' });//you can define your own way to split the string

                        arrdatum = new double[arrString.Length];
                        arrtlak = new double[arrString1.Length];
                        arrvitr = new double[arrString2.Length];
                        arrhod = new int[arrString.Length];
                        arrmin = new int[arrString.Length];

                        arrrok = new int[arrString.Length];
                        arrmesic = new int[arrString.Length];
                        arrden = new int[arrString.Length];

                        for (int i = 0; i < arrString.Length - 1; i++)   // -1 pridano
                        {
                            arrtlak[i] = double.Parse(arrString1[i], CultureInfo.InvariantCulture);
                            arrvitr[i] = double.Parse(arrString2[i], CultureInfo.InvariantCulture);
                            //arrtlak[i] = double.Parse(arrString3[i], CultureInfo.CurrentCulture);

                            //arrrosny[i] = double.Parse(arrString6[i], CultureInfo.CurrentCulture);

                            DateTime dt = Convert.ToDateTime(arrString[i]);
                            arrhod[i] = dt.Hour;
                            arrmin[i] = dt.Minute;
                            arrrok[i] = dt.Year;
                            arrmesic[i] = dt.Month;
                            arrden[i] = dt.Day;



                            //arrcas[i] = double.Parse(arrString2[i], CultureInfo.CurrentCulture);

                        }
                        //DrawLineChart2(arrtlak, arrmesic, arrden, arrhod, arrmin);
                        DrawLineChart3(arrtlak, arrmesic, arrden, arrhod, arrmin, arrvitr);
                    }

                    //File.Delete(@"Windows/www/wwwpub/amline/data.txt");
                    //File.Move(tempFile, @"Windows/www/wwwpub/amline/data.txt");
                }//if
            }//try
            catch { MessageBox.Show("chyba s nactenim data2.txt"); }
           }

        private void DrawLineChart2(double[] data, int[] mesic, int[] den, int[] hod, int[] min)
        {

            DateTime Mycas = new DateTime();
            graX.Clear(SystemColors.Control);
            graX.DrawImage(snih, 0, 0);
            int count = 1;
            int[] alWeight;
            alWeight = new int[data.Length];
            for (int i = 0; i < data.Length; i++)   //odstrani desetine misto
            {
                alWeight[i] = (int)(10 * data[i]);
            }

            int numberOfSections = data.Length;   //pocet dat x

            int maxWeight = alWeight.Max() + 10;  // najde nejvetsi hodnotu z osy y
            alWeight[data.Length - 1] = maxWeight;
            int minWeight = alWeight.Min() - 10;  // najde nejmensi hodnotu z osy y
            int meritko = heightArea / 15;         // meritko y                             //16.2
            int prvni = ((maxWeight * heightArea)/*/50*/) / (maxWeight - minWeight);
            Font drawFont1 = new Font("Arial", 8, FontStyle.Bold);
            SolidBrush drawBrush2 = new SolidBrush(Color.Black);
            Pen pen2 = new Pen(Color.Tomato);
            pen2.Width = 2;
            graX.DrawLine(new Pen(Color.Tomato, 2), topX2, 440, topX2 + 20, 440);  //legenda
            graX.DrawString("Tlak hPa", drawFont1, drawBrush2, topX2 + 25, 435);

            int smallX = topX2;
            decimal smallLength = (lengthArea2 / ((decimal)(numberOfSections - 2)));

            Rectangle rec = new Rectangle(topX2, topY, lengthArea2, heightArea);
            graX.DrawRectangle(new Pen(Color.Black), rec);
            int smallHeight = 0;

            decimal b1x = 0, b2x = 0;
            Point p1 = new Point();
            Point p2 = new Point();

            graX.DrawLine(new Pen(Color.Black), topX2, prvni + topY, topX2 + lengthArea2, prvni + topY); //nulova cara
            graX.DrawString("Vývoj atmosférického tlaku P [hPa] za dané období", new Font("Arial", 12, FontStyle.Bold), new SolidBrush(Color.Black), 200, 7, new StringFormat());  // popis

            if (numberOfSections >= lengthArea2) { count = 2; }    // kdyz je vice bodu nez pixelu
            else { count = 1; }
            if (smallLength < 1) { smallLength = 1; }

            //label1.Text = (numberOfSections - 1).ToString();
            graX.DrawString("Celkem položek: " + (numberOfSections - 1).ToString(), new Font("Arial", 8, FontStyle.Bold), new SolidBrush(Color.Black), 300, 450, new StringFormat());  // popis
            decimal kolik = ((decimal)prvni) / ((decimal)meritko); //16.2
            int krok = (int)(prvni / (kolik));

            //----legenda
            // leva Y  //---------------16.2
            for (int i = 0; i < 16; i++)
            {
                graX.DrawString((((((maxWeight - minWeight) / 15) * i) + minWeight) / 10.0).ToString("f1") + " hPa", drawFont1, drawBrush2, 5, ((int)-i * meritko) + 20 + heightArea, new StringFormat());   //leva +
                graX.DrawLine(new Pen(Color.Black), topX2, ((int)-i * meritko) + topY + heightArea, lengthArea2 + topX2, ((int)-i * meritko) + topY + heightArea);
            }
            /// ---//legenda
            for (int i = 0; i < numberOfSections - 1; i = i + count)
            {
                b1x = b2x;
                p1 = p2;

                if (i == 0)
                {
                    b2x = b2x + topX2;
                }
                else b2x = b2x + smallLength;

                smallHeight = prvni - ((alWeight[i] * prvni) / (maxWeight));

                p2.Y = topY + smallHeight;


                if (b1x != 0 && p1.Y != 0)
                {

                    graX.DrawLine(pen2, (int)b1x, p1.Y, (int)b2x, p2.Y);

                }


                Mycas = new DateTime(2010, mesic[i], den[i], hod[i], min[i], 00);

                int nasobek = 0;
                if (numberOfSections < 144) { nasobek = 12; }
                if ((numberOfSections >= 144) && (numberOfSections < 288)) { nasobek = 24; }
                if ((numberOfSections >= 288) && (numberOfSections < 432)) { nasobek = 36; }
                if ((numberOfSections >= 432) && (numberOfSections < 576)) { nasobek = 72; }
                if (nasobek == 0) { nasobek = 84; }

                if ((i % nasobek) == 0)
                {
                    graX.DrawString(Mycas.ToString("HH:mm"), drawFont1, new SolidBrush(Color.Black), (int)b2x - 15, heightArea + topY + 3, new StringFormat());   // hodiny
                    graX.DrawString(Mycas.ToString("dd/MM"), drawFont1, new SolidBrush(Color.Black), (int)b2x - 15, heightArea + topY + 15, new StringFormat());//dny
                }

                // x osa
                if ((i % 3) == 0)  //  zapis do 1x do 1hodiny 
                {
                    graX.DrawLine(new Pen(Color.Green), (int)b2x, heightArea + topY, (int)b2x, heightArea + topY - 5);
                }
                // x osa
                if ((i % 72) == 0) // 3 * 24 = 72   1den
                {
                    graX.DrawLine(new Pen(Color.Red), (int)b2x, heightArea + topY, (int)b2x, heightArea + topY - 10);

                }
            }
            p.Image = image1;
        }

        private void bt_zobraz_Click(object sender, EventArgs e)
        {
            if (klik == false)
            {
                nacti_data2();
                klik = true;
                bt_zobraz.Text = "graf teploty a vlhkosti";
            }
            else
            {
                nacti_data();
                klik = false;
                bt_zobraz.Text = "graf tlaku a větru";
            }
        }

        private void on_load(object sender, EventArgs e)
        {
            //---graf
            int delka = p.Size.Width;
            int sirka = p.Size.Height;

            lengthArea = delka - 2 * topX - 8;
            lengthArea2 = delka - topX - 91;
            heightArea = sirka - topY - 23 - 50;

            //image1 = new Bitmap(lengthArea + topX + 1, heightArea + topY + 20);
            image1 = new Bitmap(delka, sirka);
            graX = Graphics.FromImage(image1);
            p.Image = image1;
            graX.Clear(SystemColors.Control);
            graX.DrawImage(snih, 0, 0);
            p.Image = image1;

            //------graf
            nacti_data(); //nacte data
        }

        private void bt_menu_Click(object sender, EventArgs e)
        {
            /*if (MessageBox.Show("Chcete minimalizovat na pozadi?", "Dotaz", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
            {
                this.Hide();
            }
            else
            {
                //timer1.Enabled = false;
                this.Close();

            }*/
            this.Close();
        }

        

        
    }
}