共计 5079 个字符,预计需要花费 13 分钟才能阅读完成。
点击获取工具 >>
本教程将领导您实现依据根底数据源主动创立 3D 系列所需的步骤。
应该执行以下步骤,本文咱们将为大家介绍 3 个步骤及最初后果,更多残缺内容欢送继续关注!
- Step 1. 编写一个应用程序
- Step 2. 增加图表并将其绑定到数据
- Step 3. 自定义图表外观
- 后果
Step 1. 编写一个应用程序
在此步骤中,您将数据文件增加到我的项目中并设计 Model 和 ViewModel 类。
- 运行 Microsoft Visual Studio 2012, 2013, 2015, 2017 或 2019
- 创立一个新的 WPF Application 我的项目
- 将新文件夹增加到解决方案,将其命名为 Data。
- 将数据文件增加到新创建的文件夹中,因而请右键单击该文件夹,而后抉择 Add | Existing Item… 元素。
- 在 invoked 窗口中,浏览默认状况下存储在 C:UsersPublicDocumentsDevExpress Demos 20.2ComponentsWPFCSChartsDemo.WpfData 目录中的 IrisDataSet.xml 文件,单击 Add 来将文件夹引入到我的项目中。
- 而后,您须要设计一个新的模型类,因而将一个新的代码文件增加到我的项目中。右键单击该我的项目,而后抉择 Add | New Item。
- 在 invoked 窗口中,抉择 Code 组,而后在我的项目列表中抉择 Code File 项,将项目名称设置为 Iris.cs,而后单击 Add。
在 invoked 窗口中,抉择 Code 组,而后在我的项目列表中抉择 Code File 项,将项目名称设置为 Iris.cs,而后单击 Add。
C#
`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;
}
}
}`
VB.NET
`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`
- 在下一步中,开发一个视图模型,该模型将基于模型从数据文件中加载数据对象。为此请像以前一样创立一个代码文件,并将文件名设置为 IrisesViewModel。
将上面的代码放入此空白文件。
C#
`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;
}
}
}`
VB.NET
`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`
- 构建解决方案。
- 而后,将 ViewModel 调配给 Window.DataContext 属性。为此请在 design view 中抉择 window,而后在 Properties 窗口中找到 DataContext 属性,而后单击该属性对面的 New 按钮,在调用对话框中,抉择 Chart3D_Lesson3.IrisesViewModel 类,而后单击 OK。
筹备阶段实现。咱们将在下篇文章中为大家介绍如何将图表绑定到数据源。