在C#项目里做数据可视化,LiveCharts和OxyPlot是两种主流选择。LiveCharts上手快,OxyPlot可控性强。下面分别用它们实现柱状图、折线图和饼图。

一、环境准备
使用NuGet安装对应包:
- LiveCharts:安装
LiveCharts.Wpf - OxyPlot:安装
OxyPlot.Wpf
二、用LiveCharts绘制三种图表
1. 柱状图
在WPF中引用命名空间后,可用如下代码生成柱状图:
using LiveCharts;
using LiveCharts.Wpf;
// 创建柱状系列
var barSeries = new ColumnSeries
{
Title = "销量",
Values = new ChartValues<double> { 12, 19, 8, 15 }
};
var barChart = new CartesianChart
{
Series = new SeriesCollection { barSeries },
AxisX = new AxesCollection { new Axis { Title = "月份", Labels = new[] { "1月", "2月", "3月", "4月" } } },
AxisY = new AxesCollection { new Axis { Title = "数值" } }
};
2. 折线图
折线图只需把系列换成 LineSeries:
using LiveCharts;
using LiveCharts.Wpf;
var lineSeries = new LineSeries
{
Title = "温度",
Values = new ChartValues<double> { 22.1, 24.3, 21.8, 26.0 }
};
var lineChart = new CartesianChart
{
Series = new SeriesCollection { lineSeries },
AxisX = new AxesCollection { new Axis { Labels = new[] { "周一", "周二", "周三", "周四" } } }
};
3. 饼图
using LiveCharts;
using LiveCharts.Wpf;
var pieChart = new PieChart
{
Series = new SeriesCollection
{
new PieSeries { Title = "A", Values = new ChartValues<double> { 40 }, DataLabels = true },
new PieSeries { Title = "B", Values = new ChartValues<double> { 60 }, DataLabels = true }
}
};
三、用OxyPlot绘制三种图表
1. 柱状图
using OxyPlot;
using OxyPlot.Series;
using OxyPlot.Axes;
var barModel = new PlotModel { Title = "销量柱状图" };
barModel.Axes.Add(new CategoryAxis { ItemsSource = new[] { "1月", "2月", "3月" } });
barModel.Axes.Add(new LinearAxis { Title = "数值" });
barModel.Series.Add(new ColumnSeries
{
ItemsSource = new[] { 10.0, 20.0, 15.0 }
});
2. 折线图
using OxyPlot;
using OxyPlot.Series;
var lineModel = new PlotModel { Title = "温度折线图" };
lineModel.Series.Add(new LineSeries
{
Points = new[]
{
new DataPoint(0, 22.1),
new DataPoint(1, 24.3),
new DataPoint(2, 21.8)
}
});
3. 饼图
using OxyPlot;
using OxyPlot.Series;
var pieModel = new PlotModel { Title = "占比" };
pieModel.Series.Add(new PieSeries
{
Slices = new[]
{
new PieSlice("A", 40) { IsExploded = true },
new PieSlice("B", 60)
}
});
四、如何选择
如果项目追求快速集成、界面现代,选LiveCharts;如果需要导出矢量图、深度定制样式,OxyPlot更合适。两者都支持数据绑定,实际开发中按团队习惯选用即可。
C#LiveChartsOxyPlot修改时间:2026-07-26 04:57:19