Using a variable to access a chart - c#

I have a C# WinForms application that has four chart controls used to graphically show some analysis results.
I have the code working for each graph, however in an attempt to be more efficient & re-use code I've defined a code block to:
create the required series,
extracts the data from a database & assigns the results to the appropriate series
add the series to the chart
customise the charts appearance.
All of the above is done dynamically as the data does not exist at design time.
The working code I am looking to re-use is:
// Add both series to the chart.
ChartName.Series.AddRange(new Series[] { series1, series2 });
// Cast the chart's diagram to the XYDiagram type, to access its axes.
XYDiagram diagram = (XYDiagram)ChartName.Diagram;
I'd like to change the ChartName object to a variable that I can pass each of the charts in order to re-use the code. Something like (note this does not work):-
var VChart = this.Controls.Find(ChartName, true);
// Add both series to the chart.
VChart.Series.AddRange(new Series[] { series1, series2 });
// Cast the chart's diagram to the XYDiagram type, to access its axes.
XYDiagram diagram = (XYDiagram)VChart.Diagram;
Any ideas, hints, tips, etc on how-to pass a variable into the ChartName would be appreciated.
Full Code:
void Generate_Chart()
{
// Create two stacked bar series.
Series series1 = new Series("Data", ViewType.Bar);
Series series2 = new Series("Ben", ViewType.Line);
try
{
using (var cmd = new SQLiteCommand(m_dbConnection))
for (int i = LoopMin; i < LoopMax; i++)
{
// Retrieve the actual calculated values from the database
cmd.CommandText = "SELECT " + Chart_SourceActualValue + " FROM " + Chart_SourceTable + " WHERE Value = " + i + "";
Chart_SeriesA_Value = Convert.ToInt32(cmd.ExecuteScalar());
// Retrieve the expected values from the database
cmd.CommandText = "SELECT " + Chart_BenExpValue + " FROM " + Chart_SourceTable + " WHERE Value = " + i + "";
Chart_SeriesB_Value = Convert.ToInt32(cmd.ExecuteScalar());
// Add the dynamically created values to a series point for the chart
series1.Points.Add(new SeriesPoint(i, Chart_SeriesA_Value));
series2.Points.Add(new SeriesPoint(i, Chart_SeriesB_Value));
}
}
catch (Exception)
{
throw;
}
// Add both series to the chart.
//this.Controls.Find(varChart, true)
ChartName.Series.AddRange(new Series[] { series1, series2 });
// Remove the GridLines from the chart for better UI
// Cast the chart's diagram to the XYDiagram type, to access its axes.
XYDiagram diagram = (XYDiagram)ChartName.Diagram;
// Customize the appearance of the axes' grid lines.
diagram.AxisX.GridLines.Visible = false;
}
}

It sounds like you're asking to replace the hardcoded ChartName with a variable so that you can call your routine four different times, each time with a different chart. I've taken your code and replaced some of the global variable of your chart control and settings and and made them parameters you pass into the function:
void Generate_Chart(DevExpress.XtraCharts.ChartControl chartCtrl,
string chart_sourceActualValue,
string chart_sourceTable,
string chart_benExpValue
)
{
// Create two stacked bar series.
Series series1 = new Series("Data", ViewType.Bar);
Series series2 = new Series("Ben", ViewType.Line);
try
{
using (var cmd = new SQLiteCommand(m_dbConnection))
for (int i = LoopMin; i < LoopMax; i++)
{
// Retrieve the actual calculated values from the database
cmd.CommandText = "SELECT " + sourceActualValue + " FROM " +
chart_sourceTable + " WHERE Value = " + i + "";
Chart_SeriesA_Value = Convert.ToInt32(cmd.ExecuteScalar());
// Retrieve the expected values from the database
cmd.CommandText = "SELECT " + chart_benExpValue + " FROM " +
chart_sourceTable + " WHERE Value = " + i + "";
Chart_SeriesB_Value = Convert.ToInt32(cmd.ExecuteScalar());
// Add the dynamically created values
// to a series point for the chart
series1.Points.Add(new SeriesPoint(i, Chart_SeriesA_Value));
series2.Points.Add(new SeriesPoint(i, Chart_SeriesB_Value));
}
}
catch (Exception)
{
throw;
}
// Add both series to the chart.
chartCtrl.Series.AddRange(new Series[] { series1, series2 });
// Remove the GridLines from the chart for better UI
// Cast the chart's diagram to the XYDiagram type, to access its axes.
XYDiagram diagram = (XYDiagram)chartCtrl.Diagram;
// Customize the appearance of the axes' grid lines.
diagram.AxisX.GridLines.Visible = false;
}
}
Then, you end up calling this method like this using the original values as arguments:
void Generate_Chart(ChartName, Chart_SourceActualValue, Chart_SourceTable,
Chart_BenExpValue);
// call it three other times passing in the different specifics for that chart. e.g.
void Generate_Chart(SomeOtherChartName, SomeOtherChart_SourceActualValue,
SomeOhterChart_SourceTable, SomeOtherChart_BenExpValue);
.....

