Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GaussianProcessTuning/ILNumerics.2.14.4735.573/Drawing/Plots/ILBarGraph2D.cs @ 11194

Last change on this file since 11194 was 9102, checked in by gkronber, 12 years ago

#1967: ILNumerics source for experimentation

File size: 9.6 KB
RevLine 
[9102]1///
2///    This file is part of ILNumerics Community Edition.
3///
4///    ILNumerics Community Edition - high performance computing for applications.
5///    Copyright (C) 2006 - 2012 Haymo Kutschbach, http://ilnumerics.net
6///
7///    ILNumerics Community Edition is free software: you can redistribute it and/or modify
8///    it under the terms of the GNU General Public License version 3 as published by
9///    the Free Software Foundation.
10///
11///    ILNumerics Community Edition is distributed in the hope that it will be useful,
12///    but WITHOUT ANY WARRANTY; without even the implied warranty of
13///    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14///    GNU General Public License for more details.
15///
16///    You should have received a copy of the GNU General Public License
17///    along with ILNumerics Community Edition. See the file License.txt in the root
18///    of your distribution package. If not, see <http://www.gnu.org/licenses/>.
19///
20///    In addition this software uses the following components and/or licenses:
21///
22///    =================================================================================
23///    The Open Toolkit Library License
24///   
25///    Copyright (c) 2006 - 2009 the Open Toolkit library.
26///   
27///    Permission is hereby granted, free of charge, to any person obtaining a copy
28///    of this software and associated documentation files (the "Software"), to deal
29///    in the Software without restriction, including without limitation the rights to
30///    use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
31///    the Software, and to permit persons to whom the Software is furnished to do
32///    so, subject to the following conditions:
33///
34///    The above copyright notice and this permission notice shall be included in all
35///    copies or substantial portions of the Software.
36///
37///    =================================================================================
38///   
39
40using System;
41using System.Collections.Generic;
42using System.Collections;
43using System.Text;
44using ILNumerics.Drawing.Shapes;
45using ILNumerics.Drawing;
46using ILNumerics.Exceptions;
47using ILNumerics.Drawing.Misc;
48using System.Drawing;
49using ILNumerics.Drawing.Interfaces;
50using ILNumerics.Drawing.Labeling;
51using ILNumerics.Drawing.Plots;
52
53namespace ILNumerics.Drawing.Plots {
54    /// <summary>
55    /// simple updatable bar graph implementation, fixed number of bars, simple example implementation derived from ILSceneGraph
56    /// </summary>
57    public sealed class ILBarGraph2D : ILPlot, IILLegendRenderer, IILPanelConfigurator {
58
59        #region attributes
60        float m_barWidth;
61        ILQuad[] m_quads;
62        ILLabel m_legendSampleLabel;
63        ILLabel m_legendTextLabel;
64        int m_oldestBarIndex;
65        int m_oldestOpacity;
66        #endregion
67
68        #region properties
69        /// <summary>
70        /// Sets the opacity for the 'oldest' bar. May be used as 'Fading-Out' effect. (Default: 255)
71        /// </summary>
72        int OpacityOldest {
73            get { return m_oldestOpacity; }
74            set { m_oldestOpacity = value; }
75        }
76        /// <summary>
77        /// padding between graphs
78        /// </summary>
79        public float BarWidth {
80            get {
81                return m_barWidth;
82            }
83            set {
84                m_barWidth = value;
85                Invalidate();
86            }
87        }
88        /// <summary>
89        /// number of bars in the graph
90        /// </summary>
91        public override int Count {
92            get {
93                return m_quads.Length;
94            }
95        }
96        #endregion
97
98        #region constructors
99        /// <summary>
100        /// create new 2D bar graph
101        /// </summary>
102        /// <param name="panel">hosting panel</param>
103        /// <param name="data">numeric vector data, heights of bars</param>
104        public ILBarGraph2D(ILPanel panel, ILInArray<float> data)
105            : base(panel) {
106            using (ILScope.Enter(data)) {
107                // spacing between + width of bars
108                m_barWidth = 0.6f;
109                m_oldestOpacity = 255;
110                // create the bars
111                createQuads(data);
112                m_legendSampleLabel = new ILLabel(m_panel);
113                m_legendSampleLabel.Text = "Bars";
114
115                m_legendTextLabel = new ILLabel(m_panel);
116                m_legendTextLabel.Text = "3D";
117
118                m_oldestBarIndex = 0;
119            }
120        }
121        #endregion
122
123        #region public interface
124        /// <summary>
125        /// return reference to single bar (ILQuad) by index
126        /// </summary>
127        /// <param name="index">index of the bar</param>
128        /// <returns>the bar shape as ILQuad</returns>
129        public new ILQuad this [int index] {
130            get { return m_quads[index]; }
131        }
132        /// <summary>
133        /// add 'new' bar with new value and remove oldest bar
134        /// </summary>
135        /// <param name="value">height of new bar</param>
136        /// <returns>discarded value</returns>
137        public float Queue(float value) {
138            // we move all shapes by 1 left
139            ILPoint3Df offset = new ILPoint3Df(-1f,0,0);
140            for (int i = 0; i < m_quads.Length; i++) {
141                m_quads[i].Translate(offset);
142                // FADE OUt old barsss ....
143                m_quads[(i + m_oldestBarIndex) % (m_quads.Length - 1)].Opacity
144                    = (byte)(m_oldestOpacity + i * (255 - m_oldestOpacity) / m_quads.Length);
145                // tell the scene to update its size
146                m_quads[i].Invalidate();
147            }
148            // configure oldest graph
149            ILQuad newestQuad = m_quads[m_oldestBarIndex];
150            offset.X = m_quads.Length;
151            newestQuad.Translate(offset);
152            float ret = newestQuad.Vertices[2].YPosition;
153            newestQuad.Vertices[2].YPosition = value;
154            newestQuad.Vertices[3].YPosition = value;
155            newestQuad.Opacity = 255;
156
157            if (++m_oldestBarIndex >= m_quads.Length)
158                m_oldestBarIndex = 0;
159            return ret;       
160        }
161        #endregion
162
163        #region IILLegendRenderer Member
164        /* In order to show up in the legend, the plot needs to
165         * implement the IILLegendRenderer interface. Here this is
166         * done very simple via a ILShapeLabel. The first one simple draws
167         * the text "Bars" in the sample area of the legend. The original
168         * ILLabel of the graph is then used to draw its text into the
169         * label area of the legend.
170         * Better implementations should check for
171         * ** the drawing not to exceed the sampleArea and labelArea rectangles
172         * ** use 2 distinct labels instead of one (performance)
173         */
174        public void DrawToLegend(ILRenderProperties p, Rectangle sampleArea, Rectangle labelArea) {
175            m_legendSampleLabel.Position = sampleArea.Location;
176            m_legendSampleLabel.Draw(p);
177
178            m_legendTextLabel.Position = labelArea.Location;
179            m_legendTextLabel.Draw(p);
180        }
181        public Size LabelSize { get { return m_legendTextLabel.Size; } }
182        #endregion
183
184        #region private helper
185        protected void createQuads(ILInArray<float> data) {
186            using (ILScope.Enter(data)) {
187                if (data == null || data.Length == 0) {
188                    Clear();
189                    m_quads = new ILQuad[0];
190                } else {
191                    Clear();
192                    m_quads = new ILQuad[data.Length];
193                    ILColorEnumerator colors = new ILColorEnumerator();
194                    ILArray<float> fData = ILMath.check(data);
195
196                    for (int i = 0; i < m_quads.Length; i++) {
197                        m_quads[i] = new ILQuad(m_panel);
198                        m_quads[i].Border.Visible = true;
199                        m_quads[i].FillColor = colors.NextColor();
200                        ILPoint3Df pos = new ILPoint3Df();
201                        pos.X = i - m_barWidth / 2;
202                        pos.Y = 0;
203                        pos.Z = -0.5f;
204                        m_quads[i].Vertices[0].Position = pos;
205                        pos.X += m_barWidth;
206                        m_quads[i].Vertices[1].Position = pos;
207                        pos.Y += fData.GetValue(i);
208                        m_quads[i].Vertices[2].Position = pos;
209                        pos.X -= m_barWidth;
210                        m_quads[i].Vertices[3].Position = pos;
211                        // label the bar
212                        m_quads[i].Label.Text = i.ToString();
213                        m_quads[i].Label.Anchor = new PointF(.5f, -1);
214
215                        // bars will be transparent, oldest fading to OpacityOldest
216                        m_quads[i].Opacity = (byte)(m_oldestOpacity + i * (255 - m_oldestOpacity) / m_quads.Length);
217                        // add the bar to the scene graph node (base)
218                        Add(m_quads[i]);
219                    }
220                }
221            }
222        }
223        #endregion
224
225        #region IILPanelConfigurator Members
226
227        public void  ConfigurePanel(ILPanel panel) {
228            panel.Axes.XAxis.Visible = false;
229            panel.InteractiveMode = InteractiveModes.ZoomRectangle;
230            panel.Axes.YAxis.FarLines.Visible = false;
231            panel.PlotBoxScreenSizeMode = PlotBoxScreenSizeMode.Optimal;
232        }
233
234        #endregion
235    }
236}
Note: See TracBrowser for help on using the repository browser.