OxyPlot graph not rendering in child Grid
I'm making a statistics application using OxyPlot WPF where the user inputs the class limits and the frequencies for each class. Then you chose which of the 4 graphs you want displayed. Then the graph gets displayed in that white box (see image below).
Statistics app GUI
In the MainWindow
constructor, I set the this.DataContext = this
. I have a PlotModel
property called "MyModel" which is binded to the the Model property of the PlotView
control in the XAML code ( <oxy:PlotView Model="{Binding MyModel}"/>
) . This PlotView
is inside a Grid
called "Graph" which is inside the main Grid
(it's at the bottom of all my xaml code).
So when the "Display" button is clicked, inside it's event handler, it'll go into a different if statement based on which radio button is checked. At the end of each if statement, this.MyModel
gets set to the PlotModel
that was created inside it. I think by writing this.MyModel = model
it's supposed to display the graph but when i tested it, nothing gets rendered in the white part
(see all code below).
I'm not sure if this problem is because the this.MyModel= model
is inside a child grid or if there's binding issues.
I'm relatively new to WPF programming in general and any help would be appreciated. Thanks!
MainWindow.xaml :
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:StatsApp"
xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
xmlns:oxy="http://oxyplot.org/wpf"
xmlns:Properties="clr-namespace:StatsApp.Properties" x:Class="StatsApp.MainWindow"
mc:Ignorable="d"
Title="Frequency Distribution Graph Generator" Height="450" Width="800">
<Grid Background="Gray">
<Grid x:Name="Graph" HorizontalAlignment="Left" Height="306" Margin="340,39,0,0" VerticalAlignment="Top" Width="421">
<oxy:PlotView Model="{Binding MyModel}"/>
</Grid>
<!-- I left out all the unnecessary textboxes and buttons ..... -->
</Grid>
MainWindow.cs :
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
}
//private bool isfirstclick = true;
public PlotModel MyModel { get; private set; }
private void DisplayBtn_Click(object sender, RoutedEventArgs e)
{
// ---------------Retrieving Input START----------------
TextBox lowerLimitsTxtBoxes =
{
Lower1TxtBox,
Lower2TxtBox,
Lower3TxtBox,
Lower4TxtBox,
Lower5TxtBox,
Lower6TxtBox,
Lower7TxtBox,
Lower8TxtBox
};
TextBox upperLimitsTxtBoxes =
{
Upper1TxtBox,
Upper2TxtBox,
Upper3TxtBox,
Upper4TxtBox,
Upper5TxtBox,
Upper6TxtBox,
Upper7TxtBox,
Upper8TxtBox
};
TextBox freqsTxtBoxes =
{
Freq1TxtBox,
Freq2TxtBox,
Freq3TxtBox,
Freq4TxtBox,
Freq5TxtBox,
Freq6TxtBox,
Freq7TxtBox,
Freq8TxtBox
};
double lowerLimits = new double[8];
for (int i = 0; i < lowerLimits.Length; i++)
{
if (Double.TryParse(lowerLimitsTxtBoxes[i].Text, out double lower))
{
lowerLimits[i] = lower;
}
else
{
lowerLimits[i] = -1;
}
}
double upperLimits = new double[8];
for (int i = 0; i < upperLimits.Length; i++)
{
if (Double.TryParse(upperLimitsTxtBoxes[i].Text, out double upper))
{
upperLimits[i] = upper;
}
else
{
upperLimits[i] = -1;
}
}
//IMPORTANT -> The array of frequencies
int freqs = new int[8];
for (int i = 0; i < freqs.Length; i++)
{
if (Int32.TryParse(freqsTxtBoxes[i].Text, out int freq))
{
freqs[i] = freq;
}
else
{
freqs[i] = -1;
}
}
int numClasses = 0;
for (int i = 0; lowerLimits[i] != -1 && i < 8; i++)
{
numClasses++;
}
if (numClasses < 2)
{
throw new ArgumentException("Must use at least 2 classes");
}
//IMPORTANT -> The class marks array: double
double classMarks = new double[numClasses];
for (int i = 0; i < classMarks.Length; i++)
{
classMarks[i] = (lowerLimits[i] + upperLimits[i]) / 2.0;
}
//IMPORTANT -> The class marks array: string
string classMarksString = new string[numClasses];
for (int i = 0; i < numClasses; i++)
{
classMarksString[i] = classMarks[i] + "";
}
//----------Retrieving Input END--------------------
if ((bool)ScatterRBtn.IsChecked)
{
var model = new PlotModel { Title = "Scatter Plot" };
var scatter = new ScatterSeries { MarkerType = MarkerType.Circle };
model.Axes.Add(new LinearAxis { Position = AxisPosition.Left, Title = "Class Marks" });
model.Axes.Add(new LinearAxis { Position = AxisPosition.Bottom, Title = "Frequency" });
for ( int i =0; i < numClasses; i++)
{
scatter.Points.Add(new ScatterPoint(classMarks[i], freqs[i]));
}
model.Series.Add(scatter);
//This doesn't update the graph even though I binded it in XAML code
this.MyModel = model;
}
else if ((bool)RelativeFqRBtn.IsChecked)
{
var model = new PlotModel { Title = "Relative Frequency Polygon" };
model.Axes.Add(new LinearAxis { Position = AxisPosition.Left, Maximum = 1, Title = "Class Marks" });
model.Axes.Add(new LinearAxis { Position = AxisPosition.Bottom, Title = "Frequency (%)" });
var relativeFQ = new LineSeries();
int frequencyTotal = 0;
for (int i=0; i < 8 && freqs[i] != -1 ; i++)
{
frequencyTotal += freqs[i];
}
for (int i = 0; i < numClasses; i++)
{
relativeFQ.Points.Add(new DataPoint(classMarks[i], freqs[i]/frequencyTotal ));
}
model.Series.Add(relativeFQ);
//This doesn't update the graph even though I binded it in XAML code
this.MyModel = model;
}
else if ((bool)CummuFqRBtn.IsChecked)
{
var model = new PlotModel { Title = "Cummulative Frequency Polygon" };
model.Axes.Add(new LinearAxis { Position = AxisPosition.Left, Title= "Frequency" });
model.Axes.Add(new LinearAxis { Position = AxisPosition.Bottom, Title ="Class Boundaries" });
var cummulativeFQ = new LineSeries();
double classBoundaries = new double[numClasses + 1];
double midpointDistance = (lowerLimits[1] - upperLimits[0]) / 2;
classBoundaries[0] = lowerLimits[0] - midpointDistance;
for (int i = 0; i < numClasses; i++)
{
classBoundaries[i + 1] = upperLimits[i] + midpointDistance;
}
cummulativeFQ.Points.Add(new DataPoint(classBoundaries[0], 0));
for (int i = 0; i< numClasses ; i++)
{
cummulativeFQ.Points.Add(new DataPoint(classBoundaries[i+1], freqs[i]));
}
model.Series.Add(cummulativeFQ);
//This doesn't update the graph even though I binded it in XAML code
this.MyModel = model;
}
else
{
var model = new PlotModel { Title = "Histogram" };
model.Axes.Add(new LinearAxis { Title = "Frequency", Position = AxisPosition.Left });
model.Axes.Add(new CategoryAxis
{
Title = "Class Marks",
ItemsSource = classMarksString
});
var histogram = new ColumnSeries();
model.Series.Add(histogram);
for (int i = 0; i < numClasses; i++)
{
histogram.Items.Add(new ColumnItem(freqs[i]));
}
//This doesn't update the graph even though I binded it in XAML code
this.MyModel = model;
}
}
}
c# wpf visual-studio oxyplot
add a comment |
I'm making a statistics application using OxyPlot WPF where the user inputs the class limits and the frequencies for each class. Then you chose which of the 4 graphs you want displayed. Then the graph gets displayed in that white box (see image below).
Statistics app GUI
In the MainWindow
constructor, I set the this.DataContext = this
. I have a PlotModel
property called "MyModel" which is binded to the the Model property of the PlotView
control in the XAML code ( <oxy:PlotView Model="{Binding MyModel}"/>
) . This PlotView
is inside a Grid
called "Graph" which is inside the main Grid
(it's at the bottom of all my xaml code).
So when the "Display" button is clicked, inside it's event handler, it'll go into a different if statement based on which radio button is checked. At the end of each if statement, this.MyModel
gets set to the PlotModel
that was created inside it. I think by writing this.MyModel = model
it's supposed to display the graph but when i tested it, nothing gets rendered in the white part
(see all code below).
I'm not sure if this problem is because the this.MyModel= model
is inside a child grid or if there's binding issues.
I'm relatively new to WPF programming in general and any help would be appreciated. Thanks!
MainWindow.xaml :
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:StatsApp"
xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
xmlns:oxy="http://oxyplot.org/wpf"
xmlns:Properties="clr-namespace:StatsApp.Properties" x:Class="StatsApp.MainWindow"
mc:Ignorable="d"
Title="Frequency Distribution Graph Generator" Height="450" Width="800">
<Grid Background="Gray">
<Grid x:Name="Graph" HorizontalAlignment="Left" Height="306" Margin="340,39,0,0" VerticalAlignment="Top" Width="421">
<oxy:PlotView Model="{Binding MyModel}"/>
</Grid>
<!-- I left out all the unnecessary textboxes and buttons ..... -->
</Grid>
MainWindow.cs :
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
}
//private bool isfirstclick = true;
public PlotModel MyModel { get; private set; }
private void DisplayBtn_Click(object sender, RoutedEventArgs e)
{
// ---------------Retrieving Input START----------------
TextBox lowerLimitsTxtBoxes =
{
Lower1TxtBox,
Lower2TxtBox,
Lower3TxtBox,
Lower4TxtBox,
Lower5TxtBox,
Lower6TxtBox,
Lower7TxtBox,
Lower8TxtBox
};
TextBox upperLimitsTxtBoxes =
{
Upper1TxtBox,
Upper2TxtBox,
Upper3TxtBox,
Upper4TxtBox,
Upper5TxtBox,
Upper6TxtBox,
Upper7TxtBox,
Upper8TxtBox
};
TextBox freqsTxtBoxes =
{
Freq1TxtBox,
Freq2TxtBox,
Freq3TxtBox,
Freq4TxtBox,
Freq5TxtBox,
Freq6TxtBox,
Freq7TxtBox,
Freq8TxtBox
};
double lowerLimits = new double[8];
for (int i = 0; i < lowerLimits.Length; i++)
{
if (Double.TryParse(lowerLimitsTxtBoxes[i].Text, out double lower))
{
lowerLimits[i] = lower;
}
else
{
lowerLimits[i] = -1;
}
}
double upperLimits = new double[8];
for (int i = 0; i < upperLimits.Length; i++)
{
if (Double.TryParse(upperLimitsTxtBoxes[i].Text, out double upper))
{
upperLimits[i] = upper;
}
else
{
upperLimits[i] = -1;
}
}
//IMPORTANT -> The array of frequencies
int freqs = new int[8];
for (int i = 0; i < freqs.Length; i++)
{
if (Int32.TryParse(freqsTxtBoxes[i].Text, out int freq))
{
freqs[i] = freq;
}
else
{
freqs[i] = -1;
}
}
int numClasses = 0;
for (int i = 0; lowerLimits[i] != -1 && i < 8; i++)
{
numClasses++;
}
if (numClasses < 2)
{
throw new ArgumentException("Must use at least 2 classes");
}
//IMPORTANT -> The class marks array: double
double classMarks = new double[numClasses];
for (int i = 0; i < classMarks.Length; i++)
{
classMarks[i] = (lowerLimits[i] + upperLimits[i]) / 2.0;
}
//IMPORTANT -> The class marks array: string
string classMarksString = new string[numClasses];
for (int i = 0; i < numClasses; i++)
{
classMarksString[i] = classMarks[i] + "";
}
//----------Retrieving Input END--------------------
if ((bool)ScatterRBtn.IsChecked)
{
var model = new PlotModel { Title = "Scatter Plot" };
var scatter = new ScatterSeries { MarkerType = MarkerType.Circle };
model.Axes.Add(new LinearAxis { Position = AxisPosition.Left, Title = "Class Marks" });
model.Axes.Add(new LinearAxis { Position = AxisPosition.Bottom, Title = "Frequency" });
for ( int i =0; i < numClasses; i++)
{
scatter.Points.Add(new ScatterPoint(classMarks[i], freqs[i]));
}
model.Series.Add(scatter);
//This doesn't update the graph even though I binded it in XAML code
this.MyModel = model;
}
else if ((bool)RelativeFqRBtn.IsChecked)
{
var model = new PlotModel { Title = "Relative Frequency Polygon" };
model.Axes.Add(new LinearAxis { Position = AxisPosition.Left, Maximum = 1, Title = "Class Marks" });
model.Axes.Add(new LinearAxis { Position = AxisPosition.Bottom, Title = "Frequency (%)" });
var relativeFQ = new LineSeries();
int frequencyTotal = 0;
for (int i=0; i < 8 && freqs[i] != -1 ; i++)
{
frequencyTotal += freqs[i];
}
for (int i = 0; i < numClasses; i++)
{
relativeFQ.Points.Add(new DataPoint(classMarks[i], freqs[i]/frequencyTotal ));
}
model.Series.Add(relativeFQ);
//This doesn't update the graph even though I binded it in XAML code
this.MyModel = model;
}
else if ((bool)CummuFqRBtn.IsChecked)
{
var model = new PlotModel { Title = "Cummulative Frequency Polygon" };
model.Axes.Add(new LinearAxis { Position = AxisPosition.Left, Title= "Frequency" });
model.Axes.Add(new LinearAxis { Position = AxisPosition.Bottom, Title ="Class Boundaries" });
var cummulativeFQ = new LineSeries();
double classBoundaries = new double[numClasses + 1];
double midpointDistance = (lowerLimits[1] - upperLimits[0]) / 2;
classBoundaries[0] = lowerLimits[0] - midpointDistance;
for (int i = 0; i < numClasses; i++)
{
classBoundaries[i + 1] = upperLimits[i] + midpointDistance;
}
cummulativeFQ.Points.Add(new DataPoint(classBoundaries[0], 0));
for (int i = 0; i< numClasses ; i++)
{
cummulativeFQ.Points.Add(new DataPoint(classBoundaries[i+1], freqs[i]));
}
model.Series.Add(cummulativeFQ);
//This doesn't update the graph even though I binded it in XAML code
this.MyModel = model;
}
else
{
var model = new PlotModel { Title = "Histogram" };
model.Axes.Add(new LinearAxis { Title = "Frequency", Position = AxisPosition.Left });
model.Axes.Add(new CategoryAxis
{
Title = "Class Marks",
ItemsSource = classMarksString
});
var histogram = new ColumnSeries();
model.Series.Add(histogram);
for (int i = 0; i < numClasses; i++)
{
histogram.Items.Add(new ColumnItem(freqs[i]));
}
//This doesn't update the graph even though I binded it in XAML code
this.MyModel = model;
}
}
}
c# wpf visual-studio oxyplot
add a comment |
I'm making a statistics application using OxyPlot WPF where the user inputs the class limits and the frequencies for each class. Then you chose which of the 4 graphs you want displayed. Then the graph gets displayed in that white box (see image below).
Statistics app GUI
In the MainWindow
constructor, I set the this.DataContext = this
. I have a PlotModel
property called "MyModel" which is binded to the the Model property of the PlotView
control in the XAML code ( <oxy:PlotView Model="{Binding MyModel}"/>
) . This PlotView
is inside a Grid
called "Graph" which is inside the main Grid
(it's at the bottom of all my xaml code).
So when the "Display" button is clicked, inside it's event handler, it'll go into a different if statement based on which radio button is checked. At the end of each if statement, this.MyModel
gets set to the PlotModel
that was created inside it. I think by writing this.MyModel = model
it's supposed to display the graph but when i tested it, nothing gets rendered in the white part
(see all code below).
I'm not sure if this problem is because the this.MyModel= model
is inside a child grid or if there's binding issues.
I'm relatively new to WPF programming in general and any help would be appreciated. Thanks!
MainWindow.xaml :
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:StatsApp"
xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
xmlns:oxy="http://oxyplot.org/wpf"
xmlns:Properties="clr-namespace:StatsApp.Properties" x:Class="StatsApp.MainWindow"
mc:Ignorable="d"
Title="Frequency Distribution Graph Generator" Height="450" Width="800">
<Grid Background="Gray">
<Grid x:Name="Graph" HorizontalAlignment="Left" Height="306" Margin="340,39,0,0" VerticalAlignment="Top" Width="421">
<oxy:PlotView Model="{Binding MyModel}"/>
</Grid>
<!-- I left out all the unnecessary textboxes and buttons ..... -->
</Grid>
MainWindow.cs :
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
}
//private bool isfirstclick = true;
public PlotModel MyModel { get; private set; }
private void DisplayBtn_Click(object sender, RoutedEventArgs e)
{
// ---------------Retrieving Input START----------------
TextBox lowerLimitsTxtBoxes =
{
Lower1TxtBox,
Lower2TxtBox,
Lower3TxtBox,
Lower4TxtBox,
Lower5TxtBox,
Lower6TxtBox,
Lower7TxtBox,
Lower8TxtBox
};
TextBox upperLimitsTxtBoxes =
{
Upper1TxtBox,
Upper2TxtBox,
Upper3TxtBox,
Upper4TxtBox,
Upper5TxtBox,
Upper6TxtBox,
Upper7TxtBox,
Upper8TxtBox
};
TextBox freqsTxtBoxes =
{
Freq1TxtBox,
Freq2TxtBox,
Freq3TxtBox,
Freq4TxtBox,
Freq5TxtBox,
Freq6TxtBox,
Freq7TxtBox,
Freq8TxtBox
};
double lowerLimits = new double[8];
for (int i = 0; i < lowerLimits.Length; i++)
{
if (Double.TryParse(lowerLimitsTxtBoxes[i].Text, out double lower))
{
lowerLimits[i] = lower;
}
else
{
lowerLimits[i] = -1;
}
}
double upperLimits = new double[8];
for (int i = 0; i < upperLimits.Length; i++)
{
if (Double.TryParse(upperLimitsTxtBoxes[i].Text, out double upper))
{
upperLimits[i] = upper;
}
else
{
upperLimits[i] = -1;
}
}
//IMPORTANT -> The array of frequencies
int freqs = new int[8];
for (int i = 0; i < freqs.Length; i++)
{
if (Int32.TryParse(freqsTxtBoxes[i].Text, out int freq))
{
freqs[i] = freq;
}
else
{
freqs[i] = -1;
}
}
int numClasses = 0;
for (int i = 0; lowerLimits[i] != -1 && i < 8; i++)
{
numClasses++;
}
if (numClasses < 2)
{
throw new ArgumentException("Must use at least 2 classes");
}
//IMPORTANT -> The class marks array: double
double classMarks = new double[numClasses];
for (int i = 0; i < classMarks.Length; i++)
{
classMarks[i] = (lowerLimits[i] + upperLimits[i]) / 2.0;
}
//IMPORTANT -> The class marks array: string
string classMarksString = new string[numClasses];
for (int i = 0; i < numClasses; i++)
{
classMarksString[i] = classMarks[i] + "";
}
//----------Retrieving Input END--------------------
if ((bool)ScatterRBtn.IsChecked)
{
var model = new PlotModel { Title = "Scatter Plot" };
var scatter = new ScatterSeries { MarkerType = MarkerType.Circle };
model.Axes.Add(new LinearAxis { Position = AxisPosition.Left, Title = "Class Marks" });
model.Axes.Add(new LinearAxis { Position = AxisPosition.Bottom, Title = "Frequency" });
for ( int i =0; i < numClasses; i++)
{
scatter.Points.Add(new ScatterPoint(classMarks[i], freqs[i]));
}
model.Series.Add(scatter);
//This doesn't update the graph even though I binded it in XAML code
this.MyModel = model;
}
else if ((bool)RelativeFqRBtn.IsChecked)
{
var model = new PlotModel { Title = "Relative Frequency Polygon" };
model.Axes.Add(new LinearAxis { Position = AxisPosition.Left, Maximum = 1, Title = "Class Marks" });
model.Axes.Add(new LinearAxis { Position = AxisPosition.Bottom, Title = "Frequency (%)" });
var relativeFQ = new LineSeries();
int frequencyTotal = 0;
for (int i=0; i < 8 && freqs[i] != -1 ; i++)
{
frequencyTotal += freqs[i];
}
for (int i = 0; i < numClasses; i++)
{
relativeFQ.Points.Add(new DataPoint(classMarks[i], freqs[i]/frequencyTotal ));
}
model.Series.Add(relativeFQ);
//This doesn't update the graph even though I binded it in XAML code
this.MyModel = model;
}
else if ((bool)CummuFqRBtn.IsChecked)
{
var model = new PlotModel { Title = "Cummulative Frequency Polygon" };
model.Axes.Add(new LinearAxis { Position = AxisPosition.Left, Title= "Frequency" });
model.Axes.Add(new LinearAxis { Position = AxisPosition.Bottom, Title ="Class Boundaries" });
var cummulativeFQ = new LineSeries();
double classBoundaries = new double[numClasses + 1];
double midpointDistance = (lowerLimits[1] - upperLimits[0]) / 2;
classBoundaries[0] = lowerLimits[0] - midpointDistance;
for (int i = 0; i < numClasses; i++)
{
classBoundaries[i + 1] = upperLimits[i] + midpointDistance;
}
cummulativeFQ.Points.Add(new DataPoint(classBoundaries[0], 0));
for (int i = 0; i< numClasses ; i++)
{
cummulativeFQ.Points.Add(new DataPoint(classBoundaries[i+1], freqs[i]));
}
model.Series.Add(cummulativeFQ);
//This doesn't update the graph even though I binded it in XAML code
this.MyModel = model;
}
else
{
var model = new PlotModel { Title = "Histogram" };
model.Axes.Add(new LinearAxis { Title = "Frequency", Position = AxisPosition.Left });
model.Axes.Add(new CategoryAxis
{
Title = "Class Marks",
ItemsSource = classMarksString
});
var histogram = new ColumnSeries();
model.Series.Add(histogram);
for (int i = 0; i < numClasses; i++)
{
histogram.Items.Add(new ColumnItem(freqs[i]));
}
//This doesn't update the graph even though I binded it in XAML code
this.MyModel = model;
}
}
}
c# wpf visual-studio oxyplot
I'm making a statistics application using OxyPlot WPF where the user inputs the class limits and the frequencies for each class. Then you chose which of the 4 graphs you want displayed. Then the graph gets displayed in that white box (see image below).
Statistics app GUI
In the MainWindow
constructor, I set the this.DataContext = this
. I have a PlotModel
property called "MyModel" which is binded to the the Model property of the PlotView
control in the XAML code ( <oxy:PlotView Model="{Binding MyModel}"/>
) . This PlotView
is inside a Grid
called "Graph" which is inside the main Grid
(it's at the bottom of all my xaml code).
So when the "Display" button is clicked, inside it's event handler, it'll go into a different if statement based on which radio button is checked. At the end of each if statement, this.MyModel
gets set to the PlotModel
that was created inside it. I think by writing this.MyModel = model
it's supposed to display the graph but when i tested it, nothing gets rendered in the white part
(see all code below).
I'm not sure if this problem is because the this.MyModel= model
is inside a child grid or if there's binding issues.
I'm relatively new to WPF programming in general and any help would be appreciated. Thanks!
MainWindow.xaml :
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:StatsApp"
xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
xmlns:oxy="http://oxyplot.org/wpf"
xmlns:Properties="clr-namespace:StatsApp.Properties" x:Class="StatsApp.MainWindow"
mc:Ignorable="d"
Title="Frequency Distribution Graph Generator" Height="450" Width="800">
<Grid Background="Gray">
<Grid x:Name="Graph" HorizontalAlignment="Left" Height="306" Margin="340,39,0,0" VerticalAlignment="Top" Width="421">
<oxy:PlotView Model="{Binding MyModel}"/>
</Grid>
<!-- I left out all the unnecessary textboxes and buttons ..... -->
</Grid>
MainWindow.cs :
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
}
//private bool isfirstclick = true;
public PlotModel MyModel { get; private set; }
private void DisplayBtn_Click(object sender, RoutedEventArgs e)
{
// ---------------Retrieving Input START----------------
TextBox lowerLimitsTxtBoxes =
{
Lower1TxtBox,
Lower2TxtBox,
Lower3TxtBox,
Lower4TxtBox,
Lower5TxtBox,
Lower6TxtBox,
Lower7TxtBox,
Lower8TxtBox
};
TextBox upperLimitsTxtBoxes =
{
Upper1TxtBox,
Upper2TxtBox,
Upper3TxtBox,
Upper4TxtBox,
Upper5TxtBox,
Upper6TxtBox,
Upper7TxtBox,
Upper8TxtBox
};
TextBox freqsTxtBoxes =
{
Freq1TxtBox,
Freq2TxtBox,
Freq3TxtBox,
Freq4TxtBox,
Freq5TxtBox,
Freq6TxtBox,
Freq7TxtBox,
Freq8TxtBox
};
double lowerLimits = new double[8];
for (int i = 0; i < lowerLimits.Length; i++)
{
if (Double.TryParse(lowerLimitsTxtBoxes[i].Text, out double lower))
{
lowerLimits[i] = lower;
}
else
{
lowerLimits[i] = -1;
}
}
double upperLimits = new double[8];
for (int i = 0; i < upperLimits.Length; i++)
{
if (Double.TryParse(upperLimitsTxtBoxes[i].Text, out double upper))
{
upperLimits[i] = upper;
}
else
{
upperLimits[i] = -1;
}
}
//IMPORTANT -> The array of frequencies
int freqs = new int[8];
for (int i = 0; i < freqs.Length; i++)
{
if (Int32.TryParse(freqsTxtBoxes[i].Text, out int freq))
{
freqs[i] = freq;
}
else
{
freqs[i] = -1;
}
}
int numClasses = 0;
for (int i = 0; lowerLimits[i] != -1 && i < 8; i++)
{
numClasses++;
}
if (numClasses < 2)
{
throw new ArgumentException("Must use at least 2 classes");
}
//IMPORTANT -> The class marks array: double
double classMarks = new double[numClasses];
for (int i = 0; i < classMarks.Length; i++)
{
classMarks[i] = (lowerLimits[i] + upperLimits[i]) / 2.0;
}
//IMPORTANT -> The class marks array: string
string classMarksString = new string[numClasses];
for (int i = 0; i < numClasses; i++)
{
classMarksString[i] = classMarks[i] + "";
}
//----------Retrieving Input END--------------------
if ((bool)ScatterRBtn.IsChecked)
{
var model = new PlotModel { Title = "Scatter Plot" };
var scatter = new ScatterSeries { MarkerType = MarkerType.Circle };
model.Axes.Add(new LinearAxis { Position = AxisPosition.Left, Title = "Class Marks" });
model.Axes.Add(new LinearAxis { Position = AxisPosition.Bottom, Title = "Frequency" });
for ( int i =0; i < numClasses; i++)
{
scatter.Points.Add(new ScatterPoint(classMarks[i], freqs[i]));
}
model.Series.Add(scatter);
//This doesn't update the graph even though I binded it in XAML code
this.MyModel = model;
}
else if ((bool)RelativeFqRBtn.IsChecked)
{
var model = new PlotModel { Title = "Relative Frequency Polygon" };
model.Axes.Add(new LinearAxis { Position = AxisPosition.Left, Maximum = 1, Title = "Class Marks" });
model.Axes.Add(new LinearAxis { Position = AxisPosition.Bottom, Title = "Frequency (%)" });
var relativeFQ = new LineSeries();
int frequencyTotal = 0;
for (int i=0; i < 8 && freqs[i] != -1 ; i++)
{
frequencyTotal += freqs[i];
}
for (int i = 0; i < numClasses; i++)
{
relativeFQ.Points.Add(new DataPoint(classMarks[i], freqs[i]/frequencyTotal ));
}
model.Series.Add(relativeFQ);
//This doesn't update the graph even though I binded it in XAML code
this.MyModel = model;
}
else if ((bool)CummuFqRBtn.IsChecked)
{
var model = new PlotModel { Title = "Cummulative Frequency Polygon" };
model.Axes.Add(new LinearAxis { Position = AxisPosition.Left, Title= "Frequency" });
model.Axes.Add(new LinearAxis { Position = AxisPosition.Bottom, Title ="Class Boundaries" });
var cummulativeFQ = new LineSeries();
double classBoundaries = new double[numClasses + 1];
double midpointDistance = (lowerLimits[1] - upperLimits[0]) / 2;
classBoundaries[0] = lowerLimits[0] - midpointDistance;
for (int i = 0; i < numClasses; i++)
{
classBoundaries[i + 1] = upperLimits[i] + midpointDistance;
}
cummulativeFQ.Points.Add(new DataPoint(classBoundaries[0], 0));
for (int i = 0; i< numClasses ; i++)
{
cummulativeFQ.Points.Add(new DataPoint(classBoundaries[i+1], freqs[i]));
}
model.Series.Add(cummulativeFQ);
//This doesn't update the graph even though I binded it in XAML code
this.MyModel = model;
}
else
{
var model = new PlotModel { Title = "Histogram" };
model.Axes.Add(new LinearAxis { Title = "Frequency", Position = AxisPosition.Left });
model.Axes.Add(new CategoryAxis
{
Title = "Class Marks",
ItemsSource = classMarksString
});
var histogram = new ColumnSeries();
model.Series.Add(histogram);
for (int i = 0; i < numClasses; i++)
{
histogram.Items.Add(new ColumnItem(freqs[i]));
}
//This doesn't update the graph even though I binded it in XAML code
this.MyModel = model;
}
}
}
c# wpf visual-studio oxyplot
c# wpf visual-studio oxyplot
asked Nov 24 '18 at 23:45
Evan GEvan G
64
64
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Fast fix for you code
Instead <oxy:PlotView Model="{Binding MyModel}"/>
use <oxy:PlotView x:Name="myPlot"/>
Remove public PlotModel MyModel { get; private set; }
and set Model directly after you created it.
myPlot.Model=model;
P.S. Don't use Binding if you don't know how it works
Thank you it works now :). I guess for next time i'll learn how binding works. I was kind of just copying the tutorial they have on the documentation and I don't know how to learn what's going on with the binding.
– Evan G
Nov 25 '18 at 4:47
But can you explain to me what's happening. How's using binding different from using x:Name?
– Evan G
Nov 25 '18 at 16:41
You need implement INotifyPropertyChanged pattern or make DependencyProperty.
– Alexei Shcherbakov
Nov 25 '18 at 18:12
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53463405%2foxyplot-graph-not-rendering-in-child-grid%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Fast fix for you code
Instead <oxy:PlotView Model="{Binding MyModel}"/>
use <oxy:PlotView x:Name="myPlot"/>
Remove public PlotModel MyModel { get; private set; }
and set Model directly after you created it.
myPlot.Model=model;
P.S. Don't use Binding if you don't know how it works
Thank you it works now :). I guess for next time i'll learn how binding works. I was kind of just copying the tutorial they have on the documentation and I don't know how to learn what's going on with the binding.
– Evan G
Nov 25 '18 at 4:47
But can you explain to me what's happening. How's using binding different from using x:Name?
– Evan G
Nov 25 '18 at 16:41
You need implement INotifyPropertyChanged pattern or make DependencyProperty.
– Alexei Shcherbakov
Nov 25 '18 at 18:12
add a comment |
Fast fix for you code
Instead <oxy:PlotView Model="{Binding MyModel}"/>
use <oxy:PlotView x:Name="myPlot"/>
Remove public PlotModel MyModel { get; private set; }
and set Model directly after you created it.
myPlot.Model=model;
P.S. Don't use Binding if you don't know how it works
Thank you it works now :). I guess for next time i'll learn how binding works. I was kind of just copying the tutorial they have on the documentation and I don't know how to learn what's going on with the binding.
– Evan G
Nov 25 '18 at 4:47
But can you explain to me what's happening. How's using binding different from using x:Name?
– Evan G
Nov 25 '18 at 16:41
You need implement INotifyPropertyChanged pattern or make DependencyProperty.
– Alexei Shcherbakov
Nov 25 '18 at 18:12
add a comment |
Fast fix for you code
Instead <oxy:PlotView Model="{Binding MyModel}"/>
use <oxy:PlotView x:Name="myPlot"/>
Remove public PlotModel MyModel { get; private set; }
and set Model directly after you created it.
myPlot.Model=model;
P.S. Don't use Binding if you don't know how it works
Fast fix for you code
Instead <oxy:PlotView Model="{Binding MyModel}"/>
use <oxy:PlotView x:Name="myPlot"/>
Remove public PlotModel MyModel { get; private set; }
and set Model directly after you created it.
myPlot.Model=model;
P.S. Don't use Binding if you don't know how it works
edited Nov 25 '18 at 15:18
answered Nov 25 '18 at 2:39
Alexei ShcherbakovAlexei Shcherbakov
32726
32726
Thank you it works now :). I guess for next time i'll learn how binding works. I was kind of just copying the tutorial they have on the documentation and I don't know how to learn what's going on with the binding.
– Evan G
Nov 25 '18 at 4:47
But can you explain to me what's happening. How's using binding different from using x:Name?
– Evan G
Nov 25 '18 at 16:41
You need implement INotifyPropertyChanged pattern or make DependencyProperty.
– Alexei Shcherbakov
Nov 25 '18 at 18:12
add a comment |
Thank you it works now :). I guess for next time i'll learn how binding works. I was kind of just copying the tutorial they have on the documentation and I don't know how to learn what's going on with the binding.
– Evan G
Nov 25 '18 at 4:47
But can you explain to me what's happening. How's using binding different from using x:Name?
– Evan G
Nov 25 '18 at 16:41
You need implement INotifyPropertyChanged pattern or make DependencyProperty.
– Alexei Shcherbakov
Nov 25 '18 at 18:12
Thank you it works now :). I guess for next time i'll learn how binding works. I was kind of just copying the tutorial they have on the documentation and I don't know how to learn what's going on with the binding.
– Evan G
Nov 25 '18 at 4:47
Thank you it works now :). I guess for next time i'll learn how binding works. I was kind of just copying the tutorial they have on the documentation and I don't know how to learn what's going on with the binding.
– Evan G
Nov 25 '18 at 4:47
But can you explain to me what's happening. How's using binding different from using x:Name?
– Evan G
Nov 25 '18 at 16:41
But can you explain to me what's happening. How's using binding different from using x:Name?
– Evan G
Nov 25 '18 at 16:41
You need implement INotifyPropertyChanged pattern or make DependencyProperty.
– Alexei Shcherbakov
Nov 25 '18 at 18:12
You need implement INotifyPropertyChanged pattern or make DependencyProperty.
– Alexei Shcherbakov
Nov 25 '18 at 18:12
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53463405%2foxyplot-graph-not-rendering-in-child-grid%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown