Grafik Biçimlendirme

Grafik Görünümünü Ayarlama

Grafik Türleri‘nde, Aspose.Cells tarafından sunulan grafik türleri ve grafik nesnelerine kısaca bir giriş yapmıştık.

Bu makalede, farklı özelliklerin ayarlanmasıyla grafiklerin görünümünün nasıl özelleştirileceğini tartışıyoruz:

Grafik Alanını Ayarlama

Bir grafikte farklı türde alanlar bulunmakta ve Aspose.Cells her bir alanın görünümünü değiştirme esnekliği sağlamaktadır. Geliştiriciler, bir alan üzerinde farklı biçimlendirme ayarları uygulayarak öncelikli renk, arka plan rengi ve doldurma formatı gibi özellikleri değiştirebilirler.

Aşağıdaki örnekte, bir grafikte farklı türde alanlara farklı biçimlendirme ayarları uyguladık. Bu alanlar şunları içerir:

Örnek kodu çalıştırdıktan sonra aşağıdaki gibi bir sütun grafiği çalışma sayfasına eklenecektir:

Renkli alanlara sahip sütun grafiği

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(SettingChartArea.class) + "charts/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
WorksheetCollection worksheets = workbook.getWorksheets();
// Obtaining the reference of the newly added worksheet by passing its
// sheet index
Worksheet worksheet = worksheets.get(0);
Cells cells = worksheet.getCells();
// Adding a sample value to "A1" cell
cells.get("A1").setValue(50);
// Adding a sample value to "A2" cell
cells.get("A2").setValue(100);
// Adding a sample value to "A3" cell
cells.get("A3").setValue(150);
// Adding a sample value to "B1" cell
cells.get("B1").setValue(60);
// Adding a sample value to "B2" cell
cells.get("B2").setValue(32);
// Adding a sample value to "B3" cell
cells.get("B3").setValue(50);
// Adding a chart to the worksheet
ChartCollection charts = worksheet.getCharts();
// Accessing the instance of the newly added chart
int chartIndex = charts.add(ChartType.COLUMN, 5, 0, 15, 7);
Chart chart = charts.get(chartIndex);
// Adding NSeries (chart data source) to the chart ranging from "A1"
// cell
SeriesCollection nSeries = chart.getNSeries();
nSeries.add("A1:B3", true);
// Setting the foreground color of the plot area
ChartFrame plotArea = chart.getPlotArea();
Area area = plotArea.getArea();
area.setForegroundColor(Color.getBlue());
// Setting the foreground color of the chart area
ChartArea chartArea = chart.getChartArea();
area = chartArea.getArea();
area.setForegroundColor(Color.getYellow());
// Setting the foreground color of the 1st NSeries area
Series aSeries = nSeries.get(0);
area = aSeries.getArea();
area.setForegroundColor(Color.getRed());
// Setting the foreground color of the area of the 1st NSeries point
ChartPointCollection chartPoints = aSeries.getPoints();
ChartPoint point = chartPoints.get(0);
point.getArea().setForegroundColor(Color.getCyan());
// Save the Excel file
workbook.save(dataDir + "SettingChartArea_out.xls");
// Print message
System.out.println("ChartArea is settled successfully.");

Grafik Çizgilerini Ayarlama

Geliştiriciler, örnekte gösterildiği gibi SeriesCollection veya diğer grafik nesnelerinin çizgilerine veya veri işaretçilerine farklı stili uygulayabilirler. Örnek kodu çalıştırdıktan sonra çalışma sayfasına bir sütun grafiği eklenir:

Çizgi stilleri uygulandıktan sonra sütun grafiği

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(SettingChartLines.class) + "charts/";
// Instantiating a Workbook object
Workbook workbook = new Workbook(dataDir + "book1.xls");
WorksheetCollection worksheets = workbook.getWorksheets();
// Obtaining the reference of the newly added worksheet by passing its
// sheet index
Worksheet worksheet = worksheets.get(0);
Cells cells = worksheet.getCells();
// Adding a chart to the worksheet
ChartCollection charts = worksheet.getCharts();
Chart chart = charts.get(0);
// Adding NSeries (chart data source) to the chart ranging from "A1"
// cell
SeriesCollection nSeries = chart.getNSeries();
nSeries.add("A1:B3", true);
Series aSeries = nSeries.get(0);
Line line = aSeries.getSeriesLines();
line.setStyle(LineType.DOT);
// Applying a triangular marker style on the data markers of an NSeries
aSeries.getMarker().setMarkerStyle(ChartMarkerType.TRIANGLE);
// Setting the weight of all lines in an NSeries to medium
aSeries = nSeries.get(1);
line = aSeries.getSeriesLines();
line.setWeight(WeightType.MEDIUM_LINE);
// Save the Excel file
workbook.save(dataDir + "SettingChartLines_out.xls");
// Print message
System.out.println("ChartArea is settled successfully.");

