下载最新版FastCube.Net
创建Windows窗体项目,在“References”中,需要添加指向libraries(库)的链接:FastReport,FastReport.Bars,FastReport.Olap。 从工具栏中将以下控件添加到窗体: dataSource,dtDataSet,cube,slice,sliceGrid,button。在dataSource1属性中,找到DataSet并为其选择可用的dtDataSet1。在cube1控件的属性中,为DataSource选择值dataSource1。对于SoutceType属性,值为DataSource;在slice1属性中,选择Cube属性的cube1值。调整sliceGrid1的大小,在其属性中,选择Slice的slice1值。
现在转到Form(表单)代码,首先,让我们将libraries(库)添加到Using部分:
using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Windows.Forms; using FastReport.Olap.Slice;将以下变量和对象添加到类中:
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; //symbol set private Random rnd = new Random(); //randomizer private DataTable dataTable = null; //data table object private int Dims = 4; //dimensions count private int Uniques = 500; //count of unique random values private int Measures = 4; //measures count private int Records = 10000; //table records count用假数据填充表格,只是为了证明软件数据生成的可能性。除DataTable外,还可以使用数据库或流来检索数据。
创建一个DataTable并用随机数据填充它:
// The method of a random string generation private string GetNewStr(int len) { len = 1 + rnd.Next(len - 1); char[] resChars = new char[len]; for (int i = 0; i < resChars.Length; i++) { resChars[i] = chars[rnd.Next(chars.Length)]; } return new string(resChars); } // The method of adding fields to a table private DataTable CreateDS() { DataTable dt = new DataTable("Test"); for (int i = 0; i < Dims; i++) dt.Columns.Add("Dimension" + (i + 1), typeof(string)); for (int i = 0; i < Measures; i++) dt.Columns.Add("Measure" + (i + 1), typeof(int)); return dt; } // Method of filling the table with data private DataTable BuildDS() { DataTable dt = CreateDS(); int maxUniques = Uniques; SortedSet<string> randomStrings = new SortedSet<string>(); // Generating Unique Values while (randomStrings.Count < maxUniques) { randomStrings.Add(GetNewStr(10)); } int dimCount = Dims; int mesCount = Measures; object[] values = new object[dimCount + mesCount]; // Filling a table with data for (int i = 0; i < Records; i++) { for (int j = 0; j < dimCount; j++) { if (i < maxUniques) values[j] = randomStrings.ElementAt(i); else values[j] = randomStrings.ElementAt(rnd.Next(dimCount)); } for (int j = 0; j < mesCount; j++) { values[j + dimCount] = rnd.Next(255); } dt.Rows.Add(values); } return dt; }现在用一个数据源,用户可以使用数据库中的表或视图或数据流,向切片添加字段。
为表单上的唯一按钮创建OnClick事件处理程序,并编写以下代码:
private void button1_Click(object sender, EventArgs e) { // Add the generated table with the data in the DataSet cube1.Close(); DataTable dt = BuildDS(); if (dataTable != null) { dataTable.Dispose(); dataTable = null; } dataTable = dt; dtDataSet1.DataTable = dataTable; dataSource1.DeleteFields(); cube1.Open(); dtDataSet1.DataTable = null; //Update slice slice1.BeginUpdate(); //Add fields into the containers for (int i = 0; i < Dims; i++) { slice1.YAxisContainer.AddSliceField(slice1.SliceFields.GetFieldByIndex(i)); } //Add empty measures container onto X-axis slice1.XAxisContainer.AddMeasuresField(); //Add measures into the measures container for (int i = 0; i < Measures; i++) { slice1.MeasuresContainer.AddMeasure(new MeasureField( slice1, FastReport.Olap.Types.AggregateFunction.Sum, slice1.SliceFields.GetFieldByIndex(i + Dims), null, null, "Measure" + (i + 1), "Measure" + (i + 1), false )); } slice1.EndUpdate(); //Save cube into a file cube1.Save("J:/Program Files (x86)/FastReports/FastCube.Net Professional/Demos/C#/test.mdc"); }尺寸和度量存储在单独的容器中。首先,用尺寸填充YAxisContainer,在XAxisContainer中添加了一个空的度量列表。然后才开始用措施填补它。MeasureField类的构造函数的signature:public MeasureField(Slice slice,AggregateFunction aggregateFunction,SliceField baseSliceField,SliceField distinctSliceField,SliceField extraSliceField,string name,string caption,bool distinct);在代码的最后一行,将多维数据集存储在指定的文件中。
现在启动应用程序并使用按钮创建一个新的多维数据集:
本站文章除注明转载外,均为本站原创或翻译
欢迎任何形式的转载,但请务必注明出处,尊重他人劳动成果
转载请注明:文章转载自:FastReport控件中文网 [https://www.fastreportcn.com/]
本文地址:https://www.fastreportcn.com/post/2143.html
欢迎任何形式的转载,但请务必注明出处,尊重他人劳动成果
转载请注明:文章转载自:FastReport控件中文网 [https://www.fastreportcn.com/]
本文地址:https://www.fastreportcn.com/post/2143.html