Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel charts +C#

Tags:

c#

excel

charts

I am using excel to draw charts from c#, but i need the chart to be one series related to each other not two series (when i select a range that has two columns of data)
can any one help:

        xla.Visible = true; 
        Workbook wb = xla.Workbooks.Add(XlSheetType.xlWorksheet);
        Worksheet ws = (Worksheet)xla.ActiveSheet;

        // Now create the chart.
        ChartObjects chartObjs = (ChartObjects)ws.ChartObjects(Type.Missing);
        ChartObject chartObj = chartObjs.Add(100, 20, 300, 300);
        Chart xlChart = chartObj.Chart;
        Range rg = ws.get_Range("B2", "C17");
        xlChart.SetSourceData(chartRange, XlRowCol.xlColumns);

thanks

like image 682
Abd Avatar asked Apr 06 '11 23:04

Abd


People also ask

What does the C in chart stand for?

The Control Chart or C-chart is a type of control chart that is used in the monitoring of count-type data which is usually the total number of conformities per unit. It is also sometimes used in monitoring the sum of events occurring in a given unit of time.

What is the difference between C and p-chart?

A p-chart is used to record the proportion of defective units in a sample. A c-chart is used to record the number of defects in a sample.

What is c-chart in statistics?

In statistical quality control, the c-chart is a type of control chart used to monitor "count"-type data, typically total number of nonconformities per unit. It is also occasionally used to monitor the total number of events occurring in a given unit of time.


1 Answers

I cleaned the code up a bit and added the generation of random data so this should run on its own.

Random random = new Random();
Microsoft.Office.Interop.Excel.Application xla = new Microsoft.Office.Interop.Excel.Application();
xla.Visible = true;
Workbook wb = xla.Workbooks.Add(XlSheetType.xlWorksheet);

Worksheet ws = (Worksheet)xla.ActiveSheet;

// Now create the chart.
ChartObjects chartObjs = (ChartObjects)ws.ChartObjects();
ChartObject chartObj = chartObjs.Add(150, 20, 300, 300);
Chart xlChart = chartObj.Chart;

// generate some random data
for (int row = 0; row < 16; row++)
{
    ws.Cells[row + 2, 2] = row + 1;
    ws.Cells[row + 2, 3] = random.Next(100);
}

Range xValues = ws.Range["B2", "B17"];
Range values = ws.Range["C2", "C17"];

SeriesCollection seriesCollection = xlChart.SeriesCollection();

Series series1 = seriesCollection.NewSeries();
series1.XValues = xValues;
series1.Values = values; 
like image 72
Emond Avatar answered Oct 22 '22 21:10

Emond