Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel chart

Status
Not open for further replies.

vvlad

Programmer
May 27, 2002
151
DE
Hello,

If I use the code below to draw an Excel chart, that works fine. The data in column A is considered to be the X coordinates, the data in column B the Y coordinates and the line is drawn correctly.

Code:
            Excel.ChartObjects myCharts = (Excel.ChartObjects)ws.ChartObjects(System.Type.Missing);
            Excel.ChartObject myChart = myCharts.Add(100, 100, 500, 500);
            
            myChart.Chart.ChartType = XlChartType.xlXYScatter;
            Excel.Range ExRng = ws.get_Range("A1", "B5");
            myChart.Chart.SetSourceData(ExRng, Excel.XlRowCol.xlColumns);

            Excel.Axis XAxis = (Excel.Axis)myChart.Chart.Axes(Excel.XlAxisType.xlCategory, Excel.XlAxisGroup.xlPrimary);
            XAxis.MaximumScale = 100;
            XAxis.MinimumScale = 0;
            XAxis.MajorUnit = 10;

            myChart.Activate();

I would like to do the same thing with series, taking the data from a List<double> instead of columns. In this case I did not find a proper way to assign the X coordinates.

Whatever I do, no matter if I use the XValues property or not, the labels on the X axis are completely ruined. The MaximumScale, MinimumScale and MajorUnit properties of XAxis seem to be ignored.

Code:
            List<double> YY = new List<double>{2, 5, 6, 4, 8};
            List<double> XX = new List<double> { 1, 7, 8, 9, 11 };

            Excel.ChartObjects myCharts = (Excel.ChartObjects)ws.ChartObjects(System.Type.Missing);
            Excel.ChartObject myChart = myCharts.Add(100, 100, 500, 500);
            
            myChart.Chart.ChartWizard(ws.get_Range("A1", "A1"),
                                               System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing,
                                               System.Type.Missing, System.Type.Missing, "", "", "", "");
            myChart.Chart.ChartType = XlChartType.xlXYScatter;

            Excel.Axis XAxis = (Excel.Axis)myChart.Chart.Axes(Excel.XlAxisType.xlCategory, Excel.XlAxisGroup.xlPrimary);
            XAxis.MaximumScale = 100;
            XAxis.MinimumScale = 0;
            XAxis.MajorUnit = 10;

            Excel.SeriesCollection seriesCollection = (Excel.SeriesCollection)myChart.Chart.SeriesCollection(System.Type.Missing);
            Excel.Series s1 = seriesCollection.NewSeries();
            s1.Values = YY.ToArray();
            s1.XValues = XX.ToArray();
            s1.ChartType = Excel.XlChartType.xlLineMarkers;
            s1.MarkerStyle = Excel.XlMarkerStyle.xlMarkerStyleCircle;

            myChart.Activate();

Can someone help?


Thanks.


vlad
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top