Excel Chart Type vertical value - c#

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.

Related

Change bar chart axis scale using Aspose.Cells for C#

I'm trying to set the axis scale for a bar chart using Aspose.Cells for C#. What I'm currently doing is essentially this:
var chart = worksheet.Charts.Add(ChartType.BarChart, 1, 1, 15, 15)
chart.NSeries.AddSeries("{.015,.03,.04}", true)
chart.NSeries.CategoryData = "{Apples,Oranges,Pears}"
chart.SeriesAxis.IsAutomaticMajorUnit = false;
chart.SeriesAxis.MajorUnit = .01;
However, when the spreadsheet renders the chart's major unit scale is automatically set to .1, so all the bars look disproportionately small. How can I change the scale of the series axis, so it has tick marks spaced apart by .01?
Please use the following code for your needs.
C#
//Set the major unit to 0.01
ch.ValueAxis.IsAutomaticMajorUnit = false;
ch.ValueAxis.MajorUnit = 0.01;
Here is the full sample code and the screenshot showing the output Excel file generated by the code for your reference.
C#
// Create empty workbook.
Workbook wb = new Workbook();
// Access first worksheet.
Worksheet worksheet = wb.Worksheets[0];
// Add Bar chart in first worksheet.
int idx = worksheet.Charts.Add(ChartType.Bar, 5, 2, 20, 10);
// Access Bar chart.
Chart ch = worksheet.Charts[0];
// Add two number series, true means they are vertical.
ch.NSeries.Add("{.015,.03,.04}", true);
// Set the category data to show on X-axis.
ch.NSeries.CategoryData = "{Apples,Oranges,Pears}";
// Set the name of first and second series.
ch.NSeries[0].Name = "Cricket";
//Set the major unit to 0.01
ch.ValueAxis.IsAutomaticMajorUnit = false;
ch.ValueAxis.MajorUnit = 0.01;
// Save the output in xlsx format.
wb.Save("outputBarChart.xlsx", SaveFormat.Xlsx);
Note: I am working as Developer Evangelist at Aspose

Microsoft.Office.Interop.Excel: How to Apply a border to ONE CELL

