DevExpress技术交流群2:775869749 欢迎一起进群讨论 本教程将指导您完成根据基础数据源自动创建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:\Users\Public\Documents\DevExpress Demos 20.2\Components\WPF\CS\ChartsDemo.Wpf\Data目录中的IrisDataSet.xml文件,单击Add来将文件夹引入到项目中。
- 然后,您需要设计一个新的模型类,因此将一个新的代码文件添加到项目中。 右键单击该项目,然后选择Add | New Item。
- 在invoked窗口中,选择Code组,然后在项目列表中选择Code File项,将项目名称设置为Iris.cs,然后单击Add。
在invoked窗口中,选择Code组,然后在项目列表中选择Code File项,将项目名称设置为Iris.cs,然后单击Add。 C# [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 [Visual Basic .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# [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 [Visual Basic .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。
准备阶段完成。 我们将在下篇文章中为大家介绍如何将图表绑定到数据源。
|