Microsoft Excel 2007/2010 Temalarını Grafiklere Uygulama

Geliştiriciler, aşağıdaki örnekte gösterildiği gibi SeriesCollection veya diğer grafik nesnelerine farklı Microsoft Excel temaları ve renkleri uygulayabilirler.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(ApplyingThemes.class) + "charts/";
// Instantiate the workbook to open the file that contains a chart
Workbook workbook = new Workbook(dataDir + "book1.xls");
// Get the first worksheet
Worksheet worksheet = workbook.getWorksheets().get(0);
// Get the first chart in the sheet
Chart chart = worksheet.getCharts().get(0);
// Specify the FilFormat's type to Solid Fill of the first series
chart.getNSeries().get(0).getArea().getFillFormat().setFillType(FillType.SOLID);
// Get the CellsColor of SolidFill
CellsColor cc = chart.getNSeries().get(0).getArea().getFillFormat().getSolidFill().getCellsColor();
// Create a theme in Accent style
cc.setThemeColor(new ThemeColor(ThemeColorType.ACCENT_6, 0.6));
// Apply the them to the series
chart.getNSeries().get(0).getArea().getFillFormat().getSolidFill().setCellsColor(cc);
// Save the Excel file
workbook.save(dataDir + "AThemes_out.xlsx");

Grafiğin veya Eksenlerin Başlıklarını Ayarlama

Grafik ve ekselerin başlıkları Excel’i kullanarak aşağıdaki gibi bir WYSIWYG ortamında ayarlanabilir.

Grafik başlıklarının ve ekselertitslerinin Microsoft Excel kullanarak ayarlanması

todo:image_alt_text

Aspose.Cells ayrıca geliştiricilere grafiklerin ve ekselerinin başlıklarını çalışma zamanında belirleme imkanı sağlar. Tüm grafiklerin ve ekselerinin bir Title.setText yöntemi bulunur ve bu yöntem örnekte gösterildiği gibi başlıklarını belirlemek için kullanılabilir. Örnek kodu çalıştırdıktan sonra çalışma sayfasına aşağıdaki gibi bir sütun grafiği eklenecektir:

Başlık belirlendikten sonraki sütun grafiği

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(SettingTitlesAxes.class) + "charts/";
// Instantiating a Workbook object
Workbook workbook = new Workbook(dataDir + "book1.xls");
WorksheetCollection worksheets = workbook.getWorksheets();
// Obtaining the reference of the newly added worksheet by passing its
// sheet index
Worksheet worksheet = worksheets.get(0);
Cells cells = worksheet.getCells();
// Adding a chart to the worksheet
ChartCollection charts = worksheet.getCharts();
// Accessing the instance of the newly added chart
int chartIndex = charts.add(ChartType.COLUMN, 5, 0, 15, 7);
Chart chart = charts.get(chartIndex);
// Setting the title of a chart
Title title = chart.getTitle();
title.setText("ASPOSE");
// Setting the font color of the chart title to blue
Font font = title.getFont();
font.setColor(Color.getBlue());
// Setting the title of category axis of the chart
Axis categoryAxis = chart.getCategoryAxis();
title = categoryAxis.getTitle();
title.setText("Category");
// Setting the title of value axis of the chart
Axis valueAxis = chart.getValueAxis();
title = valueAxis.getTitle();
title.setText("Value");
// Adding NSeries (chart data source) to the chart ranging from "A1"
// cell
SeriesCollection nSeries = chart.getNSeries();
nSeries.add("A1:B3", true);
// Setting the foreground color of the plot area
ChartFrame plotArea = chart.getPlotArea();
Area area = plotArea.getArea();
area.setForegroundColor(Color.getBlue());
// Setting the foreground color of the chart area
ChartArea chartArea = chart.getChartArea();
area = chartArea.getArea();
area.setForegroundColor(Color.getYellow());
// Setting the foreground color of the 1st NSeries area
Series aSeries = nSeries.get(0);
area = aSeries.getArea();
area.setForegroundColor(Color.getRed());
// Setting the foreground color of the area of the 1st NSeries point
ChartPointCollection chartPoints = aSeries.getPoints();
ChartPoint point = chartPoints.get(0);
point.getArea().setForegroundColor(Color.getCyan());
// Save the Excel file
workbook.save(dataDir + "SettingTitlesAxes_out.xls");
// Print message
System.out.println("Chart Title is changed successfully.");

