[683] | 1 | using System;
|
---|
[754] | 2 | using System.Drawing;
|
---|
[697] | 3 | using HeuristicLab.Core;
|
---|
[683] | 4 |
|
---|
[754] | 5 | namespace HeuristicLab.Visualization
|
---|
| 6 | {
|
---|
| 7 | public partial class LineChart : ViewBase
|
---|
| 8 | {
|
---|
[697] | 9 | private readonly IChartDataRowsModel model;
|
---|
[754] | 10 | private Color[] lineColors;
|
---|
[684] | 11 |
|
---|
[697] | 12 | /// <summary>
|
---|
| 13 | /// This constructor shouldn't be called. Only required for the designer.
|
---|
| 14 | /// </summary>
|
---|
[754] | 15 | public LineChart()
|
---|
| 16 | {
|
---|
[684] | 17 | InitializeComponent();
|
---|
| 18 | }
|
---|
| 19 |
|
---|
[697] | 20 | /// <summary>
|
---|
| 21 | /// Initializes the chart.
|
---|
| 22 | /// </summary>
|
---|
[754] | 23 | /// <param name="model">Referenz to the model, for data</param>
|
---|
| 24 | public LineChart(IChartDataRowsModel model) : this()
|
---|
| 25 | {
|
---|
| 26 | if (model == null)
|
---|
| 27 | {
|
---|
[697] | 28 | throw new NullReferenceException("Model cannot be null.");
|
---|
| 29 | }
|
---|
[684] | 30 |
|
---|
[697] | 31 | this.model = model;
|
---|
[684] | 32 |
|
---|
[754] | 33 | //TODO: read line colors instead of static ones
|
---|
| 34 | lineColors = new Color[3];
|
---|
| 35 | lineColors[0] = Color.Red;
|
---|
| 36 | lineColors[1] = Color.Green;
|
---|
| 37 | lineColors[2] = Color.Blue;
|
---|
| 38 |
|
---|
| 39 | //TODO: correct Rectangle to fit
|
---|
| 40 | RectangleD clientRectangle = new RectangleD(ClientRectangle.Left, ClientRectangle.Top, ClientRectangle.Right,
|
---|
| 41 | ClientRectangle.Bottom);
|
---|
| 42 | canvasUI1.MainCanvas.WorldShape = new WorldShape(clientRectangle, clientRectangle);
|
---|
[697] | 43 | Reset();
|
---|
| 44 | }
|
---|
[684] | 45 |
|
---|
[697] | 46 | /// <summary>
|
---|
| 47 | /// Resets the line chart by deleting all shapes and reloading all data from the model.
|
---|
| 48 | /// </summary>
|
---|
[754] | 49 | private void Reset()
|
---|
| 50 | {
|
---|
[697] | 51 | BeginUpdate();
|
---|
| 52 |
|
---|
| 53 | // TODO clear existing shapes
|
---|
| 54 |
|
---|
[754] | 55 | WorldShape mainWorld = canvasUI1.MainCanvas.WorldShape;
|
---|
| 56 |
|
---|
| 57 | double spacing = mainWorld.BoundingBox.Width/model.Columns.Count;
|
---|
| 58 | double oldX = 0;
|
---|
| 59 | double currentX = spacing;
|
---|
| 60 | ChartDataRowsModelColumn oldColumn = null;
|
---|
[697] | 61 | // reload data from the model and create shapes
|
---|
[754] | 62 | foreach (ChartDataRowsModelColumn column in model.Columns)
|
---|
| 63 | {
|
---|
| 64 | if (oldColumn != null)
|
---|
| 65 | {
|
---|
| 66 | if (column.Values != null)
|
---|
| 67 | {
|
---|
| 68 | for (int i = 0; i < column.Values.Length; i++)
|
---|
| 69 | {
|
---|
| 70 | LineShape line = new LineShape(oldX, oldColumn.Values[i], currentX, column.Values[i], 0, lineColors[i]);
|
---|
| 71 | mainWorld.AddShape(line);
|
---|
| 72 | }
|
---|
| 73 | oldX = currentX;
|
---|
| 74 | currentX += spacing;
|
---|
| 75 | }
|
---|
| 76 | oldColumn = column;
|
---|
| 77 | }
|
---|
| 78 | else
|
---|
| 79 | {
|
---|
| 80 | oldColumn = column;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | canvasUI1.Invalidate();
|
---|
| 84 |
|
---|
| 85 | // AddColumn(column.ColumnId, column.Values);
|
---|
[684] | 86 | }
|
---|
[697] | 87 |
|
---|
| 88 | EndUpdate();
|
---|
[683] | 89 | }
|
---|
[684] | 90 |
|
---|
[697] | 91 | /// <summary>
|
---|
| 92 | /// Event handler which gets called when data in the model changes.
|
---|
| 93 | /// </summary>
|
---|
| 94 | /// <param name="type">Type of change</param>
|
---|
| 95 | /// <param name="columnId">Id of the changed column</param>
|
---|
| 96 | /// <param name="values">Values contained within the changed column</param>
|
---|
[754] | 97 | private void OnDataChanged(ChangeType type, long columnId, double[] values)
|
---|
| 98 | {
|
---|
| 99 | switch (type)
|
---|
| 100 | {
|
---|
[697] | 101 | case ChangeType.Add:
|
---|
| 102 | AddColumn(columnId, values);
|
---|
| 103 | break;
|
---|
| 104 | case ChangeType.Modify:
|
---|
| 105 | ModifyColumn(columnId, values);
|
---|
| 106 | break;
|
---|
| 107 | case ChangeType.Remove:
|
---|
| 108 | RemoveColumn(columnId);
|
---|
| 109 | break;
|
---|
| 110 | default:
|
---|
| 111 | throw new ArgumentOutOfRangeException("type");
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | /// <summary>
|
---|
| 116 | /// Adds a new column to the chart.
|
---|
| 117 | /// </summary>
|
---|
| 118 | /// <param name="columnId">Id of the column</param>
|
---|
| 119 | /// <param name="values">Values of the column</param>
|
---|
[754] | 120 | private void AddColumn(long columnId, double[] values)
|
---|
| 121 | {
|
---|
[684] | 122 | throw new NotImplementedException();
|
---|
| 123 | }
|
---|
[697] | 124 |
|
---|
| 125 | /// <summary>
|
---|
| 126 | /// Modifies an existing column of the chart.
|
---|
| 127 | /// </summary>
|
---|
| 128 | /// <param name="columnId">Id of the column</param>
|
---|
| 129 | /// <param name="values">Values of the column</param>
|
---|
[754] | 130 | private void ModifyColumn(long columnId, double[] values)
|
---|
| 131 | {
|
---|
[697] | 132 | throw new NotImplementedException();
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | /// <summary>
|
---|
| 136 | /// Removes a column from the chart.
|
---|
| 137 | /// </summary>
|
---|
| 138 | /// <param name="columnId">Id of the column</param>
|
---|
[754] | 139 | private void RemoveColumn(long columnId)
|
---|
| 140 | {
|
---|
[697] | 141 | throw new NotImplementedException();
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | #region Add-/RemoveItemEvents
|
---|
| 145 |
|
---|
[754] | 146 | protected override void AddItemEvents()
|
---|
| 147 | {
|
---|
[697] | 148 | base.AddItemEvents();
|
---|
| 149 | model.ColumnChanged += OnDataChanged;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
[754] | 152 | protected override void RemoveItemEvents()
|
---|
| 153 | {
|
---|
[697] | 154 | base.RemoveItemEvents();
|
---|
| 155 | model.ColumnChanged -= OnDataChanged;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | #endregion
|
---|
| 159 |
|
---|
| 160 | #region Begin-/EndUpdate
|
---|
| 161 |
|
---|
| 162 | private int beginUpdateCount = 0;
|
---|
| 163 |
|
---|
[754] | 164 | public void BeginUpdate()
|
---|
| 165 | {
|
---|
[697] | 166 | beginUpdateCount++;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
[754] | 169 | public void EndUpdate()
|
---|
| 170 | {
|
---|
| 171 | if (beginUpdateCount == 0)
|
---|
| 172 | {
|
---|
[697] | 173 | throw new InvalidOperationException("Too many EndUpdates.");
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | beginUpdateCount--;
|
---|
| 177 |
|
---|
[754] | 178 | if (beginUpdateCount == 0)
|
---|
| 179 | {
|
---|
[697] | 180 | Invalidate();
|
---|
| 181 | }
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | #endregion
|
---|
[684] | 185 | }
|
---|
| 186 | } |
---|