aspose-cells / Aspose.Cells-for-.NET

Aspose.Cells for .NET examples, plugins and showcases
https://products.aspose.com/cells/net
MIT License
216 stars 103 forks source link

How to implete the chart which is a combination of two charts (Column and Line). #57

Closed ksq1063126847 closed 5 years ago

ksq1063126847 commented 5 years ago
  1. Here's what I want, Result
ksq1063126847 commented 5 years ago

This is the solution.

1. All code

public static void Run() { // Instantiating a Workbook object Workbook workbook = new Workbook();

        // Obtaining the reference of the newly added worksheet by passing its sheet index
        Worksheet worksheet = workbook.Worksheets[0];

        // Adding sample values to cells
        worksheet.Cells["A1"].PutValue("Product");
        worksheet.Cells["A2"].PutValue("A");
        worksheet.Cells["A3"].PutValue("B");
        worksheet.Cells["A4"].PutValue("C");
        worksheet.Cells["A5"].PutValue("D");

        worksheet.Cells["B1"].PutValue("Count");
        worksheet.Cells["B2"].PutValue(1);
        worksheet.Cells["B3"].PutValue(2);
        worksheet.Cells["B4"].PutValue(3);
        worksheet.Cells["B5"].PutValue(4);

        worksheet.Cells["C1"].PutValue("Percent");
        worksheet.Cells["C2"].PutValue(0.1);
        worksheet.Cells["C3"].PutValue(0.2);
        worksheet.Cells["C4"].PutValue(0.3);
        worksheet.Cells["C5"].PutValue(0.4);

        // Adding a chart to the worksheet
        int chartIndex = worksheet.Charts.Add(Aspose.Cells.Charts.ChartType.Column, 5, 0, 25, 10);

        // Accessing the instance of the newly added chart
        Aspose.Cells.Charts.Chart chart = worksheet.Charts[chartIndex];

        // Adding NSeries (chart data source) to the chart ranging from "B2" cell to "C5"
        chart.NSeries.Add("B2:C5", true);

        // Setting the chart type of 2nd NSeries to display as line chart
        chart.NSeries[0].Type = Aspose.Cells.Charts.ChartType.Column;
        chart.NSeries[1].Type = Aspose.Cells.Charts.ChartType.LineStackedWithDataMarkers;

        //设置 NSeries列名称
        chart.NSeries.CategoryData = "A2:A5";
        chart.NSeries[0].Name = "Product";
        chart.NSeries[1].Name = "Percent";

        //设置第二坐标轴
        chart.NSeries[1].PlotOnSecondAxis = true;
        chart.SecondValueAxis.IsVisible = true;

        //显示DataTable
        chart.ShowDataTable = true;

        // Saving the Excel file
        workbook.Save(outputDir + "outputHowToCreateCustomChart.xlsx");

        Console.WriteLine("HowToCreateCustomChart executed successfully.");
    }

2.Screenshot

Ok

amjad-sahi commented 5 years ago

Good to know that you have sorted it out now. Your chart is custom oriented which is a combination of two charts (Column and Line), so you have to write the code accordingly.