3

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();
        }

1 Answers1

2

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

Dragos Stoica
  • 1,753
  • 1
  • 21
  • 42
  • I did as u described. But no improvements yet. Only the Chart Title and the Subtitle showing up. Couldn't figure whats happening there... – Shiran S Ekanayake Mar 28 '15 at 12:34
  • you added the assebly as described in documentation "De.TorstenMandelkow.MetroChart"(correctly --> for wpf)? It is marked as "copy local true"? try clean + rebuild, or reset visual studio. I tried myself the code and is working, so is something with your settings... maybe the target platform is 4.5.1 and you need 4.5 ? – Dragos Stoica Mar 28 '15 at 12:35
  • I created a new project add used your code now. It is working. But what I need to do is I need to add this graph to an existing project currently I'm working on. There I'm using WPF UserControlls for GUIs. Not WPF Windows. Will it be a problem? I need to display the chart in a UserControll – Shiran S Ekanayake Mar 28 '15 at 12:46
  • No, does not matters if is a usercontrol or window, make sure that you added the correct dll -> De.TorstenMandelkow.MetroChart for WPF, you have to include this dll and go to propertis and make sure that is set to copy local true. Step 1 from documentation : 1. Add a reference to De.TorstenMandelkow.MetroChart.dll to your project – Dragos Stoica Mar 28 '15 at 12:48
  • Of course I have added the assembly correctly and is set to copy local true. It is working fine when I create a new project and do the steps. But when I add it to my existing project which I need it to be, It doesn't show up.. :( – Shiran S Ekanayake Mar 28 '15 at 12:59
  • try to use a frame in your usercontrol and bind the source to a page containg your chart, in this manner works ? I suppose that you have styles problems, in your application you define universal style for labels or stackpanels or textblocks ? maybe this can be the issue, but you can add a Page controll in other project and import it with the help of a frame / or try to clean your styles to see where is the problem – Dragos Stoica Mar 28 '15 at 13:04
  • Problem Fixed! You are correct I have had a problem with the style, I have used a theme in my project. Just cleaned the things and It Worked!! – Shiran S Ekanayake Mar 28 '15 at 13:09
  • Glad to hear that you make it work! please mark the answer as correct – Dragos Stoica Mar 28 '15 at 13:15
  • 1
    This isn't actually the solution to the problem so it should not have been marked as correct. @ShiranSEkanayake, can you please post a detailed answer based on how you solved your problem? This post comes up #1 in google for this issue and the marked correct answer has absolutely nothing to do with fixing conflicting styles. –  Jun 12 '15 at 11:31