How to plot gradient line in Excel

Hi,
I need to plot a line chart in excel with the gradient filltype. Could someone please help with the sample code or the function with which we can achieve this. please refer following file.
ReferenceDoclet_withSingleCell.zip (22 Bytes)

testExport (62).zip (16.7 KB)

@VaradS

To plot a line chart in Excel with a gradient fill type using Aspose.Cells, you can follow the example below. This code demonstrates how to apply a gradient fill effect to a cell, which can be adapted for use in a chart.

Here’s a sample code snippet that shows how to create a workbook, add data, and apply a gradient fill effect:

import com.aspose.cells.Color;
import com.aspose.cells.GradientStyleType;
import com.aspose.cells.Style;
import com.aspose.cells.TextAlignmentType;
import com.aspose.cells.Workbook;
import com.aspose.cells.Worksheet;

public class GradientLineChart {
    public static void main(String[] args) throws Exception {
        // Create a new workbook
        Workbook workbook = new Workbook();
        // Get the first worksheet
        Worksheet worksheet = workbook.getWorksheets().get(0);
        
        // Input some sample data for the line chart
        worksheet.getCells().get("A1").putValue("X Values");
        worksheet.getCells().get("B1").putValue("Y Values");
        worksheet.getCells().get("A2").putValue(1);
        worksheet.getCells().get("B2").putValue(10);
        worksheet.getCells().get("A3").putValue(2);
        worksheet.getCells().get("B3").putValue(20);
        worksheet.getCells().get("A4").putValue(3);
        worksheet.getCells().get("B4").putValue(30);
        
        // Create a style for the gradient fill
        Style style = worksheet.getCells().get("B2").getStyle();
        style.setGradient(true);
        style.setTwoColorGradient(Color.fromArgb(255, 255, 255), Color.fromArgb(79, 129, 189), 
                                  GradientStyleType.HORIZONTAL, 1);
        worksheet.getCells().get("B2").setStyle(style);
        
        // Save the workbook
        workbook.save("GradientLineChart.xlsx");
    }
}

In this example:

  • A new workbook is created, and sample data is added to the first worksheet.
  • A gradient fill effect is applied to a specific cell (B2) using two colors.
  • Finally, the workbook is saved as “GradientLineChart.xlsx”.

You can modify the code to apply the gradient fill to the line chart itself by accessing the chart’s series and setting the fill properties accordingly. For more detailed customization, refer to the Aspose.Cells documentation.

If you have any specific requirements or need further assistance, feel free to ask!

@VaradS
Sorry. The compressed file you uploaded is an empty file after downloading. Would you like to re compress and upload the file here? We will check it soon.

@John.He
I have updated the file, could you please check now?

@John.He
Could you please confirm if you are able access the file now?

Hi @VaradS
I can now download two zip packages from your attachment.
1, “ReferenceDoclet_withSingleCell.zip” : This decompression is empty.
2, “testExport.zip”: This decompression has an xlsx file, as shown in the following figure:
Gradient.png (34.6 KB)

Are you wondering how to use Aspose.Cells to set this kind of Gradient line, is my understanding correct?

@leoluo
Yes, correct.

Hi @VaradS
Please try the following code:

        // Create a new workbook
        Workbook workbook = new Workbook();
        // Get the first worksheet
        Worksheet worksheet = workbook.Worksheets[0];

        // Input some sample data for the line chart
        worksheet.Cells["A1"].PutValue("X Values");
        worksheet.Cells["B1"].PutValue("Y Values");
        worksheet.Cells["A2"].PutValue(1);
        worksheet.Cells["B2"].PutValue(10);
        worksheet.Cells["A3"].PutValue(2);
        worksheet.Cells["B3"].PutValue(20);
        worksheet.Cells["A4"].PutValue(3);
        worksheet.Cells["B4"].PutValue(30);

        int chartIndex = worksheet.Charts.Add(ChartType.Line, 5, 0, 25, 8);
        Chart chart = worksheet.Charts[chartIndex];
        chart.NSeries.Add("A2:A4", true);
        chart.NSeries.CategoryData = "A2:A4";
        chart.NSeries.Add("B2:B4", true);
        chart.NSeries.CategoryData = "B2:B4";

        Series series = chart.NSeries[0];
        series.Border.FormattingType = ChartLineFormattingType.Gradient;
        series.Border.GradientFill.GradientStops.Add(0, Color.Red, 100);
        series.Border.GradientFill.GradientStops.Add(50, Color.Yellow, 100);
        series.Border.GradientFill.GradientStops.Add(100, Color.Green, 100);

        series = chart.NSeries[1];
        series.Border.FormattingType = ChartLineFormattingType.Gradient;
        series.Border.GradientFill.GradientStops.Add(0, Color.Red, 50);
        series.Border.GradientFill.GradientStops.Add(25, Color.Yellow, 100);
        series.Border.GradientFill.GradientStops.Add(70, Color.Blue, 50);
        series.Border.GradientFill.GradientStops.Add(100, Color.Black, 100);

        // Save the workbook
        workbook.Save(path + "GradientLineChart2.xlsx");

You can try to change the parameters of “GradientFill.GradientStops.Add” to get the effect you need. Its three parameters are: gradient position(0-100), gradient color, color transparency.

@leoluo
Thanks for the code.

@VaradS,

You are welcome. If you have any additional questions or feedback, please don’t hesitate to reach out to us again.