//Timer Counter //This counter is used to stop the timer when we want it to //So, it helps to make the animation go longer/shorter, faster/slower //Note this is outside of the Main() //static int myTimerCounter = 0; //Timer Method that runs every time the timer elapses public static void OnTimedEvent(Object source, ElapsedEventArgs e) { try { //define each key value from dictionary foreach (var key in Graphs.graphs) { //display the key value on the screen Console.Write(key.Key); Console.Write(" "); //We determine the sum of all rating values for key = restaurant int a = 0; decimal b = 0; for (int i = 0; i < 100; i++) { a = key.Value[i]; b += a; } Console.Write(" "); //we find the average rating for the restaurant int mid = Decimal.ToInt32(Math.Round(b / 100, MidpointRounding.AwayFromZero)); //define each value from dictionary foreach (int val in Graphs.graphs[key.Key]) { var myBackgroundColor = ConsoleColor.White; //Setting the bar graph colors Graphs g = new Graphs(); var myBarGraphColor = g.Color(val); //Setting the bar graph colors var theRating = val; Console.BackgroundColor = myBarGraphColor; //Move the cursor back to the left, where it started to draw the animation to begin with. //Once we redraw, and redraw, and redraw, it will look animated. //We are doing this because we are uisng a Console.Write to stay on the same line, so we move back, and redraw over top of the old one. Console.CursorLeft = 40; //Create bar graph, not the bar graph background //We create the bar we want first, and the background second. for (int i = 0; i < theRating; i++) { //We could run the below code here if we wanted, but we just set it once above instead of each time here. //Console.BackgroundColor = myBarGraphColor; //This creates a colored bar graph of spaces, so if you have a 5/10, you will get 5 colored spaces. Console.Write(" "); Thread.Sleep(0); } //Console.Write(" "); //Set total number for the length of the bar graph, which is also the background int myTotalNumber = 10; //Set bar graph background color //We can set the color once here, or set it over and over again in the loop below. Console.BackgroundColor = myBackgroundColor; //Draw bar graph background //The background is not seen around the edges of the bar, or also with the foreground color of the bar. We only see one color at a time. //So, we just start drawing the background after the final drawing of the bar graph based on the data. //For Example...if we have 5/10, then the bar graph is 1-5 and the background is 6-10. for (int ii = theRating; ii < myTotalNumber; ii++) { //We could run the below code here if we wanted, but we just set it once above instead of each time here. //Console.BackgroundColor = myBackgroundColor; //This creates a colored background of spaces, so if you have a 5/10, you will get 5 colored spaces to make the background after the 5 colored spaces that made up the bar. Console.Write(" "); } } // display in the graph the total / average rating value for the restaurant Graphs v = new Graphs(); var myBarGraphColor1 = v.Color(mid); //Setting the bar graph colors Console.BackgroundColor = myBarGraphColor1; //Move the cursor back to the left, where it started to draw the animation to begin with. //Once we redraw, and redraw, and redraw, it will look animated. //We are doing this because we are uisng a Console.Write to stay on the same line, so we move back, and redraw over top of the old one. Console.CursorLeft = 40; for (int i = 0; i < mid; i++)// { //We could run the below code here if we wanted, but we just set it once above instead of each time here. //Console.BackgroundColor = myBarGraphColor; //This creates a colored bar graph of spaces, so if you have a 5/10, you will get 5 colored spaces. Console.Write(" "); } var myBackgroundColor1 = ConsoleColor.White; Console.BackgroundColor = myBackgroundColor1; //Draw bar graph background //The background is not seen around the edges of the bar, or also with the foreground color of the bar. We only see one color at a time. //So, we just start drawing the background after the final drawing of the bar graph based on the data. //For Example...if we have 5/10, then the bar graph is 1-5 and the background is 6-10. for (int ii = mid; ii < 10; ii++)// { //We could run the below code here if we wanted, but we just set it once above instead of each time here. //Console.BackgroundColor = myBackgroundColor; //This creates a colored background of spaces, so if you have a 5/10, you will get 5 colored spaces to make the background after the 5 colored spaces that made up the bar. Console.Write(" "); } Console.ResetColor(); //Reset Color //Move the cursor back to the left, where it started to draw the animation to begin with. //Once we redraw, and redraw, and redraw, it will look animated. //We are doing this because we are uisng a Console.Write to stay on the same line, so we move back, and redraw over top of the old one. Console.CursorLeft = 60; Console.Write(mid); //Display the average rating of the restaurant Console.WriteLine(""); } } finally { } }
//Timer Method that runs every time the timer elapses private static void OnTimedEvent(Object source, ElapsedEventArgs e) { //Setting the bar graph colors var myBackgroundColor = ConsoleColor.White; myTimerCounter++; // We assign rating values to get animation var theRating = Rating(iiii); //Setting the colors of the histogram depending on the rating value Graphs g = new Graphs(); var myBarGraphColor = g.Color(theRating); Console.BackgroundColor = myBarGraphColor; //Move the cursor back to the left, where it started to draw the animation to begin with. //Once we redraw, and redraw, and redraw, it will look animated. //We are doing this because we are uisng a Console.Write to stay on the same line, so we move back, and redraw over top of the old one. Console.CursorLeft = 40; //Create bar graph, not the bar graph background //We create the bar we want first, and the background second. for (int ii = 1; ii <= theRating; ii++) { //We could run the below code here if we wanted, but we just set it once above instead of each time here. //Console.BackgroundColor = myBarGraphColor; //This creates a colored bar graph of spaces, so if you have a 5/10, you will get 5 colored spaces. Console.Write(" "); } //Set total number for the length of the bar graph, which is also the background int myTotalNumber = 10; //Set bar graph background color //We can set the color once here, or set it over and over again in the loop below. Console.BackgroundColor = myBackgroundColor; //Draw bar graph background //The background is not seen around the edges of the bar, or also with the foreground color of the bar. We only see one color at a time. //So, we just start drawing the background after the final drawing of the bar graph based on the data. //For Example...if we have 5/10, then the bar graph is 1-5 and the background is 6-10. for (int iii = theRating; iii < myTotalNumber; iii++) { //We could run the below code here if we wanted, but we just set it once above instead of each time here. //Console.BackgroundColor = myBackgroundColor; //This creates a colored background of spaces, so if you have a 5/10, you will get 5 colored spaces to make the background after the 5 colored spaces that made up the bar. Console.Write(" "); } //increase iii by 1 to get the next rating value iiii++; //After a bit of time, stop the animation //We change this as well for longer/shorter, faster/slower //Right now, the animation, or resetting/redrawing of the graph will happen 50 times, once every 50/1000 seconds. if (myTimerCounter == 99 || iiii > 100) { //Stop Timer myAnimationTimer.Stop(); //Add in final database variable code here //If the database data says 8/10, you would set the variable to 8 here. //Once the animation is over, redraw the bar graph one more time with the actual bar graph data Console.BackgroundColor = g.Color(mid); Console.CursorLeft = 40; //Create bar graph, not the bar graph background //We create the bar we want first, and the background second. for (int ii = 1; ii <= mid; ii++) { //We could run the below code here if we wanted, but we just set it once above instead of each time here. //Console.BackgroundColor = myBarGraphColor; //This creates a colored bar graph of spaces, so if you have a 5/10, you will get 5 colored spaces. Console.Write(" "); } //Set bar graph background color //We can set the color once here, or set it over and over again in the loop below. Console.BackgroundColor = myBackgroundColor; //Draw bar graph background //The background is not seen around the edges of the bar, or also with the foreground color of the bar. We only see one color at a time. //So, we just start drawing the background after the final drawing of the bar graph based on the data. //For Example...if we have 5/10, then the bar graph is 1-5 and the background is 6-10. for (int iii = mid; iii < myTotalNumber; iii++) { //We could run the below code here if we wanted, but we just set it once above instead of each time here. //Console.BackgroundColor = myBackgroundColor; //This creates a colored background of spaces, so if you have a 5/10, you will get 5 colored spaces to make the background after the 5 colored spaces that made up the bar. Console.Write(" "); } //Reset myTimerCounter to 0 for another restaurant myTimerCounter = 0; //Reset iii to 0 for another restaurant iiii = 0; //Reset color Console.ResetColor(); //Show the cursor again so the user can do what you need them to do. Console.CursorVisible = true; } }