C# WPF图表控件之ChartControl用法指南①

article/2025/11/5 16:12:15

 引言部分,总领全篇文章的中心内容。

   WPF的DevExpress ChartControl是一种功能强大的可视化工具,可帮助您将数据显示为二维或伪三维条形图、区域、线和许多其他形式。

01

将数据绑定到Chart Series

Step 1. 创建新项目并添加图表

  • 创建一个新的WPF应用程序项目。将其命名为第1课BindCharttoData。

  • 将ChartControl组件从DX.21.2:数据和分析工具箱部分拖动到主窗口。

    d417789978920d4925550bdeff24898a.png

  • 右键单击图表控件并在关联菜单中选择Layout | Reset All 以使图表填充整个窗口。

    d7b6c3341551f75fa8b593565687818c.png

新创建的图表包含一个空白的并排条形图和一个图例。主窗口的标记应如下所示:

<Windowxmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:Lesson1BindChartToData"xmlns:dxc="http://schemas.devexpress.com/winfx/2008/xaml/charts" x:Class="Lesson1BindChartToData.MainWindow"mc:Ignorable="d"Title="MainWindow" Height="315" Width="560"><Grid><dxc:ChartControl><dxc:ChartControl.Legends><dxc:Legend/></dxc:ChartControl.Legends><dxc:XYDiagram2D><dxc:BarSideBySideSeries2D DisplayName="Series 1"/></dxc:XYDiagram2D></dxc:ChartControl></Grid>
</Window>

对以下库的引用将自动添加到项目中:

  • DevExpress.Data.v21.2

  • DevExpress.Xpf.Core.v21.2

  • DevExpress.Charts.v21.2.Core

  • DevExpress.Xpf.Charts.v21.2

  • DevExpress.Mvvm.v21.2

  • DevExpress.Xpf.Printing.v21.2

  • DevExpress.Printing.v21.2.Core

注意:

这些引用是从全局程序集缓存(GAC)中选择的。要在本地复制它们或在以后的产品安装中包含它们,请使用以下目录:

C:\ProgramFiles(x86)\DevExpress 21.2\Components\Bin\Framework\

Step 2. 准备数据模型

您可以将图表绑定到数据库、XML文件或运行时创建的数据。数据源应该实现IEnumerable, IListSource 或者他们的后代。有关如何用数据填充图表的更多信息,请参阅提供数据部分。在本主题中,您将图表绑定到ObservableCollection<T>.

使用DataPoint类实现开发数据模型:

using System.Collections.ObjectModel;
using System.Windows;namespace Lesson1BindChartToData {public class DataPoint {public string Argument { get; set; }public double Value { get; set; }public static ObservableCollection<DataPoint> GetDataPoints() {return new ObservableCollection<DataPoint> {new DataPoint { Argument = "Asia", Value = 5.289D},new DataPoint { Argument = "Australia", Value = 2.2727D},new DataPoint { Argument = "Europe", Value = 3.7257D},new DataPoint { Argument = "North America", Value = 4.1825D},new DataPoint { Argument = "South America", Value = 2.1172D}};}}
}

Step 3. 添加ViemModel

使用以下代码实现MainWindowViewModel类:

using System.Collections.ObjectModel;
using System.Windows;namespace Lesson1BindChartToData {public class MainWindowViewModel {public ObservableCollection<DataPoint> Data { get; private set; }public MainWindowViewModel() {this.Data = DataPoint.GetDataPoints();}}
}

Step 4. 指定Data Context

d3cfee96bcf28a05f08b16e7a2c67197.png

Step 5. 绑定数据给图表

单击图表控件的智能标记。指定ChartControl.DataSource属性,如下图所示:

a3da1ef75ed82f08257e7726f8617492.png

Step 6. 用数据填充序列

指定应为系列点参数和值提供值的数据源字段。

将序列的series.ArgumentDataMember属性设置为参数。

7764609acffdc7d10685e8c92297661c.png

将序列的series.ValueDataMember属性设置为Value。

88da0e3d51978cfc8b3b80103d34c885.png

Step 7. 自定义图表

  • 指定序列名称

将Series.DisplayName属性设置为年度统计信息。显示名称标识图例中的系列。

0216ee3589eaca96a2c340eb3e4e9675.png

  • 添加图表标题并自定义其位置

单击图表控件标题属性的省略号按钮以调用标题集合编辑器。使用“添加”按钮创建新标题并将其添加到图表中。

将TitleBase.HorizontalAlignment属性设置为“中心”。

定义标题库。按地区销售的内容。单击“确定”。

6f0accc980e3dfba1af09dc0952d3f43.png

  • 配置十字光标的选项

要自定义十字线选项,请单击ChartControl.CrosshairOptions属性的“新建”按钮以创建十字线选项实例。

启用以下属性:

  1. CrosshairOptions.ShowArgumentLabels

  2. CrosshairOptions.ShowValueLabels

  3. CrosshairOptionBase.ShowValueLine

