Line | |
---|
1 | using System;
|
---|
2 | using System.IO;
|
---|
3 | using System.Windows.Forms;
|
---|
4 |
|
---|
5 | namespace HeuristicLab.Visualization.DataExport {
|
---|
6 | public class CSVDataExport : IExporter {
|
---|
7 | public string Name {
|
---|
8 | get { return "CSV Export"; }
|
---|
9 | }
|
---|
10 |
|
---|
11 | public void Export(IChartDataRowsModel model, LineChart chart) {
|
---|
12 | SaveFileDialog sfd = new SaveFileDialog();
|
---|
13 | sfd.Filter = "CSV Files|*.csv|All Files|*.*";
|
---|
14 | if (sfd.ShowDialog() != DialogResult.OK) {
|
---|
15 | return;
|
---|
16 | }
|
---|
17 |
|
---|
18 | string filename = sfd.FileName;
|
---|
19 |
|
---|
20 | Export(model, filename);
|
---|
21 | }
|
---|
22 |
|
---|
23 | private static void Export(IChartDataRowsModel model, string filename) {
|
---|
24 | using (FileStream fs = new FileStream(filename, FileMode.Create))
|
---|
25 | using (StreamWriter sw = new StreamWriter(fs)) {
|
---|
26 | CSVWriter writer = new CSVWriter(sw);
|
---|
27 |
|
---|
28 | // write headers
|
---|
29 | writer.AddString(model.XAxis.Label);
|
---|
30 | foreach (IDataRow row in model.Rows)
|
---|
31 | writer.AddString(row.RowSettings.Label);
|
---|
32 | writer.NewLine();
|
---|
33 |
|
---|
34 | // figure out max number of rows in all data rows
|
---|
35 | int maxItems = 0;
|
---|
36 | foreach (IDataRow row in model.Rows)
|
---|
37 | maxItems = Math.Max(maxItems, row.Count);
|
---|
38 |
|
---|
39 | // write rows
|
---|
40 | for (int i = 0; i < maxItems; i++) {
|
---|
41 | writer.AddNumber(i);
|
---|
42 | foreach (IDataRow row in model.Rows) {
|
---|
43 | if (i < row.Count)
|
---|
44 | writer.AddNumber(row[i]);
|
---|
45 | else
|
---|
46 | writer.AddEmpty();
|
---|
47 | }
|
---|
48 | writer.NewLine();
|
---|
49 | }
|
---|
50 | }
|
---|
51 | }
|
---|
52 | }
|
---|
53 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.