. NET Excel Reading and Writing Tool Spire. Xls uses the excelgraphic table function of the weight level (52.16.netspire.xls

Source: Internet
Author: User

. NET Excel Reading and Writing Tool Spire. Xls uses the excelgraphic table function of the weight level (52.16.netspire.xls

Previous Article: ". NET Excel Reading and Writing Tool Spire. xls (4) Data Operations and Control "introduces Spire. this section describes the data control functions of XLS. C # controls Excel to generate charts. This section describes how to generate different types of charts in C # and operations related to Excel Charts, such as resizing and saving images.

1. Charts in Excel

The world we live in is rich and colorful, and almost all the knowledge comes from vision. You may not be able to remember a series of numbers and their relationships and trends. However, you can easily remember a picture or curve. Therefore, the use of charts makes it easier to understand and exchange worksheets compiled in Excel. Excel has many advanced drawing functions and is easy to use. In this chapter, we will learn how to create a simple chart and then modify it to make the chart more refined, and how to add background, graph note, and text to the graph. In Excel, charts represent data in worksheets in graphs. For example, the weekly sales in each region are displayed in a column chart, as shown in Figure 8-1. Charts make data more interesting, attractive, easy to read, and evaluated. They can also help us analyze and compare data [1]. As shown in the chart, it is easy to see how tired the data is...

2. C # How to create an Excel chart

2.1 Spire. Xls chart class

In the Spire. Xls component, generating charts is simplified. You only need a core Chart class to add charts and perform auxiliary settings. Spire. Xls defines supported chart types, which are listed in the ExcelChartType enumeration. There are currently 74 types, which can be viewed in the Object Browser:

In actual use, adding an Excel chart mainly involves the following processes:

1. Add the expected Excel chart type to the specified Sheet;

2. Set the data range required by the chart;

3. Set the chart position and title;

4. Set the values of the series charts and axes.

The following uses the C # And Spire. Xls components to generate a pie chart and line chart.

2.2 generate a pie chart

In the work, if you need to calculate the total cost or the proportion of each part of the amount, it is generally calculated through the division of each part and the total amount, and this proportion is very abstract, we can use a pie chart tool to directly display the proportion of each component in a graphic way, and easily create a pie chart in Excel. In Spire. Xls, the Pie chart types are Pie and Pie3D in the ExcelChartType class. For good results, we can use the 3D type.

// Create a pie Chart chart Chart = sheet. charts. add (ExcelChartType. pie); // set the chart data Range, which is also used with the Range object chart obtained from the previous cell. dataRange = sheet. range ["B2: B5"]; chart. seriesDataFromRange = false; // you can specify the chart position. leftColumn = 1; // chart on the left of the chart. topRow = 6; // The upper row chart of the chart. rightColumn = 6; // chart on the right of the chart. bottomRow = 18; // bottom row of the chart // chart title chart. chartTitle = "pie chart example"; // set the font chart. chartTitleArea. isBold = true; chart. chartTitleArea. size = 12; // initialize the legend Spire. xls. charts. chartSerie cs = chart. series [0]; // The tag directory data cs of the chart. categoryLabels = sheet. range ["A2: A5"]; // The main value of the chart, that is, the data cs of the pie chart. values = sheet. range ["B2: B5"];

The effect is as follows:

2.3 generate a line chart

A line chart is a graph composed of straight lines that connect data points to display the trend of data changes in line. A line chart can display continuous data that changes over time (based on common proportions), so it is very suitable for displaying data trends at equal intervals. In a line chart, category data is evenly distributed along the horizontal axis, and all value data is evenly distributed along the vertical axis. In a line chart, features such as increasing or decreasing data, increasing or decreasing data rate, increasing or decreasing rules (periodicity, spiral, etc.), and peak values can be clearly reflected. Therefore, line charts are often used to analyze the trend of data changes over time. They can also be used to analyze the interactions and mutual effects of multiple groups of data changes over time.

Use Spire. Xls to plot the line chart process and the above type. However, if there are many legends, the settings are slightly more complex. Let's look at an example:

// Add a Chart object to the Sheet object. You can directly add a type or assign a value to the ChartType to add chart Chart = sheet. charts. add (); chart. chartType = ExcelChartType. line3D; // set the chart data range chart. dataRange = sheet. range ["A1: E5"]; // you can specify a chart storage location. leftColumn = 1; chart. topRow = 6; chart. rightColumn = 7; chart. bottomRow = 22; // chart title chart. chartTitle = "line chart example"; // set the font bold and size chart. chartTitleArea. isBold = true; chart. chartTitleArea. size = 12; // set the title chart of the abscissa. primaryCategoryAxis. title = "month"; chart. primaryCategoryAxis. font. isBold = true; chart. primaryCategoryAxis. titleArea. isBold = true; // set the ordinate, that is, the title chart of the value. primaryValueAxis. title = "sales"; chart. primaryValueAxis. hasMajorGridLines = false; chart. primaryValueAxis. titleArea. textRotationAngle = 90; chart. primaryValueAxis. minValue = 1000; chart. primaryValueAxis. titleArea. isBold = true; // cyclically plot the sales line charts of different countries, with multiple series of foreach (Spire. xls. charts. chartSerie cs in chart. series) {cs. format. options. isVaryColor = true; cs. dataPoints. defaultDataPoint. dataLabels. hasValue = true ;}

Shows the actual results and raw data:

The process of other charts is similar. You can find related examples in the help document.

3. C # Set an Excel chart

3.1 Save the chart as an image

Charts generated in Excel can be saved as images. In Spire. XLS, these operations have become very simple. First, obtain the SaveChartAsImage method of the Workbook object to obtain all charts in the specified sheet. Then, save the image objects sequentially. The following is the main code:

Workbook workbook = new Workbook();workbook.LoadFromFile("chart.xlsx", ExcelVersion.Version2010);Worksheet sheet=workbook.Worksheets[0];Image[] imgs = workbook.SaveChartAsImage(sheet);for (int i = 0; i < imgs.Length; i++){ imgs[i].Save(string.Format("img-{0}.png", i), ImageFormat.Png);}

3.2 position adjustment

The positions of charts in Excel have been described earlier. You can also adjust the chart size. Use the Width and Height methods to directly set the size.

chart.Width = 400;chart.Height = 250;

The above are some common operations and usage. In general, it is relatively simple. If you need more details, you can view the API documentation and find the functions you need based on the method, it is easy to implement.

Http://baike.haosou.com/doc/5449555-5687924.html.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.