C# show chart values on chart top - c#

I have created the chart using C# interop. I want to show the exact values on tip of the bar chart. How can I do it? My code is here.
Range chartRange;
Object misValue = System.Reflection.Missing.Value;
ChartObjects xlCharts = (ChartObjects)sheet0.ChartObjects(Type.Missing);
ChartObject myChart = (ChartObject)xlCharts.Add(10, 70, 250, 250);
Chart chartPage = myChart.Chart;
chartRange = sheet0.get_Range("$G$2:$G$12,$AB$2:$AB$12,$AT$2:$AT$12", misValue);
SeriesCollection scl = myChart.Chart.SeriesCollection();
Series xlSeries = scl.NewSeries();
chartPage.SetSourceData(chartRange, misValue);
xlSeries.XValues = sheet0.get_Range("A3:A12");
chartPage.ChartType = XlChartType.xlColumnClustered;
chartPage.Location(XlChartLocation.xlLocationAsNewSheet, "Islamic Summary Chart");
Please help me get the values on tip of the chart.
Thank you.

I don't think you can easily put them on the top on a clustered column chart.
DataLabels.Position
property is the one to look at, but even the
XlDataLabelPosition.xlLabelPositionAbove;
won't work for your type of chart.
One solution for you is to add another series as a XY scatter chart type, hide the points and display the data labels on that series since you can display it above the points. The values can be exactly the same as you have them now.

Related

Excel Chart Type vertical value

From a long time i am trying to generate graph like this
Codes i tried.
Excel.Range chartRange1;
Excel.ChartObjects xlCharts1 = (Excel.ChartObjects)worksheet.ChartObjects(Type.Missing);
Excel.ChartObject myChart1 = (Excel.ChartObject)xlCharts1.Add(350, 500, 500, 350);
Excel.Chart chartPage1 = myChart1.Chart;
chartRange1 = worksheet.get_Range("A33", "b56");
chartPage1.SetSourceData(chartRange1, Type.Missing);
chartPage1.ChartType = Excel.XlChartType.xlBarStacked;
Excel.Range xValues = worksheet.Range["B33", "B56"];
Excel.Range values = worksheet.Range["a33", "a56"];
Excel.SeriesCollection seriesCollection = (Excel.SeriesCollection)chartPage1.SeriesCollection();
Excel.Series series1 = seriesCollection.NewSeries();
series1.XValues = xValues;
series1.Values = values;
Please help which chart type i should use or i am making any mistake. A
After changing chart type, it works perfectly fine but its not working for last row text. As shown in image below.
Yes, you need to change the chart type.
using Excel = Microsoft.Office.Interop.Excel;
chartPage1.ChartType = Excel.XlChartType.xlBarClustered
You may have to adjust gridlines depending on how you want them to appear. I can provide more code for that if needed.
Edit - Also, don't forget to do
chartPage1.PlotBy = Excel.XlRowCol.xlColumns;
I don't know how but the moment i commented chartPage1.SetSourceData(chartRange1, Type.Missing); in code it works fine, might be possible two data source are set one by commented line and one by series collection.

How do I add additional series to a Excel Chart using C#

