How to disable labels and lines from pie chart? - c#

actually I'm trying to remove the labels and the line which are leading to the labels. The app is in C# and I used OxyPlot to plot the piechart.
Here is my code. As you can see all what I've tried doesn't helped me.
Thanks for your help!
public class MyViewModel
{
public PlotModel MyModel { get; set; }
public MyViewModel()
{
SingletonItem singletonItem = SingletonItem.Instance;
PieSeries pieSeries = new PieSeries();
pieSeries.Slices.Add(new PieSlice("Done", singletonItem.Done) {IsExploded = true, Fill = OxyColors.PaleVioletRed});
pieSeries.Slices.Add(new PieSlice("Undone", singletonItem.Undone) {IsExploded = true});
pieSeries.LabelField = "";
pieSeries.TickDistance = 0;
pieSeries.ValueField = "";
MyModel = new PlotModel();
MyModel.IsLegendVisible = false;
MyModel.Series.Add(pieSeries);
}
}
Screenshot of the actually page

I solved this problem by myself. For everyone who need the same information for me these lines helped:
pieSeries.OutsideLabelFormat = "";
pieSeries.TickHorizontalLength = 0.00;
pieSeries.TickRadialLength = 0.00;

Related

Apply any kind of Updates on database immedieately C#

I have this program where I want to create a button base from Products on my database(ProductTbl). I found a way to do that
Here's the code:
public void DynamicButton() //Function for retrieving record and creating a button for each product
{
string select = "select ProductID,ProductDesc,ProductPrice,ProductPic from ProductTbl" ;
sda = new SqlDataAdapter(select,sqlConn);
sda.Fill(dataTable);
for (int i = 0; i < dataTable.Rows.Count; i++)
{
ExtendedButton prodBtn = new ExtendedButton(); //with ExtendedButton this time
prodBtn._itemName = dataTable.Rows[i][1].ToString();//this asigns the product name to the extended button
prodBtn._itemID = Convert.ToInt32(dataTable.Rows[i][0]);
prodBtn._myPrice = Convert.ToDecimal(dataTable.Rows[i][2]);
prodBtn.BackgroundImageLayout = ImageLayout.Stretch;
prodBtn.Click += new EventHandler(OnButtonClick);
prodBtn.Height = 100;
prodBtn.Width = 100;
System.Drawing.Font f1 = SystemFonts.DefaultFont;
prodBtn.Font = new System.Drawing.Font(f1.FontFamily,f1.Size,FontStyle.Bold);
prodBtn.Text = dataTable.Rows[i][1].ToString();
prodBtn.TextAlign = ContentAlignment.BottomCenter;
prodBtn.ForeColor = Color.White;
prodBtn.BackgroundImageLayout = ImageLayout.Zoom;
toolTip1.Show(prodBtn.Text, prodBtn);
byte[] image = (byte[])dataTable.Rows[i][3];
prodBtn.BackgroundImage = imgConverter.byteArrayToImage(image);
prodBtn.TextAlign = ContentAlignment.MiddleCenter;
flowPanel.Controls.Add(prodBtn);
}
}
//You can see this at codeproject
Now the problem is that whenever i add a product on that table using Stored procedure. I don't know how i can sync updates to the datatable that I use with this one. Any ideas and suggestion will be highly appreciated. Thanks sorry for the long post
You can use ASP.Net Caching with SqlCacheDependency.
See this page for details:
https://msdn.microsoft.com/en-us/library/ms178604.aspx

C# Displaying text box (present in the list view) value in a message box (wpf)