Related

My database query is only querying the last element of my array

My problem in the title i have allcodes array and codes TextBox (kodTxtBox)
i will split textbox like line per element and querying all elements with for loop then
when i run it, it shows the query of only the last element of the allcodes array with the
messagebox, but the others go into else and giving error message box
some turkish words in my codes so.
aciklama = description
birim = monad
birimFiyat = Price per 1 unit
ürünler = products
ürünler.sipariskod = products.ordercode etc.
i did a lot of ways for this i used foreach all variables type is string
allCodes = kodTxtBox.Text.Split('\n');
for (int i = 0; i < allCodes.Length; i++)
{
queryString = "SELECT ürünler.siparisKod, ürünler.aciklama, ürünler.birim, ürünler.fGrup, ürünler.birimfiyat FROM ürünler WHERE (((ürünler.siparisKod)=\"" + allCodes[i] + "\"));";
using (OleDbCommand query = new OleDbCommand(queryString))
{
query.Connection = connection;
reader = query.ExecuteReader();
if (reader.Read())
{
MessageBox.Show(allCodes[i] + " Succesful");
var desc = reader["aciklama"].ToString();
var monad = reader["birim"].ToString();
var sellPrice = reader["birimFiyat"].ToString();
MessageBox.Show("Açıklama: " + desc + " Birim: " + monad + " Satış Fiyatı: " + sellPrice);
reader.Close();
}
else
{
MessageBox.Show("Hata");
}
}
}
I solved the problem by making a single query instead of multiple queries. I saved the values ​​returned in each single query into a list and at the end I made the necessary for loop using the elements of the list

Listview ForeColor per item