I am looking to apply a border to one cell using the Microsoft.Office.Interop.Excel library.
I am running a while-loop that is search for a empty cell within a certain column, once the cell is found I want to apply a border to it.
I know there many forums on this using Ranges, but I can't use the range functionality since I do not know what cell it is being applied to exactly.
My idea was:
(Excel.Range)xlWS.Cells[row,1].Borders.LineStyle = (Whatever Value);
Any advice? (besides links to other forums, I already looked through tons of forms)?
Excel.Range range = xlWS.UsedRange;
Excel.Range cell = range.Cells[row, column];
Excel.Borders border = cell.Borders;
border.LineStyle = Excel.XlLineStyle.xlContinuous;
border.Weight = 2d;
Hope this helps someone! Puts a thin line border around cell[row,column]!
Microsoft.Office.Interop.Excel.Range range = sheet.UsedRange;
Microsoft.Office.Interop.Excel.Range cell = range.Cells[1][1];
Microsoft.Office.Interop.Excel.Borders border = cell.Borders;
border[XlBordersIndex.xlEdgeLeft].LineStyle =
Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
border[XlBordersIndex.xlEdgeTop].LineStyle =
Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
border[XlBordersIndex.xlEdgeBottom].LineStyle =
Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
border[XlBordersIndex.xlEdgeRight].LineStyle =
Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
See this answer for more details.
To each cell in range
Microsoft.Office.Interop.Excel.Range chartRange;
chartRange = workSheet.get_Range("a1", "g7");
foreach (Microsoft.Office.Interop.Excel.Range cell in chartRange.Cells)
{
cell.BorderAround2();
}
To whole range
chartRange.BorderAround2();
This is based on jiverson's answer, but is much more concise for basic needs; simply create a range, get its Borders, and add a border to the bottom of the range:
var platypusRange = _xlSheet.Range[_xlSheet.Cells[1, 1], _xlSheet.Cells[1, 3]];
. . .
Borders border = platypusRange.Borders;
border[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlContinuous;
Of course, you could add a border to the top and sides, too, using xlEdgeTop, xlEdgeLeft, and xlEdgeRight.

Excel Interop - Draw All Borders in a Range

I see from Microsoft's documentation that I can access the particular border edges of a cell using the 'xlBordersIndex' property and for example set the border style for the left edge of a cell:
range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft].LineStyle = Excel.XlLineStyle.xlContinuous;
But what if I just want to draw all borders? I have tried
range.BorderAround2();
but that just draws a box around the range itself, which I understand. So then I tried
range.Cells.BorderAround2();
thinking that it would go through each of the cells within the range and place all borders around each cell. This is not what occurred. So in order to get all borders around all cells in a range, must I manually access each of the four border indices?
private void AllBorders(Excel.Borders _borders)
{
_borders[Excel.XlBordersIndex.xlEdgeLeft].LineStyle = Excel.XlLineStyle.xlContinuous;
_borders[Excel.XlBordersIndex.xlEdgeRight].LineStyle = Excel.XlLineStyle.xlContinuous;
_borders[Excel.XlBordersIndex.xlEdgeTop].LineStyle = Excel.XlLineStyle.xlContinuous;
_borders[Excel.XlBordersIndex.xlEdgeBottom].LineStyle = Excel.XlLineStyle.xlContinuous;
_borders.Color = Color.Black;
}
oRange = SHEET2.get_Range("a1", "a10");
oRange.Borders.get_Item(Excel.XlBordersIndex.xlEdgeLeft).LineStyle = Excel.XlLineStyle.xlContinuous;
oRange.Borders.get_Item(Excel.XlBordersIndex.xlEdgeRight).LineStyle = Excel.XlLineStyle.xlContinuous;
oRange.Borders.get_Item(Excel.XlBordersIndex.xlInsideHorizontal).LineStyle = Excel.XlLineStyle.xlContinuous;
oRange.Borders.get_Item(Excel.XlBordersIndex.xlInsideVertical).LineStyle = Excel.XlLineStyle.xlContinuous;
Finally, I got it. I did this without impacting the performance too. I am taking a simple excel to explain here :
Before
I managed to store the range as A1:C4 in a variable dynamically in exRange and used the below code to give border
((Range)excelSheet.get_Range(exRange)).Cells.Borders.LineStyle = XlLineStyle.xlContinuous;
After
I'm not yet familiar wit C#, but in VBA there are Range.Borders(xlInsideVertical) and Range.Borders(xlInsideHorizontal) properties. Try to use macro-recorder and apply all borders for any workbook region. Perhaps that will help.
For Each range In ranges
For Each row As Range In .Range(range).Rows
row.Cells.BorderAround(XlLineStyle.xlContinuous)
row.Cells.Borders.Item(XlBordersIndex.xlInsideHorizontal).LineStyle = XlLineStyle.xlContinuous
row.Cells.Borders.Item(XlBordersIndex.xlInsideVertical).LineStyle = XlLineStyle.xlContinuous
Next
Next
Why not to do simply:
Excel.Range tRange = xlWorkSheet.UsedRange;
tRange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
tRange.Borders.Weight = Excel.XlBorderWeight.xlThin;
Note: apply border after the row and cell (range) filled with data to get range simply using function .UsedRange()
Microsoft.Office.Interop.Excel.Range tRange = xlWorkSheet.UsedRange;
tRange.Borders.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
tRange.Borders.Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin;
Excel.Range tRange = xlWorkSheet.UsedRange;
tRange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
tRange.Borders.Weight = Excel.XlBorderWeight.xlThin;
That way is not working, you will get something like this:

C# show chart values on chart top

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.

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