Getting Values of other Series through tooltip - c#

I'm having a Chart whose data is coming from a list.
This class has id and count1 and count2 as Properties...
Now, i have a list of class...where the values are...
Id Count1 Count2
1 -10 20
2 -15 15
Now,
i do a simple bind...with multiple series
Chart1.DataSource = ListObjOfThatClass
Chart1.Series[0].XValueMember = "Id";
Chart1.Series[0].YValueMembers = "Count1";
Chart1.Series[1].YValueMembers = "Count2";
Chart1.DataBind();
Now, everthing works fine..
My Que: When i hover over the DataSeries, i show a tooltip for that particular YValueMember as "#VALY";
Chart1.Series[0].ToolTip = "#VALY";
Is there any way that I can show the value present in the other series?
i.e
Count2 value, of the series[1].YValueMember which I initialized earlier...??
Thanks

The easier way is too create your own DataPoint for the series, and not use the datasource. Then you can put whatever you want in the tooltip:
foreach (var o in ListObjOfThatClass)
{
var p1 = new DataPoint();
p1.SetValueXY(o.Id, o.Count1);
p1.ToolTip = string.Format("{0}", o.Count2);
Chart1.Series[0].Points.Add(p1);
var p2 = new DataPoint();
p2.SetValueXY(o.Id, o.Count2);
Chart1.Series[1].Points.Add(p2);
}

Related

RangeColumn: Show difference between two y values as Label

I have a RangeColumn with two y values per DataPoint. In the chart I would like to see the datapoint labeled with the difference of the two y values.
I tried:
DataPoint p = new DataPoint();
p.SetValueXY(d.Date, prev, prev + biggerResult.Count);
p.Label = biggerResult.Count.ToString();
s_AC_Checked.Points.AddXY(d.Date, prev, prev+biggerResult.Count);
Also this is not bringing me further:
s_AC_Checked.Label = "#VALY1\n#VALY2\n#VALY2-#VAL1\n#LABEL";
No chance to see the difference between the y values.
Here is what appears:
The right column should show "5" as a difference ...
Picture:
https://i.stack.imgur.com/r7Ejn.png
Found it. Doing it after the point was added to the series does the trick ...
DataPoint p = new DataPoint();
p.SetValueXY(d.Date, prev, prev + biggerResult.Count);
int index = s_AC_Checked.Points.AddXY(d.Date, prev, prev+biggerResult.Count);
DataPoint pt = s_AC_Checked.Points[index];
pt.Label = biggerResult.Count.ToString();
pt.SetCustomProperty("LabelStyle", "Left");
pt.LabelForeColor = Color.Black;
pt.IsValueShownAsLabel = true;

Sorting the whole ListView basing from a column automatically C#

I have tried several techniques online, but I really can't find any way to automatically sort the whole ListView, basing from a column.
Basically, I want the listview to be sorted without pressing any button, just when I open the form, it automatically gets sorted.
Here is what my listview looks like:
my listview
The Team Name, Wins, and Losses columns, they were from a file that I wrote into the listview. But the % is the result from wins / total games played.
lvwTeams.View = View.Details;
lvwTeams.GridLines = true;
lvwTeams.FullRowSelect = true;
lvwTeams.Columns.Add("%", 60);
lvwTeams.Columns.Add("Team Name", 300);
lvwTeams.Columns.Add("Wins", 80);
lvwTeams.Columns.Add("Losses", 80);
List<string> teamLines = File.ReadAllLines(C:\\Data).ToList();
string[] arr = new string[6];
ListViewItem itm;
foreach (var line in teamLines)
{
string[] entries = line.Split(',');
int sum = int.Parse(entries[4]) + int.Parse(entries[5]);
double percentage = double.Parse(entries[4]) / sum;
arr[0] = percentage.ToString();
arr[1] = entries[1];
arr[2] = entries[4];
arr[3] = entries[5];
itm = new ListViewItem(arr);
lvwTeams.Items.Add(itm);
}
I am really new in windows form file handling, please help

Adding series to Excel chart exception

I have an excel file filled with some data. I am trying to open the second sheet and create a chart. The problem is that the Series are giving me either a System.Runtime.InteropServices.COMException was caught or if I un-comment the commented lines a No overload for method 'SeriesCollection' takes '0' arguments. Here is the code that I have:
Microsoft.Office.Interop.Excel.ChartObjects chartObjs = (Microsoft.Office.Interop.Excel.ChartObjects)ws.ChartObjects(Type.Missing);
Microsoft.Office.Interop.Excel.ChartObject chartObj = chartObjs.Add(100, 20, 300, 300);
Microsoft.Office.Interop.Excel.Chart xlChart = chartObj.Chart;
Range rg1 = ws.get_Range("A1", "D" + rowcount);
rg1.VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;
xlChart.SetSourceData(rg1, Microsoft.Office.Interop.Excel.XlRowCol.xlColumns);
xlChart.ChartType = XlChartType.xlLine;
xlChart.Legend.Position = XlLegendPosition.xlLegendPositionBottom;
Axis axis = (Axis)xlChart.Axes(Microsoft.Office.Interop.Excel.XlAxisType.xlValue, Microsoft.Office.Interop.Excel.XlAxisGroup.xlPrimary);
axis.MaximumScaleIsAuto = false;
axis.MaximumScale = 3;
Axis Xaxis = (Axis)xlChart.Axes(Microsoft.Office.Interop.Excel.XlAxisType.xlCategory, Microsoft.Office.Interop.Excel.XlAxisGroup.xlPrimary);
Xaxis.TickLabels.Orientation = XlTickLabelOrientation.xlTickLabelOrientationDownward;
//SeriesCollection seriesCollection = (SeriesCollection)xlChart.SeriesCollection();
Series s1 = (Series)xlChart.SeriesCollection(1);
s1.Name = "Serie1";
s1.MarkerStyle = XlMarkerStyle.xlMarkerStyleCircle;
//seriesCollection.NewSeries();
Series s2 = (Series)xlChart.SeriesCollection(2);
s2.Name = "Serie2";
s2.MarkerStyle = XlMarkerStyle.xlMarkerStyleNone;
//seriesCollection.NewSeries();
Series s3 = (Series)xlChart.SeriesCollection(3);
s3.Name = "Serie3";
s3.MarkerStyle = XlMarkerStyle.xlMarkerStyleNone;
If I keep the comments, the error says invalid parameter and is shown on that line:
Series s2 = (Series)xlChart.SeriesCollection(2);
If I remove the comments, I get the second exception on that line:
SeriesCollection seriesCollection = (SeriesCollection)xlChart.SeriesCollection();
If I add 1 as a parameter, then the chart is not displayed properly. Do you have any suggestions how to make it work?
Argh that stuff still gives me nightmares. There was some weirdness around SeriesCollection - but I cannot remember exactly what it was.
Try to re-include that line
//SeriesCollection seriesCollection = (SeriesCollection)xlChart.SeriesCollection();
and refernece the seriesCollection object everywhere.
Alos it could be, that the index for SeriesCollection is zero - based, can you try that?
By Default when you create a new chart it doesn't have any series so you can't select it using chartPage.SeriesCollection(1). You need to create a series first.
In order to add a new series you need to use something like:
SeriesCollection seriesCollection = (SeriesCollection)xlChart.SeriesCollection();
Series s1 = seriesCollection.NewSeries();
s1.Name = "Serie1";
s1.MarkerStyle = XlMarkerStyle.xlMarkerStyleCircle;
Series s2 = seriesCollection.NewSeries();
s2.Name = "Serie2";
s2.MarkerStyle = XlMarkerStyle.xlMarkerStyleNone;
You may also need to add the values to the series rather than to the chart, eg:
Series ser = sc.NewSeries();
ser.XValues = _excelWorksheet.Range[YourRange];
ser.Values = _excelWorksheet.Range[YourRange];

Chart control data series

