{"id":303,"date":"2013-05-31T14:04:24","date_gmt":"2013-05-31T14:04:24","guid":{"rendered":"http:\/\/mindfusion.eu\/blog\/?p=303"},"modified":"2021-01-08T15:29:12","modified_gmt":"2021-01-08T15:29:12","slug":"line-chart-in-silverlight-with-two-legends-scatters-and-custom-labels","status":"publish","type":"post","link":"https:\/\/mindfusion.dev\/blog\/line-chart-in-silverlight-with-two-legends-scatters-and-custom-labels\/","title":{"rendered":"Line Chart in Silverlight with Two Legends, Scatters and Custom Labels"},"content":{"rendered":"<p>In this post we are discussing how to build a line chart with multiple series, scatters in custom colors,<br \/>\ntwo legends and custom labels at the X-axis. We are building the chart in Silverlight, using the<br \/>\nMindFusion.Charting for Silverlight tool.<\/p>\n<p><strong>Data<\/strong><\/p>\n<p>The data for the chart is taken from an ObservableCollection, where each member represents sales for a<br \/>\ngiven month.<\/p>\n<p><code>lineChart1.DataSource = sales;<\/code><\/p>\n<p><strong>The Axes<\/strong><\/p>\n<p>The Y-axis shows an auto scale. This is the default <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/chartlite\/index.htm?P_MindFusion_Charting_Silverlight_AxisSettings_LabelType_0.htm\">LabelType<\/a> so we don&#8217;t need to change it. But we want custom title label and intervals of 100. Here is how we set this:<\/p>\n<p><code>lineChart1.YAxisSettings.Interval = 100L;<br \/>\nlineChart1.YAxisSettings.TitleOffset = 10.0;<br \/>\nlineChart1.YAxisSettings.Title = \"USD\";<\/code><\/p>\n<p>The X-axis requires more customization. We want to show custom labels, that&#8217;s why we must set them and change the <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/chartlite\/index.htm?P_MindFusion_Charting_Silverlight_AxisSettings_LabelType_0.htm\">LabelType<\/a> to show them:<\/p>\n<p><code>lineChart1.XAxisSettings.LabelType = LabelType.CustomText;<br \/>\nlineChart1.XAxisSettings.CustomLabelPosition = CustomLabelPosition.ChartDataPoints;<\/code><\/p>\n<p>We use the <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/chartlite\/index.htm?P_MindFusion_Charting_Silverlight_AxesChart_XLabelPath_0.htm\">XLabelPath<\/a> property to bind the Month field in our <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/chartlite\/index.htm?P_MindFusion_Charting_Silverlight_Chart_DataSource_0.htm\">DataSource<\/a> to the X-labels of the chart. We also set the maximum value at the X-axis and draw pointers to the labels by setting the <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/chartlite\/index.htm?P_MindFusion_Charting_Silverlight_AxisSettings_Tick_0.htm\">Tick<\/a> property.<\/p>\n<p><code><br \/>\n<\/code><\/p>\n<p><code>lineChart1.XAxisSettings.MaxValue = sales.Count + 1;<br \/>\nlineChart1.XAxisSettings.Tick = 5.0;<\/code><\/p>\n<p><strong>The Series<\/strong><\/p>\n<p>The data for each series comes from a specific field in the <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/chartlite\/index.htm?P_MindFusion_Charting_Silverlight_Chart_DataSource_0.htm\">DataSource<\/a> collection:<\/p>\n<p><code>lSeries1.YDataPath = \"Turnover\";<br \/>\nlSeries2.YDataPath = \"Profit\";<\/code><\/p>\n<p>We want to show scatters and we use the <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/chartlite\/index.htm?P_MindFusion_Charting_Silverlight_LineSeries_ScatterType_0.htm\">ScatterType<\/a> property to set the type. By default the type is &#8220;None&#8221; and no scatters are drawn. We need to change that:<\/p>\n<p><code>lSeries1.ScatterType = ScatterType.Diamond;<br \/>\nlSeries1.ScatterSize = 20;<\/code><\/p>\n<p>The brushes for the scatters are set with a BrushCollection:<\/p>\n<p><code>BrushCollection sBrushes1 = new BrushCollection();<br \/>\nsBrushes1.Add(new SolidColorBrush(Colors.Red));<br \/>\nsBrushes1.Add(new SolidColorBrush(Colors.Red));<br \/>\nsBrushes1.Add(new SolidColorBrush(Colors.Yellow));<br \/>\nsBrushes1.Add(new SolidColorBrush(Colors.Green));<\/code><\/p>\n<p><code><br \/>\n<\/code><\/p>\n<p><code>lSeries1.ScatterFills = sBrushes1;<\/code><\/p>\n<p>Finally, don&#8217;t forget to add your LineSeries to the chart:<\/p>\n<p><code>lineChart1.Series.Add(lSeries1);<br \/>\nlineChart1.Series.Add(lSeries2);  <\/code><\/p>\n<p><strong>Legends<\/strong><\/p>\n<p>In this chart we need two legends &#8211; one is for the scatters and one for the series. They are both of<br \/>\ntype <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/chartlite\/index.htm?T_MindFusion_Charting_Silverlight_SeriesLegend_Members.htm\">SeriesLegend<\/a>, which gives us control over the brushes and labels used.<\/p>\n<p>The legend for the scatters is docked to the bottom and is aligned in the center.<\/p>\n<p><code>MindFusion.Charting.Silverlight.SeriesLegend legend =<br \/>\nnew MindFusion.Charting.Silverlight.SeriesLegend();<br \/>\nlegend.LabelsSource = new List() { \"Higher than expected\", \"Lower than expected\", \"Meets expectations\"};<br \/>\nlegend.BorderBrush = new SolidColorBrush(Colors.LightGray);<br \/>\nLayoutPanel.SetDock(legend, Dock.Bottom);<br \/>\nlegend.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;<\/code><\/p>\n<p>Here is how we set the brushes:<\/p>\n<p><code>BrushCollection brushes = new BrushCollection();<br \/>\nbrushes.Add(new SolidColorBrush(Colors.Green));<br \/>\nbrushes.Add(new SolidColorBrush(Colors.Red));<br \/>\nbrushes.Add(new SolidColorBrush(Colors.Yellow));<br \/>\nlegend.BrushesSource = brushes;<br \/>\nlineChart1.Legends.Add(legend);<\/code><\/p>\n<p>The code for the other legend is similar, but we dock it to the right and align it to the top:<\/p>\n<p><code>LayoutPanel.SetDock(sLegend, Dock.Right);<br \/>\nsLegend.VerticalAlignment = System.Windows.VerticalAlignment.Top;<\/code><\/p>\n<p>Finally, don&#8217;t forget to add the two legends to the chart:<\/p>\n<p><code>lineChart1.Legends.Add(legend);<br \/>\nlineChart1.Legends.Add(sLegend); <\/code><\/p>\n<p>Here is the final result:<\/p>\n<div id=\"attachment_305\" style=\"width: 618px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2013\/05\/line_chart_scatters.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-305\" class=\"size-full wp-image-305\" src=\"http:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2013\/05\/line_chart_scatters.png\" alt=\"Line chart with scatters, two legends and custom labels. The platform is Silverlight.\" width=\"608\" height=\"342\" srcset=\"https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2013\/05\/line_chart_scatters.png 608w, https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2013\/05\/line_chart_scatters-300x168.png 300w\" sizes=\"auto, (max-width: 608px) 100vw, 608px\" \/><\/a><p id=\"caption-attachment-305\" class=\"wp-caption-text\">Line chart with scatters, two legends and custom labels. The platform is Silverlight.<\/p><\/div>\n<p>The source code is available for download from here:<\/p>\n<p><a href=\"https:\/\/mindfusion.dev\/_samples\/Sales.zip\">Download the Complete Source Code for the Sample<\/a><\/p>\n<p>You can get the trial version of MindFusion.Charting for Silverlight from this link:<\/p>\n<p><a href=\"https:\/\/mindfusion.dev\/SilverlightChartTrial.zip\">Download MindFusion.Charting for Silverlight Trial Version<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post we are discussing how to build a line chart with multiple series, scatters in custom colors, two legends and custom labels at the X-axis. We are building the chart in Silverlight, using the MindFusion.Charting for Silverlight tool. &hellip; <a href=\"https:\/\/mindfusion.dev\/blog\/line-chart-in-silverlight-with-two-legends-scatters-and-custom-labels\/\">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":[70,103,102,65,62,101,59],"class_list":["post-303","post","type-post","status-publish","format-standard","hentry","category-charting-2","category-sample-code","tag-chart","tag-custom-colors","tag-custom-labels","tag-legend","tag-line-chart","tag-scatter-chart","tag-silverlight"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3RlKs-4T","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/303","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=303"}],"version-history":[{"count":4,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/303\/revisions"}],"predecessor-version":[{"id":2438,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/303\/revisions\/2438"}],"wp:attachment":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/media?parent=303"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/categories?post=303"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/tags?post=303"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}