如何使用C#将其他系列添加到Excel图表

时间:2014-01-20 12:25:59

标签: c# excel-interop

enter image description here我正在尝试向图表添加一个额外的数据系列,这显示了CPU阈值,我可以获取范围并创建超出阈值的图表,但我不知道如何添加图表的阈值。 我需要创建另一个图表对象吗?我可以使用现有的,只需添加新的范围吗?

Threshold values

你是如何创建图表的? - 见下面的代码。 此图表是否已在Excel文件中创建,并且您要修改Excel文件中的图表?是的,图表已经在Excel文件中了。

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

with new series

我在单元格D54中的值 - 阈值

  C55 = 0
  C56 = 1
  D55 = 75
  D56 = 75

我不知道如何删除图表中显示的2个附加轴 如果我注释掉line.ChartType,那么轴是正确的但我只得到一个阈值数据点?我不明白为什么。

2 个答案:

答案 0 :(得分:3)

var series = (SeriesCollection) yourChart.SeriesCollection();
var line = series.NewSeries();

line.Name = "CPU Threshhold";
//line.Values = ...;
//line.XValues = ...;

//formatting

答案 1 :(得分:1)

这是我找到的关于OP移除辅助轴问题的解决方案:

ALTER PROCEDURE files_uploadAll
    @document_id INT OUTPUT,
    @files NVARCHAR(MAX)

/*
DECLARE @document_id INT
DECLARE @files NVARCHAR(MAX) = N'{  
      "files": [
      {  
            "noteId": 1,
            "documentTitle": "doc1",
            "fileName": "doc1.pdf",
            "fileExtension": "pdf",
            "mimeType": "application/pdf",
            "documentTypeCd": "MSA",
            "userId": 1,
            "url": "http://www.url.com"
       },
       {  
            "noteId": 2,
            "documentTitle": "doc2",
            "fileName": "doc2.doc",
            "fileExtension": "doc",
            "mimeType": "application/msword",
            "documentTypeCd": "MSA",
            "userId": 1,
            "url": "http://www.url.com"
       }
    ]           
 }';
 EXECUTE files_uploadAll @files=@files, @document_id=@document_id OUTPUT
*/

AS

DECLARE @filesArray NVARCHAR(MAX)
SET @filesArray = (SELECT '$.files' FROM OPENJSON(@files))

DECLARE @filesList NVARCHAR(MAX), @i int
SELECT @i=0, @filesList = @filesArray

WHILE (@i < LEN(@filesList))
BEGIN
    DECLARE @item NVARCHAR(MAX)
    SELECT @item = SUBSTRING(@filesList, @i, CHARINDEX(',',@filesList,@i)-@i)

    INSERT INTO documents
    (note_id, document_title, file_name, file_extension, mime_type, document_type_cd, user_id, url)
    SELECT note_id, document_title, file_name, file_extension, mime_type, document_type_cd, user_id, url
    FROM OPENJSON(@item)
    WITH (
        note_id INT '$.noteId',
        document_title NVARCHAR(100) '$.documentTitle',
        file_name NVARCHAR(100) '$.fileName',
        file_extension NVARCHAR(25) '$.fileExtension',
        mime_type NVARCHAR(50) '$.mimeType',
        document_type_cd CHAR(5) '$.documentTypeCd',
        user_id int '$.userId',
        url NVARCHAR(1000) '$.url'
    )
    SET @document_id=SCOPE_IDENTITY()

    SET @i = CHARINDEX(',',@filesList,@i)+1
    IF(@i = 0) SET @i = LEN(@filesList)
END