summaryrefslogtreecommitdiff
path: root/protocols/CurrencyRates/CurrencyRatesChart/Form1.cs
blob: d26457e39fc5be42f8baef11f8c8c7ae3afc65c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.VisualBasic.FileIO;
using System.Runtime.InteropServices;

namespace CurrencyRatesChart
{
    public partial class FormMirandaCurrencyRatesChart : Form
    {
        public FormMirandaCurrencyRatesChart()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string[] cmd_line_args = Environment.GetCommandLineArgs();

            for (int i = 1; i < cmd_line_args.Length; ++i)
            {
                string data_file = cmd_line_args[i];
                AddDataFromFile(data_file);
            }
        }

        private void dateFrom_ValueChanged(object sender, EventArgs e)
        {
            if (dateFrom.Value < dateTo.Value)
            {
                chartCurrencyRates.ChartAreas[0].AxisX.Minimum = dateFrom.Value.ToOADate();
                chartCurrencyRates.Invalidate();
            }
        }

        private void dateTo_ValueChanged(object sender, EventArgs e)
        {
            if (dateTo.Value > dateFrom.Value)
            {
                chartCurrencyRates.ChartAreas[0].AxisX.Maximum = dateTo.Value.ToOADate();
                chartCurrencyRates.Invalidate();
            }
        }

        int WM_COPYDATA = 0x4A;

        public struct COPYDATASTRUCT
        {
            public IntPtr dwData;
            public int cbData;
            [MarshalAs(UnmanagedType.LPWStr)]
            public String lpData;
        }

        protected override void WndProc(ref Message msg)
        {
            if (msg.Msg == WM_COPYDATA)
            {
                COPYDATASTRUCT cp = (COPYDATASTRUCT)Marshal.PtrToStructure(msg.LParam, typeof(COPYDATASTRUCT));

                if (/**(cp.dwData) == 0x1945 && */cp.lpData != null)
                {
                    AddDataFromFile(cp.lpData);
                }

            }
            base.WndProc(ref msg);
        }

        private void AddDataFromFile(string data_file)
        {
            using (TextFieldParser parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(data_file))
            {
                parser.TrimWhiteSpace = true;
                parser.TextFieldType = FieldType.Delimited;
                parser.SetDelimiters("\t");

                System.Windows.Forms.DataVisualization.Charting.Series series = null;
                while (true)
                {
                    string[] parts = parser.ReadFields();
                    if (parts == null)
                    {
                        break;
                    }

                    if (parts.Length >= 3)
                    {
                        string name = parts[0];
                        string date = parts[1];
                        string value = parts[2];

                        if (series == null)
                        {
                            if (chartCurrencyRates.Series.FindByName(name) == null)
                            {
                                chartCurrencyRates.Series.Add(name);
                                series = chartCurrencyRates.Series[name];
                                series.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
                            }
                            else
                            {
                                break;
                            }
                        }

                        DateTime dt;
                        double d;
                        if (Double.TryParse(value, out d) && DateTime.TryParse(date, out dt))
                        {
                            series.Points.AddXY(dt, d);

                            if (!dateMin.HasValue)
                            {
                                dateMin = new DateTime();
                                dateMin = dt;
                            }
                            else if (dt < dateMin)
                            {
                                dateMin = dt;
                            }

                            if (!dateMax.HasValue)
                            {
                                dateMax = new DateTime();
                                dateMax = dt;
                            }
                            else if (dt > dateMax)
                            {
                                dateMax = dt;
                            }
                        }
                    }
                }
            }

            if (dateMin.HasValue && dateMax.HasValue)
            {
                chartCurrencyRates.ChartAreas[0].AxisX.Minimum = dateMin.Value.ToOADate();
                chartCurrencyRates.ChartAreas[0].AxisX.Maximum = dateMax.Value.ToOADate();

                dateFrom.Value = dateMin.Value;
                dateTo.Value = dateMax.Value;
            }
        }

        private DateTime? dateMin = null;
        private DateTime? dateMax = null;
    }
}