This repository has been archived by the owner on Feb 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
LabelPointPlot.cs
324 lines (272 loc) · 8.25 KB
/
LabelPointPlot.cs
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
NPlot - A charting library for .NET
LabelPointPlot.cs
Copyright (C) 2003
Matt Howlett
Redistribution and use of NPlot or parts there-of in source and
binary forms, with or without modification, are permitted provided
that the following conditions are met:
1. Re-distributions in source form must retain at the head of each
source file the above copyright notice, this list of conditions
and the following disclaimer.
2. Any product ("the product") that makes use NPlot or parts
there-of must either:
(a) allow any user of the product to obtain a complete machine-
readable copy of the corresponding source code for the
product and the version of NPlot used for a charge no more
than your cost of physically performing source distribution,
on a medium customarily used for software interchange, or:
(b) reproduce the following text in the documentation, about
box or other materials intended to be read by human users
of the product that is provided to every human user of the
product:
"This product includes software developed as
part of the NPlot library project available
from: http://www.nplot.com/"
The words "This product" may optionally be replace with
the actual name of the product.
------------------------------------------------------------------------
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Drawing;
using System.Data;
namespace NPlot
{
/// <summary>
/// Encapsulates functionality
/// </summary>
public class LabelPointPlot : PointPlot, ISequencePlot
{
/// <summary>
/// This class us used in conjunction with SequenceAdapter to interpret data
/// specified to the TextPlot class.
/// </summary>
private class TextDataAdapter
{
private object data_;
private object dataSource_;
private string dataMember_;
public TextDataAdapter( object dataSource, string dataMember, object data )
{
this.data_ = data;
this.dataSource_ = dataSource;
this.dataMember_ = dataMember;
}
public string this[int i]
{
get
{
// this is inefficient [could set up delegates in constructor].
if (data_ is string[])
{
return ((string[])data_)[i];
}
if (data_ is string)
{
if (dataSource_ == null)
{
throw new NPlotException( "Error: DataSource null" );
}
System.Data.DataRowCollection rows;
if ( dataSource_ is System.Data.DataSet )
{
if (dataMember_ != null)
{
// TODO error check
rows = ((DataTable)((DataSet)dataSource_).Tables[dataMember_]).Rows;
}
else
{
// TODO error check
rows = ((DataTable)((DataSet)dataSource_).Tables[0]).Rows;
}
}
else if (dataSource_ is System.Data.DataTable )
{
rows = ((DataTable)dataSource_).Rows;
}
else
{
throw new NPlotException ( "not implemented yet" );
}
return (string)((System.Data.DataRow)(rows[i]))[(string)data_];
}
if (data_ is System.Collections.ArrayList)
{
object dataPoint = ((System.Collections.ArrayList)data_)[i];
if (dataPoint is string)
return (string)dataPoint;
throw new NPlotException( "TextDataAdapter: data not in recognised format" );
}
if (data_ == null)
{
return "text";
}
throw new NPlotException( "Text data not of recognised type" );
}
}
public int Count
{
get
{
// this is inefficient [could set up delegates in constructor].
if (data_ == null)
{
return 0;
}
if (data_ is string[])
{
return ((string[])data_).Length;
}
if (data_ is System.Collections.ArrayList)
{
return ((System.Collections.ArrayList)data_).Count;
}
throw new NPlotException( "Text data not in correct format" );
}
}
}
/// <summary>
/// Enumeration of all label positions relative to a point.
/// </summary>
public enum LabelPositions
{
/// <summary>
/// Above the point
/// </summary>
Above,
/// <summary>
/// Below the point
/// </summary>
Below,
/// <summary>
/// To the left of the point
/// </summary>
Left,
/// <summary>
/// To the right of the point
/// </summary>
Right
}
/// <summary>
/// Default Constructor
/// </summary>
public LabelPointPlot()
{
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="marker">The marker type to use for this plot.</param>
public LabelPointPlot( Marker marker )
: base( marker )
{
}
/// <summary>
/// The position of the text label in relation to the point.
/// </summary>
public LabelPositions LabelTextPosition
{
get
{
return labelTextPosition_;
}
set
{
labelTextPosition_ = value;
}
}
private LabelPositions labelTextPosition_ = LabelPositions.Above;
/// <summary>
/// The text datasource to attach to each point.
/// </summary>
public object TextData
{
get
{
return textData_;
}
set
{
textData_ = value;
}
}
object textData_;
/// <summary>
/// The Font used to write text.
/// </summary>
public Font Font
{
get
{
return font_;
}
set
{
font_ = value;
}
}
private Font font_ = new Font( "Arial", 8.0f );
/// <summary>
/// Draws the plot on a GDI+ surface against the provided x and y axes.
/// </summary>
/// <param name="g">The GDI+ surface on which to draw.</param>
/// <param name="xAxis">The X-Axis to draw against.</param>
/// <param name="yAxis">The Y-Axis to draw against.</param>
public override void Draw( Graphics g, PhysicalAxis xAxis, PhysicalAxis yAxis )
{
SequenceAdapter data =
new SequenceAdapter( this.DataSource, this.DataMember, this.OrdinateData, this.AbscissaData );
TextDataAdapter textData =
new TextDataAdapter( this.DataSource, this.DataMember, this.TextData );
// first plot the marker
// we can do this cast, since the constructor accepts only this type!
for (int i=0; i<data.Count; ++i)
{
try
{
PointD pt = data[i];
if ( !Double.IsNaN(pt.X) && !Double.IsNaN(pt.Y) )
{
PointF xPos = xAxis.WorldToPhysical( pt.X, false);
PointF yPos = yAxis.WorldToPhysical( pt.Y, false);
Marker.Draw( g, (int)xPos.X, (int)yPos.Y );
if ( textData[i] != "" )
{
SizeF size = g.MeasureString( textData[i], this.Font );
switch (labelTextPosition_)
{
case LabelPositions.Above:
g.DrawString( textData[i], font_, Brushes.Black, new PointF(xPos.X-size.Width/2,yPos.Y-size.Height-Marker.Size*2/3));
break;
case LabelPositions.Below:
g.DrawString( textData[i], font_, Brushes.Black, new PointF(xPos.X-size.Width/2,yPos.Y+Marker.Size*2/3));
break;
case LabelPositions.Left:
g.DrawString( textData[i], font_, Brushes.Black, new PointF(xPos.X-size.Width-Marker.Size*2/3,yPos.Y-size.Height/2));
break;
case LabelPositions.Right:
g.DrawString( textData[i], font_, Brushes.Black, new PointF(xPos.X+Marker.Size*2/3,yPos.Y-size.Height/2));
break;
}
}
}
}
catch
{
throw new NPlotException("Error in TextPlot.Draw");
}
}
}
}
}