MSChart X-Axis Label is not displayed - c#

I'm currently building an application that shows a line-graph and uses MSChartControl.
Here is the relevant code:
WeatherDatabase _db = new WeatherDatabase(Properties.Settings.Default.SqlConnectionString);
private void AddSensorDataToGraph(int sensorType, string xTitle, string yTitle, string Title)
{
var sensorIdList = (from d in _db.Sensors where d.TypeId == sensorType select d.Id).ToArray();
var startDate = dateTimePickerStart.Value;
var stopDate = dateTimePickerStop.Value;
SetMaxMinValues(dateTimePickerStart.Value.ToOADate(), dateTimePickerStop.Value.ToOADate());
this.Text = chartValues.Titles["Titel"].Text = String.Format("{0} vom {1} bis {2}", Title, startDate, stopDate);
chartValues.ChartAreas[CHARTAREA].AxisX.Title = xTitle;
chartValues.ChartAreas[CHARTAREA].AxisY.Title = yTitle;
// Alte Datensätze löschen
chartValues.Series.Clear();
foreach (var sensorId in sensorIdList)
{
var values = (from d in _db.DeviceIO where d.IdSensor == sensorId && d.Timestamp > startDate && d.Timestamp < stopDate orderby d.Timestamp ascending select d).ToArray();
if (values.Length > 0)
{
var desc = values[0].Sensors.SensorType.Description;
chartValues.Series.Add(GenerateDefaultSeries(String.Format("{0}\r\nSensorId: {1}", desc, sensorId)));
//chartValues.Series.Add("Test" + sensorId);
int seriesId = chartValues.Series.Count - 1;
foreach (var item in values)
{
if (item.Value == null)
{
continue;
}
if ((double)item.Value == NOVALUE)
{
continue;
}
AddPointToChart((DateTime)item.Timestamp, (double)item.Value, seriesId);
}
}
}
}
private void AddPointToChart(DateTime timestamp, double value, int series)
{
DataPoint dataPoint = chartValues.Series[series].Points.Add(value);
dataPoint.XValue = timestamp.ToOADate();
dataPoint.AxisLabel = "dd.MM.yy hh:mm";
}
private void SetMaxMinValues(double min, double max)
{
if (min < max)
{
chartValues.ChartAreas[CHARTAREA].AxisX.Minimum = min;
chartValues.ChartAreas[CHARTAREA].AxisX.Maximum = max;
}
}
private Series GenerateDefaultSeries(string Name)
{
// Grundeinstellungen für die Data-Series setzen
Series series = new Series();
series.ChartType = SeriesChartType.Line;
series.Name = Name;
series.IsValueShownAsLabel = false;
series.IsVisibleInLegend = true;
series.IsXValueIndexed = false;
series.AxisLabel = "dd.MM.yy hh:mm";
// series.LabelFormat = "g";
series.XAxisType = AxisType.Primary;
series.XValueType = ChartValueType.Auto;
series.YValuesPerPoint = 1;
// series.AxisLabel = "dd.MM.yy hh:mm";
return series;
}
If i add a series in designer mode x-axies Labels are correct
Click for pic
But if i start my application and add a series programmatically it loks like this
Anyone knows why the x-axis caption / label is not displayed correctly?
EDIT:
Based on Tom's help I change some things:
var seriesName = String.Format("{0}\r\nSensorId: {1}", desc, sensorId);
var curSeries = chartValues.Series.Add(seriesName);
UpdateSeriesSettings(ref curSeries);
[...]
AddPointToChart((DateTime)item.Timestamp, (double)item.Value, curSeries);
[...]
private void UpdateSeriesSettings(ref Series series)
{
// Grundeinstellungen für die Data-Series setzen
// Series series = new Series();
series.ChartType = SeriesChartType.Spline;
series.IsValueShownAsLabel = false;
series.IsVisibleInLegend = true;
series.IsXValueIndexed = false;
series.AxisLabel = "dd.MM.yy hh:mm";
// series.LabelFormat = "g";
series.XAxisType = AxisType.Primary;
series.XValueType = ChartValueType.Auto;
series.YValuesPerPoint = 1;
// series.AxisLabel = "dd.MM.yy hh:mm";
}
But my X axis stil ain't having a label ):
Used the following code:
Fixed my problem.
var desc = values[0].Sensors.SensorType.Description;
var seriesName = String.Format("{0}\r\nSensorId: {1}", desc, sensorId);
curSeries = chartValues.Series.Add(seriesName);
curSeries.ChartType = SeriesChartType.Line;
curSeries.XValueType = ChartValueType.DateTime;
foreach (var item in values)
{
curSeries.Points.AddXY(item.Timestamp, item.Value);
}