I have a list view in which each row contains 5 entries. The list box looks like this:
I need to store (display) the name of variable and the value present in "Physical value" column text box when i press OK button. For e.g. if i enter 45 in the physical value text box (only one row at a time) then the name of the variable and value "45" should be stored (displayed). I am able to retrieve the name of the variables but not the value of the text box.
My try:
This code will populate the list view with variables and bind it to the properties.
public void Populatevariables(IList<string> variables)
{
int rowcount = 0;
dataGrid.RowDefinitions.Clear();
dataGrid.ColumnDefinitions.Clear();
RowDefinition rd = new RowDefinition();
rd.Height = new GridLength();
dataGrid.RowDefinitions.Add(rd);
dataGrid.RowDefinitions.Add(new RowDefinition());
dataGrid.ColumnDefinitions.Add(new ColumnDefinition());
Label t1 = new Label();
t1.Content = "Variables";
Grid.SetColumn(t1, 0);
Grid.SetRow(t1, rowcount);
dataGrid.Children.Add(t1);
ListView VrblPopulateList = new ListView();
GridView g1 = new GridView();
g1.AllowsColumnReorder = true;
//l1.View = g1;
GridViewColumn g2 = new GridViewColumn();
g2.Header = "Name";
g2.Width = 200;
g2.DisplayMemberBinding = new Binding("Name");
g1.Columns.Add(g2);
GridViewColumn g5 = new GridViewColumn();
g5.Header = "DataType";
g5.Width = 200;
g5.DisplayMemberBinding = new Binding("DataType");
g1.Columns.Add(g5);
GridViewColumn g3 = new GridViewColumn();
g3.Header = "Current Value";
g3.Width = 200;
DataTemplate dt1 = new DataTemplate();
FrameworkElementFactory FF1 = new FrameworkElementFactory(typeof(TextBox));
FF1.SetBinding(TextBox.BindingGroupProperty, new Binding("Current_Value"));
FF1.SetValue(FrameworkElement.HeightProperty, Height = 30);
FF1.SetValue(FrameworkElement.WidthProperty, Width = 150);
dt1.VisualTree = FF1;
g3.CellTemplate = dt1;
g1.Columns.Add(g3);
GridViewColumn g6 = new GridViewColumn();
g6.Header = "Physical Value";
g6.Width = 200;
DataTemplate dt2 = new DataTemplate();
FrameworkElementFactory FF2 = new FrameworkElementFactory(typeof(TextBox));
FF2.SetBinding(TextBox.BindingGroupProperty, new Binding("Physical_Value"));
//FF2.AddHandler(TextBox.TextChangedEvent, txtchanged, true);
FF2.SetValue(FrameworkElement.HeightProperty, Height = 30);
FF2.SetValue(FrameworkElement.WidthProperty, Width = 150);
dt2.VisualTree = FF2;
g6.CellTemplate = dt2;
g1.Columns.Add(g6);
GridViewColumn g4 = new GridViewColumn();
g4.Header = "Action";
g4.Width = 200;
DataTemplate dt = new DataTemplate();
FrameworkElementFactory FF = new FrameworkElementFactory(typeof(Button));
FF.SetBinding(Button.BindingGroupProperty, new Binding("ToDo"));
FF.SetValue(FrameworkElement.HeightProperty,Height = 30);
FF.SetValue(FrameworkElement.WidthProperty, Width = 150);
FF.SetValue(System.Windows.Controls.Button.ContentProperty,"OK");
FF.AddHandler(Button.ClickEvent, new RoutedEventHandler(b1_click));
dt.VisualTree = FF;
g4.CellTemplate = dt;
g1.Columns.Add(g4);
VrblPopulateList.View = g1;
Grid.SetRow(VrblPopulateList, rowcount + 1);
dataGrid.Children.Add(VrblPopulateList);
for (int i = 0; i < variables.Count; i++)
{
Label lb1 = new Label();
lb1.Content = variables[i].Name;
Label lb2 = new Label();
lb2.Name = variables[i].datatype;
DataTemplate dd = new DataTemplate();
TextBox tb = new TextBox();
tb.Name = "TextBox" + i.ToString();
Button b1 = new Button();
VrblPopulateList.Items.Add(new User() { Name = lb1.Content, DataType = lb2.Name, Current_Value = tb, Physical_Value = tb, ToDo = b1 });
}
}
This code defines the property which is bind while populating:
public class User
{
public object Name { get; set; }
public string DataType { get; set; }
public Control Current_Value
{
get;
set;
}
public Control Physical_Value
{
get;
set;
}
public Control ToDo { get; set; }
}
At last this code will retrieve all the items when button is clicked.
private void b1_click(object sender, RoutedEventArgs e)
{
User item = (User)((Button)sender).DataContext;
TextBox t = (TextBox)item.Physical_Value;
MessageBox.Show(item.Name.ToString() + t.Text);
}
The text box value is always empty. I know it can be solved by adding handler to "text changed" event while populating. But i dont know how to do it. Please help.
As mentioned in the comment by #Ponas Justas i have to do following changes in my code:
Set the UpdateSourceTrigger property of the text box.
Change the user model accordingly.
After doing above changes my code looks like:
FF2.SetBinding(TextBox.BindingGroupProperty, new Binding("Physical_Value"));
FF2.SetBinding(TextBox.TextProperty, new Binding("PhysicalValueTxtChanged") { UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged });
User Model
public class User
{
public object Name { get; set; }
public string DataType { get; set; }
public Control Current_Value
{
get;
set;
}
public string _PhysicalValueTxtChanged = null;
public string PhysicalValueTxtChanged
{
get { return _PhysicalValueTxtChanged; }
set
{
_PhysicalValueTxtChanged = value;
}
}
public Control ToDo { get; set; }
}
After doing that the text in the text box can be easily stored by just modifying like this:
private void b1_click(object sender, RoutedEventArgs e)
{
User item = (User)((Button)sender).DataContext;
string txt = item.PhysicalValueTxtChanged;
MessageBox.Show(item.Name.ToString() + txt);
}
Thanks a lot Ponas Justas.