This application is a message board type of app. You post a message, which is stored in an Oracle database. The user's photo, name, date and message are displayed in a listview. Everything works however a feature request for font color changing came in. Now testing things below I know I can display ALL of the messages as a certain color.
What I'm going to do is add a 'color' column to my table. Then if a user selects the text of their message to be 'red' for example, I am going to store the hex color for red in the color column of the table for that post.
So what I'm trying to figure out is when that message is retrieved, how I can set this code to 'If color column empty, post message as black, else if hex color exist for each message post that particular message in that color' Without changing the color of every message to red as the code below currently does.
List<MsgBoard> Messages = MsgBoard.find_ActiveByBoardName(Convert.ToString(cmbgroup.SelectedItem));
int i = 0;
imageList1.Images.Clear();
foreach (MsgBoard m in Messages)
{
AddImages(m.EmpPic);
ListViewItem Message = new ListViewItem("", i);
if (m.AlertNo == 0) //Default Message Post (non Alert)
{
Message.UseItemStyleForSubItems = false;
Message.SubItems.Add(m.EmpName.First);
Message.SubItems.Add(m.MessageText + Environment.NewLine).ForeColor = Color.Red;
Message.SubItems.Add(m.PostDate.ToString());
Message.SubItems.Add(m.EmpName.EmpNo.ToString());
Message.SubItems.Add(m.EmpName.Name);
listView1.Items.Add(Message);
}
i++;
}
}
Just to clarify. If 20 people post a message and all 20 select a different color to post their message in. So in the DB all 20 rows have a different color in the DB column, I need to have those messages displayed in listview individually based on the color they posted as. I currently am only aware of applying a single color to all messages in listview as shown in my example above.
Looks like thanks to Stephan, once I add
Message.SubItems.Add(m.MessageText + Environment.NewLine).ForeColor = System.Drawing.ColorTranslator.FromHtml(m.Color);
I then need help adjusting my query to add the color once colors are added to the color column. Here is that code.
Had DB admin add MESSAGE_COLOR varchar column.
I edit this below to existing code, for the newly created column MESSAGE_COLOR when posting..
public string Create()
{
try
{
OleDbDataReader result = Database.Conn.Execute(
"INSERT INTO MPCS.MEYER_BOARD (" +
"EMPLOYEE_ID, " +
"POST_DATE, " +
"BOARD_NAME," +
"ALERT_NO," +
"MESSAGE_TEXT," +
"MESSAGE_COLOR," +
"ACTIVE_FLAG" +
") VALUES (?,SYSDATE,?,?,?,?)",
new List<OleDbParameter> {
new OleDbParameter("EMPLOYEE_ID",EmpName.EmpNo),
new OleDbParameter("BOARD_NAME",BoardName),
new OleDbParameter("ALERT_NO",AlertNo),
new OleDbParameter("MESSAGE_TEXT",MessageText),
new OleDbParameter("MESSAGE_COLOR",MessageColor),
new OleDbParameter("ACTIVE_FLAG",ActiveFlag ? "Y" : "N")
}, Query.ReaderType.Reader);
result.Read();
result.Close();
return null;
}
catch (Exception ex)
{
return ex.Message;
}
}
I then editing DB read w/ the new column as well.
public static MsgBoard DBRead(OleDbDataReader result,string alias=null)
{
return new MsgBoard
{
EmpName = Employee.DBRead(result, "EMPLOYEE"),
MessageText = result[alias + "MESSAGE_TEXT"].ToString(),
MessageColor = result[alias + "MESSAGE_COLOR"].ToString(),
BoardName = result[alias +"BOARD_NAME"].ToString(),
AlertNo = (int)(decimal)result[alias +"ALERT_NO"],
PostDate = (DateTime)result[alias +"POST_DATE"],
ActiveFlag = result[alias +"ACTIVE_FLAG"].ToString().ToString() == "Y",
EmpPic = ImageResource.DBRead(result, "IR")
};
}
But do I need to edit my build_query?
public static String build_query(String where, string OrderBy = null)
{
List<String> cols = new List<String>();
cols.AddRange(db_columns.ConvertAll(c => "MPCS.MEYER_BOARD." + c + " AS MBOARD_" + c));
cols.AddRange(Employee.db_columns.ConvertAll(c => "MPCS.EMPLOYEE." + c + " AS EMPLOYEE_" + c));
cols.AddRange(ImageResource.db_columns.ConvertAll(c => "MPCS.IMAGE_RESOURCE." + c + " AS IR_" + c));
String sql =
"SELECT " + String.Join(", ", cols) + " " +
"FROM MPCS.MEYER_BOARD " +
"LEFT OUTER JOIN MPCS.EMPLOYEE " +
"ON MPCS.MEYER_BOARD.EMPLOYEE_ID=MPCS.EMPLOYEE.EMPLOYEE_ID " +
"LEFT OUTER JOIN MPCS.IMAGE_RESOURCE " +
"ON MPCS.IMAGE_RESOURCE.IR_ID=MPCS.EMPLOYEE.IMAGE_RESOURCE_ID " +
"WHERE ";
sql += where;
sql += OrderBy;
return sql;
}
UPDATE
I've gotten everything coded and I am submitting the color to the DB now. However when displaying the listview below each post still just has a default black text color. Rather than the color saved for example as "#FFF000" being converted and displayed.
foreach (MsgBoard m in Messages)
{
AddImages(m.EmpPic);
ListViewItem Message = new ListViewItem("", i);
if (m.AlertNo == 0) //Default Message Post (non Alert)
{
Message.SubItems.Add(m.EmpName.First);
Message.SubItems.Add(m.MessageText + Environment.NewLine).ForeColor = System.Drawing.ColorTranslator.FromHtml(m.MessageColor);
Message.SubItems.Add(m.PostDate.ToString());
Message.SubItems.Add(m.EmpName.EmpNo.ToString());
Message.SubItems.Add(m.EmpName.Name); //Displayed only on 'Show Details'
listView1.Items.Add(Message);
}
Final update
Turns out the last piece I was missing was to change the listview property UseItemStyleForSubItems = false; now its working.
You can convert your hex value (assuming that it is stored as a string in the format "#RRGGBB") into a color using ColorTranslator
Using this approach, you can also use named colors (like "Violet")
Message.SubItems.Add(m.MessageText + Environment.NewLine).ForeColor =
System.Drawing.ColorTranslator.FromHtml(m.Color); //sth. like "#FFCC66"
Furthermore you have to retrieve the value of the Color column. It seems that you have to add "MESSAGE_COLOR" (or whatever your column is called) to your db_columns. When you got the values from the DB, you have to map the column to the property of your Message:
return new MsgBoard
{
// ...
Color = System.Drawing.ColorTranslator.FromHtml([alias+"MESSAGE_COLOR"]),
// ...
}
(In other words: You already save it to db, now you also have to read it from DB)

labeling a legend in epplus

I'm trying to create a graph from some data I export to an .xls file. I create the graph just fine, but I'm going to have anywhere from 3 - 7 lines on the graph. I added the legend to make it easier to read, but the legend has no text on it. It just shows a line without a label. Is there anyway to label the legend key?
Here's the code of the graph I'm making:
var lineChart = ws.Drawings.AddChart(item.TestHeader, eChartType.Line) as ExcelLineChart;
lineChart.SetSize(800, 400);
lineChart.Series.Add(Char.ConvertFromUtf32(65 + distanceCount + 1).ToString() + "1:" + Char.ConvertFromUtf32(65 + distanceCount + 1).ToString() + (testCount.ToString()), "A1:A" + (testCount.ToString()));
lineChart.Title.Text = item.TestHeader;
lineChart.DataLabel.ShowSeriesName = true;
lineChart.DataLabel.ShowLegendKey = true;
lineChart.DataLabel.ShowLeaderLines = true;
lineChart.XAxis.Title.Text = "Inches from Decoder";
lineChart.YAxis.Title.Text = "Milliseconds to decode";
lineChart.YAxis.MaxValue = 1000;
Any help is better than none, thanks!
Your example generated a series name in the legend for me named "Series". But you can set it explicitly using
lineChart.Series[0].Header = "Series 1 Name";

Adding controls programmatically

I am creating ASP.NET wep page. I have a world map and I want to add some image buttons (cities) controls in C#. I made the method:
I am using stored procedure to get data from database but when I add next city to the procedure the previously added imagebutton changes its position.
private void LocateCities()
{
IDBManager dbManager = new DBManager(DataProvider.SqlServer);
dbManager.ConnectionString = #"Data Source=server; Initial Catalog=db; Integrated Security = SSPI;";
try
{
dbManager.Open();
dbManager.CreateParameters(2);
dbManager.AddParameters(0, "#Function", "All");
dbManager.AddParameters(1, "#Team", "All");
DataSet ds = new DataSet("Stuff");
ds = dbManager.ExecuteDataSet(CommandType.StoredProcedure, "sp_select_staff_and_cities");
foreach (DataRow dr in ds.Tables[0].Rows)
{
int xaxis = Convert.ToInt32(dr["xaxis"]) ;
int yaxis = Convert.ToInt32(dr["yaxis"]) ;
int textxaxis = xaxis + 30;
int textyaxis = yaxis - 10;
ImageButton btnCity = new ImageButton();
btnCity.ImageUrl = "~/Images/cyanball1.gif";
btnCity.Height = 10;
btnCity.Attributes.Add("style", "Z-INDEX:100; POSITION:relative; left:" + xaxis + "px; TOP:" + yaxis + "px; Left:10px;Right:10px");
Label lblCity = new Label();
lblCity.Text = dr["city"].ToString();
lblCity.Attributes.Add("style", "Z-INDEX: 100;POSITION:relative; left:" + textxaxis + "px; TOP:" + textyaxis + "px");
PanelMap.Controls.Add(lblCity);
PanelMap.Controls.Add(btnCity);
}
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
finally
{
dbManager.Dispose();
}
}
I am using panel to keep image with map:
.PanelMap
{
width:960px;
height:572px;
text-align:left;
}
What should be changed in above code to keep the points in its place?
I tried to use position:absolute but it cause that posiotion is derived relative to the page and I would like it would be derived relatively to the panel control.
The problem would be this, POSITION:relative, so change that to POSITION:absolute on both lines and you'll be good.

Programatically changing field order in Sharepoint 2007 list

I'm adding in two new fields into an already existing Sharepoint list programmatically through a feature. The fields are being added successfully but I have been unable to adjust the column order.
This task is done simply through the UI by going to List Settings and then Column Ordering, but I have been unable to achieve the task programmatically.
Through some research I've seen that you can use the SPContentType of the form to change the ordering of the FieldLinks (as follows):
SPList list = web.Lists["Example List"];
if (list.ContentTypes.Count > 0) {
SPContentType ct = list.ContentTypes[0];
string[] names = {"Example_x0020_One", "Example_x0020_Two", "Example_x0020_Three"};
ct.FieldLinks.Reorder(names);
ct.Update();
}
In this example, I the list would already have "Example One" and "Example Three" columns, and I would add "Example Two" later and then try to order them.
However this approach did not work for me, so if anyone has input on it, that would be appreciated.
The next item I saw is manually changing the SchemaXml of the list to have the proper order of the fields, but I wanted to see if this was the best method.
Any input would be appreciated, thank you for your help.
I took a look at the source of the Column ordering page (formEdt.aspx), it looks like they use web services, not the object model:
function DoBuildAndSubmit()
{
var numFound, currentSelect, selectValue;
var form = document.forms.aspnetForm;
var numFields = form["numSelects"].value;
var xml = "<Fields>";
numFound = 0;
while(numFound < numFields)
{
for(x = 0; x < numFields; x++)
{
currentSelect = form["FormPosition" + x];
if(currentSelect.selectedIndex == numFound)
{
selectValue = currentSelect.options[numFound].value;
xml = xml + "<Field Name=\"" + selectValue + "\"/>" + "\n";
numFound++;
}
}
}
for(x = numFields ; x < 67; x++)
xml = xml + "<Field Name=\"" + form["FormPosition" + x].value + "\"/>" + "\n";
xml = xml + "</Fields>";
document.frmLayoutSubmit["ReorderedFields"].value=xml;
document.frmLayoutSubmit.action = "http://local/_vti_bin/owssvr.dll?CS=65001";
document.frmLayoutSubmit.submit();
}
Now, it might be possible to do through the object model, but I don't have a good feeling about it when the UI is punting.
Here's a powershell version:
# Moves "FieldToBeMoved" after "Description" field
$list = $web.Lists["Documents"]
$ct = $list.ContentTypes[0] # Or find the desired CT
$newOrder = #()
foreach ($field in $ct.Fields)
{
if ($field.StaticName -ne "FieldToBeMoved")
{
$newOrder += $field.StaticName
}
if ($field.StaticName -eq "Description")
{
$newOrder += "FieldToBeMoved"
}
}
$ct.FieldLinks.Reorder($newOrder)
$ct.Update();
I used the code from your answer, except I programmatically examined the content types and fields for the list I wanted to re-order.
//Step 1 (optional): List out the content types and fields for your list to see what is in the list
SPList list = web.Lists[strListName];
string strRet="";
foreach (SPContentType spct in list.ContentTypes)
{
strRet += "<strong>Content Type: </strong>" + spct.Name + ", <strong>Fields</strong>: <br />";
foreach (SPField field in spct.Fields)
{
if (strFieldInfo != "")
{
strFieldInfo += ", ";
}
strFieldInfo += "\"" + field.StaticName + "\"";
}
strRet += strFieldInfo + "<br />-----<br />";
}
//Output the results
lblOutput.Text = strRet;
Now, you'll have an idea of how many content types your list has and what fields are in the list.
By default, if content type management is not enabled, you'll have one content type that has all the fields.
Sample output from the above code:
Content Type: Event, Fields:
"ContentType", "Title", "Location", "EventDate", "EndDate", "Description", "fAllDayEvent", "fRecurrence", "WorkspaceLink", "EventType", "UID", "RecurrenceID", "EventCanceled", "Duration", "RecurrenceData", "TimeZone", "XMLTZone", "MasterSeriesItemID", "Workspace", "Course", "CourseLocation"
Next Step 2 is to change the order of the content type. You can cut and paste from the output from step 1, re-order it, and add "{" and "};" around it to create the string array for the ordering you want.
if (list.ContentTypes.Count > 0)
{
SPContentType ct = list.ContentTypes[0]; //Specify the content type here, if you have more than one content type in your list.
string[] fieldnames = { "ContentType", "Title", "Course", "CourseLocation", "EventDate", "EndDate", "Description", "fAllDayEvent", "fRecurrence", "WorkspaceLink", "EventType", "UID", "RecurrenceID", "EventCanceled", "Duration", "RecurrenceData", "TimeZone", "XMLTZone", "MasterSeriesItemID", "Workspace", "Location"};
ct.FieldLinks.Reorder(fieldnames);
web.AllowUnsafeUpdates = true;
ct.Update(true);
web.AllowUnsafeUpdates = false;
}

Categories

Resources