It looks like you are adding each point as its own series. Usually, you would add many points into one series.
Do you intend to change seriesId right before you use it?
int seriesId = chartValues.Series.Count - 1;
This happens before you call AddPointToChart() so that is getting a different value than you used in the series
chartValues.Series.Add(GenerateDefaultSeries(String.Format("{0}\r\nSensorId: {1}", desc, sensorId)));
EDIT
The line chartValues.Series.Add(...) will return a variable of type Series which you can add to directly. see http://www.dotnetperls.com/chart
SensorID is your number and I think that there are no guarantees that the system will choose to populate its collection of Series in any particular order.
EDIT
It looks like you're adding your points to the series before you finish setting them up. Instead, set them all the way up, then add them:
private void AddPointToChart(DateTime timestamp, double value, int series)
{
DataPoint dataPoint = new DataPoint(timestamp.ToOADate(), value);
dataPoint.AxisLabel = "dd.MM.yy hh:mm"; // not sure how this will work, but try it.
chart1.Series[series].Points.Add(dataPoint );
}

Related

C# Chart growing outside of panel

My goal is to create a chart that will sit inside of a panel restricting it's size.
I managed to achieve this some time ago but today I noticed that the chart was growing inside of the panel, not allowing the data to be seen.
I have attached a picture bellow which should help understand the issue.
UPDATE
I noticed that if I rmeove 'bottom' from the Anchor property of the panel the chart does not exceed the parent panel but it does not increase with the change of the form which is what I'm looking for.
I also noticed that there was also another chart on the form that was exceeding the parent form, this time the chart would extend to the right not allowing to see the data.
This is the code that generates this second chart and places is inside of the parent panel.
panel_chart.Controls.Clear();
chart1 = new Chart();
chart1.MouseMove += chart1_MouseMove;
chart1.ChartAreas.Add(new ChartArea("chartArea1"));
chart1.Series.Clear();
chart1.Titles.Clear();
var serieOEE = new Series("OEE");
serieOEE.ChartType = SeriesChartType.Line;
serieOEE.XValueType = ChartValueType.String;
var serieProd = new Series("Prod");
serieProd.ChartType = SeriesChartType.Column;
serieProd.XValueType = ChartValueType.String;
var serieDisp = new Series("Disp");
serieDisp.ChartType = SeriesChartType.Column;
serieDisp.XValueType = ChartValueType.String;
var serieQual = new Series("Qual");
serieQual.ChartType = SeriesChartType.Column;
serieQual.XValueType = ChartValueType.String;
DateTime DataReg = DateTime.MinValue;
List<AreaOEE> listaChart = new List<AreaOEE>();
foreach (var item in ListaGrafico) //listaOEE
{
if (item.Designacao == DesignacaoLista)
{
listaChart.Add(item);
}
}
listaChart = listaChart.OrderBy(a => a.IDReg).ToList();
DateTime DataUltimoReg = DateTime.MinValue;
int j = 0;
foreach (var item in listaChart)
{
string HoraGraf = Convert.ToDateTime(item.Hora).ToString("HH:mm");
if (j == 0 || j == listaChart.Count - 1 ||
Math.Abs(Convert.ToDateTime(item.Hora).Subtract(DataUltimoReg).TotalMinutes) >= 30)
{
serieOEE.Points.AddXY(HoraGraf, item.OEE);
serieProd.Points.AddXY(HoraGraf, item.Produtividade);
serieQual.Points.AddXY(HoraGraf, item.Qualidade);
serieDisp.Points.AddXY(HoraGraf, item.Disponibilidade);
DataUltimoReg = Convert.ToDateTime(item.Hora);
if (j == listaChart.Count - 2)
{
break;
}
}
j++;
}
//Adicionar o ultimo
foreach (var item in listaOEE)
{
if (item.Designacao == DesignacaoLista)
{
string sHora = "";
try
{
sHora = item.Hora.Substring(1, 5);
}
catch (Exception ex)
{
string sEx = ex.Message;
}
foreach (var itemOee in serieOEE.Points)
{
if (itemOee.AxisLabel == sHora)
{
itemOee.YValues[0] = item.OEE;
}
}
foreach (var itemP in serieProd.Points)
{
if (itemP.AxisLabel == sHora)
itemP.YValues[0] = item.Produtividade;
}
foreach (var itemD in serieDisp.Points)
{
if (itemD.AxisLabel == sHora)
itemD.YValues[0] = item.Disponibilidade;
}
foreach (var itemQ in serieQual.Points)
{
if (itemQ.AxisLabel == sHora)
itemQ.YValues[0] = item.Qualidade;
}
}
}
chart1.Series.Add(serieProd);
chart1.Series.Add(serieQual);
chart1.Series.Add(serieDisp);
chart1.Series.Add(serieOEE);
serieOEE.BorderWidth = 4;
chart1.ChartAreas[0].AxisX.LabelStyle.Angle = 90;
chart1.ChartAreas[0].AxisX.Interval = 1;
chart1.ChartAreas[0].AxisY.Minimum = 0;
chart1.ChartAreas[0].AxisY.Maximum = 140;
chart1.Legends.Clear();
chart1.Legends.Add(serieOEE.Legend);
chart1.Titles.Add(DesignacaoLista + " " + DataTitulo.ToString("dd-MM HH:mm"));
chart1.Titles[0].Font = new Font("Arial", 13, FontStyle.Bold);
chart1.Visible = true;
chart1.Dock = DockStyle.Fill;
panel_chart.Controls.Add(chart1);
you can change the size of Chart with Chart.Size:
Chart1.Size = new Size(1000, 200); //1000px * 200px

