チャートの書式設定

グラフの外観設定

Aspose.Cellsが提供するチャートの種類では、提供される各種チャートやチャートオブジェクトについて簡単に紹介しました。

この記事では、次の異なるプロパティを設定することで、チャートの外観をカスタマイズする方法について説明します:

チャートエリアの設定

チャートにはさまざまな種類のエリアがあり、Aspose.Cellsでは各エリアの外観を変更する柔軟性を提供しています。開発者は、前景色、背景色、塗りつぶし形式などを変更することで、エリアに異なる書式設定を適用できます。

以下の例では、チャートの異なる種類のエリアに異なる書式設定を適用しました。

例のコードを実行すると、以下のようにワークシートに列のチャートが追加されます:

塗りつぶされたエリアのある列チャート

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.");

チャートラインの設定

開発者は、以下の例で示すように、SeriesCollection のラインやデータマーカーにさまざまなスタイルを適用できます。例のコードを実行すると、ワークシートに列のチャートが追加されます:

ラインスタイルを適用した列チャート

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のテーマをチャートに適用

開発者は、以下の例で示すように、SeriesCollection やその他のチャートオブジェクトに異なるMicrosoft Excelテーマと色を適用できます。

// 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");

チャートや軸のタイトルの設定

Microsoft Excelを使用して、チャートとその軸のタイトルをWYSIWYG環境で設定できます(以下のように表示されます)。

Microsoft Excelを使用してチャートとその軸のタイトルを設定する方法

todo:image_alt_text

また、Aspose.Cellsは開発者がランタイムでチャートとその軸のタイトルを設定することも可能です。すべてのチャートとその軸には、以下の例に示すようにタイトルを設定するために使用できる Title.setText メソッドが含まれています。例のコードを実行した後、ワークシートに列のチャートが以下のように追加されます。

タイトルを設定した列のチャート

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.");

メジャーグリッドラインの設定

メジャーグリッドラインの非表示

開発者は、Line オブジェクトの setVisible メソッドを使用してメジャーグリッドラインの表示を制御できます。メジャーグリッドラインを非表示にした後、ワークシートに追加された列のチャートは以下の外観になります。

メジャーグリッドラインが非表示の列のチャート

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);

メジャーグリッドラインの設定変更

開発者はメジャーグリッドラインの表示だけでなく、色などのその他のプロパティも制御できます。メジャーグリッドラインの色を設定した後、ワークシートに追加された列のチャートは以下の外観になります。

色付きメジャーグリッドラインを持つ列のチャート

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());

背面と側面の壁の境界線の設定

Microsoft Excel 2007のリリース以来、3Dチャートの壁は側壁と背面をそれぞれ表す2つのWallsオブジェクトがあります。これらは別々に表され、Chart.getBackWall()Chart.getSideWall()を使用してアクセスできます。

チャートの位置とサイズの変更

// 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());

チャートの位置とサイズの変更

時々、ワークシート内の新しいまたは既存のチャートの位置やサイズを変更したいことがあります。Aspose.Cellsはこれを達成するために Chart.getChartObject() プロパティを提供しています。新しい高さでチャートを再サイズしたり、新しいXY座標でチャートの再配置を行うためにそのサブプロパティを使用できます。

チャートの位置とサイズの変更

チャートの位置(X、Y座標)とサイズ(高さ、幅)を変更するには、以下のプロパティを使用します。

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

以下の例では、上記のプロパティの使用方法について説明しています。既存のワークブックを読み込み、最初のワークシートにチャートが含まれている場合、そのチャートのサイズを変更し、位置を移動させてからワークブックを保存します。

サンプルコードの実行前のソースファイルは以下のようになります。

サンプルコードの実行前のチャートのサイズと位置

todo:image_alt_text

サンプルコードの実行後、出力ファイルは以下のようになります。

サンプルコードの実行後のチャートのサイズと位置

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.");

デザイナーチャートの操作