Önemli Izgaraları Belirleme

Büyük Izgaraları Gizleme

Geliştiriciler, Line nesnesinin setVisible yöntemini kullanarak önemli izgaraların görünürlüğünü kontrol edebilirler. Önemli izgaraları gizledikten sonra çalışma sayfasına eklenen sütun grafiğinin aşağıdaki görünüme sahip olacaktır:

Önemli izgaraları gizlenmiş sütun grafiği

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// Hiding the major gridlines of value axis
Axis valueAxis = chart.getValueAxis();
Line majorGridLines = valueAxis.getMajorGridLines();
majorGridLines.setVisible(false);

Büyük Izgaraların Ayarlarını Değiştirme

Geliştiriciler sadece önemli izgaraların görünürlüğünü değil ayrıca rengi gibi diğer özelliklerini de ayarlayabilirler. Önemli izgaraların rengini ayarladıktan sonra çalışma sayfasına eklenen sütun grafiğinin aşağıdaki görünüme sahip olacağı:

Renkli önemli izgaralı sütun grafiği

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// Setting the color of major gridlines of value axis to silver
Axis categoryAxis = chart.getCategoryAxis();
categoryAxis.getMajorGridLines().setColor(Color.getSilver());

Arka ve Yan Duvarlar için Sınırlar Belirleme

Microsoft Excel 2007’nin piyasaya sürülmesinden beri, 3D grafiklerin duvarları ikiye bölünmüştür: yan duvar ve arka duvar, bu yüzden bunları ayrı ayrı temsil etmek için iki Walls nesnesi kullanmalı ve onlara erişmek için Chart.getBackWall() ve Chart.getSideWall() kullanabilirsiniz.

Aşağıdaki örnek, yan duvarın kenarını farklı özellikleri kullanarak nasıl ayarlayacağınızı gösterir.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// Get the side wall border line
Line sideLine = chart.getSideWall().getBorder();
// Make it visible
sideLine.setVisible(true);
// Set the solid line
sideLine.setStyle(LineType.SOLID);
// Set the line width
sideLine.setWeight(10);
// Set the color
sideLine.setColor(Color.getBlack());

Grafik Pozisyonunu ve Boyutunu Değiştirme

Bazen, çalışma sayfası içindeki yeni veya mevcut grafiğin konumunu veya boyutunu değiştirmek isteyebilirsiniz. Aspose.Cells, bunu başarmak için Chart.getChartObject() özelliğini sağlar. Yeni yükseklik ve genişlik ile grafik boyutunu veya yeni X ve Y koordinatları ile konumunu ayarlamak için alt özelliklerini kullanabilirsiniz.

Grafiğin Konumunu ve Boyutunu Değiştirme

Grafiğin pozisyonunu (X, Y koordinatları) ve boyutunu (yükseklik, genişlik) değiştirmek için bu özellikleri kullanın:

  1. Chart.getChartObject().get/setWidth()
  2. Chart.getChartObject().get/setHeight()
  3. Chart.getChartObject().get/setX()
  4. Chart.getChartObject().get/setY()

Aşağıdaki örnek yukarıdaki özelliklerin kullanımını açıklar. İlk çalışma sayfasında bir grafiği içeren mevcut bir çalışma kitabını yükler. Ardından grafiği yeniden boyutlandırır ve konumlandırır ve çalışma kitabını kaydeder.

Örnek kodun yürütülmeden önce, kaynak dosya şu şekildedir:

Örnek kodun yürütülmeden önce grafiğin boyutu ve konumu

todo:image_alt_text

Yürütüldükten sonra, çıktı dosyası şu şekildedir:

Örnek kodun yürütüldükten sonra grafiğin boyutu ve konumu

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(ChangeChartPositionAndSize.class) + "charts/";
String filePath = dataDir + "book1.xls";
Workbook workbook = new Workbook(filePath);
Worksheet worksheet = workbook.getWorksheets().get(0);
// Load the chart from source worksheet
Chart chart = worksheet.getCharts().get(0);
// Resize the chart
chart.getChartObject().setWidth(400);
chart.getChartObject().setHeight(300);
// Reposition the chart
chart.getChartObject().setX(250);
chart.getChartObject().setY(150);
// Output the file
workbook.save(dataDir + "ChangeChartPositionAndSize_out.xls");
// Print message
System.out.println("Position and Size of Chart is changed successfully.");

Tasarımcı Grafikleri Manipüle Etmek

Tasarım şablonu dosyalarınızda grafikleri manipüle etmeniz gereken bir zamandır. Aspose.Cells, içeriği ve öğeleriyle tasarım grafiklerini tamamen destekler. Veri, grafik içeriği, arka plan resmi ve biçimlendirme doğrulukla korunabilir.

Tasarım Şablonlarında Grafikleri Manipüle Etme

Tasarım şablonu dosyasındaki grafikleri manipüle etmek için tüm grafikle ilgili API çağrılarını kullanın. Örneğin, mevcut grafikleri almak için Worksheet.getCharts özelliğini kullanın.

Bir Grafik Oluşturma

Aşağıdaki örnek, bir pasta grafiği oluşturmanın nasıl yapıldığını gösterir. Bu grafiği daha sonra manipüle edeceğiz. Aşağıdaki çıktı kod tarafından oluşturulmuştur.

Giriş pasta grafiği

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(CreateChart.class) + "charts/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Obtaining the reference of the first worksheet
WorksheetCollection worksheets = workbook.getWorksheets();
Worksheet sheet = worksheets.get(0);
// Adding some sample value to cells
Cells cells = sheet.getCells();
Cell cell = cells.get("A1");
cell.setValue(50);
cell = cells.get("A2");
cell.setValue(100);
cell = cells.get("A3");
cell.setValue(150);
cell = cells.get("B1");
cell.setValue(4);
cell = cells.get("B2");
cell.setValue(20);
cell = cells.get("B3");
cell.setValue(50);
ChartCollection charts = sheet.getCharts();
// Adding a chart to the worksheet
int chartIndex = charts.add(ChartType.PYRAMID, 5, 0, 15, 5);
Chart chart = charts.get(chartIndex);
// Adding NSeries (chart data source) to the chart ranging from "A1"
// cell to "B3"
SeriesCollection serieses = chart.getNSeries();
serieses.add("A1:B3", true);
// Saving the Excel file
workbook.save(dataDir + "CreateChart_out.xls");
// Print message
System.out.println("Workbook with chart is successfully created.");

Grafiği Manipüle Etmek

Aşağıdaki örnek, mevcut grafiği nasıl manipüle edeceğinizi gösterir. Bu örnekte yukarıda oluşturulan grafiği değiştiriyoruz. Aşağıdaki çıktı kod tarafından oluşturulmuştur. Grafik başlığının renginin maviden siyaha değiştiğine dikkat edin ve ‘England 30000’ ‘United Kingdom, 30K’ olarak değiştirildi.

Pasta grafiği değiştirildi

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(ModifyPieChart.class) + "charts/";
// Instantiating a Workbook object
Workbook workbook = new Workbook(dataDir + "ModifyCharts.xlsx");
// Obtaining the reference of the first worksheet
WorksheetCollection worksheets = workbook.getWorksheets();
Worksheet sheet = worksheets.get(1);
// Load the chart from source worksheet
Chart chart = sheet.getCharts().get(0);
DataLabels datalabels = chart.getNSeries().get(0).getPoints().get(0).getDataLabels();
datalabels.setText("aspose");
// Saving the Excel file
workbook.save(dataDir + "ModifyPieChart_out.xls");
// Print message
System.out.println("Pie chart is successfully modified.");

Tasarımcı Şablonunda Bir Çizgi Grafiği Manipüle Etmek

Bu örnekte, bir çizgi grafiği manipüle edeceğiz. Mevcut grafiğe bazı veri serileri ekleyeceğiz ve bunların çizgi renklerini değiştireceğiz.

İlk olarak, tasarımcıdaki hat grafiğine bakın.

Giriş hat grafiği

todo:image_alt_text

Şimdi, aşağıdaki kodu kullanarak hat grafiğini manipüle ediyoruz (linechart.xls dosyasında bulunan). Aşağıdaki çıktı kod tarafından oluşturulmuştur.

Manipüle edilen hat grafiği

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(ModifyLineChart.class) + "charts/";
// Instantiating a Workbook object
Workbook workbook = new Workbook(dataDir + "book1.xls");
// Obtaining the reference of the first worksheet
WorksheetCollection worksheets = workbook.getWorksheets();
Worksheet sheet = worksheets.get(0);
// Load the chart from source worksheet
Chart chart = sheet.getCharts().get(0);
// Adding NSeries (chart data source) to the chart ranging from "A1"
// cell to "B3"
SeriesCollection serieses = chart.getNSeries();
serieses.add("{20,40,90}", true);
serieses.add("{110,70,220}", true);
// Saving the Excel file
workbook.save(dataDir + "ModifyLineChart_out.xls");
// Print message
System.out.println("Line chart is successfully modified.");

Sparklines Kullanma

Microsoft Excel 2010, bilgileri daha önce hiç olmadığı kadar fazla şekilde analiz etmenizi sağlar. Kullanıcıların yeni veri analiz ve görselleştirme araçlarıyla önemli veri eğilimlerini takip etmesine ve vurgulamasına izin verir. Sparklines, veriyi ve tabloyu aynı anda görüntüleyebileceğiniz mini grafiklerdir. Sparklines uygun şekilde kullanıldığında, veri analizi daha hızlı ve daha anlaşılır olur. Ayrıca, aşırı kalabalık çalışma tablolarını çok fazla meşgul grafiklerle önler. Onlar, aynı tabloda veriyi görmek için basit bir görünüm sağlar. Ayrıca, Aspose.Cells, elektronik tablolardaki sparklines’ı manipüle etmek için bir API sağlar.

Aspose.Cells, elektronik tablolardaki sparklines’ları manipüle etmek için bir API sağlar.

Microsoft Excel’de Sparklines Kullanma

Microsoft Excel 2010’da Sparklines eklemek için:

  1. Sparklines’ların görünmesini istediğiniz hücreleri seçin. Onları görüntülemeyi kolaylaştırmak için, verinin yanındaki hücreleri seçin.
  2. Menü şeridinde Ekle‘yi tıklayın, ardından Sparklines grubunda Sütun‘u seçin.

todo:image_alt_text

  1. Kaynak verileri içeren çalışma sayfasındaki hücrelerin aralığını seçin veya girin. Grafikler görünür.

Kıvılcım çizgileri, örneğin trendleri veya bir ​​beyzbol ligi için kazanma veya kaybetme kaydını görmeye yardımcı olur. Kıvılcım çizgileri, ligin her takımının tüm sezonunu hatta toplayabilir.

todo:image_alt_text

Aspose.Cells, kullanıcıların verilen veri aralığı için özel grafikleri ekleyerek seçilen hücre alanlarına farklı tipte minik grafikler ekleyebilecekleri özgürlüğü sunar.

Geliştiriciler, Aspose.Cells tarafından sağlanan API ile şablon dosyasındaki kıvılcım çizgilerini oluşturabilir, silebilir veya okuyabilir. Belirli bir veri aralığı için özel grafikler ekleyerek, geliştiriciler seçilen hücre alanlarına farklı tipte küçük grafikler eklemek için özgürlüğe sahiptir.

Aşağıdaki örnek, Sparklines özelliğini sergiler. Örnek, şunları gösterir:

  1. Basit bir şablon dosyasını açın.
  2. Bir çalışma sayfası için sparklines bilgilerini okuyun.
  3. Belirli bir veri aralığı için yeni sparklines ekleyin.
  4. Excel dosyasını diske kaydedin.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(UsingSparklines.class) + "charts/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
