{"id":37,"date":"2009-06-13T11:24:01","date_gmt":"2009-06-13T05:54:01","guid":{"rendered":"http:\/\/www.smallguru.com\/?p=36"},"modified":"2009-06-13T11:24:01","modified_gmt":"2009-06-13T05:54:01","slug":"zedgraph-csharp-graph-data-export-to-cs","status":"publish","type":"post","link":"http:\/\/www.ar-cad.com\/smallguru\/2009\/06\/zedgraph-csharp-graph-data-export-to-cs\/","title":{"rendered":"ZedGraph C# Graph Data Export to CSV Using a Custom Context Menu"},"content":{"rendered":"<p>In continuation of <a href=\"http:\/\/www.smallguru.com\/2008\/12\/opensource-c-graph-plot-library-zedgraph\/\" target=\"_blank\">my earlier post on ZedGraph example which plots a sinosoidal graph<\/a>, I have extended it further to:<\/p>\n<p><strong>Add a new custom menu item in context menu(which appears on right click on the graph)<\/strong><img decoding=\"async\" loading=\"lazy\" style=\"vertical-align: text-bottom;\" src=\"http:\/\/www.smallguru.com\/wordpress\/wp-content\/upload\/zedGraphContextMenu.png\" alt=\"\" \/><\/p>\n<p><strong>Export Graph plot data to CSV (coma separated values) file. Which can be opened by spreadsheets such as Microsoft Excel and Open Office calc.<\/strong><img decoding=\"async\" loading=\"lazy\" style=\"vertical-align: text-bottom;\" src=\"http:\/\/www.smallguru.com\/wordpress\/wp-content\/upload\/zedGraphCSV.png\" alt=\"\" \/><\/p>\n<p>For the custom context menu, the code has been derived from <a href=\"http:\/\/zedgraph.org\/wiki\/index.php?title=Edit_the_Context_Menu\" target=\"_blank\">ZedGraph Wikipage<\/a>. The following is the code of the Windows Form which has the ZedGraph control.<\/p>\n<p>[sourcecode language=&#8217;c#&#8217;]<br \/>\nusing System;<br \/>\nusing System.Collections.Generic;<br \/>\nusing System.ComponentModel;<br \/>\nusing System.Data;<br \/>\nusing System.Drawing;<br \/>\nusing System.Text;<br \/>\nusing System.Windows.Forms;<br \/>\nusing ZedGraph;<br \/>\nusing System.IO;<\/p>\n<p>namespace ZedGraphTest<br \/>\n{<br \/>\npublic partial class Form1 : Form<br \/>\n{<br \/>\nprivate PointPairList m_pointPairList;<br \/>\n\/\/CSV Writer<br \/>\nprivate StreamWriter m_CSVWriter;<\/p>\n<p>public Form1()<br \/>\n{<br \/>\nInitializeComponent();<br \/>\nCreateGraph();<br \/>\nSetSize();<\/p>\n<p>\/\/csv<br \/>\nzedGraphControl.ContextMenuBuilder +=<br \/>\nnew ZedGraphControl.ContextMenuBuilderEventHandler(MyContextMenuBuilder);<br \/>\n}<\/p>\n<p>private void CreateGraph()<br \/>\n{<br \/>\nGraphPane myPane = zedGraphControl.GraphPane;<\/p>\n<p>\/\/ Set the titles and axis labels<br \/>\nmyPane.Title.Text = &#8220;ZedGraph Test&#8221;;<br \/>\nmyPane.XAxis.Title.Text = &#8220;theta (angle)&#8221;;<br \/>\nmyPane.YAxis.Title.Text = &#8220;Sin (theta)&#8221;;<\/p>\n<p>\/\/ Make up some data points from the Sine function<br \/>\nm_pointPairList = new PointPairList();<br \/>\nfor (double x = 0; x <= 360; x += 10)\n{\ndouble y = Math.Sin(x * Math.PI \/ 180.0);\n\nm_pointPairList.Add(x, y);\n}\n\/\/ Generate a blue curve with Plus symbols,\nLineItem _myCurve1 = myPane.AddCurve(\"Sin (theta)\",\nm_pointPairList, Color.Blue, SymbolType.Plus);\n\n\/\/ Fill the pane background with a color gradient\nmyPane.Fill = new Fill(Color.White, Color.FromArgb(220, 220, 255), 45F);\n\n\/\/Make the MajorGrids of Axes visible\nmyPane.XAxis.MajorGrid.IsVisible = true;\nmyPane.YAxis.MajorGrid.IsVisible = true;\n\n\/\/ Calculate the Axis Scale Ranges\nzedGraphControl.AxisChange();\n}\n\nprivate void Form1_Resize(object sender, EventArgs e)\n{\nSetSize();\n}\n\nprivate void SetSize()\n{\nzedGraphControl.Location = new Point(10, 10);\n\/\/ Leave a small margin around the outside of the control\nzedGraphControl.Size = new Size(this.ClientRectangle.Width - 20,\nthis.ClientRectangle.Height - 20);\n}\n\nprivate void MyContextMenuBuilder(ZedGraphControl control,\nContextMenuStrip menuStrip, Point mousePt,\nZedGraphControl.ContextMenuObjectState objState)\n{\n\/\/ create a new menu item\nToolStripMenuItem _item = new ToolStripMenuItem();\n\/\/ This is the user-defined Tag so you can find this menu item later if necessary\n_item.Name = \"Export Data as CSV\";\n_item.Tag = \"export_data_csv\";\n\/\/ This is the text that will show up in the menu\n_item.Text = \"Export Data as CSV\";\n\/\/ Add a handler that will respond when that menu item is selected\n_item.Click += new System.EventHandler(ShowSaveAsForExportCSV);\n\/\/ Add the menu item to the menu,as 3rd Item\nmenuStrip.Items.Insert(2, _item);\n}\n\nprivate void ShowSaveAsForExportCSV(object sender, System.EventArgs e)\n{\ntry\n{\n\/\/show saveAs CmdDlg\nsaveFileDialog1.Filter = \"CSV files (*.csv)|*.csv\";\nsaveFileDialog1.ShowDialog();\nm_CSVWriter = new StreamWriter(saveFileDialog1.FileName);\nWriteCSVToStream();\nm_CSVWriter.Close();\nMessageBox.Show(\"CSV File Saved\", \" ZedGraph \", MessageBoxButtons.OK);\n}\ncatch (Exception ex)\n{\nm_CSVWriter.Close();\nMessageBox.Show(ex.ToString());\n}\n}\n\nprivate void WriteCSVToStream()\n{\n\/\/First line is for Headers., X and Y Axis\nstring _xAxisHeader = CheckCSVString(zedGraphControl.GraphPane.XAxis.Title.Text);\nstring _yAxisHeader = CheckCSVString(zedGraphControl.GraphPane.YAxis.Title.Text);\nm_CSVWriter.Write(_xAxisHeader + \",\" + _yAxisHeader + \"n\");\n\n\/\/subsequent lines are having data\nfor (int i = 0; i < m_pointPairList.Count; i++)\n{\nm_CSVWriter.Write(m_pointPairList[i].X + \",\" + m_pointPairList[i].Y + \"n\");\n}\n}\n\nprivate string CheckCSVString(string _string)\n{\/\/Check to see if there are any characters that can disturb the CSV delimeters.\nstring _returnString = _string;\nif (_string.IndexOfAny(\"\",x0Ax0D\".ToCharArray()) > -1)<br \/>\n{<br \/>\n_returnString = &#8220;&#8221;&#8221; + _string.Replace(&#8220;&#8221;&#8221;, &#8220;&#8221;&#8221;&#8221;) + &#8220;&#8221;&#8221;;<br \/>\n}<br \/>\nreturn _returnString;<br \/>\n}<\/p>\n<p>}<br \/>\n}<\/p>\n<p>[\/sourcecode] <\/p>\n","protected":false},"excerpt":{"rendered":"<p>In continuation of my earlier post on ZedGraph example which plots a sinosoidal graph, I have extended it further to: Add a new custom menu item in context menu(which appears on right click on the graph) Export Graph plot data &hellip; <a href=\"http:\/\/www.ar-cad.com\/smallguru\/2009\/06\/zedgraph-csharp-graph-data-export-to-cs\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":53,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","footnotes":""},"categories":[7,8],"tags":[36,37,29],"class_list":["post-37","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","category-visual-c","tag-tutorials","tag-visual-c","tag-visual-studio"],"_links":{"self":[{"href":"http:\/\/www.ar-cad.com\/smallguru\/wp-json\/wp\/v2\/posts\/37","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.ar-cad.com\/smallguru\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.ar-cad.com\/smallguru\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.ar-cad.com\/smallguru\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.ar-cad.com\/smallguru\/wp-json\/wp\/v2\/comments?post=37"}],"version-history":[{"count":0,"href":"http:\/\/www.ar-cad.com\/smallguru\/wp-json\/wp\/v2\/posts\/37\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.ar-cad.com\/smallguru\/wp-json\/wp\/v2\/media?parent=37"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.ar-cad.com\/smallguru\/wp-json\/wp\/v2\/categories?post=37"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.ar-cad.com\/smallguru\/wp-json\/wp\/v2\/tags?post=37"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}