213846e5ce37b551a9c11295e82499c7.png

将XYSeries2D.Crosshair LabelPattern设置为$V:f2}M。

082ea397528b174cdf69f93ee2550c27.png

02


Results

运行项目以查看结果。

4ec4f0d1953f99da35bbffa2227dc96a.png

生成的代码如下所示:

<Windowxmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:Lesson1BindChartToData" xmlns:dxc="http://schemas.devexpress.com/winfx/2008/xaml/charts"xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core" x:Class="Lesson1BindChartToData.MainWindow"mc:Ignorable="d" Title="MainWindow" Height="400" Width="650"><Window.DataContext><local:MainWindowViewModel/></Window.DataContext><Grid><dxc:ChartControl DataSource="{Binding Data}"><dxc:ChartControl.CrosshairOptions><dxc:CrosshairOptions ShowArgumentLabels="True" ShowValueLabels="True" ShowValueLine="True"/></dxc:ChartControl.CrosshairOptions><dxc:ChartControl.Titles><dxc:Title Content="Sales by Regions" HorizontalAlignment="Center"/></dxc:ChartControl.Titles><dxc:ChartControl.Legends><dxc:Legend/></dxc:ChartControl.Legends><dxc:XYDiagram2D><dxc:BarSideBySideSeries2D DisplayName="Annual Statistics" ArgumentDataMember="Argument" ValueDataMember="Value" CrosshairLabelPattern="${V:f2}M"/></dxc:XYDiagram2D></dxc:ChartControl></Grid>
</Window>

C# CODE

using System.Collections.ObjectModel;
using System.Windows;
namespace Lesson1BindChartToData {/// <summary>/// Interaction logic for MainWindow.xaml/// </summary>public partial class MainWindow : Window {public MainWindow() {InitializeComponent();}}public class MainWindowViewModel {public ObservableCollection<DataPoint> Data { get; private set; }public MainWindowViewModel() {this.Data = DataPoint.GetDataPoints();}}public class DataPoint {public string Argument { get; set; }public double Value { get; set; }public static ObservableCollection<DataPoint> GetDataPoints() {return new ObservableCollection<DataPoint> {new DataPoint { Argument = "Asia", Value = 5.289D},new DataPoint { Argument = "Australia", Value = 2.2727D},new DataPoint { Argument = "Europe", Value = 3.7257D},new DataPoint { Argument = "North America", Value = 4.1825D},new DataPoint { Argument = "South America", Value = 2.1172D}};}}
}

原文链接:https://docs.devexpress.com/WPF/9757/controls-and-libraries/charts-suite/chart-control/getting-started/lesson-1-bind-chart-series-to-data#results

翻译小编:mm1552923

公众号:dotNet编程大全


http://chatgpt.dhexx.cn/article/0PheVAZ7.shtml

相关文章

chartControl控件常用属性总结

chartControl可以绘制常见的柱状图&#xff0c;折线图&#xff0c;饼图&#xff0c;在windows form 窗体应用程序中可以很方便的使用。下面总结一下如何使用chartControl控件绘图&#xff0c;以及一些常用的属性。 1.添加series DevExpress.XtraCharts.Series _serTimechartCo…

在C#中使用DevExpress中的ChartControl实现极坐标图

在C#中使用DevExpress中的ChartControl实现极坐标图 背景实现思路参考代码 背景 在工控软件的开发中很多业务场景就是使用图表控件展示设备和工艺参数。如下图案例&#xff1a; 实现思路 通常简单的做法是使用图表控件实现&#xff0c;常用的图表控件有开源的ZedGraph&…

DevExpress ChartControl ToolTipPointPattern和ToolTipSeriesPattern

原本只是想改一下鼠标放到曲线上的tip显示的小数点位数 然后就发现他这个属性还挺多&#xff0c;多到有点看不懂 然后就写了小demo测试 demo代码 // Create a series and add points to it. Series series1 new Series("Series 1", ViewType.Line);series1.Points…

Dev中ChartControl添加限定线

1.单击Y轴,设置属性 2.点击ConstantLines属性,打开"Constant Line Collection Editor"界面 3.点击Add添加线条 4.通过设置Appearance中的Color属性,设置显示颜色; 设置Behavior中的AxisValue,设置线段出现的位置 设置Misc中的Name,设置显示文本

chartControl生成时间轴动态曲线

首先将dev的chartControl拖到窗体中并设置父窗体停靠&#xff0c;然后手动添加一个seriies,如下图 初始化chartxiao using DevExpress.XtraEditors; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing…

DevExpress ChartControl 折线图简单使用

c# DevExpress ChartControl 折线图简单使用 DevExpress ChartControl折线图简单使用 DevExpress ChartControl折线图简单使用 1、界面放一个panel控件 2、定义一个DataTable 存储数据 3、获取数据后放在DataTable DataTable 定义&#xff1a; DataTable res_data new DataTa…

DevExpress之ChartControl用法

