MS Charts C# DataSource from array or List

16,638

In your XY class, instead of the private fields:

  private double X;
  private double Y;

use public properties:

 public double X { get; private set; }
 public double Y { get; private set; }

DataBinding does not work on fields.

Share:
16,638
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    i want to fill the data of the charts from values i have in 2D array, one column will present the X-axis and the second is present the Y-axis.. i did it, but its not reading from the array, it gives me the default line when i run the application, i found a solution using List<>, i had an error, so if any one could help me in this, i will be thankful :D

    this is the code

    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;
    
    namespace ICS381Project
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                int[,] AndFunction = { { 0, 0, 0 }, { 0, 1, 0 }, { 1, 0, 0 }, { 1, 1, 1 } }; // intialized the function
                int[,] D_n = new int[4, 1]; // creating the D(n)
                int[,] x_n = new int[4, 3]; // creating X(n) vectors X1 --> x4
                int[,] W_n = { { 0, 0, 0 } }; // creating and intiallizing W(n) vectors W1 --> w4
                int[,] W_n_1 = { { 0, 0, 0 } };
                int[,] Delta_W_n = { { 0, 0, 0 } };
                int wx = 0; // 
                int Y_n = 0; //
                int d_y = 0; // 
                for (int i = 0; i < 4; i++)
                {
                    D_n[i, 0] = AndFunction[i, 2];
                }
                for (int i = 0; i < 4; i++)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        if (j == 2)
                            x_n[i, j] = 1;
                        else
                            x_n[i, j] = AndFunction[i, j];
                    }
                }
                int step = 0;
                int CheckIfNoLearning = 0;
                int RowsPointer = 0;
                while (CheckIfNoLearning < 4)
                {
    
                    //Console.Write("step= " + step+1 + "\n");
                    wx = (x_n[RowsPointer, 0] * W_n[0, 0]) + (x_n[RowsPointer, 1] * W_n[0, 1]) + (x_n[RowsPointer, 2] * W_n[0, 2]);
                    //Console.Write("[ " + x_n[RowsPointer, 0] + ", " + x_n[RowsPointer, 1] + ", " + x_n[RowsPointer, 2] + "] \t");
                    //Console.Write("[ " + W_n[0, 0] + ", " + W_n[0, 1] + ", " + W_n[0, 2] + "] \t");
                    //Console.Write("" + wx + "\t");
                    if (wx < 0)
                        Y_n = 0;
                    else
                        Y_n = 1;
                   // Console.Write("" + Y_n + "\t");
                    d_y = D_n[RowsPointer, 0] - Y_n;
                   // Console.Write("" + d_y + "\t");
                    Delta_W_n[0, 0] = d_y * x_n[RowsPointer, 0];
                    Delta_W_n[0, 1] = d_y * x_n[RowsPointer, 1];
                    Delta_W_n[0, 2] = d_y * x_n[RowsPointer, 2];
                   // Console.Write("[ " + Delta_W_n[0, 0] + ", " + Delta_W_n[0, 1] + ", " + Delta_W_n[0, 2] + "] \t");
                    for (int i = 0; i < 3; i++)
                    {
                        W_n_1[0, i] = W_n[0, i] + Delta_W_n[0, i];
                    }
                    //Console.Write("[ " + W_n_1[0, 0] + ", " + W_n_1[0, 1] + ", " + W_n_1[0, 2] + "] \n");
    
                    if (W_n_1[0, 0] == W_n[0, 0] && W_n_1[0, 1] == W_n[0, 1] && W_n_1[0, 2] == W_n[0, 2] && CheckIfNoLearning == RowsPointer)
                        CheckIfNoLearning++;
                    else
                        CheckIfNoLearning = 0;
                    W_n[0, 0] = W_n_1[0, 0];
                    W_n[0, 1] = W_n_1[0, 1];
                    W_n[0, 2] = W_n_1[0, 2];
                    //Console.Write("W_n= [ " + W_n[0, 0] + ", " + W_n[0, 1] + ", " + W_n[0, 2] + "] \n");
                    RowsPointer = (RowsPointer + 1) % 4;
                    step++;
                }
    
                double[,] equation = {{-10,0},{-9,0},{-8,0},
                                  {-7,0},{-6,0},{-5,0},
                                  {-4,0},{-3,0},{-2,0},
                                  {-1,0},{0,0},{1,0},
                                  {2,0},{3,0},{4,0},
                                  {5,0},{6,0},{7,0},
                                  {8,0},{9,0},{10,0}};
                List<XY> xy = new List<XY>();
                for (int i = 0; i < 21; i++)
                {
                    equation[i, 1] = (-1 * (W_n_1[0, 1] * equation[i, 0] + W_n_1[0, 2])) / W_n_1[0, 0];
                    xy.Add(new XY(equation[i,0], equation[i,1]));
                }
    
                chart1.DataSource = xy;
                chart1.Series[0].XValueMember = "Y";
                chart1.Series[0].YValueMembers = "X";
                chart1.ChartAreas["ChartArea1"].AxisX.Interval = 1;
                chart1.DataBind();
    
    
    
    
    
            }
        }
        public class XY
        {
            private double X;
            private double Y;
    
            public double DayOfWeek
            {
                get { return X; }
                set { X = value; }
            }
    
            public double Sales
            {
                get { return Y; }
                set { Y = value; }
            }
            public XY(double X, double Y)
            {
                this.X = X;
                this.Y = Y;
            }
        }
    }