Search code examples
c#.netwpfchartsmodern-ui

Modern UI (Metro) Charts WPF not showing up


I'm Using .NET Framework 4.5 and WPF for my project. I need to generate several types of charts so I'm thinking to use http://modernuicharts.codeplex.com/ library to do that. I followed the documentation there in that codeplex page : http://modernuicharts.codeplex.com/documentation and completed the steps.

Now I'm not getting any kind of errors or warning messages in Visual Studio 13, But simply the chart is not showing up. I can only see the title and the subtitle of the chart.

What are the possible causes for this problem and how to correct them?

Thank you

The XAML code :

<Canvas Margin="570,90,29,92" Background="White" >
                <chart:PieChart
        Style="{StaticResource MinimalChartStyle}"
        ChartTitle="Minimal Pie Chart"
        ChartSubTitle="Chart with fixed width and height"
        SelectedItem="{Binding Path=SelectedItem, Mode=TwoWay}" >
                    <chart:PieChart.Series>
                        <chart:ChartSeries
                SeriesTitle="Errors"
                DisplayMember="Category"
                ValueMember="Number"
                ItemsSource="{Binding Path=Errors}" />
                    </chart:PieChart.Series>
                </chart:PieChart>
            </Canvas>

The ViewModel Class which is used as the dataContext :

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ModernUIForWPFSample.WithoutBackButton.Views
{
    public class MainViewModel
    {
        public ObservableCollection<TestClass> Errors { get; private set; }

        public MainViewModel()
        {
            Errors = new ObservableCollection<TestClass>();
            Errors.Add(new TestClass() { Category = "Globalization", Number = 75 });
            Errors.Add(new TestClass() { Category = "Features", Number = 2 });
            Errors.Add(new TestClass() { Category = "ContentTypes", Number = 12 });
            Errors.Add(new TestClass() { Category = "Correctness", Number = 83});
            Errors.Add(new TestClass() { Category = "Best Practices", Number = 29 });
        }

        private object selectedItem = null;
        public object SelectedItem
        {
            get
            {
                return selectedItem;
            }
            set
            {
                // selected item has changed
                selectedItem = value;                
            }
        }
    }

    // class which represent a data point in the chart
    public class TestClass
    {
        public string Category { get; set; }

        public int Number  { get; set; }        
    }

}

In the code Bihind :

public FinalAnalysis()
        {
            InitializeComponent();
            this.DataContext = new MainViewModel();
        }

Solution

  • This is a working example :

    App XAML:

    <Application x:Class="WpfApplication1.App"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 StartupUri="MainWindow.xaml"
                 xmlns:chart="clr-namespace:De.TorstenMandelkow.MetroChart;assembly=De.TorstenMandelkow.MetroChart">
        <Application.Resources>
            <ResourceDictionary>
                <Style x:Key="MinimalChartStyle" TargetType="chart:ChartBase">
                    <Setter Property="Width" Value="500"/>
                    <Setter Property="Height" Value="500"/>
                </Style>
            </ResourceDictionary>
        </Application.Resources>
    </Application>
    

    MainWindow XAML :

    <Window 
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            x:Class="WpfApplication1.MainWindow"
            Title="MainWindow" Height="1500" Width="1525"
            xmlns:metroChart="clr-namespace:De.TorstenMandelkow.MetroChart;assembly=De.TorstenMandelkow.MetroChart">
    
    
            <metroChart:PieChart
            Style="{StaticResource MinimalChartStyle}"
            ChartTitle="Minimal Pie Chart"
            ChartSubTitle="Chart with fixed width and height"
            SelectedItem="{Binding Path=SelectedItem, Mode=TwoWay}" >
                <metroChart:PieChart.Series>
                    <metroChart:ChartSeries
                    SeriesTitle="Errors"
                    DisplayMember="Category"
                    ValueMember="Number"
                    ItemsSource="{Binding Path=Errors}" />
                </metroChart:PieChart.Series>
            </metroChart:PieChart>
    
    </Window>
    

    MainWindow cs :

    public MainWindow()
    {
    
        InitializeComponent();
        DataContext = new MainViewModel();
    }
    

    MainWindowViewModel is same as yours, I will not copy paste it here. Pay attention to assembly declaration :

     xmlns:metroChart="clr-namespace:De.TorstenMandelkow.MetroChart;assembly=De.TorstenMandelkow.MetroChart"
    

    and do not forget to add the style in App xaml