{"id":403,"date":"2013-07-01T11:19:59","date_gmt":"2013-07-01T11:19:59","guid":{"rendered":"http:\/\/mindfusion.eu\/blog\/?p=403"},"modified":"2021-01-08T15:46:38","modified_gmt":"2021-01-08T15:46:38","slug":"line-chart-with-a-separator-in-winforms","status":"publish","type":"post","link":"https:\/\/mindfusion.dev\/blog\/line-chart-with-a-separator-in-winforms\/","title":{"rendered":"Line Chart with a Separator in WinForms"},"content":{"rendered":"<p>In today&#8217;s post we&#8217;ll show how to build a line chart with several line series and a separator line. The separator line is drawn at a given height and divides the chart into two halves.<\/p>\n<p><strong>The Line Series<\/strong><\/p>\n<p>The line series are three, so we must add three lists with data to the <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/masterchart\/index.htm?P_MindFusion_Charting_WinForms_LineChart_XData_0.htm\">XData<\/a> and <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/masterchart\/index.htm?P_MindFusion_Charting_WinForms_LineChart_YData_0.htm\">YData<\/a> properties. We can do this in three ways: type the values in design time, write them in code or use data binding. In design time we use the built-in collection editors of the control:<\/p>\n<div id=\"attachment_407\" style=\"width: 265px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2013\/07\/collection_editor.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-407\" class=\"size-full wp-image-407\" src=\"http:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2013\/07\/collection_editor.png\" alt=\"The Series collection editor\" width=\"255\" height=\"309\" srcset=\"https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2013\/07\/collection_editor.png 255w, https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2013\/07\/collection_editor-247x300.png 247w\" sizes=\"auto, (max-width: 255px) 100vw, 255px\" \/><\/a><p id=\"caption-attachment-407\" class=\"wp-caption-text\">The Series collection editor<\/p><\/div>\n<p>In code we make lists with the data and add them to <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/masterchart\/index.htm?P_MindFusion_Charting_LineChart_XData_0.htm\">XData<\/a> or <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/masterchart\/index.htm?P_MindFusion_Charting_WinForms_LineChart_YData_0.htm\">YData<\/a>:<\/p>\n<pre>lineChart1.YData.Clear();\nlineChart1.YData.Add(new List { 45, 64, 38.2, 33.03, 56, 68, 39, 42 });\nlineChart1.YData.Add(new List { 34, 42, 28, 42, 35, 31, 62, 55 });\nlineChart1.YData.Add(new List { 22, 19, 32, 28, 17, 25, 31, 36 });\n<\/pre>\n<p>If you want to use data binding then set <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/masterchart\/index.htm?P_MindFusion_Charting_WinForms_Chart_DataSource_0.htm\">DataSource<\/a> to the name of the data source, <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/masterchart\/index.htm?P_MindFusion_Charting_WinForms_Chart_DataMember_0.htm\">DataMember<\/a> to specify the name of the table which will supply the data and <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/masterchart\/index.htm?P_MindFusion_Charting_WinForms_LineChart_XDataFields_0.htm\">XDataFields<\/a>\/<a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/masterchart\/index.htm?P_MindFusion_Charting_WinForms_LineChart_YDataFields_0.htm\">YDataFields<\/a> to provide the name(s) of the fields. In this case we will require three data base columns for each of the three series &#8211; separate the names with a comma e.g.<\/p>\n<pre>lineChart1.YDataFields = \"Sales1,Sales2,Sales3\";\n<\/pre>\n<p>For <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/masterchart\/index.htm?P_MindFusion_Charting_WinForms_LineChart_YDataFields_0.htm\">XData<\/a> we use the same list because we want the series to appear exactly under each other:<\/p>\n<pre>lineChart1.XData.Clear();\nlineChart1.XData.Add(new List { 10, 20, 30, 40, 50, 60, 70, 80 });\nlineChart1.XData.Add(new List { 10, 20, 30, 40, 50, 60, 70, 80 });\nlineChart1.XData.Add(new List { 10, 20, 30, 40, 50, 60, 70, 80 });\n<\/pre>\n<p><strong>The Series<\/strong><\/p>\n<p>We set <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/masterchart\/index.htm?P_MindFusion_Charting_WinForms_LineChart_LineType_0.htm\">LineType<\/a> to Line and Scatter because we want to show scatters at data points:<\/p>\n<pre>lineChart1.LineType = MindFusion.Charting.LineTypes.Line |  \nMindFusion.Charting.LineTypes.Scatter;\n<\/pre>\n<p>The <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/masterchart\/index.htm?T_MindFusion_Charting_LineTypes.htm\">LineTypes<\/a> enumeration allows bit wise combining of its members. We use <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/masterchart\/index.htm?P_MindFusion_Charting_WinForms_Chart_ChartPens_0.htm\">ChartPens<\/a> to set the pens for the line series. <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/masterchart\/index.htm?P_MindFusion_Charting_WinForms_LineChart_ShapePens_0.htm\">ShapesPens<\/a> and <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/masterchart\/index.htm?P_MindFusion_Charting_WinForms_LineChart_ShapeBrushes_0.htm\">ShapeBrushes<\/a> set the pens for the outline and the brushes for filling the scatters:<\/p>\n<pre>lineChart1.ChartPens.Add(new MindFusion.Drawing.Pen(Color.FromArgb(102, 205, 170), 6));\nlineChart1.ShapePens.Add(new MindFusion.Drawing.Pen(Color.FromArgb(102, 205, 170), 2));   \n<\/pre>\n<p>The pens for the series are thicker than the pens for the scatters. The fill brush is slightly lighter:<\/p>\n<pre>lineChart1.ShapeBrushes.Add(new MindFusion.Drawing.SolidBrush(Color.FromArgb(175, 251, 175)));\n<\/pre>\n<p>Finally &#8211; we set the size of the shapes and their type:<\/p>\n<pre>lineChart1.Shapes.Add(MindFusion.Charting.Shape.Circle);\nlineChart1.ShapeSizes.Add(10);\n<\/pre>\n<p><strong>The Separator<\/strong><\/p>\n<p>There are two ways to draw the separator line.<\/p>\n<p>The first is to add a custom summary value. In this case the separator line will be drawn from the<br \/>\nsmallest X-data value to the biggest one, parallel to the axes.<\/p>\n<pre>lineChart1.AddCustomSummary(40.0, \"\");\n<\/pre>\n<p>The summary line is drawn with shapes at both ends, but we can hide them by setting their size to 0:<\/p>\n<pre>lineChart1.SummaryBrushes.Add(new MindFusion.Drawing.SolidBrush(Color.Gray));\nlineChart1.SummaryShapeSizes.Add(0);\n<\/pre>\n<p>The second way to draw the separator line is to add it as a 4th series in the <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/masterchart\/index.htm?P_MindFusion_Charting_WinForms_LineChart_XData_0.htm\">XData<\/a> and <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/masterchart\/index.htm?P_MindFusion_Charting_WinForms_LineChart_YData_0.htm\">YData<\/a> lists and add a pen for it in the <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/masterchart\/index.htm?P_MindFusion_Charting_WinForms_Chart_ChartPens_0.htm\">ChartPens<\/a> list. The advantage is that we can make the line as long as we want, in our case &#8211; as long as the length of the X-axis:<\/p>\n<pre>lineChart1.XData.Add(new List { 0, 90 });\n<\/pre>\n<p><strong>The Legend<\/strong><\/p>\n<p>We use <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/masterchart\/index.htm?P_MindFusion_Charting_WinForms_Chart_LegendLabels_0.htm\">LegendLabels<\/a> to add the labels for the line series:<\/p>\n<pre>lineChart1.LegendLabels = new List() { \"2010\", \"2011\", \"2012\" };\n<\/pre>\n<p>We set the background of the legend, but we don&#8217;t have to add any brushes or shapes for the legend items &#8211; they are taken automatically from the line series settings:<\/p>\n<pre>lineChart1.LegendBackgroundBrush = new MindFusion.Drawing.SolidBrush(Color.FromArgb(253, 253,  \n253));\n<\/pre>\n<p><strong><br \/>\nThe Grid<\/strong><\/p>\n<p>The <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/masterchart\/index.htm?P_MindFusion_Charting_WinForms_AxesChart_GridType_0.htm\">GridType<\/a> is <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/masterchart\/index.htm?T_MindFusion_Charting_GridType.htm\">GridType.HorScale<\/a>. We use <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/masterchart\/index.htm?P_MindFusion_Charting_WinForms_AxesChart_GridBrush_0.htm\">GridBrush<\/a>, <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/masterchart\/index.htm?P_MindFusion_Charting_WinForms_AxesChart_AltGridBrush_0.htm\">AltGridBrush<\/a> and <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/masterchart\/index.htm?P_MindFusion_Charting_WinForms_AxesChart_GridPen_0.htm\">GridPen<\/a> to set the colors for the grid.<\/p>\n<pre>lineChart1.GridPen = new MindFusion.Drawing.Pen(Color.FromArgb(200, 200, 200));\nlineChart1.GridPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;\nlineChart1.GridType = MindFusion.Charting.GridType.HorScale;\n<\/pre>\n<p>If you make the changes to the chart in code, don&#8217;t forget to call <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/masterchart\/index.htm?M_MindFusion_Charting_WinForms_Chart_UpdateChart_0.htm\">UpdateChart()<\/a> to make sure the<br \/>\ncontrol knows changes have happened and the chart must be updated.<\/p>\n<p>Here is the final chart:<\/p>\n<div id=\"attachment_412\" style=\"width: 568px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2013\/07\/linechart_separator.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-412\" class=\"size-full wp-image-412\" src=\"http:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2013\/07\/linechart_separator.png\" alt=\"A line chart with a separator line.\" width=\"558\" height=\"465\" srcset=\"https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2013\/07\/linechart_separator.png 558w, https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2013\/07\/linechart_separator-300x249.png 300w\" sizes=\"auto, (max-width: 558px) 100vw, 558px\" \/><\/a><p id=\"caption-attachment-412\" class=\"wp-caption-text\">A line chart with a separator line.<\/p><\/div>\n<p>The sample is available for download from here:<\/p>\n<p align=\"center\"><a href=\"https:\/\/mindfusion.dev\/_samples\/LineChartSample.zip\">Download Line Chart with Separator Line Sample<\/a><\/p>\n<p>You can download the trial version of the component with extensive documentation and many other samples from here:<\/p>\n<p align=\"center\"><a href=\"https:\/\/www.mindfusion.dev\/ChartWinFormsTrial.zip\">Download MindFusion.Charting for WinForms Trial Version<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In today&#8217;s post we&#8217;ll show how to build a line chart with several line series and a separator line. The separator line is drawn at a given height and divides the chart into two halves. The Line Series The line &hellip; <a href=\"https:\/\/mindfusion.dev\/blog\/line-chart-with-a-separator-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":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[61,74],"tags":[122,106,62,117,121,29],"class_list":["post-403","post","type-post","status-publish","format-standard","hentry","category-charting-2","category-sample-code","tag-data-binding","tag-grid","tag-line-chart","tag-scatters","tag-separator","tag-winforms"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3RlKs-6v","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/403","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=403"}],"version-history":[{"count":7,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/403\/revisions"}],"predecessor-version":[{"id":2453,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/403\/revisions\/2453"}],"wp:attachment":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/media?parent=403"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/categories?post=403"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/tags?post=403"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}