DevExpress中的ChartControl顾名思义就是数据基于图表展示&#xff0c;其关键在于Series上的处理。 using System; using System.Drawing; using DevExpress.XtraCharts;namespace DevExpressUtilHelpV3 { public static class ChartToolV3 { /// <summary> /// 创建Seri…

ChartControl控件绘制折线图

新建DevExpress窗体 SQLServer数据库中的数据如下&#xff1a; 拖入XtraTabControl控件&#xff0c;并修改Text属性 分别拖入GridControl控件和ChartControl控件 在GridControl控件中点击Run Designer,添加三列数据并分别设置FieldName(与数据库中对应) 在chartcontrol控件中…

DevExpress chartControl 数据绑定

DevExpress chartControl 数据绑定 chartControl 数据绑定ChartControl直接绑定Series 绑定例程附件 chartControl 数据绑定 这里介绍两种绑定方式ChartControl直接绑定以及ChartControl里的series绑定 ChartControl直接绑定 通过chartControl的DataSource属性直接bingdings…

DevExpress ChartControl 实现多轴

先看成图 如果是您需要的效果&#xff0c;请往后看&#xff1a; //曲线 Color[] Colorlist new Color[7] { Color.FromArgb(255, 79, 129, 189), Color.FromArgb(255, 192, 80, 77), Color.FromArgb(255, 155, 187, 89), Color.FromArgb(255, 128, 100, 162), Col…

chartControl

关键代码&#xff1a; /// <summary>/// 设置X轴Title/// </summary>/// <param name"chart">ChartControl</param>/// <param name"titleText">Title文字</param> /// <param name"titleColor">Titl…

Dev ChartControl

1、ChartControl 绘制曲线图&#xff0c;横坐标表示距离起点距离&#xff0c;纵坐标表示高程&#xff0c;均为double类型值&#xff0c;定义一个数据源的类。 /// <summary>/// 画图控件数据源/// </summary>public class ChartDatasource{/// <summary>/// …

ChartControl控件

ChartControl控件 ChartControl控件是一个可视化的图表控件&#xff0c;它支持你能想到的所有图形。用于对统计数据的可视化显示。最近我摸索了PivotGridControl控件与ChartControl控件的配合使用。本文简单的描述一下ChartControl控件。 常用的图表类型 通过代码设置图形 //条…

DevExpress中使用ChartControl绘制折线图和导出图表为Excel文件

一、实现效果 ①手动创建线性图表(添加图表标题) ②绘制单条线性图(可实现设置X和Y轴名称、绑定数据、缩放、复选框勾选是否显示、查看指定点信息) ③绘制多条线性图可实现设置X和Y轴名称、绑定数据、缩放、复选框勾选是否显示、查看指定点信息) ④导出图表为Excel 二、…

DEV控件之ChartControl用法

一、总体概述 这个控件包含3层&#xff0c;最外面的chartControl层、中间的XYDiagram层、最里面的Series层。功能非常强大&#xff0c;但同时使用起来也相对复杂&#xff0c;需要各个层之间相互协调设置才能达到自己想要的效果。 二、chartControl层 像DEV的其它控件一样&#…

C# DevExpress组件 - ChartControl图表控件

C# DevExpress组件 - ChartControl图表控件学习-整体感知&#xff08;一&#xff09; 1 逻辑框架图梳理 以逻辑框架图&#xff0c;进行代码验证 2 代码实现 2.1 实现内容 代码实现&#xff1a;添加一个ChartControl图表控件&#xff0c;并提供显示三个变量显示在三个位置 …

C# DevExpress ChartControl用法总结

C# DevExpress ChartControl用法总结 ₯近期使用C#DevExpress制作看板时用到过的ChartControl中的相关图表&#xff0c;在此做个记录&#xff08;未完&#xff09;。 1、Bar&#xff08;柱形图&#xff09; Series series1 new Series("日期", ViewType.Bar) …

DevExpress chartControl 基本结构说明

chartControl 使用教程及chart数据绑定 DevExpress说明ChartControl 结构series 主要Diagram 主要Chart TitlesLegendsAnnotations DevExpress说明 DevExpress是一个功能强大的跨平台控件库&#xff0c;支持winform、VB、WPF、UWP、asp等等&#xff0c; chart图是软件开发中常…

Dev中ChartControl——属性熟悉与简单应用

图表元素之间的关系&#xff1a; 根据DevExpress帮助文档中描述&#xff1a; 创建点图&#xff1a; 1、创建图表 /// <summary> /// 创建图表 /// </summary> private void CreatChart() {// Create a new chart.图表控件ChartControl pointChart new ChartCon…

运用getParameterNames()方法和getParameterValues()方法获取请求参数名称和内容

一.例子代码&#xff1a; 填写信息页面&#xff1a; <!DOCTYPE html> <html> <head> <meta charset"UTF-8"> <title>Insert title here</title> </head> <body> <form action"request_demo04.jsp" m…