0

I am creating a chart for the visualization of weekly stats. That means that I need a 7 columns chart and, if the data is not available, leave an empty space on the corresponding column. I am having a lot of troubles because I haven't found a proper guide for charting in WPF (if you have any feel free to share)

This is my chart:

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

<charting:Chart Name="LastWeekChart" Grid.Row="0"  Width="895" Height ="250"  HorizontalAlignment="Center" >
    <charting:Chart.LegendStyle>
        <Style TargetType="datavis:Legend">
            <Setter Property="Width" Value="0" />
        </Style>
    </charting:Chart.LegendStyle>
    <charting:ColumnSeries DependentValuePath="Value" IndependentValuePath="Name" ItemsSource="{Binding}" />
</charting:Chart>

And this is the code behind:

public static List<SingleBar> LastWeek = new List<SingleBar>();
class SingleBar
{
    public string Name { get; set; }
    public double Value { get; set; }
    public SingleBar()
    {
        this.Name = "";
        this.Value = 0;
    }
    public SingleBar(string name, double value)
    {
        this.Name = name;
        this.Value = value;
    }
}

And this is how I fill the chart if there are missing data:

if (LastWeek.Count() < 7)
for (int i = LastWeek.Count(); i < 7; i++)
    LastWeek.Insert(0, new SingleBar());

So if there are less than 7 data, it inserts empty data at the beginning. Now I saw that on the code the bars are added, but on the graph only one empty bar is displayed, no matter how many empty bars I added. Anyone can help me?

EDIT: To reproduce it, try something like this:

LastWeek.Add(new SingleBar());
LastWeek.Add(new SingleBar());
LastWeek.Add(new SingleBar());
LastWeek.Add(new SingleBar());
LastWeek.Add(new SingleBar("Friday",50));
LastWeek.Add(new SingleBar("Saturday", 75));
LastWeek.Add(new SingleBar("Sunday",60));
LastWeekChart.DataContext = LastWeek;
2
  • Your problem is unreproducible from your code.
    – Sheridan
    Jun 10, 2014 at 9:44
  • I read logs from an external device, so you can't reproduce it. Try the code I added now, it creates the same conditions.
    – Hamma
    Jun 10, 2014 at 9:58

1 Answer 1

0

I don't know if it's relevant, but I have used Oxyplot in the past and find it much easier to work with (read as: wpf tool kit is a pain).

Here's an example using oxyplot with some empty bars in the start of each series:

enter image description here

You can find their examples here . Just click the one you like ,and you can view the code (on the bottom you have a code/plot tab).

Here's the code for screetshot in case you wonder:

Xaml:

  <Window x:Class="wpfoxyempty.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:oxy="http://oxyplot.codeplex.com"
          Title="MainWindow" Height="350" Width="525"
          Name="OxyDemo"
          >
      <Grid>
          <oxy:Plot Model="{Binding ElementName=OxyDemo, Path=OutputChart}" 
                    Margin="5" />

      </Grid>
  </Window>

Code behind:

public partial class MainWindow : Window
{
    public MainWindow()

    {
        OutputChart = new PlotModel();
        SetUpModel(OutputChart);
        InitializeComponent();
    }

    private void SetUpModel(PlotModel plotModel)
    {

        var plotModel1 = new PlotModel();
        plotModel1.LegendBorderThickness = 0;
        plotModel1.LegendOrientation = LegendOrientation.Horizontal;
        plotModel1.LegendPlacement = LegendPlacement.Outside;
        plotModel1.LegendPosition = LegendPosition.BottomCenter;
        plotModel1.Title = "No axes defined";

        var linearAxis1 = new LinearAxis();
        linearAxis1.MinimumPadding = 0;
        linearAxis1.Position = AxisPosition.Bottom;
        plotModel1.Axes.Add(linearAxis1);

        var categoryAxis1 = new CategoryAxis();
        categoryAxis1.MinorStep = 1;
        categoryAxis1.Position = AxisPosition.Left;
        categoryAxis1.Labels.Add("1");
        categoryAxis1.Labels.Add("2");
        categoryAxis1.Labels.Add("3");
        categoryAxis1.Labels.Add("4"); 
        categoryAxis1.Labels.Add("5");
        categoryAxis1.Labels.Add("6");
        categoryAxis1.Labels.Add("7");
        plotModel1.Axes.Add(categoryAxis1);

        var barSeries1 = new BarSeries();
        barSeries1.StrokeThickness = 1;
        barSeries1.Title = "Series 1";
        barSeries1.Items.Add(new BarItem(0, -1));
        barSeries1.Items.Add(new BarItem(0, -1));
        barSeries1.Items.Add(new BarItem(0, -1));
        barSeries1.Items.Add(new BarItem(0, -1));
        barSeries1.Items.Add(new BarItem(137, -1));
        barSeries1.Items.Add(new BarItem(18, -1));
        barSeries1.Items.Add(new BarItem(40, -1));
        plotModel1.Series.Add(barSeries1);

        var barSeries2 = new BarSeries();
        barSeries2.StrokeThickness = 1;
        barSeries2.Title = "Series 2";
        barSeries2.Items.Add(new BarItem(0, -1));
        barSeries2.Items.Add(new BarItem(0, -1));
        barSeries2.Items.Add(new BarItem(30, -1));
        barSeries2.Items.Add(new BarItem(40, -1)); 
        barSeries2.Items.Add(new BarItem(12, -1));
        barSeries2.Items.Add(new BarItem(68, -1));
        barSeries2.Items.Add(new BarItem(8, -1));
        plotModel1.Series.Add(barSeries2);

        OutputChart = plotModel1;

    }

    /// <summary>
    /// Holds the plot (chart) we'll display
    /// </summary>
    public PlotModel OutputChart
    {
        get { return _outputChart; }
        set
        {
            if (Equals(value, _outputChart)) return;
            _outputChart = value;

        }
    }
    private PlotModel _outputChart;

}
3
  • Look nice; to be honest I saw Oxyplot first, but then I decided to go for WPF toolkit because it looked simpler. Unfortunately, I discovered a lot of limitation while using it, so I might reconsider...
    – Hamma
    Jun 10, 2014 at 13:34
  • it takes me a couple of hours to get my feet after a long time of not using it, but when I first started hunting for graphs, I tried WPF toolkit, and was highly disappointed. I've done some cool things with Oxyplot, and it's under development, and you can get help on their forums as well.
    – Noctis
    Jun 10, 2014 at 21:50
  • Thank you, I will definitely try it!
    – Hamma
    Jun 11, 2014 at 8:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.