デザイナーテンプレートファイルでチャートを操作または修正する必要がある場合があります。Aspose.Cellsは、その内容や要素とともにデザイナーチャートの操作を完全にサポートしています。データ、チャートの内容、背景画像、および書式設定を正確に保存できます。

テンプレートファイルでのデザイナーチャートの操作

テンプレートファイルでのデザイナーチャートを操作するには、チャート関連のすべてのAPI呼び出しを使用します。たとえば、テンプレートファイル内の既存のチャートコレクションを取得するために Worksheet.getCharts プロパティを使用します。

チャートの作成

以下の例では、円グラフの作成方法を示しています。後でこのチャートを操作します。以下の出力は、コードによって生成されます。

入力された円グラフ

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.");

チャートの操作

以下の例は、既存のチャートを操作する方法を示しています。この例では、上記で作成したチャートを変更します。以下は、コードによって生成された出力です。チャートタイトルの色が青から黒に変更され、‘England 30000’ が ‘United Kingdom, 30K’ に変更されていることに注意してください。

パイチャートが変更されました

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.");

デザイナーテンプレート内の折れ線グラフの操作

この例では、折れ線グラフを操作します。既存のチャートにいくつかのデータシリーズを追加し、それらの線の色を変更します。

まず、デザイナーの折れ線グラフを見てみましょう。

入力折れ線グラフ

todo:image_alt_text

今、以下のコードを使用して折れ線グラフ(linechart.xls ファイルに含まれている)を操作します。以下は、コードによって生成された出力です。

操作された折れ線グラフ

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.");

スパークラインの使用

Microsoft Excel 2010では、これまで以上に情報をさまざまな方法で分析できます。新しいデータ分析および可視化ツールを使用して重要なデータトレンドを追跡および強調することができます。スパークラインは、表内に配置してデータとグラフを同時に表示できるミニチャートです。スパークラインを適切に使用すると、データ分析が迅速かつ的確になります。また、情報のシンプルな表示を提供し、多くの複雑なチャートでワークシートが過度に混雑するのを避けることもできます。

Aspose.Cellsは、スプレッドシート内のスパークラインを操作するためのAPIを提供しています。

Microsoft Excelでのスパークライン

Microsoft Excel 2010にスパークラインを挿入するには:

  1. スパークラインが表示されるセルを選択します。視覚的にわかりやすくするため、データの横のセルを選択します。
  2. リボンの挿入を選択し、スパークライングループでを選択します。

todo:image_alt_text

  1. ソースデータを含むワークシートのセル範囲を選択または入力します。 これにより、チャートが表示されます。

スパークラインを使用すると、トレンドやソフトボールリーグの勝敗記録などの傾向を把握することができます。 スパークラインは、リーグ内の各チームのシーズン全体をまとめることさえできます。

todo:image_alt_text

Aspose.Cellsを使用したスパークライン

開発者は、Aspose.Cells が提供する API を使用して、テンプレートファイル内のスパークライン(を作成、削除、または読み取ることができます。特定のデータ範囲に異なるタイプの小さなチャートを追加することができます。

以下の例は、スパークライン機能を示しています。この例では次のことを示しています:

  1. 簡単なテンプレートファイルを開きます。
  2. ワークシートのスパークライン情報を読み取ります。 1.指定されたデータ範囲に新しいスパークラインをセル領域に追加します。
  3. Excel ファイルをディスクに保存します。
// 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.");

チャートに 3D フォーマットを適用する

シナリオに適した 3D チャートスタイルを取得できるかもしれません。 Aspose.Cells API は、この記事で示されているように、Microsoft Excel 2007 の 3D 書式設定を適用するための関連する API を提供しています。

チャートに 3D フォーマットを設定する

以下に、チャートを作成し、Microsoft Excel 2007 の 3D 書式を適用する方法を示す完全な例があります。上記の例コードを実行した後、ワークシートに 3D 効果のある列のチャートが追加されます。

3Dフォーマットを使用した縦型棒グラフ

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.");

高度なトピック