WorksheetCollection worksheets = workbook.getWorksheets();
// Obtaining the reference of the first worksheet
Worksheet worksheet = worksheets.get(0);
Cells cells = worksheet.getCells();
System.out.println("Sparkline count: " + worksheet.getSparklineGroupCollection().getCount());
for (int i = 0; i < worksheet.getSparklineGroupCollection().getCount(); i++) {
SparklineGroup g = worksheet.getSparklineGroupCollection().get(i);
System.out.println("sparkline group: type:" + g.getType());
for (int j = 0; j < g.getSparklineCollection().getCount(); i++) {
Sparkline gg = g.getSparklineCollection().get(i);
System.out.println("sparkline: row:" + gg.getRow() + ", col:" + gg.getColumn() + ", dataRange:"
+ gg.getDataRange());
}
}
// Add Sparklines
// Define the CellArea D2:D10
CellArea ca = new CellArea();
ca.StartColumn = 4;
ca.EndColumn = 4;
ca.StartRow = 1;
ca.EndRow = 7;
int idx = worksheet.getSparklineGroupCollection().add(SparklineType.COLUMN, "Sheet1!B2:D8", false, ca);
SparklineGroup group = worksheet.getSparklineGroupCollection().get(idx);
// Create CellsColor
CellsColor clr = workbook.createCellsColor();
clr.setColor(Color.getChocolate());
group.setSeriesColor(clr);
workbook.save(dataDir + "UsingSparklines_out.xls");
// Print message
System.out.println("Workbook with chart is created successfully.");

Grafiğe 3D Format Uygulama

Senaryonuz için sadece sonuçları alabilmeniz için 3D grafik stili gerekebilir. Aspose.Cells API’leri, bu makalede gösterildiği gibi Microsoft Excel 2007 3D biçimlendirmesini uygulamak için ilgili API’yi sağlar.

Grafiğe 3D Format Ayarlama

Microsoft Excel 2007 3D biçimlendirmesi uygulamak için aşağıda tam bir örnek verilmiştir. Yukarıdaki örnek kodu çalıştırdıktan sonra, aşağıda gösterildiği gibi bir sütun grafiği (3D efektlerle) çalışma sayfasına eklenecektir.

3D biçimlendirmeli sütun grafiği

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(Applying3DFormat.class) + "charts/";
// Instantiate a new Workbook
Workbook book = new Workbook();
// Add a Data Worksheet
Worksheet dataSheet = book.getWorksheets().add("DataSheet");
// Add Chart Worksheet
Worksheet sheet = book.getWorksheets().add("MyChart");
// Put some values into the cells in the data worksheet
dataSheet.getCells().get("B1").putValue(1);
dataSheet.getCells().get("B2").putValue(2);
dataSheet.getCells().get("B3").putValue(3);
dataSheet.getCells().get("A1").putValue("A");
dataSheet.getCells().get("A2").putValue("B");
dataSheet.getCells().get("A3").putValue("C");
// Define the Chart Collection
ChartCollection charts = sheet.getCharts();
// Add a Column chart to the Chart Worksheet
int chartSheetIdx = charts.add(ChartType.COLUMN, 5, 0, 25, 15);
// Get the newly added Chart
Chart chart = book.getWorksheets().get(2).getCharts().get(0);
// Set the background/foreground color for PlotArea/ChartArea
chart.getPlotArea().getArea().setBackgroundColor(Color.getWhite());
chart.getChartArea().getArea().setBackgroundColor(Color.getWhite());
chart.getPlotArea().getArea().setForegroundColor(Color.getWhite());
chart.getChartArea().getArea().setForegroundColor(Color.getWhite());
// Hide the Legend
chart.setShowLegend(false);
// Add Data Series for the Chart
chart.getNSeries().add("DataSheet!B1:B3", true);
// Specify the Category Data
chart.getNSeries().setCategoryData("DataSheet!A1:A3");
// Get the Data Series
Series ser = chart.getNSeries().get(0);
// Apply the 3D formatting
ShapePropertyCollection spPr = ser.getShapeProperties();
Format3D fmt3d = spPr.getFormat3D();
// Specify Bevel with its height/width
Bevel bevel = fmt3d.getTopBevel();
bevel.setType(BevelPresetType.CIRCLE);
bevel.setHeight(5);
bevel.setWidth(9);
// Specify Surface material type
fmt3d.setSurfaceMaterialType(PresetMaterialType.WARM_MATTE);
// Specify surface lighting type
fmt3d.setSurfaceLightingType(LightRigType.THREE_POINT);
// Specify lighting angle
fmt3d.setLightingAngle(20);
// Specify Series background/foreground and line color
ser.getArea().setBackgroundColor(Color.getMaroon());
ser.getArea().setForegroundColor(Color.getMaroon());
ser.getBorder().setColor(Color.getMaroon());
// Save the Excel file
book.save(dataDir + "A3DFormat_out.xls");
// Print message
System.out.println("3D format is applied successfully.");

Gelişmiş Konular