mercredi 22 juin 2016

C# - Graphable Data Class - Best method for performing calculations on properties

I would like to have an object that represents graphable data. I couldn't find anything that exists that meets the requirements, so I started implementing it.

The class contains properties which represent values at increments along the x-axis, and a property for current units (two are possible). I used properties because I would like to access by name (instead of just a list of numbers). This seems like it could be overkill and slow things down.

I need to perform various calculations which are done at each x-axis increment. So far, I have tried using Reflection and just performing the calculation on all properties (except units). This seems repetitive.

I am not totally versed with lambda functions, but it seems like that is an option too.

Is there a better way to approach this? Is there something that exists already that I missed?

Below is the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Reflection;

namespace CustomDataType
{
    // two units for x-axis values
    public enum Units
    {
        Power = 1,
        dB = 0
    }

    public class GraphableData
    {
        // values along x-axis
        public double A50 { get; set; }
        public double A63 { get; set; }
        public double A80 { get; set; }
        public double A100 { get; set; }
        public double A125 { get; set; }
        public double A160 { get; set; }
        public double A200 { get; set; }

        // More values of the same...

        public Units units {get; set;}

        // Constructor for DataRow
        public GraphableData(DataRow Row)
        {
            try
            {
                //Retrive row information
                A50 = Convert.ToDouble(Row["A50"].ToString());
                A63 = Convert.ToDouble(Row["A63"].ToString());
                A80 = Convert.ToDouble(Row["A80"].ToString());
                A100 = Convert.ToDouble(Row["A100"].ToString());
                A125 = Convert.ToDouble(Row["A125"].ToString());
                A160 = Convert.ToDouble(Row["A160"].ToString());
                A200 = Convert.ToDouble(Row["A200"].ToString());

                // More values....


            }
            catch (Exception Ex)
            {
                // not sure what to do yet
            }

            units = TLUnits.dB;

        }

        // Constructor for emtpy dataset
        public GraphableData(TLUnits _units = TLUnits.dB)
        {
            A50 = 0;
            A63 = 0;
            A80  = 0;
            A100 = 0;
            A125 = 0;
            A160 = 0;
            A200 = 0;

            units = _units;
        }

        // Method 1:
        // Calculation on x-axis values using Reflection
        public void convertToPower()
        {
            if (units == TLUnits.Power)
            {
                return;
            }

            // run through properties and apply conversion
            PropertyInfo[] properties = typeof(GraphableData).GetProperties();

            foreach (PropertyInfo property in properties)
            {
                // skip units property
                if ( property.PropertyType == typeof(double) )
                {
                    double value = (double)property.GetValue(this, null);
                    double Power =  Math.Pow(10d, -0.1 * value);

                    property.SetValue(this, Power, null);
                }
            }
            units = TLUnits.Power;
        }


        // Method 2: 
        // Perform calculations on every property.
        public void convertTodB()
        {
            if (units == TLUnits.dB)
            {
                return;
            }

            A50 = Math.Round(-10 * Math.Log10(A50));
            A63 = Math.Round(-10 * Math.Log10(A63));
            A80 = Math.Round(-10 * Math.Log10(A80));
            A100 = Math.Round(-10 * Math.Log10(A100));
            A125 = Math.Round(-10 * Math.Log10(A125));
            A160 = Math.Round(-10 * Math.Log10(A160));
            A200 = Math.Round(-10 * Math.Log10(A200));

            units = TLUnits.dB;
        }

        // Method 3: (not implemented)
        // Perhaps use lambda functions to operate on
        // individual x-axis data. Pass in specific
        // calculation.


        // Other examples of calculations that will be done
        public static GraphableData addPaths(GraphableData Path1, GraphableData Path2)
        {
            GraphableData sumData = new GraphableData();

            // Add two datasets based on x-axis values

            return sumData;
        }

        public static GraphableData addPaths(GraphableData Path1, GraphableData Path2, GraphableData Path3)
        {
            GraphableData sumData = new GraphableData();

            // Add three datasets based on x-axis values

            return sumData;
        }

    }


}





Aucun commentaire:

Enregistrer un commentaire