7_A

Given 2 variables from a csv compute and represent the statistical regression lines (X to Y and viceversa) and the scatterplot.

header

7_A assignament

Request

Given 2 variables from a csv compute and represent the statistical regression lines (X to Y and viceversa) and the scatterplot. Optionally, represent also the histograms on the “sides” of the chart (one could be draw vertically and the other one horizontally, in the position that you prefer). [Remember that all our charts must alway be done within “dynamic viewports” (movable/resizable rectangles). No third party libraries, to ensure ownership of creative process. May choose the language you prefer.].

My Solution

Code in C#

Class Main Form in C#

  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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
public partial class Bivarianteform : Form
    {
        public Bivarianteform()
        {
            InitializeComponent();
            contb = new Bitmap(755, 681);
            g2 = Graphics.FromImage(contb);
           
        }


        Dictionary<int, ElementoDisribuzione> csvContent = new Dictionary<int, ElementoDisribuzione>();
        Distribuzione distr = new Distribuzione();
        MediaCalOnline medie = new MediaCalOnline();
        String[,] bivariantMatrix;
        List<String> attributename = new List<string>();
        List<String> bivariante = new List<string>();//per ora poi diventera n variante;
        Bitmap contb;
        Graphics g2;
       
        List<PointF> points = new List<PointF>();
        List<PointF> regressiony = new List<PointF>();
        List<PointF> regressionx = new List<PointF>();

        Matrix m = new Matrix();
        Matrix m2 = new Matrix();
        Matrix m3 = new Matrix();
        int nr, nc;

        //Distributions 
        SortedDictionary<Tuple<double, double>, int> firstdistr;
        SortedDictionary<Tuple<double, double>, int> seconddistr;
        bool movable = false;
        bool resiable = false;
        // movable view port
        int contgleft = 0;
        int contgtop = 50;
        int contgwid = 400;
        int contgheight = 400;
        int mouseDeltax=0;
        int mouseDeltay=0;
        // window
        double minX_Window = 0;
        double maxX_Window = 400;
        double minY_Window = 0;
        double maxY_Window = 400;


        Rectangle viewPortcontig=new Rectangle(0,50, 400, 400);






        private void button1_Click(object sender, EventArgs e)


        {

            var filePath = string.Empty;
            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "csv files (*.csv)|*.csv|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                //Get the path of specified file
                filePath = openFileDialog1.FileName;

                //Read the contents of the file into a stream
                using (TextFieldParser csvParser = new TextFieldParser(filePath))
                {
                    csvParser.CommentTokens = new string[] { "#" };
                    csvParser.SetDelimiters(new string[] { "," });
                    csvParser.HasFieldsEnclosedInQuotes = true;

                    // Save the row with the column names
                    string[] fieldsNames = csvParser.ReadFields();
                    attributename.AddRange(fieldsNames);
                    int i = 0;
                    while (!csvParser.EndOfData)
                    {
                        // Read current line fields, pointer moves to the next line.

                        string[] fields = csvParser.ReadFields();
                        ElementoDisribuzione elem = new ElementoDisribuzione(fields[0]);
                        int j = 0;
                        foreach (String field in fields)
                        {
                            if (!String.IsNullOrEmpty(field))
                            {
                                double tmp;
                                if (Double.TryParse(field, out tmp)) { elem.setVariable(fieldsNames[j], new Tuple<Object, Type>(tmp, tmp.GetType())); medie.addAttribute(fieldsNames[j], tmp); }
                                else elem.setVariable(fieldsNames[j], new Tuple<Object, Type>(field, field.GetType()));
                            }
                            j++;
                        }
                        csvContent.Add(i, elem);
                        distr.addElementoDef(elem);
                        i++;
                    }
                }
                button2.Visible = true;
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            ;

            foreach (var elem in attributename)
                contextMenuStrip1.Items.Add(elem);
            contextMenuStrip1.Visible = true;

        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {

        }

        private void contextMenuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

            bivariante.Add(e.ClickedItem.Text);
          
            button2.Visible = false;
            foreach (var elem in attributename)
                contextMenuStrip2.Items.Add(elem);
            contextMenuStrip2.Visible = true;
        }

        private void contextMenuStrip2_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

            bivariante.Add(e.ClickedItem.Text);

            double mediax = 0;
            double mediay = 0;
            double sigmax2=0;
            double sigmay2=0;
            double sigmaxy=0;

            bool isDouble = false;

            List<Double> valuesx = new List<double>(0);
            List<Double> valuesy = new List<double>(0);
            medie.getMedia(bivariante.ElementAt(0), out mediax);
            //se la media è diversa da zero sono sicuro che è double
            medie.getMedia(bivariante.ElementAt(1), out mediay);

            if (mediax != 0)
            {
                if (mediay != 0) {


                    isDouble = true;
                    Tuple<Object, Type> tmp;
                    foreach (var elm in csvContent.Values)
                    {
                        elm.getVariable(bivariante.ElementAt(0), out tmp);
                        valuesx.Add((double)tmp.Item1);

                        elm.getVariable(bivariante.ElementAt(1), out tmp);
                        valuesy.Add((double)tmp.Item1);

                    }
                    medie.getdDeviation2(bivariante.ElementAt(0), valuesx, out sigmax2);
                    distr.getdistribuzioneN(bivariante.ElementAt(0), out firstdistr);
                    medie.getdDeviation2(bivariante.ElementAt(1), valuesy, out sigmay2);    
                    distr.getdistribuzioneN(bivariante.ElementAt(1), out seconddistr);
                    medie.getbivariantDeviation(bivariante.ElementAt(0), bivariante.ElementAt(1), valuesx,valuesy, out sigmaxy);

                    for (int i = 0; i <= maxX_Window; i++)
                    {
                        regressionx.Add(new PointF(i, (float) (((sigmaxy / sigmax2) * (i - mediax)) + mediay)));
                            
                     }

                    for (int i = 0; i <= maxY_Window; i++)
                    {
                        regressiony.Add(new PointF((float)(((sigmaxy / sigmay2) * (i - mediay)) + mediax), i));

                    }



                } }

            bivariantMatrix = distr.getbivariantmatrix(bivariante, csvContent.Values, out nr, out nc);            
            this.button3.Visible = true;


        }


        
        private void button3_Click(object sender, EventArgs e)
        {
            creaGrafici();
        }

        private void creaGrafici()
        {
          
            foreach (var elem in csvContent)
            {
                Tuple<Object, Type> a, b;

                float x, y;
                elem.Value.getVariable(bivariante.ElementAt(0), out a);
                elem.Value.getVariable(bivariante.ElementAt(1), out b);
                x = (float)(double)a.Item1;
                y = (float)(double)b.Item1;


                points.Add(new PointF(x, y));

            }

          
                
            
            //contingency
            createconting(g2, 0, 50,400,400);

        }

        private void createconting(Graphics g2, int dinamicleft, int dinamictop,int contgw,int contgh)
        {

            viewPortcontig.X = dinamicleft;
            viewPortcontig.Y = dinamictop;
            viewPortcontig.Width = contgw;
            viewPortcontig.Height = contgh;
            //viewPortcontig.Location= new Point(viewPortcontig.Location.X+dinamicleft,viewPortcontig.Location.Y+dinamictop);
            g2.Clear(BackColor);
            g2.FillRectangle(Brushes.White, viewPortcontig);
            // window drow 
            g2.DrawLine(new Pen(Color.Black, 2), viewPortcontig.X, viewPortcontig.Y, viewPortcontig.X, (viewPortcontig.Y + viewPortcontig.Height));
            g2.DrawLine(new Pen(Color.Black, 2), viewPortcontig.X, (viewPortcontig.Y + viewPortcontig.Height), (viewPortcontig.X + viewPortcontig.Width), (viewPortcontig.Y + viewPortcontig.Height));

            m.Reset();
            m.Translate(-(int)minX_Window, -(int)minY_Window, MatrixOrder.Append);
            m.Scale((int)(viewPortcontig.Width / (maxX_Window - minX_Window)), (int)(-viewPortcontig.Height / (maxY_Window - minY_Window)), MatrixOrder.Append);
            m.Translate(viewPortcontig.Left, viewPortcontig.Top + viewPortcontig.Height, MatrixOrder.Append);
            
         
            PointF[] myPoints = points.ToArray();
            PointF[] myPointsreal = points.ToArray();
            PointF[] pRegressionx = regressionx.ToArray();
            PointF[] pRegressiony = regressiony.ToArray();


            int i = 0;
            m.TransformPoints(myPoints);
            m.TransformPoints(pRegressionx);
            m.TransformPoints(pRegressiony);

            foreach (PointF punto in myPoints)
            {
                if (myPointsreal[i].X <= maxX_Window && myPointsreal[i].Y <= maxY_Window)
                {
                    g2.FillEllipse(Brushes.Black, new Rectangle(new Point((int)(punto.X - 2), (int)(punto.Y - 2)), new Size(4, 4)));

                    g2.DrawString(myPointsreal[i].ToString(), new Font("Arial", 10), Brushes.Black, (int)(punto.X), (int)(punto.Y));
                    g2.FillEllipse(Brushes.Red, new Rectangle(new Point((int)(punto.X - 2), (int)(viewPortcontig.Y + viewPortcontig.Height - 2)), new Size(4, 4)));
                    g2.FillEllipse(Brushes.Red, new Rectangle(new Point((int)(viewPortcontig.X), (int)(punto.Y - 2)), new Size(4, 4)));


                }
                i++; ;

            }
            foreach (PointF punto in pRegressionx)
            {
                g2.FillEllipse(Brushes.Red, new Rectangle(new Point((int)(punto.X - 2), (int)(punto.Y - 2)), new Size(4, 4)));
            }
            foreach (PointF punto in pRegressiony)
            {
                g2.FillEllipse(Brushes.Green, new Rectangle(new Point((int)(punto.X - 2), (int)(punto.Y - 2)), new Size(4, 4)));
            }




            pictureBox2.Image = contb ;
        }

        private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
        {
            if (   (e.Location.Y>=viewPortcontig.Top &&e.Location.Y <= (viewPortcontig.Top+viewPortcontig.Height))&&(e.Location.X >= viewPortcontig.Left && e.Location.X <= (viewPortcontig.Left + viewPortcontig.Width)))
            {
                if (e.Button == MouseButtons.Left)
                    movable = true;
                if (e.Button == MouseButtons.Right)
                    resiable = true;
                mouseDeltax = e.Location.X;
                mouseDeltay = e.Location.Y;

            }
           

        }

        private void pictureBox2_MouseLeave(object sender, EventArgs e)
        {
            resiable = false;
            movable = false;
        }

        private void pictureBox2_MouseMove(object sender, MouseEventArgs e)
        {
            if (movable == true)
            {
              
                contgleft = e.Location.X;
                contgtop = e.Location.Y;
                if (contgleft <= 0) contgleft = 0;
                if (contgtop <= 20) contgtop = 20;
                createconting(g2, contgleft, contgtop, contgwid, contgheight);
            }
            if (resiable == true)
            {
                mouseDeltax = -mouseDeltax + e.Location.X ;
                mouseDeltay = -mouseDeltay + e.Location.Y;
                contgwid += mouseDeltax/40;
                contgheight += mouseDeltay/40;
                createconting(g2, contgleft, contgtop, contgwid, contgheight);
            }

               
                

            

        }

        private void pictureBox2_MouseUp(object sender, MouseEventArgs e)
        {
            resiable = false;
            movable = false;
        }

      
    }
    }
comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy