I want to remove the legend entry for some, but not all, series in my Excel chart. From my experience it seems as if SeriesCollection.Item(index) and LegendEntries.Item(index) are not related. Given a series n how can I remove only the legend for that series?
I'm using Office Interop 2010 with Visual Studio 2010. This is easily accomplished via the GUI by selecting the legend entry, then right clicking and choosing "delete".
To delete a legend entry you have to know the index of the legend you want to delete. Unfortunately, there doesn't seem to be a relationship available through the interop api that exposes the relationship between legend and series. There is one hokey workaround however. To remove a legend for a specific series the approach that worked for me was to remove the legend immediately after adding the series. This is the only time that the legend index is known.
// .
// . code to add series
// .
// remove the legend entry for the newly added series
chart.Legend.LegendEntries(chart.Legend.LegendEntries().Count).Delete();
I know the original question was about Office Interop 2010, but this seems to be a good reference point for dynamically showing and hiding series in a chart too, so I'll add the following top in case it helps others.
If you want to show/hide a series on a chartobject on a worksheet in Office 2013 (in VBA at least; not sure about interop), you can do the following:
Worksheets("MySheetName").ChartObjects("MyChartName").Chart.FullSeriesCollection("MyLedendSeriesName").IsFiltered = false
This hides the series without removing it. Set it to true to show the series again.
I needed to remove the last two legend entries from a chart because they were fake series added to create a cross-hairs effect. One series made the vertical line and the other horizontal. After the legend entry was removed, the series remained on the chart, which is what I wanted. I used the code below:
Microsoft.Office.Interop.Excel.ChartObject chartObj = null;
Microsoft.Office.Interop.Excel.Chart chart = null;
Microsoft.Office.Interop.Excel.Legend legend = null;
Microsoft.Office.Interop.Excel.LegendEntries legendEntries = null;
Microsoft.Office.Interop.Excel.LegendEntry legendItem;
int legendEntryCount = 0;
chartObj = (Microsoft.Office.Interop.Excel.ChartObject) xlws.ChartObjects("Chart 1");
chart = chartObj.Chart;
legend = chart.Legend;
legendEntries = (Microsoft.Office.Interop.Excel.LegendEntries) chart.Legend.LegendEntries();
legendEntryCount = legendEntries.Count;
if (legendEntryCount > 2)
{
legendItem = (Microsoft.Office.Interop.Excel.LegendEntry) legend.LegendEntries(legendEntryCount);
legendItem.Delete();
legendItem = (Microsoft.Office.Interop.Excel.LegendEntry) legend.LegendEntries(legendEntryCount - 1);
legendItem.Delete();
}
Related
I am building an application using .net to automatically generate charts and save them into memory streams from data. I met the problem of data labels being obscured by other series. I enabled the following.
series.SmartLabelStyle.Enabled = true
series.SmartLabelStyle.AllowOutsidePlotArea = LabelOutsidePlotAreaStyle.Yes
series.SmartLabelStyle.IsMarkerOverlappingAllowed = false
series.SmartLabelStyle.MovingDirection = LabelAlignmentStyles.Right
And data label can still be obscured by other data series, as the example shown below.
So the question is, how can I make all data labels visible and clear?
Thanks in advance.
EDIT: If that matters, I selectively enable data label on the series when a DataPoint is added.
Edit 2: I add data points one by one using Points.AddXY(SomeDate.ToOADate(), SomeValue). I go through the data, and add one point to each series, and enable the data label under some conditions. The critical part is like the following.
var index = series1.Points.AddXY(date.ToOADate(), value)
series2.Points.AddXY(date.ToOADate(), value)
series3.Points.AddXY(date.ToOADate(), value)
if (some condition)
{
series1.Points.[index].IsValueShownAsLabel = true
series2.Points.[index].IsValueShownAsLabel = true
series3.Points.[index].IsValueShownAsLabel = true
}
I set the SmartLabelStyle for each series, and then add the series to the chart, and then add data points to the series.
I am having a group box of width 900px and height 250px. and have to place 20 around radio buttons in it in a 4 rows * 5 columns tabular format. But at present its coming in 20 rows * 1 column format.
And i have to do this using groupbox.
I'll be glad for the answers thank you .
Unfortunately, this is not imporssible, as Group control does not support overflow style child management. You have two options (maybe more):
Create your custom group box and implement the "row overflow" logic yourself;
You might embed a child TableLayoutPanel or ListBox in your group box, and add child items there;
for the TableLayoutPanel option, you might try below snippet to see if it matches your requriment:
TableLayoutPanel Table = new TableLayoutPanel();
Table.AutoSize = true;
Table.RowCount = 4;
Table.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
Table.GrowStyle = System.Windows.Forms.TableLayoutPanelGrowStyle.AddCols;
this.Controls.Add(Table);
for the listbox, you can try this RadioListBox, and also set:
listBox1.MultiColumn = true;
And also you need adjust the listbox height so that exactly 4 rows are there.
If WPF is an option, this will be trivial as you can do this with XAML, and implementation your own ItemTemplate is just a breeze.
I have a fully functioning Dynamic chart control( just the regular ASP.net Chart ) It works great but I've run into a problem trying to add check boxes to the legend. I'm trying to add them next to the series names so the user can hide or view the respective series data. The chart is plotting data for roughly 42 employees. So being able to select and hide data is very important. I've been researching this for a few days now and I've found examples for 3rd party chart tools but i need to do this in MSVS 2010 standard charting tool.
This is how I create the Chart.
for (int emp = 1; emp < empRowList.Length; emp++)
{
chartB.Series.Add(empRowList[emp]);
chartB.Series[empRowList[emp]].ChartType = SeriesChartType.Point;
chartB.Series[empRowList[emp]].MarkerSize = 10;
chartB.Series[empRowList[emp]].MarkerStyle = MarkerStyle.Star4;
for (int month = 1; month < 12; month++)
{
chartB.Series[empRowList[emp]].Points.AddXY(mfi.GetMonthName(month), employeeStats[month, emp]);
chartB.Series[empRowList[emp]].Points[chartB.Series[empRowList[emp]].Points.Count - 1].ToolTip = empRowList[emp] + " - " + employeeStats[month, emp];
}
}
Here is how I format the chart
chartB.DataSource = t.Tables["info"];
chartB.DataBind();
chartB.Legends.Add(new Legend("Legend"));
chartB.Legends["Legend"].Alignment = StringAlignment.Center;
chartB.Legends["Legend"].Docking = Docking.Top;
I looked through this post thinking it could be augmented to help but since his series aren't added dynamically i'm not sure if it is the right direction to pursue.
ASP .net 4 Chart Control Legend formatting is not displaying at all
I've also looked in to using custom legends but read that they aren't linked to the data so i thought that might also be that wrong direction.
If anyone could help it would be greatly appreciated i'm kind of at a stand still till I can figure this out.
Thanks in advance
Some code on this would have been wonderful as I was looking to do something similar.
I did however find a post on msdn that states custom legends are not possible.
http://msdn.microsoft.com/en-us/library/bb677428(v=sql.100).aspx
I was able to find a solution similar to what i wanted using the code from the link above but could never quite get the legend to work properly so i scrapped that idea and just dynamically created a check-box bank underneath my chart that hides/shows series. Wasn't too difficult just needed to create a class and deal with the click event verses the redraw of the graph.
I need to set a static width of a bar chart series using dotNetCharting. My Graph has three series collections of line, spline, and column type. I would just like to set a static width for all series of column type.
I've tried using:
myChart.DefaultAxis.StaticColumnWidth = 10;
But the .DefaultAxis property isn't valid in intellisense for the whole chart nor the Column series collection. Do the series have to be added with data first before setting this?
Also, I've tried adding properties in the series object, no luck.
Any ideas?
series["PixelPointWidth"] = "your value for width";
part of chart series collection: a custom property
chart1.Series["Series1"]["PixelPointWidth"] = "1";
I'm trying to create a word-document on the fly and in there I'm suppose to have a chart. For that, I have
doc.InlineShapes.AddChart(Microsoft.Office.Core.XlChartType.xlCylinderCol, ref oRange);
However that opens Excel, reads data from some default data source of some kind and closes again.
How do I control this chart and choose the data source, and labels on axis?
This helped me a lot when I had the exact same question -
How to add graph in word
The example shows adding a graph as an OLE object, but the AddChart method works in a very similar manner. To add a graph to a Range, you would essentially do
InlineShape objShape = doc.InlineShapes.AddChart(XlChartType.xlCylinderCol, ref oRange);
To get access to the relevant objects
Chart objChart = objShape.Chart;
Workbook book = objChart.ChartData.Workbook;
Worksheet dataSheet = book.Worksheets["Sheet1"];
Now you can manipulate all the properties on the Chart and Datasheet like Axes, Data, Colors etc.
Another helpful tip, if you are not sure how to find something in the API, fire up Excel and start Record Macro to capture the changes you want, and then look at the Macro code. Looking at the Recorded Macros usually sets me on the right path when I know how to do something using the UI but not in the API.