{"id":1959,"date":"2018-03-27T13:58:28","date_gmt":"2018-03-27T13:58:28","guid":{"rendered":"https:\/\/mindfusion.eu\/blog\/?p=1959"},"modified":"2018-03-27T14:07:36","modified_gmt":"2018-03-27T14:07:36","slug":"real-time-chart-in-winforms","status":"publish","type":"post","link":"https:\/\/mindfusion.dev\/blog\/real-time-chart-in-winforms\/","title":{"rendered":"Real-Time Chart in WinForms"},"content":{"rendered":"<p>In this sample we will build a line chart that reads its data in real time and gets updated each second.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/mindfusion.dev\/samples\/winforms\/charting\/realtime-winforms-chart.gif\" alt=\"Real-time WinForms Chart\" \/><\/p>\n<p>We start by creating an empty WinForms project. We assume you have installed the Charting for WinForms component and you can see a list with chart components in the VisualStudio toolbox.<\/p>\n<p>Drag the <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.winforms\/index.htm?T_MindFusion_Charting_WinForms_LineChart_1.htm\" title=\"MindFusion WinForms Chart API Reference: LineChart\">LineChart<\/a> control and drop it on the form. You should now see references to the three libraries that you need automatically added to your project:<\/p>\n<ul>\n<li>MindFusion.Charting;<\/li>\n<li>MindFusion.Charting.WinForms;<\/li>\n<li>MindFusion.Common;<\/li>\n<\/ul>\n<p><strong>1. Series and Data<\/strong><\/p>\n<p>Our chart will render time stamps at the X-axis. That&#8217;s why we choose the <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.winforms\/index.htm?T_MindFusion_Charting_DateTimeSeries.htm\" title=\"MindFusion Charting for WinForms API Reference: DateTimeSeries\">DateTimeSeries<\/a> class to create the series for the chart. The constructor requires two lists &#8211; one with DateTime values and one with double values. They provide data for the X and Y axes respectively. We must also provide two more parameters: one for the start and one for the end of the time period.<\/p>\n<pre>\r\nList<DateTime> dates = new List<DateTime>();\r\nList<double> values = new List<double>();\r\n<\/pre>\n<p><\/p>\n<p>In order to mimic the real-time functionality we will use a timer. Our timer will generate a random number each second. At the beginning we will have a list with no values. Gradually, we will fill the values and once we reach the count of 100 we will start to delete the first value and add a new value at the end of the arrays.<\/p>\n<p>That&#8217;s how we initialize the timer:<\/p>\n<pre>\r\nRandom ran;\r\nran = new Random();         \r\n        \r\nTimer dataTimer = new Timer();\r\ndataTimer.Tick += new EventHandler(GenerateData);\r\ndataTimer.Interval = 1000;\r\ndataTimer.Start();\r\n<\/pre>\n<p><\/p>\n<p>Let&#8217;s look at the GenerateData method:<\/p>\n<pre>\r\nprivate void GenerateData(object sender, EventArgs e)\r\n    {\r\n         dates.Add(DateTime.Now);\r\n         values.Add(2 + ran.NextDouble() * 8.0);\r\n\r\n         if (dates.Count > 100)\r\n         {\r\n             dates.RemoveAt(0);\r\n             values.RemoveAt(0);\r\n         }\r\n...\r\n}<\/pre>\n<p><\/p>\n<p>There we always add the current DateTime. Since the timer ticks every second that&#8217;s exactly what we want. Once the values are 100, we start to remove values at the beginning of the list.<\/p>\n<p>Now let&#8217;s create the DateTime series. It requires parameters for the start and end of the period. We always allocate the first and last DateTime values as the bounds of the DateTime data for this series:<\/p>\n<pre>\r\nDateTimeSeries series = new DateTimeSeries(dates, values, dates[0], dates[dates.Count-1]);\r\n<\/pre>\n<p><\/p>\n<p>Then we see if we have already added a Series and if so &#8211; replace it. If no Series has been added &#8211; add the new one:<\/p>\n<pre>\r\nif (lineChart1.Series.Count > 0)\r\n       lineChart1.Series[0] = series;\r\nelse\r\n       lineChart1.Series.Add(series);\r\n<\/pre>\n<p><\/p>\n<p>By default the <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.winforms\/index.htm?T_MindFusion_Charting_DateTimeSeries.htm\" title=\"JavaScript Schdeuler API Reference: DateTimeSeries\">DateTimeSeries<\/a> renders its values from the X-axis &#8211; the DateTime instances as labels at the X-axis. We can customize how they look with the <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.winforms\/index.htm?P_MindFusion_Charting_DateTimeSeries_DateTimeFormat_0.htm\" title=\"MindFusion Charting for WinForms API Reference: DateTimeFormat\">DateTimeFormat<\/a> property. The predefined <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.winforms\/index.htm?P_MindFusion_Charting_DateTimeSeries_DateTimeFormat_0.htm\" title=\"MindFusion Charting for WinForms API Reference: DateTimeFormat\">DateTimeFormat<\/a> members don&#8217;t have an option where the values are rendered as &#8220;14:23:34&#8221; as a time stamp. So, we choose as <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.winforms\/index.htm?P_MindFusion_Charting_DateTimeSeries_DateTimeFormat_0.htm\" title=\"MindFusion Charting for WinForms API Reference: DateTimeFormat\">DateTimeFormat<\/a> &#8220;CustomDateTime&#8221; and use the <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.winforms\/index.htm?P_MindFusion_Charting_DateTimeSeries_CustomDateTimeFormat_0.htm\" title=\"MindFusion Charting for WinForms API Reference: CustomDateTime\">CustomDateTime<\/a> to specify the format:<\/p>\n<pre>\r\nseries.DateTimeFormat = DateTimeFormat.CustomDateTime;\r\nseries.CustomDateTimeFormat = \"H:mm:ss\"; \r\n<\/pre>\n<p><\/p>\n<p><strong>2. The X-Axis<\/strong><\/p>\n<p>We want at each moment only the last 10 values to be visible. The other 90 or however they are should be rendered but the user must scroll to see them. We achieve with the MinValue and MaxValue properties of the DateTimeSeries:<\/p>\n<pre>\r\nseries.MinValue = 0;\r\nseries.MaxValue = 0.1 * dates.Count;\r\n<\/pre>\n<p><\/p>\n<p>In order to render only the last 10 series, we use the XAxis properties &#8211; <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.winforms\/index.htm?P_MindFusion_Charting_Axis_MaxValue_0.htm\" title=\"MindFusion Charting for WinForms API Reference: MaxValue\">MaxValue<\/a> and <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.winforms\/index.htm?P_MindFusion_Charting_Axis_MinValue_0.htm\" title=\"MindFusion Charting for WinForms API Reference: MinValue\">MinValue<\/a> Once the <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.winforms\/index.htm?P_MindFusion_Charting_Axis_MaxValue_0.htm\" title=\"MindFusion Charting for WinForms API Reference: MaxValue\">MaxValue<\/a> of the DateTimeSeries is more than 1, which means that at least 10 values have been added, we adjust the visible range of values at the X-axis so that only the last 10 are visible:<\/p>\n<pre>\r\nif (series.MaxValue > 1)\r\n  {\r\n      lineChart1.XAxis.MaxValue = series.MaxValue;\r\n      lineChart1.XAxis.MinValue = series.MaxValue - 1.0;\r\n  }\r\n<\/pre>\n<p><\/p>\n<p>We also set the <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.winforms\/index.htm?P_MindFusion_Charting_Axis_Title_0.htm\" title=\"MindFusion Charting for WinForms API Reference: Title\">Title<\/a> for this axis:<\/p>\n<pre>\r\nlineChart1.XAxis.Title = \"Time\";\r\n<\/pre>\n<p>\nLet&#8217;s hide the numeric values from rendering at the top of the DateTime stamps with the <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.winforms\/index.htm?P_MindFusion_Charting_WinForms_BiaxialChart_ShowXCoordinates_0.htm\" title=\"MindFusion Charting for WinForms API Reference: ShowXCoordinates\">ShowXCoordinates<\/a> property:<\/p>\n<pre>\r\nlineChart1.ShowXCoordinates = false; \r\n<\/pre>\n<p><\/p>\n<p><strong>3. The Y-Axis<\/strong><\/p>\n<p>By default the range of an axis is calculated based on the data. Let&#8217;s make our chart render the line graphics more to the beginning of the axis by increasing the <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.winforms\/index.htm?P_MindFusion_Charting_Axis_MaxValue_0.htm\" title=\"MindFusion Charting for WinForms API Reference: MaxValue\">MaxValue<\/a> of the Y-axis while we fix the <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.winforms\/index.htm?P_MindFusion_Charting_Axis_MinValue_0.htm\" title=\"MindFusion Charting for WinForms API Reference: MinValue\">MinValue<\/a> at 0:<\/p>\n<pre>\r\nlineChart1.YAxis.MinValue = 0;\r\nlineChart1.YAxis.MaxValue = 20;\r\nlineChart1.YAxis.Interval = 2;\r\n<\/pre>\n<p><\/p>\n<p>We also change the axis <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.winforms\/index.htm?P_MindFusion_Charting_Axis_Title_0.htm\" title=\"MindFusion Charting for WinForms API Reference: Title\">Title<\/a> and we use <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.winforms\/index.htm?P_MindFusion_Charting_Axis_NumberFormat_0.htm\" title=\"MindFusion Charting for WinForms API Reference: NumberFormat\">NumberFormat<\/a> property to render the intervals with floating points:<\/p>\n<pre>\r\nlineChart1.YAxis.Title = \"Visitors (in thousands)\";\r\nlineChart1.YAxis.NumberFormat = \"N\";\r\n<\/pre>\n<p><\/p>\n<p><strong>4. Grid<\/strong><\/p>\n<p>Let&#8217;s render vertical grid stripes. We want them to be dashed and light gray. We use <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.winforms\/index.htm?P_MindFusion_Charting_WinForms_BiaxialChart_GridType_0.htm\" title=\"MindFusion Charting for WinForms API Reference: GridType\">GridType<\/a> <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.winforms\/index.htm?P_MindFusion_Charting_Theme_GridColor2_0.htm\" title=\"MindFusion Charting for WinForms API Reference: GridColor\">GridColor<\/a> and <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.winforms\/index.htm?P_MindFusion_Charting_Theme_GridLineStyle_0.htm\" title=\"MindFusion Charting for WinForms API Reference: GridLineStyle\">GridLineStyle<\/a> to customize the grid:<\/p>\n<pre>\r\nlineChart1.GridType = GridType.Vertical;\r\nlineChart1.Theme.GridLineStyle = System.Drawing.Drawing2D.DashStyle.Dash;\r\nlineChart1.Theme.GridLineColor = Color.FromArgb(192, 192, 192);\r\n<\/pre>\n<p><\/p>\n<p>Note that <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.winforms\/index.htm?P_MindFusion_Charting_Theme_GridColor1_0.htm\" title=\"MindFusion Charting for WinForms API Reference: GridColor\">GridColor<\/a> and <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.winforms\/index.htm?P_MindFusion_Charting_Theme_GridLineStyle_0.htm\" title=\"MindFusion Charting for WinForms API Reference: GridLineStyle\">GridLineStyle<\/a> are properties of the <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.winforms\/index.htm?T_MindFusion_Charting_Theme_0.htm\" title=\"MindFusion Charting for WinForms API Reference: Theme\">Theme<\/a> property of <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.winforms\/index.htm?T_MindFusion_Charting_WinForms_LineChart_1.htm\" title=\"MindFusion Charting for WinForms API Reference: LineChart\">LineChart<\/a> add one extra &#8211; stop the grid stripes from moving around when the user scrolls the chart along the axis:<\/p>\n<pre>\r\nlineChart1.PinGrid = true;\r\n<\/pre>\n<p><\/p>\n<p><strong>5. Legend<\/strong><\/p>\n<p>The legend gets its labels from the <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.winforms\/index.htm?P_MindFusion_Charting_Axis_Title_0.htm\" title=\"MindFusion Charting for WinForms API Reference: Title\">Title<\/a> property of a <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.winforms\/index.htm?T_MindFusion_Charting_Series.htm\" title=\"MindFusion Charting for WinForms API Reference: Series.\">Series.<\/a> In our case we set:<\/p>\n<pre>\r\nseries.Title = \"Server Requests\";\r\n<\/pre>\n<p><\/p>\n<p>We would like to change the legend background to make the legend easier to spot:<\/p>\n<pre>\r\nlineChart1.Theme.LegendBackground = new MindFusion.Drawing.SolidBrush(Color.FromArgb(120, 230, 230, 230));\r\n<\/pre>\n<p><\/p>\n<p>We use a semi-transparent brush that let&#8217;s chart details be visible when the user moves the legend onto another chart element.<\/p>\n<p><strong>6. Line Colors<\/strong><\/p>\n<p>We want to render the line in red. We choose the <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/chart.winforms\/index.htm?T_MindFusion_Charting_MixedSeriesStyle.htm\" title=\"MindFusion Charting for WinForms API Reference: MixedSeriesStyle\">MixedSeriesStyle<\/a> class as the styling class for our application. You can use any other *SeriesStyle class that is appropriate in your case:<\/p>\n<pre>\r\n\/\/ assign a reb brush for the series\r\nlineChart1.Plot.SeriesStyle = new MixedSeriesStyle()\r\n    {  \r\n         UniformStrokeThickness = 5,\r\n         UniformStroke = new MindFusion.Drawing.SolidBrush(Color.Red),\r\n         UniformFill = new MindFusion.Drawing.SolidBrush(Color.Red)\r\n     };\r\n<\/pre>\n<p><\/p>\n<p>With that our sample is complete. You can download the full code with the necessary charting dll-s from this link:<\/p>\n<p align=\"center\"><a href=\"https:\/\/mindfusion.dev\/samples\/winforms\/charting\/RealTimeChart.zip\">Download the WinForms Real-TimeChart Sample<\/a><\/p>\n<p><em>About MindFusion Charting for WinForms:<\/em> A versatile dashboard component that provides your WinForms application with the ability to create fascinating charts, interactive dashboards and practical gauges. The component combines a flexible API that allows custom combination of chart components to build any type of chart you want. You can add as many axes of any type you want, combine various chart series into a single chart with different data providers each. The control also supports pan and zoom, scroll, unlimited number of legends, grid and a dashboard panel. Linear and oval gauges complete the tool set and guarantee every feature you might need to build the perfect gauge, chart of any type or dashboard in WinForms is right at your fingertips.  Learn more at <a href=\"https:\/\/mindfusion.dev\/winforms-chart.html\">https:\/\/mindfusion.dev\/winforms-chart.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this sample we will build a line chart that reads its data in real time and gets updated each second. We start by creating an empty WinForms project. We assume you have installed the Charting for WinForms component and &hellip; <a href=\"https:\/\/mindfusion.dev\/blog\/real-time-chart-in-winforms\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[61,74],"tags":[506,504,254,505,503],"class_list":["post-1959","post","type-post","status-publish","format-standard","hentry","category-charting-2","category-sample-code","tag-c-chart-control","tag-c-real-time-chart","tag-real-time-chart","tag-real-time-chart-control","tag-real-time-winforms-chart"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3RlKs-vB","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/1959","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/comments?post=1959"}],"version-history":[{"count":6,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/1959\/revisions"}],"predecessor-version":[{"id":1966,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/1959\/revisions\/1966"}],"wp:attachment":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/media?parent=1959"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/categories?post=1959"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/tags?post=1959"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}