{"id":371,"date":"2013-06-17T11:40:35","date_gmt":"2013-06-17T11:40:35","guid":{"rendered":"http:\/\/mindfusion.eu\/blog\/?p=371"},"modified":"2021-01-08T15:37:13","modified_gmt":"2021-01-08T15:37:13","slug":"line-chart-with-datetime-data-in-asp-net","status":"publish","type":"post","link":"https:\/\/mindfusion.dev\/blog\/line-chart-with-datetime-data-in-asp-net\/","title":{"rendered":"Line Chart with DateTime Data in ASP.NET"},"content":{"rendered":"<p>In this post we demonstrate how to use MindFusion.Charting for ASP.NET component to create a line chart that shows the number of unique visitors to a store\/website in a period of 6 weeks.<\/p>\n<p><strong>Data<\/strong><\/p>\n<p>The data for the X-axis are DateTime values. We create an array with the DateTime values that we&#8217;ll use and add it to the <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/chartingaspnet\/index.htm?P_MindFusion_Charting_WebForms_LineChart_XData_0.htm\">XData<\/a> property of our chart. Before that we have to delete the predefined array that is added when the control is dropped at the form:<\/p>\n<pre>DateTime dt1 = new DateTime(2013, 6, 14);\nDateTime dt2 = new DateTime(2013, 6, 7);\nDateTime dt3 = new DateTime(2013, 5, 31);\nDateTime dt4 = new DateTime(2013, 5, 24);\nDateTime dt5 = new DateTime(2013, 5, 17);\nDateTime dt6 = new DateTime(2013, 5, 10);\n\nArrayList data = new ArrayList() { dt1, dt2, dt3, dt4, dt5, dt6 };\nLineChart1.XData.Clear();\nLineChart1.XData.Add(data);<\/pre>\n<p>Next, we must make some adjustments in the settings for the X-axis to tell the control that DateTime data is used. We set the <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/chartingaspnet\/index.htm?P_MindFusion_Charting_AxisSettings_DataFormat_0.htm\">DataFormat<\/a> property to DateTime and we specify the time range for the axis. This is how we do this:<\/p>\n<pre>LineChart1.XAxisSettings.DataFormat = MindFusion.Charting.DataFormat.DateTime;\n\nLineChart1.XAxisSettings.StartDateTime = new DateTime(2013, 5, 3);\nLineChart1.XAxisSettings.EndDateTime = new DateTime(2013, 6, 20);\n\/\/set the interval to one week - 7 days\nLineChart1.XAxisSettings.TimeSpan = new TimeSpan(7, 0, 0, 0);<\/pre>\n<p>The data for the Y-axis are numbers. We can set them through the property grid or set them in code.<\/p>\n<pre>LineChart1.YData.Clear();\nArrayList data1 = new ArrayList() { 56, 13, 45, 17, 82, 22 };\nLineChart1.YData.Add(data1);<\/pre>\n<p><strong>The X-axis<\/strong><\/p>\n<p>First, we must change the <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/chartingaspnet\/index.htm?P_MindFusion_Charting_AxisSettings_AxisLabelType_0.htm\">LabelType<\/a> property of <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/chartingaspnet\/index.htm?P_MindFusion_Charting_WebForms_AxesChart_XAxisSettings_0.htm\">XAxisSettings<\/a> from &#8220;ChartData&#8221;, which is the default to &#8220;AutoScale&#8221;. This will make the axis show the time range we&#8217;ve set in code above. Then, we change how the DateTime values will be formatted. The Default <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/chartingaspnet\/index.htm?P_MindFusion_Charting_AxisSettings_DateTimeFormat_0.htm\">DateTimeFormat<\/a> shows the full time and date and is not suitable. We change it to &#8220;LongDate&#8221;, which does not draw any time.<\/p>\n<pre>XAxisSettings-DateTimeFormat=\"LongDate\" \nXAxisSettings-DrawTicksUniformly=\"False\" XAxisSettings-DrawZero=\"True\" \nXAxisSettings-LabelBrush=\"s:#FF696969\" XAxisSettings-LabelOffset=\"10\" \nXAxisSettings-LabelType=\"AutoScale\" XAxisSettings-TitleLabel=\"Week\" \nXAxisSettings-TitleLabelBrush=\"s:#FF696969\" XAxisSettings-TitleLabelOffset=\"10\"<\/pre>\n<p>We type &#8220;Week&#8221; as <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/chartingaspnet\/index.htm?P_MindFusion_Charting_AxisSettings_AxisLabel_0.htm\">TitleLabel<\/a> for the axis and set the <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/chartingaspnet\/index.htm?P_MindFusion_Charting_AxisSettings_DrawZero_0.htm\">DrawZero<\/a> property to true to show the first label, which is otherwise omitted.<\/p>\n<p>Upon preview we notice that the labels are too close to the axis, that&#8217;s why we use <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/chartingaspnet\/index.htm?P_MindFusion_Charting_AxisSettings_LabelsOffset_0.htm\">LabelOffset<\/a> and <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/chartingaspnet\/index.htm?P_MindFusion_Charting_AxisSettings_AxisLabelOffset_0.htm\">TitleLabelOffset<\/a> to add some space before them. Finally, we change the color of the labels, to make them dark gray rather than black.<\/p>\n<p><strong>The Y-axis<\/strong><\/p>\n<p>Customizing the Y-axis is rather simple. We change the interval with <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/chartingaspnet\/index.htm?P_MindFusion_Charting_AxisSettings_AxisDelta_0.htm\">AxisDelta<\/a> to 5 and increase the <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/chartingaspnet\/index.htm?P_MindFusion_Charting_AxisSettings_MaxValue_0.htm\">MaxValue<\/a> to 100. We don&#8217;t need decimal fractions for the labels, that&#8217;s why we change the <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/chartingaspnet\/index.htm?CC_P_MindFusion_Charting_AxisSettings_NumberFormat_0_0.htm\">NumberFormat<\/a>. We add a <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/chartingaspnet\/index.htm?P_MindFusion_Charting_AxisSettings_AxisLabel_0.htm\">TitleLabel<\/a> and change its orientation with <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/chartingaspnet\/index.htm?P_MindFusion_Charting_AxisSettings_AxisLabelOrientation_0.htm\">TitleLabelOrientation<\/a>. Finally we use <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/chartingaspnet\/index.htm?P_MindFusion_Charting_AxisSettings_LabelBrush_0.htm\">LabelBrush<\/a> and <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/chartingaspnet\/index.htm?P_MindFusion_Charting_AxisSettings_AxisLabelBrush_0.htm\">TitleLabelBrush<\/a> to change the colors of the labels &#8211; we use the same brushes as for the X-axis.<\/p>\n<pre>YAxisSettings-AxisDelta=\"5\" \nYAxisSettings-LabelBrush=\"s:#FF696969\" \nYAxisSettings-MaxValue=\"100\" YAxisSettings-NumberFormat=\"Fixed_point_0Digits\" \nYAxisSettings-TitleLabel=\"Unique Visitors\" \nYAxisSettings-TitleLabelBrush=\"s:#FF696969\" \nYAxisSettings-TitleLabelOrientation=\"BottomToTop\"<\/pre>\n<p><strong>The Grid<\/strong><\/p>\n<p>Initially the chart shows no grid &#8211; but we want to show a grid. That&#8217;s why we change <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/chartingaspnet\/index.htm?P_MindFusion_Charting_WebForms_AxesChart_GridType_0.htm\">GridType<\/a> to &#8220;Crossed&#8221; and set a <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/chartingaspnet\/index.htm?P_MindFusion_Charting_WebForms_AxesChart_GridPen_0.htm\">GridPen<\/a>. The dark gray background of the plot area together with its outlining are set with <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/chartingaspnet\/index.htm?CC_P_MindFusion_Charting_AxesChart_PlotAreaOutlinePen_0_0.htm\">PlotAreaOutlinePen<\/a> and <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/chartingaspnet\/index.htm?P_MindFusion_Charting_AxesChart_PlotAreaBrush_0.htm\">PlotAreaBrush<\/a>.<\/p>\n<pre>GridPen=\"n:0\/#FFE1E1E1\/0\/0\/0\/\/0\/0\/10\/\" GridType=\"Crossed\" PlotAreaBrush=\"s:#FFC0C0C0\" \nPlotAreaOutlinePen=\"n:0\/#FF787878\/0\/0\/0\/\/0\/0\/10\/\"<\/pre>\n<p>This is the code that was generated by the designer because we set the properties through the property grid. If we set them with code, it will be:<\/p>\n<pre>LineChart1.GridPen = new MindFusion.Drawing.Pen(Color.FromArgb(225,225,225));\nLineChart1.GridType = MindFusion.Charting.GridType.Crossed;\nLineChart1.PlotAreaBrush = new MindFusion.Drawing.SolidBrush(Color.FromArgb(192, 192, 192));\nLineChart1.PlotAreaOutlinePen = new MindFusion.Drawing.Pen(Color.FromArgb(120, 120, 120));<\/pre>\n<p><strong><br \/>\nThe Line Series<\/strong><\/p>\n<p>We want scatters at data points and we want to show labels above those scatters. The <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/chartingaspnet\/index.htm?P_MindFusion_Charting_WebForms_LineChart_ScatterLabelType_0.htm\">LabelType<\/a> property lets us set the type to be both line and scatters:<\/p>\n<pre>LineChart1.LineType = MindFusion.Charting.LineTypes.Line | MindFusion.Charting.LineTypes.Scatter;<\/pre>\n<p>This is the default type, so you don&#8217;t need to set it if you have not changed it before. We use <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/chartingaspnet\/index.htm?P_MindFusion_Charting_WebForms_LineChart_ShapeBrushes_0.htm\">ShapeBrushes<\/a>, <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/chartingaspnet\/index.htm?P_MindFusion_Charting_WebForms_LineChart_ShapePens_0.htm\">ShapePens<\/a> and <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/chartingaspnet\/index.htm?P_MindFusion_Charting_WebForms_LineChart_ShapeSizes_0.htm\">ShapeSizes<\/a> to set the brushes and size of the scatters. We can do this in the property grid or in code. Finally, we want to show labels above scatters. We use <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/chartingaspnet\/index.htm?P_MindFusion_Charting_WebForms_LineChart_ScatterLabelType_0.htm\">LabelType<\/a> and <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/chartingaspnet\/index.htm?P_MindFusion_Charting_WebForms_LineChart_LabelFormat_0.htm\">LabelFormat<\/a> to set what kind of labels are drawn and since our labels are numbers &#8211; how they are formatted.<\/p>\n<pre>LabelBorder=\"RoundedRect\" LabelBorderBackground=\"s:#FFFFFFE0\" LabelBorderOutline=\"n:0\/#FF787878\/0\/0\/0\/\/0\/0\/10\/\" LabelFormat=\"Fixed_point_0Digits\" LabelType=\"Data\"<\/pre>\n<p>In code you write:<\/p>\n<p>LineChart1.LabelBorder = MindFusion.Charting.Border.RoundedRect;<br \/>\nLineChart1.LabelBorderBackground = new MindFusion.Drawing.SolidBrush(Color.FromArgb(255, 255, 224));<br \/>\nLineChart1.LabelBorderOutline = new MindFusion.Drawing.Pen(Color.FromArgb(120, 120,120));<br \/>\nLineChart1.LabelFormat = MindFusion.Charting.NumberFormat.Fixed_point_0Digits;<br \/>\nLineChart1.LabelType = MindFusion.Charting.LabelType.Data;<\/p>\n<p>Here is the final chart:<\/p>\n<div id=\"attachment_374\" style=\"width: 618px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2013\/06\/asp_line_chart.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-374\" class=\"size-full wp-image-374\" src=\"http:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2013\/06\/asp_line_chart.png\" alt=\"Line chart with DateTime values in ASP.NET\" width=\"608\" height=\"365\" srcset=\"https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2013\/06\/asp_line_chart.png 608w, https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2013\/06\/asp_line_chart-300x180.png 300w\" sizes=\"auto, (max-width: 608px) 100vw, 608px\" \/><\/a><p id=\"caption-attachment-374\" class=\"wp-caption-text\">Line chart with DateTime values in ASP.NET<\/p><\/div>\n<p>You can download the sample from this link:<\/p>\n<p align=\"center\"><a href=\"https:\/\/mindfusion.dev\/_samples\/DateTimeLineChart.zip\">Download Line Chart for ASP.NET Sample<\/a><\/p>\n<p>The trial version of the component is available from here:<\/p>\n<p align=\"center\"><a href=\"https:\/\/www.mindfusion.dev\/WebChartingTrial.zip\">MindFusion.Charting for ASP.NET Trial Version Download<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post we demonstrate how to use MindFusion.Charting for ASP.NET component to create a line chart that shows the number of unique visitors to a store\/website in a period of 6 weeks. Data The data for the X-axis are &hellip; <a href=\"https:\/\/mindfusion.dev\/blog\/line-chart-with-datetime-data-in-asp-net\/\">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":[7,114,116,106,62,117,115],"class_list":["post-371","post","type-post","status-publish","format-standard","hentry","category-charting-2","category-sample-code","tag-asp-net","tag-chart-control","tag-datetime","tag-grid","tag-line-chart","tag-scatters","tag-webforms"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3RlKs-5Z","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/371","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=371"}],"version-history":[{"count":6,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/371\/revisions"}],"predecessor-version":[{"id":2446,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/371\/revisions\/2446"}],"wp:attachment":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/media?parent=371"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/categories?post=371"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/tags?post=371"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}