I am trying to add an additional data series to the chart this shows CPU threshold, I can get the range and create the graph with out the threshold on it, but I don't know how to add the threshold value to the chart.
do I need to create another chart object? can I use the existing and just add teh new range in?
How are you creating the chart? -- see code below.
Is this chart already created in the excel file, and you want to modify the chart in the excel file? yes the chart is already in a Excel file.
Excel.ChartObjects sCPUChart;
Excel.ChartObject sCPUChartObjects;
sCPUChart = sDBSheet.ChartObjects(Type.Missing);
sCPUChartObjects = sCPUChart.Add(49, 15, 360, 215);
Excel.Chart sChartCPU;
sChartCPU = sCPUChartObjects.Chart;
sChartCPU.SetSourceData(cpuChartRange, Missing.Value);
sChartCPU.ChartWizard(Source: cpuChartRange, Gallery: Excel.XlChartType.xlLine, Format: 2, HasLegend: true);
sChartCPU.Location(Excel.XlChartLocation.xlLocationAsObject, sDBSheet.Name);
//CPU Chart Axis
Excel.Axis xSChartCPUAxis;
xSChartCPUAxis = sChartCPU.Axes(Excel.XlAxisType.xlCategory, Excel.XlAxisGroup.xlPrimary);
Excel.Axis ySChartCPUAxis;
ySChartCPUAxis = syChartCPU.Axes(Excel.XlAxisType.xlValue, Excel.XlAxisGroup.xlPrimary);
ySChartCPUAxis.HasMajorGridlines = true;
ySChartCPUAxis.MaximumScaleIsAuto = true;
//Set Summary CPU Series
Excel.Series sCPUSeries = sChartCPU.SeriesCollection(1);
sCPUSeries.Name = "CPU";
//-------
// this is where I am having my issue
//I don't know how to add the threshold line to the graph with the existing graph being displayed
//sChartCPU.set_HasAxis(Excel.XlAxisType.xlCategory, Excel.XlAxisGroup.xlSecondary, true);
//summaryChartCPU.SetSourceData(summaryMemThreshold, Type.Missing); -- things break
//-------
I have now done the following:
Excel.SeriesCollection threshold = sChartCPU.sseriesCollection();
Excel.Series line = threshold.NewSeries();
line.Formula = "=SERIES(Summ!$D$54,Summ!$C$55:$C$56,Summ!$D$55:$D$56)";
line.ChartType = Excel.XlChartType.xLScatterLinesNoMarkers;
when the threshold line is created I have the following
the values I have in cells D54 - threshold
C55 = 0
C56 = 1
D55 = 75
D56 = 75
I don't know how to remove the 2 additional axis that appear in chart
If I comment out the line.ChartType, then the axis is correct but I only get one threshold data point ?? I don't understand why.
var series = (SeriesCollection) yourChart.SeriesCollection();
var line = series.NewSeries();
line.Name = "CPU Threshhold";
//line.Values = ...;
//line.XValues = ...;
//formatting
Here's the solution I found to the OP's question of how to remove the secondary axes:
Excel.SeriesCollection threshold = sChartCPU.sseriesCollection();
Excel.Series line = threshold.NewSeries();
// line.Formula = "=SERIES(Summ!$D$54,Summ!$C$55:$C$56,Summ!$D$55:$D$56)";
// instead of setting the Formula, I set the series values
line.Values = "='Summ!'$D$55:$D$56";
line.XValues = "='Summ!'$C$55:$C$56";
line.ChartType = Excel.XlChartType.xlXYScatterLinesNoMarkers;
// creates Axis objects for the secondary axes
Excel.Axis YAxis2 = (Excel.Axis)sChartCPU.Axes(Excel.XlAxisType.xlValue, Excel.XlAxisGroup.xlSecondary);
Excel.Axis XAxis2 = (Excel.Axis)sChartCPU.Axes(Excel.XlAxisType.xlCategory, Excel.XlAxisGroup.xlSecondary);
// remove the major and minor tick marks from the secondary (top) x-axis
XAxis2.MajorTickMark = Excel.XlTickMark.xlTickMarkNone;
XAxis2.MinorTickMark = Excel.XlTickMark.xlTickMarkNone;
// change the secondary x-axis max range to 1.0 so the bar will go all the way to the right side
XAxis2.MaximumScale = 1.0;
// delete the secondary x-axis labels
XAxis2.TickLabels.Delete();
// I chose to delete the secondary y-axis so the line would graph on the primary axis scale
YAxis2.Delete();

How to read(get) the Y-Axis Scale Values(Min&Max) from Excel Chart using c#

Is there any way to get the Y-Axis Min & Max values from the Excel Chart using C#, I've the following chart which is drawn using set of values in C#, and exported into .xls file.
Now I want to read the Y-Axis Scale Min & Max values in C#
EDIT:
any help on this would be much appreciated, Thanks.!
You can use:
Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(1,1,100,100);
chartPage = myChart.Chart;
Excel.Axis yAxis = (Excel.Axis)chartPage.Axes(Excel.XlAxisType.xlValue);
yAxis.MinimumScale = 0.0;
yAxis.MaximumScale = 30;
yAxis.MajorUnit = 5;
yAxis.MinorUnit = 1;

Creating multiple charts and the relation between Chart, Series, ChartArea

