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 |
|
---|
40 | using System.Collections.Generic;
|
---|
41 | using System.Drawing;
|
---|
42 | using ILNumerics.Drawing.Interfaces;
|
---|
43 | using ILNumerics.Drawing.Misc;
|
---|
44 | using ILNumerics.Exceptions;
|
---|
45 |
|
---|
46 | namespace ILNumerics.Drawing.Plots {
|
---|
47 |
|
---|
48 | /// <summary>
|
---|
49 | /// Simple 3D Bar Graph - plot
|
---|
50 | /// </summary>
|
---|
51 | public sealed class ILBarGraph3D : ILPlot, IILPanelConfigurator {
|
---|
52 |
|
---|
53 | #region attributes
|
---|
54 | ILLitBox3D[,] m_boxes;
|
---|
55 | float m_barLengthX = 1.0f;
|
---|
56 | float m_barLengthY = 1.0f;
|
---|
57 | float m_paddingX = 0.3f;
|
---|
58 | float m_paddingY = 0.4f;
|
---|
59 | Color m_barColor = Color.FromArgb(170, 210, 210, 255);
|
---|
60 | Color m_barColorGradient = Color.FromArgb(170, 180, 180, 255);
|
---|
61 | #endregion
|
---|
62 |
|
---|
63 | /// <summary>
|
---|
64 | /// create a new 3D Bar Graph plot, provide data matrix
|
---|
65 | /// </summary>
|
---|
66 | /// <param name="panel">panel hosting the scene</param>
|
---|
67 | /// <param name="data">data matrix, at least 2x2 entries</param>
|
---|
68 | public ILBarGraph3D(ILPanel panel, ILInArray<float> data)
|
---|
69 | : base(panel) {
|
---|
70 | using (ILScope.Enter(data)) {
|
---|
71 | if (data == null)
|
---|
72 | throw new ILArgumentException("data argument must not be null!");
|
---|
73 | create(data, Colormaps.ILNumerics);
|
---|
74 | }
|
---|
75 | }
|
---|
76 | /// <summary>
|
---|
77 | /// get/set opacity of the bars
|
---|
78 | /// </summary>
|
---|
79 | public byte Opacity {
|
---|
80 | set {
|
---|
81 | foreach (ILLitBox3D box in m_boxes) {
|
---|
82 | box.Opacity = value;
|
---|
83 | }
|
---|
84 | }
|
---|
85 | }
|
---|
86 | /// <summary>
|
---|
87 | /// individual access to each bar (ILLitBox)
|
---|
88 | /// </summary>
|
---|
89 | /// <param name="row">row index, 0 based</param>
|
---|
90 | /// <param name="col">column index, 0 based</param>
|
---|
91 | /// <returns>lit box shape at specified position</returns>
|
---|
92 | public ILLitBox3D this[int row, int col] {
|
---|
93 | get {
|
---|
94 | return m_boxes[row,col];
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | #region public interface
|
---|
99 | /// <summary>
|
---|
100 | /// set axes labels and tick labels at once
|
---|
101 | /// </summary>
|
---|
102 | /// <param name="panel">panel hosting the plot</param>
|
---|
103 | /// <param name="xlabel">label text for x axis</param>
|
---|
104 | /// <param name="ylabel">label text for y axis</param>
|
---|
105 | /// <param name="zlabel">label text for z axis</param>
|
---|
106 | /// <param name="xtickLabels">collection of strings for columns labels (x-direction)</param>
|
---|
107 | /// <param name="ytickLabels">collection of strings for row labels (x-direction)</param>
|
---|
108 | public void SetLabels(ILPanel panel
|
---|
109 | , string xlabel, string ylabel, string zlabel
|
---|
110 | , ICollection<string> xtickLabels
|
---|
111 | , ICollection<string> ytickLabels
|
---|
112 | ) {
|
---|
113 | panel.Axes[0].LabeledTicks.Clear();
|
---|
114 | int counter = 0;
|
---|
115 | if (xtickLabels != null)
|
---|
116 | foreach (string s in xtickLabels) {
|
---|
117 | panel.Axes[0].LabeledTicks.Add(m_boxes[0, counter].Center.X, s);
|
---|
118 | counter++;
|
---|
119 | }
|
---|
120 | panel.Axes[1].LabeledTicks.Clear();
|
---|
121 | counter = 0;
|
---|
122 | if (ytickLabels != null)
|
---|
123 | foreach (string s in ytickLabels) {
|
---|
124 | panel.Axes[1].LabeledTicks.Add(m_boxes[counter, 0].Center.Y, s);
|
---|
125 | counter++;
|
---|
126 | }
|
---|
127 | panel.Axes[0].Label.Text = xlabel;
|
---|
128 | panel.Axes[1].Label.Text = ylabel;
|
---|
129 | panel.Axes[2].Label.Text = zlabel;
|
---|
130 |
|
---|
131 | }
|
---|
132 |
|
---|
133 | #endregion
|
---|
134 |
|
---|
135 | #region private helper
|
---|
136 | private void create(ILInArray<float> data, Colormaps colormap) {
|
---|
137 | using (ILScope.Enter(data)) {
|
---|
138 | ILArray<float> dataF = ILMath.check(data);
|
---|
139 | m_boxes = new ILLitBox3D[data.Size[0], data.Size[1]];
|
---|
140 | float maxY = data.Size[0] * (m_barLengthY + m_paddingY);
|
---|
141 | // prepare coloring for top quads
|
---|
142 | ILColormap cmap = new ILColormap(colormap);
|
---|
143 | float minV, maxV, mult;
|
---|
144 | dataF.GetLimits(out minV, out maxV);
|
---|
145 | if (maxV > minV) {
|
---|
146 | mult = (cmap.Length - 1) / (maxV - minV);
|
---|
147 | } else {
|
---|
148 | minV = 0;
|
---|
149 | mult = 0;
|
---|
150 | }
|
---|
151 | for (int r = 0; r < data.Size[0]; r++) {
|
---|
152 | for (int c = 0; c < data.Size[1]; c++) {
|
---|
153 | float val = dataF.GetValue(r, c);
|
---|
154 | ILPoint3Df max = new ILPoint3Df(
|
---|
155 | (float)(c * (m_paddingX + m_barLengthX) + m_barLengthX)
|
---|
156 | , (float)(maxY - r * (m_paddingY + m_barLengthY))
|
---|
157 | , val);
|
---|
158 | ILPoint3Df min = new ILPoint3Df(
|
---|
159 | max.X - m_barLengthX
|
---|
160 | , max.Y - m_barLengthY
|
---|
161 | , 0);
|
---|
162 | Color topColor = cmap.Map((double)(val - minV) * mult);
|
---|
163 | ILLitBox3D box = new ILLitBox3D(m_panel, min, max, m_barColor, topColor);
|
---|
164 | box.GradientColor = m_barColorGradient;
|
---|
165 | box.TopLabel.Color = topColor;
|
---|
166 | box.TopLabel.Text = "";
|
---|
167 | m_boxes[r, c] = box;
|
---|
168 | Add(box);
|
---|
169 | }
|
---|
170 | }
|
---|
171 | }
|
---|
172 | }
|
---|
173 | #endregion
|
---|
174 |
|
---|
175 |
|
---|
176 | #region IILPanelConfigurator Members
|
---|
177 | /// <summary>
|
---|
178 | /// configure default view of panel, called when adding the plot to the panel
|
---|
179 | /// </summary>
|
---|
180 | /// <param name="panel">panel hosting the plot</param>
|
---|
181 | public void ConfigurePanel(ILPanel panel) {
|
---|
182 | panel.BackgroundFilled = false;
|
---|
183 | panel.ClipViewData = false;
|
---|
184 | panel.Axes.LinesVisible = false;
|
---|
185 | panel.Projection = Projection.Perspective;
|
---|
186 | panel.DefaultView.SetDeg(-35, 55, 250);
|
---|
187 |
|
---|
188 | // configure light
|
---|
189 | panel.Lights[0].Enabled = true;
|
---|
190 | panel.Lights[0].Position = new ILPoint3Df(0, 0, 250);
|
---|
191 |
|
---|
192 | panel.BackgroundFilled = false;
|
---|
193 | panel.BackColor = Color.White;
|
---|
194 | panel.Axes.GridVisible = false;
|
---|
195 | panel.Axes.LinesVisible = false;
|
---|
196 | // configure X axis
|
---|
197 | panel.Axes[0].LabeledTicks.Mode = TickMode.Manual;
|
---|
198 | panel.Axes[0].LabeledTicks.Color = Color.Black;
|
---|
199 | panel.Axes[0].LabeledTicks.Clear();
|
---|
200 |
|
---|
201 | panel.Axes[1].NearLines.Visible = false;
|
---|
202 | panel.Axes[1].FarLines.Visible = false;
|
---|
203 | panel.Axes[1].Label.Color = Color.Black;
|
---|
204 |
|
---|
205 | panel.Axes[2].NearLines.Visible = false;
|
---|
206 | panel.Axes[2].FarLines.Visible = false;
|
---|
207 | panel.Axes[2].Grid.Visible = true;
|
---|
208 | panel.Axes[2].Grid.Color = Color.DarkGray;
|
---|
209 | panel.Axes[2].Grid.Antialiasing = true;
|
---|
210 |
|
---|
211 | panel.Axes[2].Label.Color = Color.Black;
|
---|
212 |
|
---|
213 | panel.Axes[2].LabeledTicks.Color = Color.Black;
|
---|
214 | panel.Axes[0].NearLines.Visible = false;
|
---|
215 | panel.Axes[0].FarLines.Visible = false;
|
---|
216 | panel.Axes[0].Label.Color = Color.Black;
|
---|
217 |
|
---|
218 | // configure Y axis
|
---|
219 | panel.Axes[1].LabeledTicks.Mode = TickMode.Manual;
|
---|
220 | panel.Axes[1].LabeledTicks.Color = Color.Black;
|
---|
221 | }
|
---|
222 |
|
---|
223 | #endregion
|
---|
224 | }
|
---|
225 | }
|
---|