{"id":531,"date":"2013-10-16T07:09:36","date_gmt":"2013-10-16T07:09:36","guid":{"rendered":"http:\/\/mindfusion.eu\/blog\/?p=531"},"modified":"2021-01-08T15:57:07","modified_gmt":"2021-01-08T15:57:07","slug":"scatter-line-chart-in-winforms-with-a-custom-legend","status":"publish","type":"post","link":"https:\/\/mindfusion.dev\/blog\/scatter-line-chart-in-winforms-with-a-custom-legend\/","title":{"rendered":"Scatter Line Chart in WinForms with a Custom Legend"},"content":{"rendered":"<p>In this post we show you how to build a multi-series scatter chart with a legend and a separator. We use MindFusion.Charting tool for WinForms.<\/p>\n<p><strong>The Data<\/strong><\/p>\n<p>The properties that specify data in the control are <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>. We add three series to each one for the three series that we want to show. Before that, we clear the arrays, to make sure no previously added data shows on the chart:<\/p>\n<pre> lineChart1.XData.Clear();\n lineChart1.XData.Add(new List{3,4,5,6,7,8,9});\n lineChart1.XData.Add(new List{1,2,3,4,5,6,7,8});\n lineChart1.XData.Add(new List{1,2,3,4,5,6,7,8,9,10});\n\n lineChart1.YData.Clear();\n lineChart1.YData.Add(new List{92, 112, 241, 195, 201, 188, 212});\n lineChart1.YData.Add(new List{512, 480, 321, 491, 460, 320, 298, 241});\n lineChart1.YData.Add(new List { 340, 302, 322, 401, 487, 503, 421, 460, 513, 490 });\n<\/pre>\n<p><strong>Chart Series<\/strong><\/p>\n<p>We want to show line series with scatters &#8211; since this is the default <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/masterchart\/index.htm?P_MindFusion_Charting_WinForms_LineChart_LineType_0.htm\">LineType<\/a>, we don&#8217;t have to set anything. In order to customize our series, we add new pens to the <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/masterchart\/index.htm?P_MindFusion_Charting_WinForms_Chart_ChartPens_0.htm\">ChartPens<\/a> property. The colors of the scatters are customized with <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/masterchart\/index.htm?P_MindFusion_Charting_WinForms_LineChart_ShapePens_0.htm\">ShapePens<\/a> and <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/masterchart\/index.htm?P_MindFusion_Charting_WebForms_LineChart_ShapeBrushes_0.htm\">ShapeBrushes<\/a>. We make the chart pens a bit thicker &#8211; 3 pixels.<\/p>\n<pre> lineChart1.ShapeBrushes.Add(new MindFusion.Drawing.SolidBrush(Color.FromArgb(175, 251, 175)));\n lineChart1.ShapeBrushes.Add(new MindFusion.Drawing.SolidBrush(Color.FromArgb(176, 224, 230)));\n lineChart1.ShapeBrushes.Add(new MindFusion.Drawing.SolidBrush(Color.FromArgb(216, 191, 216)));\n\n lineChart1.ChartPens.Add(new MindFusion.Drawing.Pen(Color.FromArgb(144,238,144), 3.0f));\n lineChart1.ChartPens.Add(new MindFusion.Drawing.Pen(Color.FromArgb(70, 130, 180), 3.0f));\n lineChart1.ChartPens.Add(new MindFusion.Drawing.Pen(Color.FromArgb(186, 85, 211), 3.0f));\n\n lineChart1.ShapePens.Add(new MindFusion.Drawing.Pen(Color.FromArgb(144, 238, 144)));\n lineChart1.ShapePens.Add(new MindFusion.Drawing.Pen(Color.FromArgb(70, 130, 180)));\n lineChart1.ShapePens.Add(new MindFusion.Drawing.Pen(Color.FromArgb(186, 85, 211)));\n<\/pre>\n<p><strong>Separator Line<\/strong><\/p>\n<p>We would like to show a separator line that indicates the average value from all chart data. We add a <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/masterchart\/index.htm?T_MindFusion_Charting_Summary.htm\">SummaryValue<\/a> to the <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/masterchart\/index.htm?P_MindFusion_Charting_WinForms_AxesChart_SummaryValues_0.htm\">SummaryValues<\/a> collection. Then we customize the summary line by specifying its pen, scatter type and scatter size. We also set the pen and the brush for the scatters. Here is how we do it:<\/p>\n<pre>  lineChart1.SummaryPens.Add(new MindFusion.Drawing.Pen(Color.FromArgb(255,69,0), 4.0f);\n  lineChart1.SummaryShapeBrushes.Add(new MindFusion.Drawing.SolidBrush(Color.FromArgb(255,228,225))); \n  lineChart1.SummaryShapePens.Add(new MindFusion.Drawing.Pen(Color.FromArgb(255,69,0));\n  lineChart1.SummaryShapes.Add(MindFusion.Charting.Shape.Rhombus);\n  lineChart1.SummaryShapeSizes.Add(15.0);\n  lineChart1.SummaryValues.Add(MindFusion.Charting.Summary.Average);   \n<\/pre>\n<p><strong>Axes Labels<\/strong><\/p>\n<p>We want to show custom text at the X-axis, so we set <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/masterchart\/index.htm?P_MindFusion_Charting_AxisSettings_AxisLabelType_0.htm\">XAxisSettings.LabelType<\/a> to <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/masterchart\/index.htm?T_MindFusion_Charting_AxisLabelType.htm\">AxisLabelType.CustomText<\/a>. We use the <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/masterchart\/index.htm?P_MindFusion_Charting_WinForms_AxesChart_XLabels_0.htm\">XLabels<\/a> property to add the labels. We also add a title with the <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/masterchart\/index.htm?P_MindFusion_Charting_AxisSettings_AxisLabel_0.htm\">XAxisSettings.TitleLabel<\/a> property.<\/p>\n<p>For the Y-axis we want to show the auto scale &#8211; we set it with <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/masterchart\/index.htm?P_MindFusion_Charting_AxisSettings_MaxValue_0.htm\">YAxisSettings.MaxValue<\/a>, <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/masterchart\/index.htm?P_MindFusion_Charting_AxisSettings_MinValue_0.htm\">YAxisSettings.MinValue<\/a> and <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/masterchart\/index.htm?P_MindFusion_Charting_AxisSettings_AxisDelta_0.htm\">YAxisSettings.AxisDelta<\/a>. We show ticks on both axes with the <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/masterchart\/index.htm?P_MindFusion_Charting_AxisSettings_MajorTickLength_0.htm\">MajorTickLength<\/a> property.<\/p>\n<pre> lineChart1.YAxisSettings.AxisDelta = 50;\n lineChart1.YAxisSettings.MajorTickLength = 2F;\n lineChart1.YAxisSettings.MaxValue = 600;\n lineChart1.YAxisSettings.MinValue = 0;\n\n lineChart1.XAxisSettings.AxisDelta = 1;\n lineChart1.XAxisSettings.LabelType = MindFusion.Charting.AxisLabelType.CustomText;\n lineChart1.XAxisSettings.MajorTickLength = 5F;\n lineChart1.XAxisSettings.MaxValue = 11;\n lineChart1.XAxisSettings.MinValue = 0;\n lineChart1.XAxisSettings.TitleLabel = \"Year\";\n<\/pre>\n<p><strong>The Legend<\/strong><\/p>\n<p>The labels for the legend are set with the <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/masterchart\/index.htm?P_MindFusion_Charting_WinForms_Chart_LegendLabels_0.htm\">LegendLabels<\/a> property. The colors are picked automatically from the <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/masterchart\/index.htm?P_MindFusion_Charting_WinForms_Chart_ChartPens_0.htm\">ChartPens<\/a> property for each series. We place the legend at the bottom with <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/masterchart\/index.htm?P_MindFusion_Charting_WinForms_Chart_LegendPosition_0.htm\">LegendPosition<\/a> and increase its offset with <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/masterchart\/index.htm?P_MindFusion_Charting_WinForms_Chart_LegendOffset_0.htm\">LegendOffset<\/a>. We want the legend in one row, so we set <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/masterchart\/index.htm?P_MindFusion_Charting_WinForms_Chart_LegendColumns_0.htm\">LegendColumns<\/a> to the count of the labels &#8211; 3.<\/p>\n<pre> lineChart1.LegendColumns = 3;\n lineChart1.LegendLabels = new List{\"Europe, Asia, North America\"};\n lineChart1.LegendOffset = 30f;\n lineChart1.LegendPosition = MindFusion.Charting.Position.Bottom;\n<\/pre>\n<p>Here is a screenshot from the final chart:<\/p>\n<div id=\"attachment_536\" style=\"width: 718px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2013\/10\/scatter_chart_legend.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-536\" class=\"size-full wp-image-536\" src=\"http:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2013\/10\/scatter_chart_legend.png\" alt=\"Scatter Chart with a Custom Legend\" width=\"708\" height=\"481\" srcset=\"https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2013\/10\/scatter_chart_legend.png 708w, https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2013\/10\/scatter_chart_legend-300x203.png 300w\" sizes=\"auto, (max-width: 708px) 100vw, 708px\" \/><\/a><p id=\"caption-attachment-536\" class=\"wp-caption-text\">Scatter Chart with a Custom Legend<\/p><\/div>\n<p>You can download the sample from this link:<\/p>\n<p align=\"center\"><a href=\"https:\/\/mindfusion.dev\/_samples\/ScatterChartWLegend.zip\">Download Scatter Chart with a Custom Legend Sample<\/a><\/p>\n<p>The trial version of MindFusion.Chart for WinForms boasts many different samples, great charting tips and step by step tutorials. You can download it directly from here:<\/p>\n<p align=\"center\"><a href=\"https:\/\/www.mindfusion.dev\/ChartWinFormsTrial.zip\">Download MindFusion.Charting for WinForms 3.5 Trial Version<\/a><\/p>\n<p><em>About MindFusion.Charting for WinForms:<\/em> a professional programming component for WinForms, which lets you create remarkable charts fast and easy. The tool supports all major chart types \u2013 line, pie, radar and bar \u2013 and numerous variations of them \u2013 column, area, bubble, polar, doughnut etc. 3D charts are supported as well.<\/p>\n<p>Charting for WinForms supports a rich user interaction model with features like zoom, hit testing, drill down, mouse dragging and more. You can use delegates to present mathematical functions, undefined values are also acceptable. Values can be data arrays or retrieved through a database.<\/p>\n<p>The appearance of each chart is fully customizable. The control offers strong design-time support with custom collection editors and chart wizards. At your disposal is a set of predefined appearance themes and a theme editor tool. A full list of the features can be read <a href=\"http:\/\/mindfusion.dev\/features-masterchart.html\">here.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post we show you how to build a multi-series scatter chart with a legend and a separator. We use MindFusion.Charting tool for WinForms. The Data The properties that specify data in the control are XData and YData. We &hellip; <a href=\"https:\/\/mindfusion.dev\/blog\/scatter-line-chart-in-winforms-with-a-custom-legend\/\">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":[110,102,148,62,101,121,29],"class_list":["post-531","post","type-post","status-publish","format-standard","hentry","category-charting-2","category-sample-code","tag-net","tag-custom-labels","tag-custom-legend","tag-line-chart","tag-scatter-chart","tag-separator","tag-winforms"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3RlKs-8z","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/531","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=531"}],"version-history":[{"count":8,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/531\/revisions"}],"predecessor-version":[{"id":2467,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/531\/revisions\/2467"}],"wp:attachment":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/media?parent=531"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/categories?post=531"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/tags?post=531"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}