I have been generating charts using MSChart for some time now, but I have never created multiple charts within one chart object. Thinking about this task has revealed a gap in my knowledge.
How I think about creating a chart
Create Chart object
Add ChartArea object to Chart object
Create Series and add data
Add Series to Chart
The object structure ends up looking like this
Chart
/ \
ChartArea Series
As far as I have been concerned in the past, ChartArea is simply the area I set up the labels and that sort of thing. To add another, I will be wanting to add another ChartArea and one or more series.
___________________ Chart ___________________
/ / \ \
ChartArea0 ChartArea1 Series0 Series1
How do I associate Series0 to ChartArea0? It would make sense to add a Series to a ChartArea, but that is not possible. For what reason is it beneficial to associate a Series with a Chart, rather than a ChartArea?
Series are associated with chart areas like so
Chart Chart0 = new Chart();
ChartArea ChartArea0 = new ChartArea("name");
Chart0.ChartAreas.Add(ChartArea0);
Series Series0 = new Series();
Chart0.Series.Add(Series0);
// link series to area here
Series0.ChartArea = "name";
A Chart can be divided into multiple Areas where one area can be a Bar chart other can be Pie chart.
System.Windows.Forms.DataVisualization.Charting.Chart chart1 = new System.Windows.Forms.DataVisualization.Charting.Chart();
System.Windows.Forms.DataVisualization.Charting.ChartArea chartarea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
System.Windows.Forms.DataVisualization.Charting.ChartArea chartarea2 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
chart1.ChartAreas.Clear();
chart1.ChartAreas.Add(chartarea1);
chart1.ChartAreas.Add(chartarea2);
Then you create some series; each series will be associated with a chart area. if you create 5 series and associate series1, series2 and series3 to chartarea1 then those series must be same or compatible chart type. otherwise Runtime error will occur. Multiple series In same Chart Area may have different x axis component in some cases. for example in the following code: series1 has 3 data points and series2 has 5, in this case chartarea will show first three x values from series1 and next two x values from series2.
chart1.Series.Clear();
chart1.Series.Add("Series1");
chart1.Series[0].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Column;
chart1.Series[0].ChartArea = chart1.ChartAreas[0].Name;
chart1.Series[0].Points.AddXY("Point1", 20);
chart1.Series[0].Points.AddXY("Point2", 50);
chart1.Series[0].Points.AddXY("Point3",30);
chart1.Series.Add("Series2");
chart1.Series[1].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Column;
chart1.Series[1].ChartArea = chart1.ChartAreas[0].Name;
chart1.Series[1].Points.AddXY("newname1", 10);
chart1.Series[1].Points.AddXY("newname2", 20);
chart1.Series[1].Points.AddXY("newname3", 30);
chart1.Series[1].Points.AddXY("newname4", 40);
chart1.Series[1].Points.AddXY("newname5", 50);
this.tabPage3.Controls.Add(chart1);
chart1.Dock = System.Windows.Forms.DockStyle.Fill;
Previous answer breaks width of chart, this example uses elementposition objects, specifically set with to 100% (all nr's are %()
This example: "Two Chart Areas, vertically divided 80/20":
ElementPosition ePos = new ElementPosition();
ePos.Width = 100; ePos.Y = 0; ePos.X = 2; ePos.Height = 80;
ElementPosition ePos2 = new ElementPosition();
ePos2.Width = 100; ePos2.Y = 80; ePos2.X = 2; ePos2.Height = 20;
chartCandleStick.ChartAreas[0].Position = ePos;
chartCandleStick.ChartAreas[1].Position = ePos2;

Creating a X x Y chart in Excel with C#

I'm trying to create a chart in Excel with C#. What I did so far is populating an Excel spreadsheet with the data I need to plot. I Have data on my spreadsheet that looks like this:
T U
10 10
20 5
30 3,333333333
40 2,5
50 2
60 1,666666667
70 1,428571429
80 1,25
90 1,111111111
100 1
110 0,909090909
120 0,833333333
130 0,769230769
140 0,714285714
150 0,666666667
160 0,625
I have data from line 1 to line 40.
I want to create a chart in Excel like this one:
Notice that on the X axis, I want to put the values (or some of the values) from column T, and in the Y axis, the values from the U column.
I made the following code to create a chart:
object misValue = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Excel.Application xla = new Microsoft.Office.Interop.Excel.Application();
Workbook wb = xla.Workbooks.Add(XlSheetType.xlWorksheet);
Worksheet ws = (Worksheet)xla.ActiveSheet;
Microsoft.Office.Interop.Excel.Range chartRange;
Microsoft.Office.Interop.Excel.ChartObjects xlCharts = (Microsoft.Office.Interop.Excel.ChartObjects)ws.ChartObjects(Type.Missing);
Microsoft.Office.Interop.Excel.ChartObject myChart = (Microsoft.Office.Interop.Excel.ChartObject)xlCharts.Add(240, 90, 468, 315);
Microsoft.Office.Interop.Excel.Chart chartPage = myChart.Chart;
chartRange = ws.get_Range("U1", "U40");
chartPage.SetSourceData(chartRange, misValue);
chartPage.ChartType = Microsoft.Office.Interop.Excel.XlChartType.xlLine;
which got me the following result:
which is pretty close to what I need, but I couldn't set the values for the X axis.
Can someone please tell me how to assign the values from the range T1 to T40 to the X axis?
And if possible, how can I remove the label "Série 1" from the chart?
This is six months old, so I imagine that you've already moved on, but:
You've already got a reference to the chart (chartPage), so setting the x-axis values and removing the legend should be simple. You just need a reference to the series itself.
Excel.Series ser = (Excel.Series)chartPage.SeriesCollection(1);
ser.xValues = ws.Range[ws.cells[row,col],ws.cells[row,col]];
chartPage.hasLegend = false;
This was written assuming that you've aliased Microsoft.Office.Interop.Excel like this:
using Excel = Microsoft.Office.Interop.Excel;
and that the series you're working on is actually the first one. You could also set the y-values the same way, instead of setting the source data.
ser.Values = ws.Range[etc...];

Categories

Resources