简体   繁体   中英

Binding Pie Chart with WPF Toolkit not showing points

I have a piechart that I have defined as below:

        <chartingToolkit:Chart DataContext="1,10 2,20 3,30 4,40" 
                           HorizontalAlignment="Left" Margin="334,238,0,0" 
                           Name="chart1"  VerticalAlignment="Top" Height="177"
                           Width="218" BorderBrush="#00000000">
        <chartingToolkit:PieSeries 
        ItemsSource="{Binding PieCollection}"
        IndependentValueBinding="{Binding Path=Name}"
        DependentValueBinding="{Binding Path=Share}" />
    </chartingToolkit:Chart>

And my xaml is also referenced here:

 xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit">

So no problem there. I have a dataset I declare in my ViewModel:

 private ObservableCollection<PiePoint> _pieCollection;
 public ObservableCollection<PiePoint> PieCollection { get { return _pieCollection; } set { _pieCollection = value; OnPropertyChanged("PieCollection"); } }

Where PiePoint is an object I define like so:

    public class PiePoint
    {
        public string Name { get; set; }
        public Int16 Share { get; set; }
    }

When I start up my ViewModel in my constructor, I added some test data points here to see if I could get some data on my chart:

    PieCollection = new ObservableCollection<PiePoint>();
    PieCollection.Add(new PiePoint { Name = "Mango", Share = 10 });
    PieCollection.Add(new PiePoint { Name = "Banana", Share = 36 });

But nothing shows up on the chart. The breakpoint hits, I see the chart's background, and I know that the items source is bound to the right collection, but I can't seem to get it to work. Any suggestions?

Here's what I got using your data:

XAML:

<Grid>
    <chartingToolkit:Chart Margin="0" Title="Chart Title">
        <chartingToolkit:Chart.DataContext>
            <local:PieCollection/>
        </chartingToolkit:Chart.DataContext>
        <chartingToolkit:PieSeries ItemsSource="{Binding Mode=OneWay}" DependentValuePath="Share" IndependentValuePath="Name" DataContext="{Binding Mode=OneWay}" >
        </chartingToolkit:PieSeries>
    </chartingToolkit:Chart>
</Grid>

Result:

在此处输入图片说明

Notice that I'm creating the PieCollection within XAML, so it is associated with the DataContext for the chart. In your case you're creating your PieCollection in code behind, so you probably need to do something like

chart.DataContext = PieCollection

EDIT : creating PieCollection dynamically:

public partial class MainWindow : Window
{
    PieCollection pieCollection1;

    public MainWindow()
    {
        InitializeComponent();

        pieCollection1 = new PieCollection();

        pieCollection1.Add(new PiePoint { Name = "Mango", Share = 10 });
        pieCollection1.Add(new PiePoint { Name = "Banana", Share = 36 });
        pieCollection1.Add(new PiePoint { Name = "Grapes", Share = 15 });
        pieCollection1.Add(new PiePoint { Name = "Apple", Share = 20 });

        Chart1.DataContext = pieCollection1;
    }
}

Result:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM