how to get a value and not the formula - c#

I am trying to obtain the minimum value from a worksheet however when I generate the minimum and place it in a cell, and then extract that value I am getting the entire formula and not the double value in the cell.... what am I doing wrong? Below is my generate graph method.
Another thing, I would also like to delete the graph after I save it I have tried .delete() but that just threw and error, how do I go about doing that
private void GenerateGraph(Worksheet worksheet, int lastRow, int lastColumn, string filename)
{
string topLeft = ToCell(0, 0);
string bottomRight = ToCell(lastRow - 1, lastColumn - 1);
worksheet.get_Range(ToCell(0, 0), missing).Formula = "Max(B2:" + bottomRight + ")";
worksheet.get_Range(ToCell(0, 0), missing).FormulaHidden = true;
worksheet.get_Range(ToCell(0, 0), missing).Calculate();
Range range = (Range)worksheet.Cells[1,1];
//
//here is where my problem is, small is being given the formula from above
//
string small = (string)range.Value2;
double min = Convert.ToDouble(small);
worksheet.get_Range(ToCell(0,0),missing).Formula = "";
//Generates the graph
range = worksheet.get_Range(topLeft, bottomRight);
ChartObjects Xlchart = (ChartObjects)worksheet.ChartObjects(missing);
ChartObject chart = (ChartObject)Xlchart.Add(20, 160, 600, 500);
Excel.Chart myChart = chart.Chart;
myChart.SetSourceData(range, missing);
//sets the y axis
Axis axis = (Axis)myChart.Axes(XlAxisType.xlValue, XlAxisGroup.xlPrimary);
axis.MinimumScaleIsAuto = true;
axis.MaximumScaleIsAuto = true;
axis.HasTitle = true;
axis.AxisTitle.Text = "Measure (m)";
axis.CrossesAt = (int)(min-1);
//sets the x axis
Axis xAxis = (Axis)myChart.Axes(XlAxisType.xlCategory, XlAxisGroup.xlPrimary);
xAxis.HasTitle = true;
xAxis.AxisTitle.Text = "Position (m)";
//makes the graph a line graph
myChart.ChartType = XlChartType.xlXYScatterLinesNoMarkers;
//titles the graph
myChart.HasTitle = true;
myChart.ChartTitle.Text = "Profiles";
//saves the graph
myChart.Export(filename, "JPG", missing);
//
//here is where I would like to delete the graph
//
}

You need:
Formula = "=Max(B2:" + bottomRight + ")"
You were missing the equals sign in the formula.

Related

In the ms chart - winform, I want the RangeBar graph to be centered on the x-axis

In the ms chart, I want the RangeBar graph to be centered on the x-axis.
Only number 5 is in the middle and the other numbers are not middle children. I want to center this.
This happens when you adjust the PixelPointWidth.
When set to 1, it goes to the center, but the thickness is too small.
private void AddSerise(StartAndEndTime startAndEndTime)
{
int z = 0;
if (searchDeviceId.Contains(startAndEndTime.DeviceID))
{
z = searchDeviceId.IndexOf(startAndEndTime.DeviceID);
}
else
{
Series series2 = new Series(startAndEndTime.DeviceID);
mainChart.Series.Add(series2);
searchDeviceId.Add(startAndEndTime.DeviceID);
z = searchDeviceId.IndexOf(startAndEndTime.DeviceID);
}
mainChart.Series[startAndEndTime.DeviceID].ChartArea = "MainArea";
// mainChart.Series[startAndEndTime.DeviceID].Label = startAndEndTime.DeviceID;
//mainChart.Series[startAndEndTime.DeviceID].IsVisibleInLegend = true;
mainChart.Series[startAndEndTime.DeviceID].ChartType = SeriesChartType.RangeBar;
mainChart.Series[startAndEndTime.DeviceID].AxisLabel = z.ToString();
mainChart.Series[startAndEndTime.DeviceID].LegendText = startAndEndTime.DeviceID + "." + startAndEndTime.EventID;
mainChart.Series[startAndEndTime.DeviceID].Points.AddXY(z, startAndEndTime.startTime.ToOADate(), startAndEndTime.endTime.ToOADate());
mainChart.Series[startAndEndTime.DeviceID]["PixelPointWidth"] = "50";
}

C # word to insert a shape to the table the specified cell

Excuse me, how to insert in the word header table shape to the designated cell?
The following code doesn't work, the two shapes can only in the first cell.
Range oCell1 = newTable.Cell(1, 1).Range;
float top1 = oWord.Selection.get_Information(WdInformation.wdVerticalPositionRelativeToPage);
float left1 = oWord.Selection.get_Information(WdInformation.wdHorizontalPositionRelativeToPage);
newTable.Cell(1, 1).VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalBottom;
newTable.Cell(1, 1).Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
Microsoft.Office.Interop.Word.Shape shape1 = oDoc.Shapes.AddShape(shapeId, left1, top1 , oWord.CentimetersToPoints(float.Parse("1.1")), oWord.CentimetersToPoints(float.Parse("0.5")), oCell1);
Range oCell2 = newTable.Cell(1, 3).Range;
float top2 = oWord.Selection.get_Information(WdInformation.wdVerticalPositionRelativeToPage);
float left2 = oWord.Selection.get_Information(WdInformation.wdHorizontalPositionRelativeToPage);
Microsoft.Office.Interop.Word.Shape shape2 = oDoc.Shapes.AddShape(shapeId, left2, top2 , oWord.CentimetersToPoints(float.Parse("1.1")), oWord.CentimetersToPoints(float.Parse("0.5")), oCell2);
enter image description here
Word sometimes does not place things appropriately. One way you can get around it is to make it elsewhere and then copy/paste to the desired location.
Selection sel = doc.Application.Selection;
Table newTable = sel.Tables[1];
Cell cell_1 = newTable.Cell(1, 1);
Range oCell1 = cell_1.Range;
float top1 = sel.get_Information(WdInformation.wdVerticalPositionRelativeToPage);
float left1 = sel.get_Information(WdInformation.wdHorizontalPositionRelativeToPage);
cell_1.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalBottom;
cell_1.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
Interop.Shape shape1 = doc.Shapes.AddShape((int)MsoAutoShapeType.msoShapeCube,
left1, top1, app.CentimetersToPoints(float.Parse("1.1")),
app.CentimetersToPoints(float.Parse("0.5")), oCell1);
shape1.ConvertToInlineShape();
oCell1.Copy();
Range oCell2 = newTable.Cell(1, 3).Range;
oCell2.Select();
oCell2.Paste();

Add percent symbol to label on y axis in StackedColumn100

How do I format the Y axis label so it includes % symbol when using StackedColumn100?
I am using System.Windows.Forms.DataVisualization.Charting.Series charts.
here is my method for defining the chart look and feel (so far)...
private Series SetSeriesStyleStackedColumnPercentage(string sessionname, string color)
{
Series series = new Series(sessionname);
series.ChartType = SeriesChartType.StackedColumn100;
series.Color = Color.FromArgb(byte.Parse(color.Split(',')[0]),
byte.Parse(color.Split(',')[1]),
byte.Parse(color.Split(',')[2]),
byte.Parse(color.Split(',')[3]));
series.BorderWidth = 1;
series.BorderColor = Color.FromArgb(255, 0, 0, 0);
series.IsVisibleInLegend = true;
series.IsValueShownAsLabel = false;
return series;
}
There is no magic needed, just escape the percent character:
chart1.ChartAreas[yourCharArea].AxisY.LabelStyle.Format = "###0\\%";
or
chart1.ChartAreas[yourCharArea].AxisY.LabelStyle.Format = "###0.0\\%";
or whatever numeric formatting you need..

Axis text isn't printed and coordinates aren't plotted

