-
Notifications
You must be signed in to change notification settings - Fork 5
/
live3.fsx
183 lines (131 loc) · 4.34 KB
/
live3.fsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
(*
ITT8060 -- Advanced Programming 2013
Department of Computer Science
Tallinn University of Technology
------------------------------------
Lecture 3: interoperability of F# and .NET libraries
James Chapman and Juhan Ernits
Material based on chapter 4 of RWFP
*)
#light
open System
let data = "Test data,1233"
let convertDataRow(csvLine:string) =
let cells = List.ofSeq(csvLine.Split(','))
match cells with
| title :: number :: _ ->
let parsedNumber = Int32.Parse(number)
(title, parsedNumber)
| _ -> failwith "Incorrect data format!"
convertDataRow(data)
String.Concat("first part", "second part")
// F#!
String.concat ", " ["One"; "Two" ; "Three"]
String.Join(", ", [| "One"; "Two" ; "Three" |] )
let anArray = [| "One"; "Two" ; "Three" |]
let aList = ["One"; "Two" ; "Three"]
let rec processLines (lines) =
match lines with
| [] -> []
| currentLine :: remaining ->
let parsedLine = convertDataRow(currentLine)
let parsedRest = processLines(remaining)
parsedLine :: parsedRest
open System.IO
let lines = List.ofSeq(File.ReadAllLines(@"c:\tmp\data.csv"))
processLines(lines)
let rec calculateSum (rows) =
match rows with
| [] -> 0
| (_, value) :: tail ->
let remainingSum = calculateSum(tail)
value + remainingSum
calculateSum (processLines (lines))
let i1 = 1233
let i2 = 1233u
let i3 = 1233L
let i4 = 1233L
let f1 = 3.4
let f2 = 3.14f
// this is an F# float!
let f11 = Double.Parse("3,14")
let b1 = 127y
let b2 = 255uy
let d = 3.14M
let bigint = 1I
let (succ, num) = Int32.TryParse("123")
if succ then Console.Write("Succeeded: {0}", num)
else Console.Write("Failed!")
open System
open System.Drawing
open System.Windows.Forms
let mainForm = new Form(Width = 620, Height=450, Text = "Pie chart")
let menu = new ToolStrip()
let btnOpen = new ToolStripButton("Open")
let btnSave = new ToolStripButton("Save")
ignore (menu.Items.Add(btnOpen))
ignore (menu.Items.Add(btnSave))
let boxChart =
new PictureBox
(BackColor = Color.White, Dock = DockStyle.Fill,
SizeMode = PictureBoxSizeMode.CenterImage)
mainForm.Controls.Add(menu)
mainForm.Controls.Add(boxChart)
//[<STAThread>]
//do
//Application.Run(mainForm)
//mainForm.Show()
let rnd = new Random()
let randomBrush() =
let r, g, b = rnd.Next(256), rnd.Next(256), rnd.Next(256)
new SolidBrush(Color.FromArgb(r,g,b))
let fnt = new Font("Times New Roman", 11.0f)
let centerX, centerY = 300.0, 200.0
let labelDistance = 150.0
let drawLabel(gr:Graphics, title, startAngle, angle) =
let lblAngle = float (startAngle + angle / 2)
let ra = Math.PI * 2.0 * lblAngle / 360.0
let x = centerX + labelDistance * cos(ra)
let y = centerY + labelDistance * sin(ra)
let size = gr.MeasureString(title,fnt)
let rc = new PointF(float32(x) - size.Width / 2.0f,
float32(y) - size.Height / 2.0f)
gr.DrawString(title,fnt,Brushes.Black,new RectangleF(rc, size))
let drawPieSegment(gr:Graphics, title, startAngle,occupiedAngle) =
let br = randomBrush()
gr.FillPie (br , 170, 70, 260, 260, startAngle, occupiedAngle)
br.Dispose()
let drawStep (drawingFunc, gr:Graphics, sum, data) =
let rec drawStepUtil (data, angleSoFar) =
match data with
| [] -> ()
| [title, value] ->
let angle = 360 - angleSoFar
drawingFunc(gr, title, angleSoFar, angle)
| (title, value) :: tail ->
let angle = int (float(value) / sum * 360.0)
drawingFunc(gr,title, angleSoFar,angle)
drawStepUtil(tail,angleSoFar + angle)
drawStepUtil(data,0)
let drawChart(file) =
let lines = List.ofSeq (File.ReadAllLines(file))
let data = processLines(lines)
let sum = float (calculateSum(data))
let pieChart = new Bitmap(600, 400)
let gr = Graphics.FromImage(pieChart)
gr.Clear(Color.White)
drawStep(drawPieSegment, gr, sum, data)
drawStep(drawLabel, gr, sum, data)
gr.Dispose()
pieChart
let openAndDrawChart(e) =
let dlg = new OpenFileDialog(Filter="CSV Files|*.csv")
if (dlg.ShowDialog() = DialogResult.OK) then
let pieChart = drawChart(dlg.FileName)
boxChart.Image <- pieChart
btnSave.Enabled <- true
[<STAThread>]
do
btnOpen.Click.Add(openAndDrawChart)
//Application.Run(mainForm)
mainForm.Show()