Search code examples
c#dictionaryplotvisual-studio-2017zedgraph

How do I plot a dictionary (KeyValuePair) in a graph (zedgraph) in C#


I have a Dictionary with Key and Value Pairs. I need to create a Line Graph on zedGraph, plotting the Key in the X-Axis and the Value in the Y-Axis. I am using Visual Studio 2017.

the Key is a DateTime type. The Value is a string type.

I was thinking of creating 2 arrays, an X-Axis Array and a Y-Axis array.

Any other/quicker/more optimal suggestions?

I have already tried converting the DateTime values into Ticks, but i cannot create a loop to print them. It gives me an Error:

void addGraph(List<KeyValuePair<DateTime, string>> dataDic)
        {
            DateTime DT = new DateTime();


            long TimeTicks = DT.Ticks;


            foreach (var element in TimeTicks)
            {
                Console.WriteLine(element);
            }

Expectation: Output of all values in the TimeTicks variable/array.

Reality/Error: Severity Code Description Project File Line Suppression State Error CS1579 foreach statement cannot operate on variables of type 'long' because 'long' does not contain a public definition for 'GetEnumerator' TCD_Interface C:\Users\Z0044MTW\Desktop\TCD_Interface\TCD_Interface\Plant_OV.cs 156 Active


Solution

  • You cannot enumerate – use as source in a foreach loop – something that is not a collection (or looks very like a collection).

    A long is a singular value, not a collection, so cannot be enumerated.

    You need to iterate over the set of points you want to plot, not the values of the coordinates of those points.

    (Whether separate arrays extracted from the underlying dictionary makes sense depends on the details of the graph library.)