Clearing controls from a FlowLayoutPanel

I'm trying to remove the controls of the FlowLayoutPanel in the code that is attached below. The problem is that I use a search function in the same frame, so I can't find other objects without clearing it first. I already tried:
fPanelUpperMainScreen.Controls.Remove(a);
This is my method that I am working on.
public void GetArtistLayout()
{
ArtistInformation a = new ArtistInformation();
fPanelUpperMainScreen.Controls.Add(a);
int valueInt = int.Parse(tBMainScreen_Search.Text);
a.pictureBox1.ImageLocation = ar.GetArtist(valueInt).artistPic;
a.lblArtistInformation_ArtistName.Text = ar.GetArtist(valueInt).artistName;
var reviews = rr.getMatchingReviewsArtist(valueInt);
foreach (var review in reviews)
{
UserControl1 u = new UserControl1();
u.lblUser.Text = review.username;
u.lblComment.Text = review.comments;
u.lblDate.Text = review.date;
u.lblRating.Text = review.rating.ToString();
a.fpArtistInformation_Reviews.Controls.Add(u);
}
}

Looking for a better way to organize my charts in C#

So to try and give you an idea of what im working with, I have two drop down boxes. The first drop down box has a list of 4 Applications. The second drop down box changes dynamically with my selection in the first drop down box. I have a chart for each selection in the second drop down box. I have 16 charts total. Each time i change the second selection, the chart changes so that there is only one chart showing at a time. Im using if else statements and it has become way to hard to keep up with. I also have labels that i have to toggle with each chart so its getting out of control. Here is a small example of one of my else if statements.
else if (ddlApplication.SelectedItem.Text == "Rapp" && ddlTests.SelectedItem.Text == "Total Test Runs")
{
string query = string.Format("select TestName,Count (TestName) AS Counts from VExecutionGlobalHistory where TestTypeID = 2 group by TestName", ddlTests.SelectedItem.Value);
DataTable dt = GetData(query);
//Loop and add each datatable row to the Pie Chart Values
foreach (DataRow row in dt.Rows)
{
SpecificTestsRapp.PieChartValues.Add(new AjaxControlToolkit.PieChartValue
{
Category = row["TestName"].ToString(),
Data = Convert.ToDecimal(row["Counts"])
});
}
string SpecificTestsRappS = null;
string sql2 = "select Count (TestName) AS Counts from VExecutionGlobalHistory where TestTypeID = 2 ";
string connString2 = ";Initial Catalog=Performance;User ID=;Password=";
using (SqlConnection conn = new SqlConnection(connString2))
{
conn.Open();
using (SqlCommand command = new SqlCommand(sql2, conn))
{
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
SpecificTestsRappS = reader[0].ToString();
lblTotalTestsRapp.Text = SpecificTestsRappS;
break;
}
}
conn.Close();
}
SpecificTestsRapp.ChartTitle = "Total Tests Run";
TotalTestsWeb6.Visible = false;
HoursWeb6.Visible = false;
TotalValidationsWeb6.Visible = false;
CostComparisonWeb6.Visible = false;
SpecificTestsWeb6.Visible = false;
TotalTestsRapp.Visible = false;
TotalValidationsRapp.Visible = false;
SpecificTestsRapp.Visible = true;
HoursRapp.Visible = false;
IONChart.Visible = false;
CostComparisonRapp.Visible = false;
txtTotalHoursRapp.Visible = false;
lblTotalHoursRapp.Visible = false;
txtTotalHoursRappA.Visible = false;
lblTotalHoursRappA.Visible = false;
txtTotalCostRappM.Visible = false;
lblTotalCostRappM.Visible = false;
txtTotalCostRappA.Visible = false;
lblTotalCostRappA.Visible = false;
txtTotalTestsRapp.Visible = true;
lblTotalTestsRapp.Visible = true;
lblTotalValidationsRapp.Visible = false;
txtTotalValidationsRapp.Visible = false;
lblTotalValidations.Visible = false;
txtTotalValidations.Visible = false;
lblTotalTests.Visible = false;
txtTotalTests.Visible = false;
txtTotalHours.Visible = false;
lblTotalHours.Visible = false;
txtTotalHoursA.Visible = false;
lblTotalHoursA.Visible = false;
txtTotalCostA.Visible = false;
lblTotalCostA.Visible = false;
txtTotalCostM.Visible = false;
lblTotalCostM.Visible = false;
Label1.Visible = false;
Label2.Visible = false;
}
Technically i will need 16 of these when im done since i have 16 charts. There has to be a more efficient way to do this. Any suggestions?
I'm thinking Startegy and Factory design patterns.
You could do something in the lines of:
In your UI:
var chartStartegyFactory = new ChartStrategyFactory();
var chartStategy = chartStartegyFactory.Create(ddlApplication.SelectedItem.Text, ddlTests.SelectedItem.Text);
var chart = chartStategy.CreateChart();
lblTotalTestsRapp.Text = chart.ChartData;
SpecificTestsRapp.ChartTitle = chart.ChartTitle;
TotalTestsWeb6.Visible = chart.TotalTestsWeb6Visible;
// continue assigning properties in your UI
In your business layer:
public class ChartStrategyFactory
{
public IChartStrategy Create(string application, string test)
{
if (application == "Rapp" && test == "Total Test Runs")
return new RappTotalTestsRunChartStrategy();
// add strategies for other charts
throw new NotSupportedException();
}
}
public interface IChartStrategy
{
Chart CreateChart();
}
public class Chart
{
public string ChartTitle { get; set; }
public string ChartData { get; set; }
public bool TotalTestsWeb6Visible { get; set; }
// create all properties you need
}
public class RappTotalTestsRunChartStrategy : IChartStrategy
{
public Chart CreateChart(){
Chart chart = new Chart();
chart.ChartData = GetDataFromDatabase();
chart.ChartTitle = "Your Chart Title";
chart.TotalTestsWeb6Visible = false;
// continue assigning properties
return chart;
}
}
Basically your Chart creation code will be encapsulated in each Startegy and you will gain extensibility (it suffices to modify your ChartStrategyFactory when a new chart is developed.

Dynamic names for panels

I'm working on a program that is supposed to use with multiple users. I'm getting the users from a database. So far so good...
I'm dynamically adding a panel to the form, which would hold some data. I'm using this piece of code to achieve that:
string panel_name = "email_in_" + user_credentials[counter_snel][0];
Panel new_user_panel = new Panel();
new_user_panel.AutoSize = true;
new_user_panel.Dock = System.Windows.Forms.DockStyle.Fill;
new_user_panel.Location = new System.Drawing.Point(0, 0);
new_user_panel.Name = panel_name;
new_user_panel.Visible = false;
Label new_item = new Label();
new_item.AutoSize = true;
new_item.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
string new_item_text = string.Format("{0,0}\n{1,0}\n{2,0}", user_credentials[counter_snel][1], user_credentials[counter_snel][2],user_credentials[counter_snel][9]);
new_item.Text = new_item_text;
splitContainer2.panel_name.Controls.Add(new_item);
as you will probably notice, this line of code is not working:
splitContainer2.panel_name.Controls.Add(new_item);
How can I achieve this, so when I want to make panel email_in_1 visible, I can use that, and make email_in_8 not visible?
### EDIT 1 ###
the code now looks like this:
string panel_name = "email_in_" + user_credentials[counter_snel][0];
Panel new_user_panel = new Panel();
new_user_panel.AutoSize = true;
new_user_panel.Dock = System.Windows.Forms.DockStyle.Fill;
new_user_panel.Location = new System.Drawing.Point(0, 0);
new_user_panel.Name = panel_name;
new_user_panel.Visible = true;
user_email_in_panels.Add(new_user_panel);
splitContainer2.Panel1.Controls.Add(new_user_panel);
Label new_item = new Label();
new_item.AutoSize = true;
new_item.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
string new_item_text = string.Format("{0,0}\n{1,0}\n{2,0}", user_credentials[counter_snel][1], user_credentials[counter_snel][2],user_credentials[counter_snel][9]);
new_item.Text = new_item_text;
int aantal_panels = 0;
foreach (Panel panel in user_email_in_panels) {
aantal_panels++;
}
int counter_1 = 0;
foreach (Panel panel in user_email_in_panels) {
if(counter_1 == (aantal_panels -1)){
MessageBox.Show("We are here");
panel.Controls.Add(new_item);
splitContainer1.Panel1.Controls.Add(panel);
}
counter_1++;
}
But somehow, i don't see any labels shown in the form... am i missing something? The messsagebox with the text We are Here is shown, so it come's to the add statement....
and i've got an another question besides my first question. My question is, how can i make the counter of the list better?
### Edit for Sean Vaughn
i've updated it to this:
public class MyPanelClass {
public string Name {
get;
set;
}
public bool Visible {
get;
set;
}
public string YourLabelsText {
get;
set;
}
}
Label new_item = new Label();
new_item.AutoSize = true;
new_item.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
string new_item_text = string.Format("{0,0}\n{1,0}\n{2,0}", user_credentials[counter_snel][1], user_credentials[counter_snel][2], user_credentials[counter_snel][9]);
new_item.Text = new_item_text;
Panel base_panel = new Panel(); //this is your base panel, you don't need to add this line of code because Visual Studio will do that for you.
List<MyPanelClass> myPanelList = new List<MyPanelClass>(); //this will keep records of all of your panels.
MyPanelClass panel_name = new MyPanelClass();
panel_name.Name = "email_in_" + user_credentials[counter_snel][0]; ;
panel_name.Visible = true;
panel_name.YourLabelsText = string.Format("{0,0}\n{1,0}\n{2,0}", user_credentials[counter_snel][1], user_credentials[counter_snel][2], user_credentials[counter_snel][9]);
//Now add the new created panel to the list.
myPanelList.Add(panel_name);
base_panel.Name = myPanelList[counter_snel].Name;
base_panel.Visible = myPanelList[counter_snel].Visible; //You probably don't need this because base_panel will always be visible
new_item.Text = myPanelList[counter_snel].YourLabelsText;
But i still doesn't see anything...
Does it matter that it execudes in an public void??? i don't think so, but it want to elliminate all the possibilities...
Use a List that will keep track of all the panels.
List<Panel> myPanels = new List<Panel>();
Whenever you need to add a panel, do this:
mypanels.Add(yourPanel);
Now, for the other problem, create a function like this:
private void HideAllOtherPanels(List<Panel> panelList, Int index /*the index of the panel you want to show*/)
{
foreach(Panel panel in panelList)
if(panel.Visible) panel.Hide();
panelList[index].Show();
}
I see you are creating a lot of new panels. My advice, don't do that, you're giving a lot of load to the system.
Instead make a class.
public class MyPanelClass
{
public string Name
{
get; set;
}
public bool Visible
{
get; set;
}
public string YourLabelsText
{
get; set;
}
}
After creating the class, create a base_panel on your form that will change it's contents based on the your intentions.
And then, all you need to do is change the panel's content rather than creating a new Panel. Store the other panels data in a List<MyPanelClass>.
So you'll be doing this:
Panel base_panel = new Panel(); //this is your base panel, you don't need to add this line of code because Visual Studio will do that for you.
List<MyPanelClass> myPanelList = new List<MyPanelClass>(); //this will keep records of all of your panels.
MyPanelClass panel_name = new MyClassPanel();
panel_name.Name = "email_in_" + user_credentials[counter_snel][0];;
panel_name.Visible = false;
panel_name.YourLabelsText = string.Format("{0,0}\n{1,0}\n{2,0}", user_credentials[counter_snel][1], user_credentials[counter_snel][2],user_credentials[counter_snel][9]);
//Now add the new created panel to the list.
myPanelList.Add(panel_name);
Now whenever you need to activate a stored Panel, just do this:
base_panel.Name = myPanelList[yourPanelsIndex].Name;
base_panel.Visible = myPanelList[yourPanelsIndex].Visible; //You probably don't need this because base_panel will always be visible
yourLabel.Text = myPanelList[yourPanelsIndex].YourLabelsText;
This code is much less bulky on the machine and is easy to handle too.
I'd try something like this to add an item.
var panel = splitContainer2.Controls.FirstOrDefault(p => p is Panel && ((Panel)p).Name == panel_name);
if(panel != nul)
{
panel.Controls.Add(new_item);
}
To hide a particular panel given panel_name:
foreach(var control in splitContainer2.Controls)
{
if(control is Panel)
{
((Panel)control).Visible = ((Panel)control).Name == panel_name;
}
}

Categories

Resources