The following code will only display the standard graph, but will not print the axis text nor plot any of the coordinates.
public void JapaneseCandleStick()
{
//GraphPane myPane = base.GraphPane;
GraphPane myPane = new GraphPane();
myPane.Title.Text = "Japanese Candlestick Chart Demo";
myPane.XAxis.Title.Text = "Trading Date";
myPane.YAxis.Title.Text = "Share Price, $US";
StockPointList spl = new StockPointList();
Random rand = new Random();
// First day is jan 1st
XDate xDate = new XDate(2006, 1, 1);
double open = 50.0;
for (int i = 0; i < 50; i++)
{
double x = xDate.XLDate;
double close = open + rand.NextDouble() * 10.0 - 5.0;
double hi = Math.Max(open, close) + rand.NextDouble() * 5.0;
double low = Math.Min(open, close) - rand.NextDouble() * 5.0;
StockPt pt = new StockPt(x, hi, low, open, close, 100000);
spl.Add(pt);
open = close;
// Advance one day
xDate.AddDays(1.0);
// but skip the weekends
if (XDate.XLDateToDayOfWeek(xDate.XLDate) == 6)
xDate.AddDays(2.0);
}
JapaneseCandleStickItem myCurve = myPane.AddJapaneseCandleStick("trades", spl);
myCurve.Stick.IsAutoSize = true;
myCurve.Stick.Color = Color.Blue;
// Use DateAsOrdinal to skip weekend gaps
myPane.XAxis.Type = AxisType.DateAsOrdinal;
// pretty it up a little
myPane.Chart.Fill = new Fill(Color.White, Color.LightGoldenrodYellow, 45.0f);
myPane.Fill = new Fill(Color.White, Color.FromArgb(220, 220, 255), 45.0f);
zedGraphControl1.AxisChange();
//base.ZedGraphControl.AxisChange();
}
What is wrong with the above and why cannot I see any text or see the plots? It all compiles without errors, hence the referencing and the ZedGraph implementation/referencing seems to be in order.
when you create an instance of Graphpane it must be referenced to zedGraphControl1, use the following line of code :
GraphPane myPane = zedGraphControl1.GraphPane;
& here's the output:

deleting a graph from an excel file

I have a function here that takes a worksheet and generates a graph from it. I would like to delete the graph, from the excel sheet, after I save it I have tried .delete() but that just threw and error, how do I go about doing that
private void GenerateGraph(Worksheet worksheet, int lastRow, int lastColumn, string filename)
{
string topLeft = ToCell(0, 0);
string bottomRight = ToCell(lastRow - 1, lastColumn - 1);
worksheet.get_Range(ToCell(0, 0), missing).Formula = "Max(B2:" + bottomRight + ")";
worksheet.get_Range(ToCell(0, 0), missing).FormulaHidden = true;
worksheet.get_Range(ToCell(0, 0), missing).Calculate();
Range range = (Range)worksheet.Cells[1,1];
string small = (string)range.Value2;
double min = Convert.ToDouble(small);
worksheet.get_Range(ToCell(0,0),missing).Formula = "";
//Generates the graph
range = worksheet.get_Range(topLeft, bottomRight);
ChartObjects Xlchart = (ChartObjects)worksheet.ChartObjects(missing);
ChartObject chart = (ChartObject)Xlchart.Add(20, 160, 600, 500);
Excel.Chart myChart = chart.Chart;
myChart.SetSourceData(range, missing);
//sets the y axis
Axis axis = (Axis)myChart.Axes(XlAxisType.xlValue, XlAxisGroup.xlPrimary);
axis.MinimumScaleIsAuto = true;
axis.MaximumScaleIsAuto = true;
axis.HasTitle = true;
axis.AxisTitle.Text = "Measure (m)";
axis.CrossesAt = (int)(min-1);
//sets the x axis
Axis xAxis = (Axis)myChart.Axes(XlAxisType.xlCategory, XlAxisGroup.xlPrimary);
xAxis.HasTitle = true;
xAxis.AxisTitle.Text = "Position (m)";
//makes the graph a line graph
myChart.ChartType = XlChartType.xlXYScatterLinesNoMarkers;
//titles the graph
myChart.HasTitle = true;
myChart.ChartTitle.Text = "Profiles";
//saves the graph
myChart.Export(filename, "JPG", missing);
//
//here is where I would like to delete the graph
//
}
If mychart.delete doesn't do it, try mychart.parent.delete

Categories