I have a chart control.I am plotting price along y axis and month-year along x axis.
I add series1 1st and then series2 to the same chart area.
Then I plot the points for series 1 and 2 using the below code
curveChart.Series.Add("Series1");
curveChart.Series["Series1"].XValueType = ChartValueType.DateTime;
curveChart.Series["Series1"].Points.DataBind(list1, "MonthYear", "PriceValue", null);
curveChart.Series["Series1"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
curveChart.Series["Series1"].BorderWidth = 3;
curveChart.ChartAreas["0"].AxisX.Interval = 1;
curveChart.Series.Add("Series2");
curveChart.Series["Series2"].XValueType = ChartValueType.DateTime;
curveChart.Series["Series2"].Points.DataBind(list2, "MonthYear", "PriceValue", null);
curveChart.Series["Series2"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
curveChart.Series["Series2"].BorderWidth = 3;
curveChart.ChartAreas["0"].AxisX.Interval = 1;
The problem I am facing is that list2 contains data only till Dec-2015 and list1 contains data till Dec-2016 but when the graph is plotted both the lines in the graph extend upto Dec-2016 though list2 doesnt have data till Dec-2016.How can I solve this?
I tried to simulate your problem. I added two data series one with 3 points, one with 2 points. The chart rendered correctly. This makes me think you are going to have to massage your data before you bind it.
curveChart.Series.Clear();
curveChart.Series.Add("Series1");
curveChart.Series["Series1"].XValueType = ChartValueType.DateTime;
curveChart.Series["Series1"].Points.AddXY(DateTime.Now, 12.00m);
curveChart.Series["Series1"].Points.AddXY(DateTime.Now.AddDays(1), 13m);
curveChart.Series["Series1"].Points.AddXY(DateTime.Now.AddDays(2), 8m);
curveChart.Series["Series1"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
curveChart.Series["Series1"].BorderWidth = 3;
curveChart.ChartAreas["0"].AxisX.Interval = 1;
curveChart.Series.Add("Series2");
curveChart.Series["Series2"].XValueType = ChartValueType.DateTime;
curveChart.Series["Series2"].Points.AddXY(DateTime.Now, 5.00m);
curveChart.Series["Series2"].Points.AddXY(DateTime.Now.AddDays(1), 7m);
curveChart.Series["Series2"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
curveChart.Series["Series2"].BorderWidth = 3;
curveChart.ChartAreas["0"].AxisX.Interval = 1;

Converting data from discrete properties of objects in an IEnumerable into an array/series for charting?

I have an IEnumerable (List<>) of a particular Type. The Type has a set of properties which I would like to chart as individual series (MVC3/Razor).
Is there a way I can transpose the data in the list of objects into a list of the properties?
e.g.
Given the code below, the end result I am trying to acheive is a set of Series so I have
Bucket1: IEnumerable<Date, Value>
Bucket2: IEnumerable<Date, Value>
etc
So that I can then chart each series to get a line chart with Date on x and value on y, for (in this example) 3 series/lines. This code works perfectly, but just feels wrong somehow?
var buckets = new List<Bucket>(){
new Bucket{
Date=DateTime.Today.AddDays(-3),
Bucket1=1000,
Bucket2=2000,
Bucket3=3000},
new Bucket{
Date=DateTime.Today.AddDays(-2),
Bucket1=1000,
Bucket2=2020,
Bucket3=3300},
new Bucket{
Date=DateTime.Today.AddDays(-1),
Bucket1=1000,
Bucket2=2040,
Bucket3=3600}
};
var chart = new Chart(){ Height = 400, Width = 600 };
var area = new ChartArea();
chart.ChartAreas.Add(area);
var series1 = chart.Series.Add("Bucket 1");
var series2 = chart.Series.Add("Bucket 2");
var series3 = chart.Series.Add("Bucket 3");
foreach (var series in chart.Series)
{
series.ChartType = SeriesChartType.Line;
}
foreach (var item in buckets)
{
series1.Points.AddXY(item.Date, item.Bucket1);
series2.Points.AddXY(item.Date, item.Bucket2);
series3.Points.AddXY(item.Date, item.Bucket3);
}
What about just using a Lambda expression for each serie (code below was not tested - it's just a prototype):
var series1 = buckets.Select(b => new { b.Date, b.Bucket1 });
var series2 = buckets.Select(b => new { b.Date, b.Bucket2 });
Thanks to the comments posted I kept my original code and don't think there's an easier way to do this.

Categories