You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用LiveCharts创建饼图异常求助:出现同心饼图而非双切片饼图

Fix: LiveCharts Pie Chart Showing Concentric Circles Instead of Multiple Slices

Ah, I see the issue here! Those concentric circles you're seeing happen when multiple PieSeries are added to the chart, where each series only has a single value (so each renders a full pie, stacked right on top of each other). Even though your code looks like it's adding one series with two values, it's likely that code is being executed twice (e.g., in both the constructor and a Loaded event, or accidentally duplicated), or there's a subtle mistake in how you're defining the series.

Let's fix this step by step:

1. Ensure You're Adding a Single Series with Multiple Values

A single PieSeries should hold all the values for your pie chart slices. Multiple PieSeries will create separate pie charts that overlap, resulting in that concentric effect.

Corrected Code-Behind

First, make sure your code runs only once, and clear existing series before adding to avoid accidental duplicates:

public YourWindow()
{
    InitializeComponent();
    
    // Clear any existing series to prevent duplicates
    myPieChart.Series.Clear();
    
    // Create ONE PieSeries with both values for your two slices
    var pieSeries = new LiveCharts.Wpf.PieSeries
    {
        Values = new LiveCharts.ChartValues<decimal> { 1, 3 },
        // Optional: Add labels to see the values clearly
        DataLabels = true
    };
    
    myPieChart.Series.Add(pieSeries);
}

2. Check for Duplicate Code Execution

Double-check if your series-adding code is being triggered more than once:

  • Did you paste the same code in both the window constructor and a Window_Loaded event handler?
  • Is the code inside a loop or a method that gets called multiple times?

Using myPieChart.Series.Clear() before adding the new series will fix this even if there's accidental duplication.

For a cleaner setup, bind the series collection to your view instead of directly manipulating the UI elements:

XAML

<Grid>
    <lvc:PieChart x:Name="myPieChart" 
                  Series="{Binding PieSeries}"
                  DataLabels="True"/>
</Grid>

Code-Behind (Implement INotifyPropertyChanged)

using LiveCharts;
using LiveCharts.Wpf;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;

public partial class YourWindow : Window, INotifyPropertyChanged
{
    private SeriesCollection _pieSeries;
    public SeriesCollection PieSeries
    {
        get => _pieSeries;
        set
        {
            _pieSeries = value;
            OnPropertyChanged();
        }
    }

    public YourWindow()
    {
        InitializeComponent();
        DataContext = this;
        
        PieSeries = new SeriesCollection
        {
            new PieSeries { Values = new ChartValues<decimal> { 1, 3 } }
        };
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

This approach keeps your UI and logic separated, making it easier to debug and maintain as your project grows.

内容的提问来源于stack exchange,提问作者Emmet

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:43:08