本教程将指导您完成基于底层数据源自动创建3D系列所需的步骤。
- 步骤1.准备申请表
- 步骤2.添加图表并将其绑定到数据
- 步骤3.自定义图表外观
- 结果
扩展步骤1.准备应用程序
在此步骤中,您将向项目添加数据文件并设计Model和ViewModel类。
- 运行Microsoft Visual Studio 2012,2013,2015或2017。
- 创建一个新的WPF应用程序项目。
- 向解决方案添加新文件夹,将其命名为Data。
- 将数据文件添加到新创建的文件夹中。 为此,请右键单击此文件夹,然后选择 Add | Existing Item... 元素。
- 在调用的窗口中,默认情况下浏览存储在C:\ Users \ Public \ Documents \ DevExpress Demos 18.2 \ Components \ WPF \ CS \ ChartsDemo.Wpf \ Data目录中的IrisDataSet.xml文件。 单击“Add”将文件包含到项目中。
- 然后,您需要设计一个新的模型类。 为此,请将新代码文件添加到项目中。 右键单击该项目,然后选择“Add” 新物品。
- 在调用的窗口中,选择“Code”组,然后在项目列表中选择“Code File”项。 然后,将项目名称设置为Iris.cs,然后单击“Add”。
- 通过将以下代码放在先前创建的文件中来实现数据模型演示。
namespace Chart3D_Lesson3 { public class IrisData { public string Species { get; private set; } public double SepalWidth { get; private set; } public double SepalLength { get; private set; } public double PetalWidth { get; private set; } public double PetalLength { get; private set; } public IrisData( string species, double sepalWidth, double sepalLength, double petalWidth, double petalLength ) { Species = species; SepalWidth = sepalWidth; SepalLength = sepalLength; PetalWidth = petalWidth; PetalLength = petalLength; } } }
Namespace Chart3D_Lesson3 Public Class IrisData Private privateSpecies As String Public Property Species() As String Get Return privateSpecies End Get Private Set(ByVal value As String) privateSpecies = value End Set End Property Private privateSepalWidth As Double Public Property SepalWidth() As Double Get Return privateSepalWidth End Get Private Set(ByVal value As Double) privateSepalWidth = value End Set End Property Private privateSepalLength As Double Public Property SepalLength() As Double Get Return privateSepalLength End Get Private Set(ByVal value As Double) privateSepalLength = value End Set End Property Private privatePetalWidth As Double Public Property PetalWidth() As Double Get Return privatePetalWidth End Get Private Set(ByVal value As Double) privatePetalWidth = value End Set End Property Private privatePetalLength As Double Public Property PetalLength() As Double Get Return privatePetalLength End Get Private Set(ByVal value As Double) privatePetalLength = value End Set End Property Public Sub New(ByVal species As String, ByVal sepalWidth As Double, ByVal sepalLength As Double, ByVal petalWidth As Double, ByVal petalLength As Double) Me.Species = species Me.SepalWidth = sepalWidth Me.SepalLength = sepalLength Me.PetalWidth = petalWidth Me.PetalLength = petalLength End Sub End Class End Namespace
using System; using System.Collections.ObjectModel; using System.Windows; using System.Windows.Resources; using System.Xml.Linq; namespace Chart3D_Lesson3 { public class IrisesViewModel { public ObservableCollection<IrisData> Irises { get; set; } public IrisesViewModel() { this.Irises = DataLoader.GetIrises("/Data/IrisDataSet.xml"); } } static class DataLoader { public static ObservableCollection<IrisData> GetIrises(string filepath) { ObservableCollection<IrisData> irisDataSet = new ObservableCollection<IrisData>(); Uri uri = new Uri(filepath, UriKind.RelativeOrAbsolute); StreamResourceInfo info = Application.GetResourceStream(uri); XDocument document = XDocument.Load(info.Stream); if (document == null) return irisDataSet; foreach (XElement element in document.Element("IrisDataSet").Elements("IrisData")) { double sepalLength = Convert.ToDouble(element.Element("SepalLength").Value); double sepalWidth = Convert.ToDouble(element.Element("SepalWidth").Value); double petalLength = Convert.ToDouble(element.Element("PetalLength").Value); double petalWidth = Convert.ToDouble(element.Element("PetalWidth").Value); string species = element.Element("Species").Value.ToString(); irisDataSet.Add(new IrisData(species, sepalWidth, sepalLength, petalWidth, petalLength)); } return irisDataSet; } } }
Imports System Imports System.Collections.ObjectModel Imports System.Windows Imports System.Windows.Resources Imports System.Xml.Linq Namespace Chart3D_Lesson3 Public Class IrisesViewModel Public Property Irises() As ObservableCollection(Of IrisData) Public Sub New() Me.Irises = DataLoader.GetIrises("/Data/IrisDataSet.xml") End Sub End Class Friend NotInheritable Class DataLoader Private Sub New() End Sub Public Shared Function GetIrises(ByVal filepath As String) As ObservableCollection(Of IrisData) Dim irisDataSet As New ObservableCollection(Of IrisData)() Dim uri As New Uri(filepath, UriKind.RelativeOrAbsolute) Dim info As StreamResourceInfo = Application.GetResourceStream(uri) Dim document As XDocument = XDocument.Load(info.Stream) If document Is Nothing Then Return irisDataSet End If For Each element As XElement In document.Element("IrisDataSet").Elements("IrisData") Dim sepalLength As Double = Convert.ToDouble(element.Element("SepalLength").Value) Dim sepalWidth As Double = Convert.ToDouble(element.Element("SepalWidth").Value) Dim petalLength As Double = Convert.ToDouble(element.Element("PetalLength").Value) Dim petalWidth As Double = Convert.ToDouble(element.Element("PetalWidth").Value) Dim species As String = element.Element("Species").Value.ToString() irisDataSet.Add(New IrisData(species, sepalWidth, sepalLength, petalWidth, petalLength)) Next element Return irisDataSet End Function End Class End Namespace
筹备阶段已经完成。 转到下一步,了解如何将图表绑定到数据源。
扩展步骤2.添加图表并将其绑定到数据
在本节中,您将添加一个图表并将此图表绑定到准备好的数据源。 请按照以下步骤执行此操作。
- 将Chart3DControl组件添加到主窗口,就像在第1课(步骤1)中所做的那样。
- 然后,将默认系列3D存储更改为Series3DDataSourceAdapter对象。 为此,在Properties窗口中找到Chart3DControl.SeriesSourceProperties,并在下拉列表中选择Series3DDataSourceAdapter项。
- 在“Properties”窗口中找到Series3DDataSourceAdapter.DataSourceProperties,然后单击Properties标记。 选择“Create Data Binding...”项。
- 在调用的Create Data Binding窗口中,选择ViewModel的IrisesProperties,然后单击OK。
- 指定将定义用于为图表提供数据的数据源字段的数据成员,如下所示。
扩展步骤3.自定义图表外观
在此步骤中,您将学习如何配置外观图表设置。 为此,请按照以下步骤操作。
- 要修改所有生成的系列的外观,请使用系列模板。 要创建新模板,请在“Properties”窗口中找到Series3DDataSourceAdapter.SeriesTemplateProperties,然后单击“New”按钮。
- 然后,更改系列视图。 为此,请找到Series3DBase.ViewProperties,然后在下拉列表中选择Bubble3DSeriesView项。
- 修改系列点表单。 为此,请将Marker3DSeriesView.MarkerModelProperties指定给Marker3DSpherePointModel对象。
- 将Bubble3DSeriesView.MaxSizeProperties定义为0.5以设置最大气泡大小。 将Bubble3DSeriesView.MinSizeProperties定义为0.1以设置最小气泡大小。
结果
执行上述所有步骤后,您的代码将显示如下。
using System; using System.Collections.ObjectModel; using System.Windows; using System.Windows.Resources; using System.Xml.Linq; namespace Chart3D_Lesson3 { public class IrisesViewModel { public ObservableCollection<IrisData> Irises { get; set; } public IrisesViewModel() { this.Irises = DataLoader.GetIrises("/Data/IrisDataSet.xml"); } } static class DataLoader { public static ObservableCollection<IrisData> GetIrises(string filepath) { ObservableCollection<IrisData> irisDataSet = new ObservableCollection<IrisData>(); Uri uri = new Uri(filepath, UriKind.RelativeOrAbsolute); StreamResourceInfo info = Application.GetResourceStream(uri); XDocument document = XDocument.Load(info.Stream); if (document == null) return irisDataSet; foreach (XElement element in document.Element("IrisDataSet").Elements("IrisData")) { double sepalLength = Convert.ToDouble(element.Element("SepalLength").Value); double sepalWidth = Convert.ToDouble(element.Element("SepalWidth").Value); double petalLength = Convert.ToDouble(element.Element("PetalLength").Value); double petalWidth = Convert.ToDouble(element.Element("PetalWidth").Value); string species = element.Element("Species").Value.ToString(); irisDataSet.Add(new IrisData(species, sepalWidth, sepalLength, petalWidth, petalLength)); } return irisDataSet; } } }
Imports System Imports System.Collections.ObjectModel Imports System.Windows Imports System.Windows.Resources Imports System.Xml.Linq Namespace Chart3D_Lesson3 Public Class IrisesViewModel Public Property Irises() As ObservableCollection(Of IrisData) Public Sub New() Me.Irises = DataLoader.GetIrises("/Data/IrisDataSet.xml") End Sub End Class Friend NotInheritable Class DataLoader Private Sub New() End Sub Public Shared Function GetIrises(ByVal filepath As String) As ObservableCollection(Of IrisData) Dim irisDataSet As New ObservableCollection(Of IrisData)() Dim uri As New Uri(filepath, UriKind.RelativeOrAbsolute) Dim info As StreamResourceInfo = Application.GetResourceStream(uri) Dim document As XDocument = XDocument.Load(info.Stream) If document Is Nothing Then Return irisDataSet End If For Each element As XElement In document.Element("IrisDataSet").Elements("IrisData") Dim sepalLength As Double = Convert.ToDouble(element.Element("SepalLength").Value) Dim sepalWidth As Double = Convert.ToDouble(element.Element("SepalWidth").Value) Dim petalLength As Double = Convert.ToDouble(element.Element("PetalLength").Value) Dim petalWidth As Double = Convert.ToDouble(element.Element("PetalWidth").Value) Dim species As String = element.Element("Species").Value.ToString() irisDataSet.Add(New IrisData(species, sepalWidth, sepalLength, petalWidth, petalLength)) Next element Return irisDataSet End Function End Class End Namespace
运行项目以查看结果。 下图显示了应用程序运行时生成的结果图表。
买 DevExpress Universal Subscription 免费赠 万元汉化资源包1套!
限量15套!先到先得,送完即止!立即抢购>>
本站文章除注明转载外,均为本站原创或翻译
欢迎任何形式的转载,但请务必注明出处,尊重他人劳动成果
转载请注明:文章转载自:DevExpress控件中文网 [https://www.devexpresscn.com/]
本文地址:https://www.devexpresscn.com/post/1140.html
欢迎任何形式的转载,但请务必注明出处,尊重他人劳动成果
转载请注明:文章转载自:DevExpress控件中文网 [https://www.devexpresscn.com/]
本文地址:https://www.devexpresscn.com/post/1140.html