[683] | 1 | using System;
|
---|
[861] | 2 | using System.Collections.Generic;
|
---|
[928] | 3 | using System.Drawing;
|
---|
[1559] | 4 | using System.Drawing.Drawing2D;
|
---|
[928] | 5 | using System.Windows.Forms;
|
---|
[697] | 6 | using HeuristicLab.Core;
|
---|
[1781] | 7 | using HeuristicLab.Visualization.DataExport;
|
---|
[1233] | 8 | using HeuristicLab.Visualization.Legend;
|
---|
[1195] | 9 | using HeuristicLab.Visualization.Options;
|
---|
[1457] | 10 | using HeuristicLab.Visualization.Test;
|
---|
[683] | 11 |
|
---|
[861] | 12 | namespace HeuristicLab.Visualization {
|
---|
[1187] | 13 | public partial class LineChart : ViewBase {
|
---|
[697] | 14 | private readonly IChartDataRowsModel model;
|
---|
[1240] | 15 | private readonly Canvas canvas;
|
---|
| 16 |
|
---|
[1285] | 17 | private readonly TextShape titleShape = new TextShape("Title");
|
---|
| 18 | private readonly LegendShape legendShape = new LegendShape();
|
---|
| 19 | private readonly XAxis xAxis = new XAxis();
|
---|
[1876] | 20 | private readonly XAxisGrid xAxisGrid = new XAxisGrid();
|
---|
[1285] | 21 | private readonly List<RowEntry> rowEntries = new List<RowEntry>();
|
---|
[684] | 22 |
|
---|
[1285] | 23 | private readonly Dictionary<IDataRow, RowEntry> rowToRowEntry = new Dictionary<IDataRow, RowEntry>();
|
---|
[1038] | 24 |
|
---|
[1341] | 25 | private readonly ViewSettings viewSettings;
|
---|
| 26 |
|
---|
[1285] | 27 | private readonly WorldShape userInteractionShape = new WorldShape();
|
---|
| 28 | private readonly RectangleShape rectangleShape = new RectangleShape(0, 0, 0, 0, Color.FromArgb(50, 0, 0, 255));
|
---|
| 29 | private IMouseEventListener mouseEventListener;
|
---|
[983] | 30 |
|
---|
[1285] | 31 | private const int YAxisWidth = 100;
|
---|
[1462] | 32 | private const int XAxisHeight = 40;
|
---|
[1285] | 33 |
|
---|
[697] | 34 | /// <summary>
|
---|
| 35 | /// This constructor shouldn't be called. Only required for the designer.
|
---|
| 36 | /// </summary>
|
---|
[861] | 37 | public LineChart() {
|
---|
[684] | 38 | InitializeComponent();
|
---|
| 39 | }
|
---|
| 40 |
|
---|
[697] | 41 | /// <summary>
|
---|
| 42 | /// Initializes the chart.
|
---|
| 43 | /// </summary>
|
---|
[754] | 44 | /// <param name="model">Referenz to the model, for data</param>
|
---|
[861] | 45 | public LineChart(IChartDataRowsModel model) : this() {
|
---|
[1045] | 46 | if (model == null) {
|
---|
[697] | 47 | throw new NullReferenceException("Model cannot be null.");
|
---|
[1045] | 48 | }
|
---|
[684] | 49 |
|
---|
[1240] | 50 | canvas = canvasUI.Canvas;
|
---|
[983] | 51 |
|
---|
[1285] | 52 | this.model = model;
|
---|
[1341] | 53 | viewSettings = model.ViewSettings;
|
---|
[1342] | 54 | viewSettings.OnUpdateSettings += UpdateViewSettings;
|
---|
[1038] | 55 |
|
---|
[983] | 56 | Item = model;
|
---|
[1038] | 57 |
|
---|
[1240] | 58 | UpdateLayout();
|
---|
| 59 | canvasUI.Resize += delegate { UpdateLayout(); };
|
---|
[1187] | 60 |
|
---|
[1285] | 61 | ZoomToFullView();
|
---|
[697] | 62 | }
|
---|
[684] | 63 |
|
---|
[1346] | 64 | /// <summary>
|
---|
| 65 | /// updates the view settings
|
---|
| 66 | /// </summary>
|
---|
[1342] | 67 | private void UpdateViewSettings() {
|
---|
[1341] | 68 | titleShape.Font = viewSettings.TitleFont;
|
---|
| 69 | titleShape.Color = viewSettings.TitleColor;
|
---|
[1839] | 70 | titleShape.Text = model.Title;
|
---|
[1337] | 71 |
|
---|
[1341] | 72 | legendShape.Font = viewSettings.LegendFont;
|
---|
| 73 | legendShape.Color = viewSettings.LegendColor;
|
---|
[1337] | 74 |
|
---|
[1341] | 75 | xAxis.Font = viewSettings.XAxisFont;
|
---|
| 76 | xAxis.Color = viewSettings.XAxisColor;
|
---|
[1337] | 77 |
|
---|
[1345] | 78 | SetLegendPosition();
|
---|
[1342] | 79 |
|
---|
[1337] | 80 | canvasUI.Invalidate();
|
---|
| 81 | }
|
---|
| 82 |
|
---|
[1038] | 83 | /// <summary>
|
---|
| 84 | /// Layout management - arranges the inner shapes.
|
---|
| 85 | /// </summary>
|
---|
| 86 | private void UpdateLayout() {
|
---|
[1285] | 87 | canvas.ClearShapes();
|
---|
| 88 |
|
---|
[1608] | 89 | titleShape.Text = model.Title;
|
---|
| 90 |
|
---|
[1876] | 91 | if (model.ShowXAxisGrid) {
|
---|
| 92 | canvas.AddShape(xAxisGrid);
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[1350] | 95 | foreach (YAxisDescriptor yAxisDescriptor in model.YAxes) {
|
---|
| 96 | YAxisInfo info = GetYAxisInfo(yAxisDescriptor);
|
---|
[1458] | 97 | if (yAxisDescriptor.ShowGrid) {
|
---|
| 98 | info.Grid.Color = yAxisDescriptor.GridColor;
|
---|
| 99 | canvas.AddShape(info.Grid);
|
---|
| 100 | }
|
---|
[1285] | 101 | }
|
---|
| 102 |
|
---|
| 103 | foreach (RowEntry rowEntry in rowEntries) {
|
---|
| 104 | canvas.AddShape(rowEntry.LinesShape);
|
---|
| 105 | }
|
---|
| 106 |
|
---|
[1462] | 107 | xAxis.ShowLabel = model.ShowXAxisLabel;
|
---|
| 108 | xAxis.Label = model.XAxisLabel;
|
---|
| 109 |
|
---|
[1285] | 110 | canvas.AddShape(xAxis);
|
---|
| 111 |
|
---|
[1457] | 112 | int yAxesWidthLeft = 0;
|
---|
| 113 | int yAxesWidthRight = 0;
|
---|
[1343] | 114 |
|
---|
[1350] | 115 | foreach (YAxisDescriptor yAxisDescriptor in model.YAxes) {
|
---|
| 116 | YAxisInfo info = GetYAxisInfo(yAxisDescriptor);
|
---|
| 117 | if (yAxisDescriptor.ShowYAxis) {
|
---|
| 118 | canvas.AddShape(info.YAxis);
|
---|
[1462] | 119 | info.YAxis.ShowLabel = yAxisDescriptor.ShowYAxisLabel;
|
---|
| 120 | info.YAxis.Label = yAxisDescriptor.Label;
|
---|
[1457] | 121 | info.YAxis.Position = yAxisDescriptor.Position;
|
---|
| 122 | switch (yAxisDescriptor.Position) {
|
---|
| 123 | case AxisPosition.Left:
|
---|
| 124 | yAxesWidthLeft += YAxisWidth;
|
---|
| 125 | break;
|
---|
| 126 | case AxisPosition.Right:
|
---|
| 127 | yAxesWidthRight += YAxisWidth;
|
---|
| 128 | break;
|
---|
| 129 | default:
|
---|
| 130 | throw new NotImplementedException();
|
---|
| 131 | }
|
---|
[1343] | 132 | }
|
---|
[1285] | 133 | }
|
---|
| 134 |
|
---|
| 135 | canvas.AddShape(titleShape);
|
---|
| 136 | canvas.AddShape(legendShape);
|
---|
| 137 |
|
---|
| 138 | canvas.AddShape(userInteractionShape);
|
---|
| 139 |
|
---|
[1038] | 140 | titleShape.X = 10;
|
---|
[1240] | 141 | titleShape.Y = canvasUI.Height - 10;
|
---|
[1038] | 142 |
|
---|
[1457] | 143 | RectangleD linesAreaBoundingBox = new RectangleD(yAxesWidthLeft,
|
---|
[1285] | 144 | XAxisHeight,
|
---|
[1457] | 145 | canvasUI.Width - yAxesWidthRight,
|
---|
[1285] | 146 | canvasUI.Height);
|
---|
[1240] | 147 |
|
---|
[1285] | 148 | foreach (RowEntry rowEntry in rowEntries) {
|
---|
| 149 | rowEntry.LinesShape.BoundingBox = linesAreaBoundingBox;
|
---|
| 150 | }
|
---|
[1182] | 151 |
|
---|
[1876] | 152 | xAxisGrid.BoundingBox = linesAreaBoundingBox;
|
---|
| 153 |
|
---|
[1350] | 154 | foreach (YAxisDescriptor yAxisDescriptor in model.YAxes) {
|
---|
| 155 | YAxisInfo info = GetYAxisInfo(yAxisDescriptor);
|
---|
| 156 | info.Grid.BoundingBox = linesAreaBoundingBox;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
[1285] | 159 | int yAxisLeft = 0;
|
---|
[1457] | 160 | int yAxisRight = (int)linesAreaBoundingBox.X2;
|
---|
| 161 |
|
---|
[1350] | 162 | foreach (YAxisDescriptor yAxisDescriptor in model.YAxes) {
|
---|
| 163 | YAxisInfo info = GetYAxisInfo(yAxisDescriptor);
|
---|
| 164 | if (yAxisDescriptor.ShowYAxis) {
|
---|
[1457] | 165 | switch (yAxisDescriptor.Position) {
|
---|
| 166 | case AxisPosition.Left:
|
---|
| 167 | info.YAxis.BoundingBox = new RectangleD(yAxisLeft,
|
---|
| 168 | linesAreaBoundingBox.Y1,
|
---|
| 169 | yAxisLeft + YAxisWidth,
|
---|
| 170 | linesAreaBoundingBox.Y2);
|
---|
| 171 | yAxisLeft += YAxisWidth;
|
---|
| 172 | break;
|
---|
| 173 | case AxisPosition.Right:
|
---|
| 174 | info.YAxis.BoundingBox = new RectangleD(yAxisRight,
|
---|
| 175 | linesAreaBoundingBox.Y1,
|
---|
| 176 | yAxisRight + YAxisWidth,
|
---|
| 177 | linesAreaBoundingBox.Y2);
|
---|
| 178 | yAxisRight += YAxisWidth;
|
---|
| 179 | break;
|
---|
| 180 | default:
|
---|
| 181 | throw new NotImplementedException();
|
---|
| 182 | }
|
---|
[1343] | 183 | }
|
---|
[1285] | 184 | }
|
---|
[1187] | 185 |
|
---|
[1285] | 186 | userInteractionShape.BoundingBox = linesAreaBoundingBox;
|
---|
| 187 | userInteractionShape.ClippingArea = new RectangleD(0, 0, userInteractionShape.BoundingBox.Width, userInteractionShape.BoundingBox.Height);
|
---|
[1182] | 188 |
|
---|
[1285] | 189 | xAxis.BoundingBox = new RectangleD(linesAreaBoundingBox.X1,
|
---|
[1038] | 190 | 0,
|
---|
[1285] | 191 | linesAreaBoundingBox.X2,
|
---|
| 192 | linesAreaBoundingBox.Y1);
|
---|
[1049] | 193 |
|
---|
[1345] | 194 | SetLegendPosition();
|
---|
[1342] | 195 | }
|
---|
| 196 |
|
---|
[1350] | 197 | private readonly Dictionary<YAxisDescriptor, YAxisInfo> yAxisInfos = new Dictionary<YAxisDescriptor, YAxisInfo>();
|
---|
| 198 |
|
---|
| 199 | private YAxisInfo GetYAxisInfo(YAxisDescriptor yAxisDescriptor) {
|
---|
| 200 | YAxisInfo info;
|
---|
| 201 |
|
---|
| 202 | if (!yAxisInfos.TryGetValue(yAxisDescriptor, out info)) {
|
---|
| 203 | info = new YAxisInfo();
|
---|
| 204 | yAxisInfos[yAxisDescriptor] = info;
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | return info;
|
---|
| 208 | }
|
---|
| 209 |
|
---|
[1346] | 210 | /// <summary>
|
---|
| 211 | /// sets the legend position
|
---|
| 212 | /// </summary>
|
---|
[1345] | 213 | private void SetLegendPosition() {
|
---|
| 214 | switch (viewSettings.LegendPosition) {
|
---|
| 215 | case LegendPosition.Bottom:
|
---|
| 216 | setLegendBottom();
|
---|
| 217 | break;
|
---|
| 218 |
|
---|
| 219 | case LegendPosition.Top:
|
---|
| 220 | setLegendTop();
|
---|
| 221 | break;
|
---|
| 222 |
|
---|
| 223 | case LegendPosition.Left:
|
---|
| 224 | setLegendLeft();
|
---|
| 225 | break;
|
---|
| 226 |
|
---|
| 227 | case LegendPosition.Right:
|
---|
| 228 | setLegendRight();
|
---|
| 229 | break;
|
---|
| 230 | }
|
---|
| 231 | }
|
---|
| 232 |
|
---|
[1342] | 233 | public void setLegendRight() {
|
---|
| 234 | // legend right
|
---|
[1388] | 235 | legendShape.BoundingBox = new RectangleD(canvasUI.Width - legendShape.GetMaxLabelLength(), 10, canvasUI.Width, canvasUI.Height - 50);
|
---|
[1342] | 236 | legendShape.ClippingArea = new RectangleD(0, 0, legendShape.BoundingBox.Width, legendShape.BoundingBox.Height);
|
---|
| 237 | legendShape.Row = false;
|
---|
| 238 | legendShape.CreateLegend();
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 | public void setLegendLeft() {
|
---|
| 242 | // legend left
|
---|
[1388] | 243 | legendShape.BoundingBox = new RectangleD(10, 10, canvasUI.Width, canvasUI.Height - 50);
|
---|
[1342] | 244 | legendShape.ClippingArea = new RectangleD(0, 0, legendShape.BoundingBox.Width, legendShape.BoundingBox.Height);
|
---|
| 245 | legendShape.Row = false;
|
---|
| 246 | legendShape.CreateLegend();
|
---|
[1285] | 247 |
|
---|
| 248 | canvasUI.Invalidate();
|
---|
[1038] | 249 | }
|
---|
| 250 |
|
---|
[1342] | 251 | public void setLegendTop() {
|
---|
| 252 | // legend top
|
---|
| 253 | legendShape.BoundingBox = new RectangleD(100, canvasUI.Height - canvasUI.Height, canvasUI.Width, canvasUI.Height - 10);
|
---|
| 254 | legendShape.ClippingArea = new RectangleD(0, 0, legendShape.BoundingBox.Width, legendShape.BoundingBox.Height);
|
---|
| 255 | legendShape.Row = true;
|
---|
| 256 | legendShape.Top = true;
|
---|
| 257 | legendShape.CreateLegend();
|
---|
| 258 | }
|
---|
| 259 |
|
---|
| 260 | public void setLegendBottom() {
|
---|
| 261 | // legend bottom
|
---|
[1608] | 262 | legendShape.BoundingBox = new RectangleD(100, 10, canvasUI.Width, canvasUI.Height /*legendShape.GetHeight4Rows()*/);
|
---|
[1342] | 263 | legendShape.ClippingArea = new RectangleD(0, 0, legendShape.BoundingBox.Width, legendShape.BoundingBox.Height);
|
---|
| 264 | legendShape.Row = true;
|
---|
| 265 | legendShape.Top = false;
|
---|
| 266 | legendShape.CreateLegend();
|
---|
| 267 | }
|
---|
| 268 |
|
---|
[1242] | 269 | private void optionsToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
[1341] | 270 | OptionsDialog optionsdlg = new OptionsDialog(model);
|
---|
[1459] | 271 | optionsdlg.Show();
|
---|
[1242] | 272 | }
|
---|
| 273 |
|
---|
[1781] | 274 | private void exportToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
| 275 | ExportDialog exportdlg = new ExportDialog();
|
---|
| 276 | exportdlg.ShowDialog(this);
|
---|
| 277 |
|
---|
| 278 | IExporter exporter = exportdlg.SelectedExporter;
|
---|
| 279 |
|
---|
| 280 | if (exporter != null)
|
---|
| 281 | exporter.Export(model);
|
---|
| 282 | }
|
---|
| 283 |
|
---|
[1242] | 284 | public void OnDataRowChanged(IDataRow row) {
|
---|
[1285] | 285 | RowEntry rowEntry = rowToRowEntry[row];
|
---|
| 286 |
|
---|
| 287 | rowEntry.LinesShape.UpdateStyle(row);
|
---|
| 288 |
|
---|
[1343] | 289 | UpdateLayout();
|
---|
| 290 |
|
---|
[1242] | 291 | canvasUI.Invalidate();
|
---|
| 292 | }
|
---|
| 293 |
|
---|
[861] | 294 | #region Add-/RemoveItemEvents
|
---|
| 295 |
|
---|
| 296 | protected override void AddItemEvents() {
|
---|
| 297 | base.AddItemEvents();
|
---|
| 298 |
|
---|
| 299 | model.DataRowAdded += OnDataRowAdded;
|
---|
| 300 | model.DataRowRemoved += OnDataRowRemoved;
|
---|
| 301 | model.ModelChanged += OnModelChanged;
|
---|
[869] | 302 |
|
---|
[1045] | 303 | foreach (IDataRow row in model.Rows) {
|
---|
[869] | 304 | OnDataRowAdded(row);
|
---|
[1045] | 305 | }
|
---|
[683] | 306 | }
|
---|
[684] | 307 |
|
---|
[861] | 308 | protected override void RemoveItemEvents() {
|
---|
| 309 | base.RemoveItemEvents();
|
---|
| 310 |
|
---|
| 311 | model.DataRowAdded -= OnDataRowAdded;
|
---|
| 312 | model.DataRowRemoved -= OnDataRowRemoved;
|
---|
| 313 | model.ModelChanged -= OnModelChanged;
|
---|
[697] | 314 | }
|
---|
| 315 |
|
---|
[861] | 316 | private void OnDataRowAdded(IDataRow row) {
|
---|
| 317 | row.ValueChanged += OnRowValueChanged;
|
---|
| 318 | row.ValuesChanged += OnRowValuesChanged;
|
---|
[1237] | 319 | row.DataRowChanged += OnDataRowChanged;
|
---|
| 320 |
|
---|
[1049] | 321 | legendShape.AddLegendItem(new LegendItem(row.Label, row.Color, row.Thickness));
|
---|
| 322 | legendShape.CreateLegend();
|
---|
[1285] | 323 |
|
---|
[987] | 324 | InitLineShapes(row);
|
---|
[1285] | 325 |
|
---|
| 326 | UpdateLayout();
|
---|
[684] | 327 | }
|
---|
[697] | 328 |
|
---|
[1240] | 329 | private void OnDataRowRemoved(IDataRow row) {
|
---|
| 330 | row.ValueChanged -= OnRowValueChanged;
|
---|
| 331 | row.ValuesChanged -= OnRowValuesChanged;
|
---|
| 332 | row.DataRowChanged -= OnDataRowChanged;
|
---|
[1285] | 333 |
|
---|
| 334 | rowToRowEntry.Remove(row);
|
---|
| 335 | rowEntries.RemoveAll(delegate(RowEntry rowEntry) { return rowEntry.DataRow == row; });
|
---|
| 336 |
|
---|
| 337 | UpdateLayout();
|
---|
[1240] | 338 | }
|
---|
| 339 |
|
---|
| 340 | #endregion
|
---|
| 341 |
|
---|
[1249] | 342 | public void ZoomToFullView() {
|
---|
[1285] | 343 | SetClipX(-0.1, model.MaxDataRowValues - 0.9);
|
---|
[987] | 344 |
|
---|
[1285] | 345 | foreach (RowEntry rowEntry in rowEntries) {
|
---|
[1350] | 346 | YAxisDescriptor yAxisDescriptor = rowEntry.DataRow.YAxis;
|
---|
[1249] | 347 |
|
---|
[1285] | 348 | SetClipY(rowEntry,
|
---|
[1350] | 349 | yAxisDescriptor.MinValue - ((yAxisDescriptor.MaxValue - yAxisDescriptor.MinValue)*0.05),
|
---|
| 350 | yAxisDescriptor.MaxValue + ((yAxisDescriptor.MaxValue - yAxisDescriptor.MinValue)*0.05));
|
---|
[1285] | 351 | }
|
---|
| 352 |
|
---|
| 353 | canvasUI.Invalidate();
|
---|
[987] | 354 | }
|
---|
| 355 |
|
---|
[1285] | 356 | private void SetClipX(double x1, double x2) {
|
---|
[1876] | 357 | xAxisGrid.ClippingArea = new RectangleD(x1,
|
---|
| 358 | xAxisGrid.ClippingArea.Y1,
|
---|
| 359 | x2,
|
---|
| 360 | xAxisGrid.ClippingArea.Y2);
|
---|
| 361 |
|
---|
[1285] | 362 | xAxis.ClippingArea = new RectangleD(x1,
|
---|
| 363 | 0,
|
---|
| 364 | x2,
|
---|
| 365 | XAxisHeight);
|
---|
[1249] | 366 |
|
---|
[1285] | 367 | foreach (RowEntry rowEntry in rowEntries) {
|
---|
| 368 | rowEntry.LinesShape.ClippingArea = new RectangleD(x1,
|
---|
| 369 | rowEntry.LinesShape.ClippingArea.Y1,
|
---|
| 370 | x2,
|
---|
| 371 | rowEntry.LinesShape.ClippingArea.Y2);
|
---|
[1249] | 372 | }
|
---|
[1350] | 373 |
|
---|
| 374 | foreach (YAxisDescriptor yAxisDescriptor in model.YAxes) {
|
---|
| 375 | YAxisInfo info = GetYAxisInfo(yAxisDescriptor);
|
---|
| 376 | info.Grid.ClippingArea = new RectangleD(x1,
|
---|
| 377 | info.Grid.ClippingArea.Y1,
|
---|
| 378 | x2,
|
---|
| 379 | info.Grid.ClippingArea.Y2);
|
---|
| 380 | info.YAxis.ClippingArea = new RectangleD(0,
|
---|
| 381 | info.YAxis.ClippingArea.Y1,
|
---|
| 382 | YAxisWidth,
|
---|
| 383 | info.YAxis.ClippingArea.Y2);
|
---|
| 384 | }
|
---|
[1285] | 385 | }
|
---|
[1249] | 386 |
|
---|
[1350] | 387 | private void SetClipY(RowEntry rowEntry, double y1, double y2) {
|
---|
[1876] | 388 | xAxisGrid.ClippingArea = new RectangleD(xAxisGrid.ClippingArea.X1,
|
---|
| 389 | y1,
|
---|
| 390 | xAxisGrid.ClippingArea.X2,
|
---|
| 391 | y2);
|
---|
| 392 |
|
---|
[1285] | 393 | rowEntry.LinesShape.ClippingArea = new RectangleD(rowEntry.LinesShape.ClippingArea.X1,
|
---|
| 394 | y1,
|
---|
| 395 | rowEntry.LinesShape.ClippingArea.X2,
|
---|
| 396 | y2);
|
---|
[1350] | 397 |
|
---|
| 398 | YAxisInfo info = GetYAxisInfo(rowEntry.DataRow.YAxis);
|
---|
| 399 |
|
---|
| 400 | info.Grid.ClippingArea = new RectangleD(info.Grid.ClippingArea.X1,
|
---|
| 401 | y1,
|
---|
| 402 | info.Grid.ClippingArea.X2,
|
---|
| 403 | y2);
|
---|
| 404 | info.YAxis.ClippingArea = new RectangleD(info.YAxis.ClippingArea.X1,
|
---|
| 405 | y1,
|
---|
| 406 | info.YAxis.ClippingArea.X2,
|
---|
| 407 | y2);
|
---|
[981] | 408 | }
|
---|
| 409 |
|
---|
[987] | 410 | private void InitLineShapes(IDataRow row) {
|
---|
[1285] | 411 | RowEntry rowEntry = new RowEntry(row);
|
---|
| 412 | rowEntries.Add(rowEntry);
|
---|
| 413 | rowToRowEntry[row] = rowEntry;
|
---|
| 414 |
|
---|
[1242] | 415 | if ((row.LineType == DataRowType.SingleValue)) {
|
---|
| 416 | if (row.Count > 0) {
|
---|
| 417 | LineShape lineShape = new HorizontalLineShape(0, row[0], double.MaxValue, row[0], row.Color, row.Thickness,
|
---|
| 418 | row.Style);
|
---|
[1285] | 419 | rowEntry.LinesShape.AddShape(lineShape);
|
---|
[1242] | 420 | }
|
---|
[1249] | 421 | } else {
|
---|
[1561] | 422 | rowEntry.showMarkers(row.ShowMarkers);
|
---|
[1242] | 423 | for (int i = 1; i < row.Count; i++) {
|
---|
[1283] | 424 | LineShape lineShape = new LineShape(i - 1, row[i - 1], i, row[i], row.Color, row.Thickness, row.Style);
|
---|
[1285] | 425 | rowEntry.LinesShape.AddShape(lineShape);
|
---|
[1608] | 426 | rowEntry.LinesShape.AddMarkerShape(new MarkerShape(i - 1, row[i - 1], 8, row.Color));
|
---|
[1242] | 427 | }
|
---|
[1608] | 428 | if (row.Count > 0) {
|
---|
[1559] | 429 | rowEntry.LinesShape.AddMarkerShape(new MarkerShape((row.Count - 1), row[(row.Count - 1)], 8, row.Color));
|
---|
[1608] | 430 | }
|
---|
[1242] | 431 | }
|
---|
[1249] | 432 |
|
---|
[981] | 433 | ZoomToFullView();
|
---|
[697] | 434 | }
|
---|
| 435 |
|
---|
[869] | 436 | private void OnRowValueChanged(IDataRow row, double value, int index, Action action) {
|
---|
[1285] | 437 | RowEntry rowEntry = rowToRowEntry[row];
|
---|
| 438 |
|
---|
[1242] | 439 | if (row.LineType == DataRowType.SingleValue) {
|
---|
| 440 | if (action == Action.Added) {
|
---|
| 441 | LineShape lineShape = new HorizontalLineShape(0, row[0], double.MaxValue, row[0], row.Color, row.Thickness,
|
---|
| 442 | row.Style);
|
---|
[1285] | 443 | rowEntry.LinesShape.AddShape(lineShape);
|
---|
[1249] | 444 | } else {
|
---|
[1285] | 445 | LineShape lineShape = rowEntry.LinesShape.GetShape(0);
|
---|
| 446 | lineShape.Y1 = value;
|
---|
| 447 | lineShape.Y2 = value;
|
---|
[1242] | 448 | }
|
---|
[1249] | 449 | } else {
|
---|
[1608] | 450 | if (index > rowEntry.LinesShape.Count + 1) {
|
---|
| 451 | //MarkersShape is on position zero
|
---|
[1242] | 452 | throw new NotImplementedException();
|
---|
| 453 | }
|
---|
[861] | 454 |
|
---|
[1603] | 455 | if (action == Action.Added) {
|
---|
| 456 | // new value was added
|
---|
| 457 | if (index > 0 && index == rowEntry.LinesShape.Count + 1) {
|
---|
| 458 | LineShape lineShape = new LineShape(index - 1, row[index - 1], index, row[index], row.Color, row.Thickness,
|
---|
| 459 | row.Style);
|
---|
| 460 | rowEntry.LinesShape.AddShape(lineShape);
|
---|
| 461 | rowEntry.LinesShape.AddMarkerShape(new MarkerShape(index, row[index], 8, row.Color));
|
---|
| 462 | }
|
---|
[1608] | 463 | } else if (action == Action.Modified) {
|
---|
[1603] | 464 | // not the first value
|
---|
| 465 | if (index > 0) {
|
---|
| 466 | rowEntry.LinesShape.GetShape(index - 1).Y2 = value;
|
---|
[1608] | 467 | ((MarkerShape)rowEntry.LinesShape.markersShape.GetShape(index - 1)).Y = value;
|
---|
[1603] | 468 | }
|
---|
[861] | 469 |
|
---|
[1603] | 470 | // not the last value
|
---|
| 471 | if (index > 0 && index < row.Count - 1) {
|
---|
| 472 | rowEntry.LinesShape.GetShape(index).Y1 = value;
|
---|
[1608] | 473 | ((MarkerShape)rowEntry.LinesShape.markersShape.GetShape(index)).Y = value;
|
---|
[1603] | 474 | }
|
---|
[1242] | 475 | }
|
---|
[1045] | 476 | }
|
---|
[861] | 477 |
|
---|
[981] | 478 | ZoomToFullView();
|
---|
[697] | 479 | }
|
---|
| 480 |
|
---|
[869] | 481 | private void OnRowValuesChanged(IDataRow row, double[] values, int index, Action action) {
|
---|
[1045] | 482 | foreach (double value in values) {
|
---|
[869] | 483 | OnRowValueChanged(row, value, index++, action);
|
---|
[1045] | 484 | }
|
---|
[861] | 485 | }
|
---|
[761] | 486 |
|
---|
[1182] | 487 | private void OnModelChanged() {
|
---|
[1876] | 488 | UpdateLayout();
|
---|
[1240] | 489 | canvasUI.Invalidate();
|
---|
[1182] | 490 | }
|
---|
| 491 |
|
---|
[697] | 492 | #region Begin-/EndUpdate
|
---|
| 493 |
|
---|
[1242] | 494 | private int beginUpdateCount;
|
---|
[697] | 495 |
|
---|
[861] | 496 | public void BeginUpdate() {
|
---|
[697] | 497 | beginUpdateCount++;
|
---|
| 498 | }
|
---|
| 499 |
|
---|
[861] | 500 | public void EndUpdate() {
|
---|
[1045] | 501 | if (beginUpdateCount == 0) {
|
---|
[697] | 502 | throw new InvalidOperationException("Too many EndUpdates.");
|
---|
[1045] | 503 | }
|
---|
[697] | 504 |
|
---|
| 505 | beginUpdateCount--;
|
---|
| 506 |
|
---|
[1045] | 507 | if (beginUpdateCount == 0) {
|
---|
[1240] | 508 | canvasUI.Invalidate();
|
---|
[1045] | 509 | }
|
---|
[697] | 510 | }
|
---|
| 511 |
|
---|
| 512 | #endregion
|
---|
[928] | 513 |
|
---|
[1059] | 514 | #region Zooming / Panning
|
---|
| 515 |
|
---|
[1249] | 516 | private void Pan(Point startPoint, Point endPoint) {
|
---|
[1351] | 517 | RectangleD clippingArea = Translate.ClippingArea(startPoint, endPoint, xAxis.ClippingArea, xAxis.Viewport);
|
---|
[1285] | 518 |
|
---|
[1351] | 519 | SetClipX(clippingArea.X1, clippingArea.X2);
|
---|
[1285] | 520 |
|
---|
| 521 | foreach (RowEntry rowEntry in rowEntries) {
|
---|
[1390] | 522 | if (rowEntry.DataRow.YAxis.ClipChangeable) {
|
---|
| 523 | clippingArea = Translate.ClippingArea(startPoint, endPoint, rowEntry.LinesShape.ClippingArea, rowEntry.LinesShape.Viewport);
|
---|
| 524 | SetClipY(rowEntry, clippingArea.Y1, clippingArea.Y2);
|
---|
| 525 | }
|
---|
[1285] | 526 | }
|
---|
| 527 |
|
---|
| 528 | canvasUI.Invalidate();
|
---|
[1249] | 529 | }
|
---|
| 530 |
|
---|
[1351] | 531 | private void PanEnd(Point startPoint, Point endPoint) {
|
---|
| 532 | Pan(startPoint, endPoint);
|
---|
[1249] | 533 | }
|
---|
| 534 |
|
---|
| 535 | private void SetClippingArea(Rectangle rectangle) {
|
---|
[1351] | 536 | RectangleD clippingArea = Transform.ToWorld(rectangle, xAxis.Viewport, xAxis.ClippingArea);
|
---|
| 537 |
|
---|
| 538 | SetClipX(clippingArea.X1, clippingArea.X2);
|
---|
| 539 |
|
---|
[1285] | 540 | foreach (RowEntry rowEntry in rowEntries) {
|
---|
[1390] | 541 | if (rowEntry.DataRow.YAxis.ClipChangeable) {
|
---|
| 542 | clippingArea = Transform.ToWorld(rectangle, rowEntry.LinesShape.Viewport, rowEntry.LinesShape.ClippingArea);
|
---|
[1249] | 543 |
|
---|
[1390] | 544 | SetClipY(rowEntry, clippingArea.Y1, clippingArea.Y2);
|
---|
| 545 | }
|
---|
[1285] | 546 | }
|
---|
| 547 |
|
---|
[1249] | 548 | userInteractionShape.RemoveShape(rectangleShape);
|
---|
[1285] | 549 | canvasUI.Invalidate();
|
---|
[1249] | 550 | }
|
---|
| 551 |
|
---|
| 552 | private void DrawRectangle(Rectangle rectangle) {
|
---|
| 553 | rectangleShape.Rectangle = Transform.ToWorld(rectangle, userInteractionShape.Viewport, userInteractionShape.ClippingArea);
|
---|
| 554 | canvasUI.Invalidate();
|
---|
| 555 | }
|
---|
| 556 |
|
---|
[1608] | 557 | private void canvasUI1_KeyDown(object sender, KeyEventArgs e) {}
|
---|
[1059] | 558 |
|
---|
[928] | 559 | private void canvasUI1_MouseDown(object sender, MouseEventArgs e) {
|
---|
[1058] | 560 | Focus();
|
---|
[1249] | 561 |
|
---|
[1237] | 562 | if (e.Button == MouseButtons.Right) {
|
---|
[1781] | 563 | contextMenu.Show(PointToScreen(e.Location));
|
---|
[1249] | 564 | } else if (e.Button == MouseButtons.Left) {
|
---|
| 565 | if (ModifierKeys == Keys.None) {
|
---|
| 566 | PanListener panListener = new PanListener(e.Location);
|
---|
| 567 | panListener.Pan += Pan;
|
---|
| 568 | panListener.PanEnd += PanEnd;
|
---|
| 569 |
|
---|
| 570 | mouseEventListener = panListener;
|
---|
| 571 | } else if (ModifierKeys == Keys.Control) {
|
---|
| 572 | ZoomListener zoomListener = new ZoomListener(e.Location);
|
---|
| 573 | zoomListener.DrawRectangle += DrawRectangle;
|
---|
| 574 | zoomListener.SetClippingArea += SetClippingArea;
|
---|
| 575 |
|
---|
| 576 | rectangleShape.Rectangle = RectangleD.Empty;
|
---|
| 577 | userInteractionShape.AddShape(rectangleShape);
|
---|
| 578 |
|
---|
| 579 | mouseEventListener = zoomListener;
|
---|
[1187] | 580 | }
|
---|
| 581 | }
|
---|
[928] | 582 | }
|
---|
| 583 |
|
---|
[1249] | 584 | private void canvasUI_MouseMove(object sender, MouseEventArgs e) {
|
---|
| 585 | if (mouseEventListener != null) {
|
---|
| 586 | mouseEventListener.MouseMove(sender, e);
|
---|
| 587 | }
|
---|
| 588 | }
|
---|
[1244] | 589 |
|
---|
[1249] | 590 | private void canvasUI_MouseUp(object sender, MouseEventArgs e) {
|
---|
| 591 | if (mouseEventListener != null) {
|
---|
| 592 | mouseEventListener.MouseUp(sender, e);
|
---|
| 593 | }
|
---|
[1240] | 594 |
|
---|
[1249] | 595 | mouseEventListener = null;
|
---|
[1240] | 596 | }
|
---|
| 597 |
|
---|
[1058] | 598 | private void canvasUI1_MouseWheel(object sender, MouseEventArgs e) {
|
---|
| 599 | if (ModifierKeys == Keys.Control) {
|
---|
[1351] | 600 | double zoomFactor = (e.Delta > 0) ? 0.7 : 1.3;
|
---|
[1058] | 601 |
|
---|
[1351] | 602 | PointD world;
|
---|
| 603 |
|
---|
| 604 | world = Transform.ToWorld(e.Location, xAxis.Viewport, xAxis.ClippingArea);
|
---|
| 605 |
|
---|
| 606 | double x1 = world.X - (world.X - xAxis.ClippingArea.X1)*zoomFactor;
|
---|
| 607 | double x2 = world.X + (xAxis.ClippingArea.X2 - world.X)*zoomFactor;
|
---|
| 608 |
|
---|
| 609 | SetClipX(x1, x2);
|
---|
| 610 |
|
---|
[1285] | 611 | foreach (RowEntry rowEntry in rowEntries) {
|
---|
[1351] | 612 | world = Transform.ToWorld(e.Location, rowEntry.LinesShape.Viewport, rowEntry.LinesShape.ClippingArea);
|
---|
| 613 |
|
---|
[1608] | 614 | double y1 = world.Y - (world.Y - rowEntry.LinesShape.ClippingArea.Y1)*zoomFactor;
|
---|
[1351] | 615 | double y2 = world.Y + (rowEntry.LinesShape.ClippingArea.Y2 - world.Y)*zoomFactor;
|
---|
| 616 |
|
---|
| 617 | SetClipY(rowEntry, y1, y2);
|
---|
[1285] | 618 | }
|
---|
[1351] | 619 |
|
---|
| 620 | canvasUI.Invalidate();
|
---|
[1058] | 621 | }
|
---|
| 622 | }
|
---|
| 623 |
|
---|
[1249] | 624 | #endregion
|
---|
[928] | 625 |
|
---|
[1285] | 626 | private class LinesShape : WorldShape {
|
---|
[1559] | 627 | public readonly CompositeShape markersShape = new CompositeShape();
|
---|
| 628 |
|
---|
[1285] | 629 | public void UpdateStyle(IDataRow row) {
|
---|
| 630 | foreach (IShape shape in shapes) {
|
---|
| 631 | LineShape lineShape = shape as LineShape;
|
---|
| 632 | if (lineShape != null) {
|
---|
| 633 | lineShape.LSColor = row.Color;
|
---|
| 634 | lineShape.LSDrawingStyle = row.Style;
|
---|
| 635 | lineShape.LSThickness = row.Thickness;
|
---|
| 636 | }
|
---|
| 637 | }
|
---|
[1561] | 638 | this.markersShape.ShowChildShapes = row.ShowMarkers;
|
---|
[1285] | 639 | }
|
---|
[928] | 640 |
|
---|
[1559] | 641 | public override void Draw(Graphics graphics) {
|
---|
| 642 | GraphicsState gstate = graphics.Save();
|
---|
| 643 |
|
---|
| 644 | graphics.SetClip(Viewport);
|
---|
| 645 | foreach (IShape shape in shapes) {
|
---|
| 646 | // draw child shapes using our own clipping area
|
---|
| 647 | shape.Draw(graphics);
|
---|
| 648 | }
|
---|
| 649 | markersShape.Draw(graphics);
|
---|
| 650 | graphics.Restore(gstate);
|
---|
| 651 | }
|
---|
| 652 |
|
---|
| 653 | public void AddMarkerShape(IShape shape) {
|
---|
| 654 | shape.Parent = this;
|
---|
| 655 | markersShape.AddShape(shape);
|
---|
| 656 | }
|
---|
| 657 |
|
---|
[1285] | 658 | public int Count {
|
---|
| 659 | get { return shapes.Count; }
|
---|
| 660 | }
|
---|
[928] | 661 |
|
---|
[1285] | 662 | public LineShape GetShape(int index) {
|
---|
[1608] | 663 | return (LineShape)shapes[index]; //shapes[0] is markersShape!!
|
---|
[1285] | 664 | }
|
---|
| 665 | }
|
---|
| 666 |
|
---|
| 667 | private class RowEntry {
|
---|
| 668 | private readonly IDataRow dataRow;
|
---|
| 669 |
|
---|
| 670 | private readonly LinesShape linesShape = new LinesShape();
|
---|
| 671 |
|
---|
| 672 | public RowEntry(IDataRow dataRow) {
|
---|
| 673 | this.dataRow = dataRow;
|
---|
[1559] | 674 | linesShape.markersShape.Parent = linesShape;
|
---|
[1285] | 675 | }
|
---|
| 676 |
|
---|
| 677 | public IDataRow DataRow {
|
---|
| 678 | get { return dataRow; }
|
---|
| 679 | }
|
---|
| 680 |
|
---|
[1350] | 681 | public LinesShape LinesShape {
|
---|
| 682 | get { return linesShape; }
|
---|
| 683 | }
|
---|
[1559] | 684 |
|
---|
[1561] | 685 | public void showMarkers(bool flag) {
|
---|
| 686 | linesShape.markersShape.ShowChildShapes = flag;
|
---|
[1559] | 687 | }
|
---|
[1350] | 688 | }
|
---|
| 689 |
|
---|
[1608] | 690 | private class YAxisInfo {
|
---|
[1876] | 691 | private readonly YAxisGrid grid = new YAxisGrid();
|
---|
[1350] | 692 | private readonly YAxis yAxis = new YAxis();
|
---|
| 693 |
|
---|
[1876] | 694 | public YAxisGrid Grid {
|
---|
[1285] | 695 | get { return grid; }
|
---|
| 696 | }
|
---|
| 697 |
|
---|
| 698 | public YAxis YAxis {
|
---|
| 699 | get { return yAxis; }
|
---|
| 700 | }
|
---|
| 701 | }
|
---|
[684] | 702 | }
|
---|
[1608] | 703 | } |
---|