Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Intro

Text from the 'Technical Overview' section

Excerpt

...

Blurb shown on index page/master list of ASP.NET samples.

See how to create Scatter Charts with ExcelApplication.

 Demonstrates how to create a Scatter chart with ExcelApplication.

Code

Code Block
  Insert  codeclass fromScatterChart
sample  stored in SVN here

Downloads

...

  {
        private ExcelApplication xlw;
        private Workbook wb;
        private Worksheet ws;

        /// <summary>
        /// Build the report with ExcelApplication
        /// </summary>
        public void GenerateReport()
        {
            //Create an instance of ExcelApplication 
            //and get a reference to the first worksheet.
            
            xlw = new ExcelApplication();
            wb = xlw.Create(ExcelApplication.FileFormat.Xlsx);
            ws = wb.Worksheets[0];

            //Populate the worksheet with values for the chart. 
            this.PopulateValues();

            //Add the scatter chart. 
            this.CreateScatterChart();

            //Save the report 
            xlw.Save(wb, @"..\..\ExcelOutputFiles\ScatterChart_output.xlsx");
        }


        /// <summary> Demonstrates how to create a scatter chart.</summary>
        private void CreateScatterChart()
        {
            //Create a chartsheet for the scatter chart.
            //Name it "ScatterChart" and place it before
            //other worksheets in the workbook.
            
            Chartsheet chrtsheet = wb.Worksheets.CreateChartsheet(ChartType.Scatter.StandardScatter, "ScatterChart", 0);
            Chart chrt = chrtsheet.Chart;

            //Create a series for the chart 
            Series srs = chrt.SeriesCollection.CreateSeries("DataSheet!B2:B10");

            //For scatter charts, use the setScatterValues method 
            srs.ScatterValues = "DataSheet!A2:A10";

            srs.NameFormula = "DataSheet!B1";
            srs.DataPointMarker.BackgroundColor = Color.SystemColor.Red;

            //Make the chart sheet the first visible sheet 
            chrtsheet.Select();
        }

        /// <summary> Add values to the worksheet.  
        /// This is the source data for the scatter chart.
        /// </summary>
        private void PopulateValues()
        {
            ws["A1"].Value = "Daily Rainfall";
            ws["A2"].Value = 4.1;
            ws["A3"].Value = 4.3;
            ws["A4"].Value = 5.7;
            ws["A5"].Value = 5.4;
            ws["A6"].Value = 5.9;
            ws["A7"].Value = 5.0;
            ws["A8"].Value = 3.6;
            ws["A9"].Value = 1.9;
            ws["A10"].Value = 7.3;

            ws["B1"].Value = "Particulate";
            ws["B2"].Value = 122;
            ws["B3"].Value = 100;
            ws["B4"].Value = 132;
            ws["B5"].Value = 94;
            ws["B6"].Value = 110;
            ws["B7"].Value = 100;
            ws["B8"].Value = 128;
            ws["B9"].Value = 137;
            ws["B10"].Value = 90;
            ws.Name = "DataSheet";
        }
    }

Downloads