简体   繁体   English

更改图形c#的x轴值

[英]Change the x axis values of graph c#

I am a little new to c# and Visual Studio and I am trying to plot a graph based on the values of a csv file located somewhere else. 我对c#和Visual Studio有点新,我试图根据位于其他地方的csv文件的值绘制图形。 I have used the ChartObjects and ChartWizard property in c# to create the graph. 我在c#中使用了ChartObjects和ChartWizard属性来创建图形。 The graph plotted should be the column range I am providing, in the Y-axis and X-axis should have the current row number(1,2,3,4 etc). 绘制的图表应该是我提供的列范围,在Y轴和X轴应该具有当前行号(1,2,3,4等)。 However my graph by default takes the X-axis to be the first column in my csv file. 但是我的图表默认情况下将X轴作为我的csv文件中的第一列。 It plots properly if I specify a range for X-axis too but how can I get the current row number there? 如果我也为X轴指定了一个范围,那么它会正确绘制但是如何获得当前的行号呢?

I went through a lot of articles and questions even on Stack Overflow but none seemed to help. 我甚至在Stack Overflow上经历了很多文章和问题,但似乎没有任何帮助。

Here's a snippet of my code: 这是我的代码片段:

Microsoft.Office.Interop.Excel.Application xlexcel;
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;

object misValue = System.Reflection.Missing.Value;
xlexcel = new Microsoft.Office.Interop.Excel.Application();

var xlWorkBooks = xlexcel.Workbooks;

xlexcel.Visible = false;

xlWorkBooks.OpenText(@"C:\" + processName + ".csv", misValue, misValue, Microsoft.Office.Interop.Excel.XlTextParsingType.xlDelimited,          Microsoft.Office.Interop.Excel.XlTextQualifier.xlTextQualifierNone, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue);

// Set Sheet 1 as the sheet you want to work with
xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBooks[1].Worksheets.get_Item(1);

xlWorkSheet.Shapes.AddChart(misValue, misValue, misValue, misValue, misValue).Select();

//~~> Make it a Line Chart
      xlexcel.ActiveChart.ApplyCustomType(Microsoft.Office.Interop.Excel.XlChartType.xlLine);

//~~> Set the data range
xlexcel.ActiveChart.SetSourceData(xlWorkSheet.Range["E2:E200"]);
xlexcel.ActiveChart.ChartWizard(misValue, Title: chartName + " (" + processName + ")", CategoryTitle: "Iterations", ValueTitle: processType);

Microsoft.Office.Interop.Excel.ChartObjects chartObjects =(Microsoft.Office.Interop.Excel.ChartObjects)(xlWorkSheet.ChartObjects(Type.Missing));
foreach (Microsoft.Office.Interop.Excel.ChartObject co in chartObjects)
{
    co.Select();
    Microsoft.Office.Interop.Excel.Chart chart = (Microsoft.Office.Interop.Excel.Chart)co.Chart;
    chart.Export(ConfigurationManager.AppSettings.Get("Charts") + "\\ProcessFiles" + @"\" + chartName + " (" + processName + "of" + processType + ")" + ".png", "PNG", false);
    co.Delete();
 }
 xlWorkBooks[1].Close(true, misValue, misValue);
 xlexcel.Quit();

Any guidance would be greatly appreciated. 任何指导将不胜感激。 Thanks! 谢谢!

I tried your code and made some modifications, I hope this will work 我尝试了你的代码并进行了一些修改,我希望这会有效

using Excel = Microsoft.Office.Interop.Excel;

Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;

object misValue = System.Reflection.Missing.Value;
//string appPath = Path.GetDirectoryName(Application.ExecutablePath);
string fileName = "" + "YOUR_PATH" + "\\Templates\\myCSV.csv";

string processName = "test";
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Open(fileName);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

xlWorkSheet.Shapes.AddChart(misValue, misValue, misValue, misValue, misValue).Select();

//~~> Make it a Line Chart
            xlApp.ActiveChart.ApplyCustomType(Microsoft.Office.Interop.Excel.XlChartType.xlLine);

//~~> Set the data range
xlApp.ActiveChart.SetSourceData(xlWorkSheet.Range["B1:B30"]);
string chartName = "TEST CHART", processType="TEST TYPE";
xlApp.ActiveChart.ChartWizard(misValue, Title: chartName + " (" + processName + ")", CategoryTitle: "Iterations", ValueTitle: processType);

Excel.ChartObjects chartObjects = (Excel.ChartObjects)(xlWorkSheet.ChartObjects(Type.Missing));

foreach (Excel.ChartObject co in chartObjects)
{
  co.Select();
  Excel.Chart chart = (Excel.Chart)co.Chart;
  chart.Export("C:\\YOUR_PATH" + @"\" + chart.Name + ".png", "PNG", false);
}

xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();


xlWorkSheet = null;
xlWorkBook = null;
xlApp = null;
releaseObject(xlWorkBook);
releaseObject(xlWorkSheet);
releaseObject(xlApp);

private void releaseObject(object obj)
{
  try
  {
    System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
    obj = null;
  }
  catch (Exception ex)
  {
    obj = null;
    MessageBox.Show(ex.Message);
  }
  finally
  {
    GC.Collect();
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM