Create cool effects: Elegantly create Excel mini-charts with Java

Abstract: This article is original and first published by the Grape City technical team. Please indicate the source for reprinting: Grape City official website . Grape City provides developers with professional development tools, solutions and services to empower developers.

Preface

Sparklines are a concise and effective way of visualizing data, often used to show trends and changes. It usually consists of a set of small lines or bars used to represent changes in data. The main features of sparklines are that they take up little space and are easy to understand.

Sparklines are commonly used in data dashboards, reports, and presentations to show trends across multiple data sets in a limited space. Through sparklines, users can quickly analyze data trends and discover key information. Today I will introduce to you how to create Excel sparklines in Java.

1. Create sparklines in Java

There are three types of sparklines in Excel:

  • line chart
  • Column chart
  • profit and loss chart

Three types of sparklines can be easily created using the code below.

Workbook wb = new Workbook();
 IWorksheet sheet = wb.getActiveSheet();

 // 定义数据
 Object[][] data = new Object[][]
 {
   {1, -3, 2},
   {4, -6, 5},
   {7, -9, 8},
   {10, 12, -11}
 };
 sheet.getRange("A1:C4").setValue(data);

 // 添加迷你图
 sheet.getRange("D1:D4").getSparklineGroups().add(SparkType.Line, "A1:C4");
 sheet.getRange("E1:E4").getSparklineGroups().add(SparkType.Column, "A1:C4");
 sheet.getRange("F1:F4").getSparklineGroups().add(SparkType.ColumnStacked100, "A1:C4");
 wb.save("output/sparkline.xlsx");

The effect is shown in the figure below:

2. Create sparkline combinations in Java

In Excel, when you create multiple sparklines, a combination will be automatically added to them, and sparklines in the same combination will use the same settings (such as line type). When you select a sparkline, Excel will select the same combination of sparklines with a blue box.

Java also provides corresponding APIs to re-create combinations of existing sparklines, and to modify the configuration of sparklines through combinations.

(1) Add combinations to existing mini-pictures

// 创建workbook,并获取当前sheet
 Workbook wb = new Workbook();
 IWorksheet sheet = wb.getActiveSheet();
 // 定义数据区域
 Object[][] data = new Object[][]
 {
   { 1, 2, 3 },
   { 4, 5, 6 },
   { 7, 8, 9 },
   { 10, 11, 12 }
 };
 sheet.getRange("A1:C4").setValue(data);
 sheet.getRange("D1:D4").getSparklineGroups().add(SparkType.Line, "A1:C4");
 sheet.getRange("F1:H4").setValue(data);

 // 添加一组新的迷你图
 sheet.getRange("J1:J4").getSparklineGroups().add(SparkType.Column, "F1:H4");

 // 基于区域"J2"的迷你图设置,创建一个新的组合
 sheet.getRange("A1:J4").getSparklineGroups().group(sheet.getRange("J2"));
 wb.save("output/sparkline.xlsx");

The implementation effect is shown in the figure below. You can see that because of the newly created combination, the polyline mini-charts of D1:D4 have become column mini-charts.

(2) Modify the configuration and sparkline style of the combination

// 创建workbook,并获取当前sheet
 Workbook wb = new Workbook();
 IWorksheet sheet = wb.getActiveSheet();
 // 定义数据区域
 Object[][] data = new Object[][]
 {
   { 1, 2, 3 },
   { 4, 5, 6 },
   { 7, 8, 9 },
   { 10, 11, 12 }
 };
 sheet.getRange("A1:C4").setValue(data);
 // 添加迷你图
 sheet.getRange("D1:D4").getSparklineGroups().add(SparkType.Line, "A1:C4");
 // 定义日期数据
 Object[] date_data = new Object[]
 {
   new GregorianCalendar(2011, 11, 16),
   new GregorianCalendar(2011, 11, 17),
   new GregorianCalendar(2011, 11, 18)
 };
 sheet.getRange("A7:C7").setValue(date_data);

 // 修改迷你图的配置
 ISparklineGroup sparklinegroup = sheet.getRange("D1").getSparklineGroups().get(0);
 sparklinegroup.setLineWeight(2.5);
 sparklinegroup.getPoints().getMarkers().getColor().setColor(Color.GetRed());
 sparklinegroup.getPoints().getMarkers().setVisible(true);
 sparklinegroup.getSeriesColor().setColor(Color.GetPurple());
 wb.save("output/sparkline.xlsx");

The effect is shown in the figure below:

Summarize

The above is an introduction to the usage of sparklines. If you want to learn more about the gameplay and techniques of mini-graphs, you can refer to this help manual . The manual contains more usage of mini-graphs. Whether you are a beginner or an experienced professional, this help manual Will provide you with valuable guidance and assistance.

Extension link:

From form-driven to model-driven, interpret the development trend of low-code development platforms

What is a low-code development platform?

Branch-based version management helps low-code move from project delivery to customized product development

The author of the open source framework NanUI switched to selling steel, and the project was suspended. The first free list in the Apple App Store is the pornographic software TypeScript. It has just become popular, why do the big guys start to abandon it? TIOBE October list: Java has the biggest decline, C# is approaching Java Rust 1.73.0 Released A man was encouraged by his AI girlfriend to assassinate the Queen of England and was sentenced to nine years in prison Qt 6.6 officially released Reuters: RISC-V technology becomes the key to the Sino-US technology war New battlefield RISC-V: Not controlled by any single company or country, Lenovo plans to launch Android PC
{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/powertoolsteam/blog/10117333