EP Plus - Error Table range collides with table

I am building an export to excel functionality using EP plus and c# application. I am currently getting the error.
'Table range collides with table tblAllocations29'
In my code logic below, I am looping through a data structure that contains key and collection as a value.
I looping across each key and once again loop through each collection belonging to that key.
I basically need to print tabular information for each collection along with its totals.
In the current scenario, I am getting the error when it is trying to print
three arrays
The first array has 17 records
The second array has 29 records
The third array has 6 records
I have taken a note of the ranges it is creating while debugging
The ranges are
A1 G18
A20 G50
A51 G58
controller
[HttpGet]
[SkipTokenAuthorization]
public HttpResponseMessage DownloadFundAllocationDetails(int id, DateTime date)
{
var ms = GetStrategy(id);
DateTime d = new DateTime(date.Year, date.Month, 1).AddMonths(1).AddDays(-1);
if (ms.FIRM_ID != null)
{
var firm = GetService<FIRM>().Get(ms.FIRM_ID.Value);
IEnumerable<FIRMWIDE_MANAGER_ALLOCATION> allocationsGroup = null;
var allocationsGrouped = GetAllocationsGrouped(EntityType.Firm, firm.ID, d);
string fileName = string.Format("{0} as of {1}.xlsx", "test", date.ToString("MMM, yyyy"));
byte[] fileContents;
var newFile = new FileInfo(fileName);
using (var package = new OfficeOpenXml.ExcelPackage(newFile))
{
FundAllocationsPrinter.Print(package, allocationsGrouped);
fileContents = package.GetAsByteArray();
}
var result = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new ByteArrayContent(fileContents)
};
result.Content.Headers.ContentDisposition =
new ContentDispositionHeaderValue("attachment")
{
FileName = fileName
};
result.Content.Headers.ContentType =
new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
return result;
}
return null;
#endregion
}
I have written the following utility that will try and export. It works sometimes when there are two array collections and it failed when processing three. Could somebody tell me what the problems are
FundsAllocationsPrinter.cs
public class FundAllocationsPrinter
{
public static void Print(ExcelPackage package, ILookup<string, FIRMWIDE_MANAGER_ALLOCATION> allocation)
{
ExcelWorksheet wsSheet1 = package.Workbook.Worksheets.Add("Sheet1");
wsSheet1.Protection.IsProtected = false;
int count = 0;
int previouscount = 0;
var position = 2;
int startposition = 1;
IEnumerable<FIRMWIDE_MANAGER_ALLOCATION> allocationGroup = null;
foreach (var ag in allocation)
{
allocationGroup = ag.Select(a => a);
var allocationList = allocationGroup.ToList();
count = allocationList.Count();
using (ExcelRange Rng = wsSheet1.Cells["A" + startposition + ":G" + (count + previouscount + 1)])
{
ExcelTableCollection tblcollection = wsSheet1.Tables;
ExcelTable table = tblcollection.Add(Rng, "tblAllocations" + count);
//Set Columns position & name
table.Columns[0].Name = "Manager Strategy";
table.Columns[1].Name = "Fund";
table.Columns[2].Name = "Portfolio";
table.Columns[3].Name = "As Of";
table.Columns[4].Name = "EMV (USD)";
table.Columns[5].Name = "Percent";
table.Columns[6].Name = "Allocations";
wsSheet1.Column(1).Width = 45;
wsSheet1.Column(2).Width = 45;
wsSheet1.Column(3).Width = 55;
wsSheet1.Column(4).Width = 15;
wsSheet1.Column(5).Width = 25;
wsSheet1.Column(6).Width = 20;
wsSheet1.Column(7).Width = 20;
// table.ShowHeader = true;
table.ShowFilter = true;
table.ShowTotal = true;
//Add TotalsRowFormula into Excel table Columns
table.Columns[0].TotalsRowLabel = "Total Rows";
table.Columns[4].TotalsRowFormula = "SUBTOTAL(109,[EMV (USD)])";
table.Columns[5].TotalsRowFormula = "SUBTOTAL(109,[Percent])";
table.Columns[6].TotalsRowFormula = "SUBTOTAL(109,Allocations])";
table.TableStyle = TableStyles.Dark10;
}
foreach (var ac in allocationGroup)
{
wsSheet1.Cells["A" + position].Value = ac.MANAGER_STRATEGY_NAME;
wsSheet1.Cells["B" + position].Value = ac.MANAGER_FUND_NAME;
wsSheet1.Cells["C" + position].Value = ac.PRODUCT_NAME;
wsSheet1.Cells["D" + position].Value = ac.EVAL_DATE.ToString("dd MMM, yyyy");
wsSheet1.Cells["E" + position].Value = ac.UsdEmv;
wsSheet1.Cells["F" + position].Value = Math.Round(ac.GroupPercent,2);
wsSheet1.Cells["G" + position].Value = Math.Round(ac.WEIGHT_WITH_EQ,2);
position++;
}
position++;
previouscount = position;
// position = position + 1;
startposition = position;
position++;
}
}
}
This is how the data looks when it is displayed successfully
Your issue is entirely in your Print method. You've been bitten by creating a slightly over-complicated row tracking mechanism and combining that with magic numbers. This causes you to position each table after the first one row higher than it should be. The header and subtotals are not part of the table, so you have a couple rows of leeway for the error. Tables can't overlap as you've seen, so EPPlus starts barking at you after you've exhausted your leeway.
All you need to do is keep track of the current row that you are writing to, and account for the space taken by your table header and footer (the subtotals) if you use them.
You declare these:
int count = 0;
int previouscount = 0;
var position = 2;
int startposition = 1;
But to write to the correct row, all you need is this:
var rowNumber = 1;
This will properly start writing your data in row one of the Excel sheet. As you write your table rows, you'll track and increment only the rowNumber. But what about the header and footer of each table? If you start writing at the first row of your table you'll overwrite the header, and if you don't account for both the header and footer you'll start having collisions like you've seen. So lets do this:
var showFilter = true;
var showHeader = true;
var showTotals = true;
var rowAdderForHeader = Convert.ToInt32(showHeader);
var rowAdderForFooter = Convert.ToInt32(showTotals);
These are pretty self explanatory, you'll use the rowAdders to hop the header or footer when needed. rowNumber will always be your current row to create your table and write your data. You use the count when defining your table, but we've made it irrelevant for anything else, so we move it:
var allocationList = allocationGroup.ToList();
//Moved here
var count = allocationList.Count();
Your using statement becomes:
using (ExcelRange Rng = wsSheet1.Cells["A" + rowNumber + ":G" + (count + rowNumber)])
Next, it isn't mentioned in your post, but you are going to run into a problem with the following:
ExcelTableCollection tblcollection = wsSheet1.Tables;
ExcelTable table = tblcollection.Add(Rng, "tblAllocations" + count);
Your table names have to be unique, but you could very well wind up with multiple allocations having the same count, which will cause EPPlus to throw an exception at you for duplicating a table name. So you'll want to also track the index of your current table:
var rowNumber = 1;
var tableIndex = 0;
//...
foreach (var ag in allocation)
{
tableIndex += 1;
//...
}
And use it to ensure unique table names:
ExcelTableCollection tblcollection = wsSheet1.Tables;
ExcelTable table = tblcollection.Add(Rng, "tblAllocations" + tableIndex);
We use our format control variables:
// table.ShowHeader = true;
table.ShowFilter = true;
table.ShowTotal = true;
//Changes to
table.ShowHeader = showHeader;
table.ShowFilter = showFilter;
table.ShowTotal = showTotals;
You have a small typo here:
table.Columns[6].TotalsRowFormula = "SUBTOTAL(109,Allocations])";
//Should be:
table.Columns[6].TotalsRowFormula = "SUBTOTAL(109,[Allocations])";
After you are done defining your table, you begin writing your data with a foreach loop. In order to prevent overwriting the table header if it exists, we'll have to advance one row. We also have to advance one row for each FIRMWIDE_MANAGER_ALLOCATION. If you are using the subtotals, we have to advance one row after the loop completes in order to properly position the next table:
rowNumber += rowAdderForHeader;
foreach (var ac in allocationGroup)
{
//...
rowNumber += 1;
}
rowNumber += rowAdderForFooter;
And that's it. We now properly track our position using just one variable, and we modify the position as necessary if there is a header or footer on your table.
The following is a complete working example that can be run in LinqPad as long as you add the EPPlus package through Nuget. It creates a random number of allocation groups each with a random number of allocations, and then exports them. Change the output file path to something that works for you:
void Main()
{
var dataGenerator = new DataGenerator();
var allocations = dataGenerator.Generate();
var xlFile = new FileInfo(#"d:\so-test.xlsx");
if (xlFile.Exists)
{
xlFile.Delete();
}
using(var xl = new ExcelPackage(xlFile))
{
FundAllocationsPrinter.Print(xl, allocations);
xl.Save();
}
}
// Define other methods and classes here
public static class FundAllocationsPrinter
{
public static void Print(ExcelPackage package, ILookup<string, FIRMWIDE_MANAGER_ALLOCATION> allocation)
{
ExcelWorksheet wsSheet1 = package.Workbook.Worksheets.Add("Sheet1");
wsSheet1.Protection.IsProtected = false;
IEnumerable<FIRMWIDE_MANAGER_ALLOCATION> allocationGroup = null;
var rowNumber = 1;
int tableIndex = 0;
var showFilter = true;
var showHeader = true;
var showTotals = true;
var rowAdderForHeader = Convert.ToInt32(showHeader);
var rowAdderForFooter = Convert.ToInt32(showTotals);
foreach (var ag in allocation)
{
tableIndex += 1;
Console.WriteLine(tableIndex);
allocationGroup = ag.Select(a => a);
var allocationList = allocationGroup.ToList();
var count = allocationList.Count();
using (ExcelRange Rng = wsSheet1.Cells["A" + rowNumber + ":G" + (count + rowNumber)])
{
ExcelTableCollection tblcollection = wsSheet1.Tables;
ExcelTable table = tblcollection.Add(Rng, "tblAllocations" + tableIndex);
//Set Columns position & name
table.Columns[0].Name = "Manager Strategy";
table.Columns[1].Name = "Fund";
table.Columns[2].Name = "Portfolio";
table.Columns[3].Name = "As Of";
table.Columns[4].Name = "EMV (USD)";
table.Columns[5].Name = "Percent";
table.Columns[6].Name = "Allocations";
wsSheet1.Column(1).Width = 45;
wsSheet1.Column(2).Width = 45;
wsSheet1.Column(3).Width = 55;
wsSheet1.Column(4).Width = 15;
wsSheet1.Column(5).Width = 25;
wsSheet1.Column(6).Width = 20;
wsSheet1.Column(7).Width = 20;
table.ShowHeader = showHeader;
table.ShowFilter = showFilter;
table.ShowTotal = showTotals;
//Add TotalsRowFormula into Excel table Columns
table.Columns[0].TotalsRowLabel = "Total Rows";
table.Columns[4].TotalsRowFormula = "SUBTOTAL(109,[EMV (USD)])";
table.Columns[5].TotalsRowFormula = "SUBTOTAL(109,[Percent])";
table.Columns[6].TotalsRowFormula = "SUBTOTAL(109, [Allocations])";
table.TableStyle = TableStyles.Dark10;
}
//Account for the table header
rowNumber += rowAdderForHeader;
foreach (var ac in allocationGroup)
{
wsSheet1.Cells["A" + rowNumber].Value = ac.MANAGER_STRATEGY_NAME;
wsSheet1.Cells["B" + rowNumber].Value = ac.MANAGER_FUND_NAME;
wsSheet1.Cells["C" + rowNumber].Value = ac.PRODUCT_NAME;
wsSheet1.Cells["D" + rowNumber].Value = ac.EVAL_DATE.ToString("dd MMM, yyyy");
wsSheet1.Cells["E" + rowNumber].Value = ac.UsdEmv;
wsSheet1.Cells["F" + rowNumber].Value = Math.Round(ac.GroupPercent, 2);
wsSheet1.Cells["G" + rowNumber].Value = Math.Round(ac.WEIGHT_WITH_EQ, 2);
rowNumber++;
}
//Account for the table footer
rowNumber += rowAdderForFooter;
}
}
}
public class FIRMWIDE_MANAGER_ALLOCATION
{
public FIRMWIDE_MANAGER_ALLOCATION(string name, Random rnd)
{
Name = name;
MANAGER_STRATEGY_NAME = "strategy name";
MANAGER_FUND_NAME = "fund name";
PRODUCT_NAME = "product name";
EVAL_DATE = DateTime.Now;
UsdEmv = (decimal)rnd.NextDouble() * 100000000;
GroupPercent = (decimal)rnd.NextDouble() * 100;
WEIGHT_WITH_EQ = 0;
}
public string Name { get; set; }
public string MANAGER_STRATEGY_NAME { get; set; }
public string MANAGER_FUND_NAME { get; set; }
public string PRODUCT_NAME { get; set; }
public DateTime EVAL_DATE { get; set; }
public decimal UsdEmv { get; set; }
public decimal GroupPercent { get; set; }
public decimal WEIGHT_WITH_EQ { get; set; }
}
public class DataGenerator
{
public static Random rnd = new Random();
public ILookup<string, FIRMWIDE_MANAGER_ALLOCATION> Generate()
{
var data = new List<FIRMWIDE_MANAGER_ALLOCATION>();
var itemCount = rnd.Next(1, 100);
for (var itemIndex = 0; itemIndex < itemCount; itemIndex++)
{
var name = Path.GetRandomFileName();
data.AddRange(GenerateItems(name));
}
return data.ToLookup(d => d.Name, d => d);
}
private IEnumerable<FIRMWIDE_MANAGER_ALLOCATION> GenerateItems(string name)
{
var itemCount = rnd.Next(1,100);
var items = new List<FIRMWIDE_MANAGER_ALLOCATION>();
for (var itemIndex = 0; itemIndex < itemCount; itemIndex++)
{
items.Add(new FIRMWIDE_MANAGER_ALLOCATION(name, rnd));
}
return items;
}
}

Trying to display default selected value from ListView

When I select a particular item in ListView item, I am able to display that particular item, and when the form loads I am getting the last item displayed by default (say the 10th item). However, what I want is to display the first item by default. How can I do this? I tried to do something like
listView1.Items[1].Selected = true;
but it's not working:
public partial class GroupExmStart : Form
{
string[] totDisplayQsn = null;
string[] QAndA = null;
string[] words = null;
private List<Question> questions;
ListViewItem lvi;
public GroupExmStart(string GroupName, string DurationID)
{
InitializeComponent();
this.GrpID=GroupName;
TopiID=db.GetTopicIDForGroup(GrpID);
string[] conf = db.GetConfiguration(Convert.ToInt16(DurationID)).Split('|');
Question qsn = new Question();
questions = qsn.Foo(TopiID, conf);
int z = Quiz(questions);
totQsn = Convert.ToInt16(conf[0]);
for (int kk = 1; kk <= totQsn; kk++)//using this I am adding items to listview
{
lvi = new ListViewItem();
lvi.Text = kk.ToString();
listView1.Items.Add(lvi);
}
totDisplayQsn = new string[totQsn + 1];
}
int Quiz(List<Question> questions)//using this I a passing set of questions to be displayed
{
foreach (Question question in questions)
{
DisplayQuestion(question);
}
return 0;
}
private void DisplayQuestion(Question question)//using this i am displaying questions
{
string Q = question.Text;
label5.Text = Q;
string OP1 = question.Option1;
string OP2 = question.Option2;
string OP3 = question.Option3;
string OP4 = question.Option4;
radioButton12.Text = OP4;
radioButton11.Text = OP4;
radioButton10.Text = OP4;
radioButton9.Text = OP4;
}
}
private void listView1_MouseClick(object sender, MouseEventArgs e)//using this i am selection particular item and displaying it
{
if (totDisplayQsn.GetLength(0) >= 0)
{
if (listView1.SelectedItems.Count > 0)
{
var q = Convert.ToInt16(listView1.SelectedItems[0].Text);
var selectedQuestion = questions[q-1];
DisplayQuestion(selectedQuestion);
}
}
}
Thanks in advance for any help.
Try this
listView1.SelectedItems[0].Focused = true;
To select the first item, access it by its zero based index. Place the code just after your items initialization code.
for (int kk = 1; kk <= totQsn; kk++)//using this I am adding items to listview
{
lvi = new ListViewItem();
lvi.Selected = false;
lvi.Text = kk.ToString();
listView1.Items.Add(lvi);
}
listView1.Items[0].Selected = true;
DisplayQuestion(questions[0]);
Remove the following code:
int Quiz(List<Question> questions)//using this I a passing set of questions to be displayed
{
foreach (Question question in questions)
{
DisplayQuestion(question);
}
return 0;
}
Ok try the below code:
ListViewItem foundItem = listView1.FindItemWithText("Select4", false, 0, true);
if (foundItem != null)
{
listView1.Items[foundItem.Index].Selected = true;
}
That's All.

How to tchart x-axis date and time control?

I'm use Teechart version .Net & VS2005.
X-axis of the graph type of date and time, Y-axis was added to the line Series.
I do not want to represent the null value of the x-axis date and time.
---------data table---------------------
----- Teechart x-axis display -----------
The red line is cursor tool. I set the value of the 'follow mouse = true'.
Cursor tool indicates to text a date and time value of x-axis. shows the time not in the data table. Following values ​​18:23:49 shows the 18:23:50 example. I want to express to the x-axis value for date and time of the data table.
my code:
private void TeeChartSet()
{
Steema.TeeChart.Styles.Line line = new Steema.TeeChart.Styles.Line();
TChart1.Series.Add(line);
line.XValues.DateTime = true;
TChart1.Axes.Bottom.Increment = Steema.TeeChart.Utils.DateTimeStep[(int)Steema.TeeChart.DateTimeSteps.OneSecond];
line.Stairs = true;
TChart1.Series[i].XValues.DataMember = "SetTime";
TChart1.Series[i].YValues.DataMember = "Park Brk Pres Sw1";
TChart1.Series[i].DataSource = Datatable;
TChart1.Axes.Bottom.Labels.DateTimeFormat = "HH:mm:ss";
Steema.TeeChart.AxisLabelStyle.PointValue;
}
private void cursor1_Change(object sender, CursorChangeEventArgs e)
{
TChart1.Invalidate();
DateTime Time = DateTime.FromOADate(e.XValue);
sli_CursorTime.Text = "Cursor Time : " + Time.ToString("yyyy-MM-dd HH:mm:ss");
}
private void MakeDt() //Make datatable Sample Code
{
DataTable dt = new DataTable();
dt.Columns.Add("SetTime");
dt.Columns.Add("Park Brk Pres Sw1");
DataRow row;
for (int i = 0; i < 10; i++)
{
row = dt.NewRow();
row["SetTime"] = DateTime.Now.AddSeconds(i * 5);
row["Park Brk Pres Sw1"] = 0;
dt.Rows.Add(row);
}
}
Many thanks for your extra information, it has been very useful for us. I have made a code, because you can use it to solve your problem. To make code, I have used annotation tool, cursor tool and series line, where only is shown the values of series point in annotation tool. Also I have changed the DateTimeFormat of series. I have added a line of code to export data values as excel format because you can see the results to change DateTimeFormat and it works in your correct way.
Steema.TeeChart.Tools.CursorTool cursorTool1;
private Steema.TeeChart.Tools.Annotation ann;
private double xval;
private void InitializeChart()
{
tChart1.Aspect.View3D = false;
//Tools
cursorTool1 = new Steema.TeeChart.Tools.CursorTool(tChart1.Chart);
cursorTool1.Style = Steema.TeeChart.Tools.CursorToolStyles.Vertical;
// cursorTool1.Pen.Style = System.Drawing.Drawing2D.DashStyle.Dash;
cursorTool1.FollowMouse = true;
// cursorTool1.OriginalCursor = Cursors.Arrow;
ann = new Steema.TeeChart.Tools.Annotation(tChart1.Chart);
ann.Shape.Pen.Visible = false;
ann.Shape.Shadow.Visible = false;
ann.Shape.ShapeStyle = Steema.TeeChart.Drawing.TextShapeStyle.RoundRectangle;
//Series
Steema.TeeChart.Styles.Line line1 = new Steema.TeeChart.Styles.Line(tChart1.Chart);
line1.XValues.DateTime = true;
line1.DateTimeFormat = "HH:mm:ss";
cursorTool1.Series = line1;
line1.Stairs = true;
line1.XValues.DataMember = "SetTime";
line1.YValues.DataMember = "Park Brk Pres Sw1";
line1.DataSource = GetDataTable();
tChart1.Axes.Bottom.Labels.DateTimeFormat = "HH:mm:ss";
tChart1.Axes.Bottom.Labels.Style = Steema.TeeChart.AxisLabelStyle.PointValue;
tChart1.Export.Data.Excel.Save("C:\\Test1.xls");
//Events
tChart1.AfterDraw += new PaintChartEventHandler(tChart1_AfterDraw);
cursorTool1.Change += new CursorChangeEventHandler(cursorTool1_Change);
tChart1.Draw();
}
//DataTable
private DataTable GetDataTable()
{
DataTable dataTable1 = new DataTable("DataSet");
//Condition to filter
//AddColumns in new Table
DataColumn xval = new DataColumn("SetTime", typeof(DateTime));
DataColumn yval = new DataColumn("Park Brk Pres Sw1", typeof(double));
dataTable1.Columns.Add(xval);
dataTable1.Columns.Add(yval);
DateTime dt = DateTime.Now;
for (int i = 0; i < 10; i++)
{
DataRow newRow = dataTable1.NewRow();
newRow[xval] = dt.AddSeconds(i * 5);
newRow[yval] = 0;
dataTable1.Rows.Add(newRow);
}
return dataTable1;
}
//Calculate Interpolate point
private double InterpolateLineSeries(Steema.TeeChart.Styles.Custom series, int firstindex, int lastindex, double xvalue)
{
int index;
for (index = firstindex; index <= lastindex; index++)
{
if (index == -1 || series.XValues.Value[index] > xvalue) break;
}
// safeguard
if (index < 1) index = 1;
else if (index >= series.Count) index = series.Count - 1;
// y=(y2-y1)/(x2-x1)*(x-x1)+y1
double dx = series.XValues[index] - series.XValues[index - 1];
double dy = series.YValues[index] - series.YValues[index - 1];
if (dx != 0.0) return dy * (xvalue - series.XValues[index - 1]) / dx + series.YValues[index - 1];
else return 0.0;
}
private double InterpolateLineSeries(Steema.TeeChart.Styles.Custom series, double xvalue)
{
return InterpolateLineSeries(series, series.FirstVisibleIndex, series.LastVisibleIndex, xvalue);
}
private void cursorTool1_Change(object sender, Steema.TeeChart.Tools.CursorChangeEventArgs e)
{
xval = e.XValue;
ann.Text = "";
this.Text = e.ValueIndex.ToString();
foreach (Steema.TeeChart.Styles.Series s in tChart1.Series)
{
ann.Text += DateTime.FromOADate(s.XValues[e.ValueIndex]).ToString("HH:mm:ss");
}
}
private void tChart1_AfterDraw(object sender, Steema.TeeChart.Drawing.Graphics3D g)
{
int xs = tChart1.Axes.Bottom.CalcXPosValue(xval);
int ys;
g.Brush.Visible = true;
g.Brush.Solid = true;
for (int i = 0; i < tChart1.Series.Count; i++)
if (tChart1.Series[i] is Steema.TeeChart.Styles.Custom)
{
ys = tChart1.Series[i].GetVertAxis.CalcYPosValue(InterpolateLineSeries(tChart1.Series[i] as Steema.TeeChart.Styles.Custom, xval));
//Draw elipse above cursor tool.
g.Brush.Color = tChart1.Series[i].Color;
//Draw annotation as label above cursor tool.
ann.Top = ys;
ann.Left = xs;
}
}
Could you please, tell us if previous code help you to solve your problem?
I hope will helps.
Thanks,

Graphing data of survey in charts

I want to make a survey with asp.net and c#.
It is clear that I can do it. But for the results I want to show them in a pie-chart and histogram.
The questions have only two answers "yes" and "no".
In pie-chart the whole percentage of "yes" and percentage of "no" like %55 "yes", %45 "no".
In histogram I want to show each question which is divided into 2 part ("yes" and "no").
In order to do these (pie-chart and the histogram), should I use component like Telerik?
Or can I do this with drawing library in .NET?
You can use Asp.net Chart controls for representing the data in pie charts......and it also depends on databinding.
if you are using datatable to get data from database then asp.net chart controls much better ...
would you pls take a look at this link for more information....
https://web.archive.org/web/20211020111731/https://www.4guysfromrolla.com/articles/120804-1.aspx
EDIT
This is function getting data from database..
public DataTable GetVisits(System.DateTime startdate , System.DateTime enddate)
{
const string sql = #"SELECT CONCAT(UPPER(SUBSTRING(visit_Status, 1, 1)), SUBSTRING(visit_Status FROM 2)) as Status, COUNT('x') AS Visits
FROM visits
WHERE visit_Date BETWEEN #startdate AND #enddate
GROUP BY visit_Status";
return sqlexecution(startdate, enddate, sql);
}
I am representing this data in stackcolumn chart.
if you want to represent in pie chart , you can change in code
public void DrawMembersvisits(Chart targetchartcontrol, DateTime startdate, DateTime enddate)
{
chart1 = targetchartcontrol;
Series series = null;
Title title;
string area = null;
chart1.ChartAreas.Clear();
chart1.Series.Clear();
chart1.Titles.Clear();
DataTable membervisits = null;
area = "Visits";
chart1.ChartAreas.Add(area);
series = chart1.Series.Add(area);
series.ChartArea = area;
title = chart1.Titles.Add("Member Visits");
title.Alignment = ContentAlignment.MiddleCenter;
title.Font = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular);
title.DockedToChartArea = area;
chart1.Titles.Add("").DockedToChartArea = area;
foreach (Title titles in chart1.Titles)
{
titles.IsDockedInsideChartArea = false;
}
foreach (ChartArea areas in chart1.ChartAreas)
{
areas.Area3DStyle.Enable3D = true;
areas.AxisX.LabelStyle.IsEndLabelVisible = false; areas.AxisX.LabelStyle.Angle = -90;
areas.AxisX.LabelStyle.IsEndLabelVisible = true;
areas.AxisX.LabelStyle.Enabled = true;
}
foreach (Legend legends in chart1.Legends)
{
legends.Enabled = false;
}
foreach (Series serie in chart1.Series)
{
serie.ChartType = SeriesChartType.StackedColumn;
// change here to get the pie charts
// charttypes.ChartType = SeriesChartType.Pie;
// charttypes["LabelStyle"] = "Outside";
// charttypes["DoughnutRadius"] = "30";
// charttypes["PieDrawingStyle"] = "SoftEdge";
// charttypes.BackGradientStyle = GradientStyle.DiagonalLeft;
serie["LabelStyle"] = "Outside";
serie["ColumnDrawingStyle"] = "SoftEdge";
serie["LabelStyle"] = "Top";
serie.IsValueShownAsLabel = true;
serie.BackGradientStyle = GradientStyle.DiagonalLeft;
}
membervisits = visistsdataf.GetVisits(startdate, enddate);
chart1.Series[0].Points.DataBindXY(membervisits.Rows, "Status", membervisits.Rows, "Visits");
foreach (Series chartSeries in chart1.Series)
{
foreach (DataPoint point in chartSeries.Points)
{
switch (point.AxisLabel)
{
case "Accepted": point.Color = Color.Green; break;
case "Refused": point.Color = Color.Red; break;
}
point.Label = string.Format("{0:0} - {1}", point.YValues[0], point.AxisLabel);
}
}
}

Categories

Resources