.NET でプレゼンテーションチャートの書式設定

チャートエンティティの書式設定

Aspose.Slides for .NET は、開発者がスライドにカスタムチャートをゼロから追加できるようにします。この記事では、チャートのカテゴリ軸や値軸を含むさまざまなチャートエンティティの書式設定方法を説明します。

Aspose.Slides for .NET は、さまざまなチャートエンティティを管理し、カスタム値で書式設定するためのシンプルな API を提供します。

  1. Presentation クラスのインスタンスを作成します。
  2. インデックスでスライドの参照を取得します。
  3. デフォルトデータと任意のタイプのチャートを追加します(この例では ChartType.LineWithMarkers を使用します)。
  4. チャートの値軸にアクセスし、次のプロパティを設定します:
    1. 値軸の主要グリッド線の Line format を設定します。
    2. 値軸の副次グリッド線の Line format を設定します。
    3. 値軸の Number Format を設定します。
    4. 値軸の Min, Max, Major and Minor units を設定します。
    5. 値軸データの Text Properties を設定します。
    6. 値軸の Title を設定します。
    7. 値軸の Line Format を設定します。
  5. チャートのカテゴリ軸にアクセスし、次のプロパティを設定します:
    1. カテゴリ軸の主要グリッド線の Line format を設定します。
    2. カテゴリ軸の副次グリッド線の Line format を設定します。
    3. カテゴリ軸データの Text Properties を設定します。
    4. カテゴリ軸の Title を設定します。
    5. カテゴリ軸の Label Positioning を設定します。
    6. カテゴリ軸ラベルの Rotation Angle を設定します。
  6. チャートの凡例にアクセスし、Text Properties を設定します。
  7. チャートと重ならないように凡例を表示設定します。
  8. チャートの Secondary Value Axis にアクセスし、次のプロパティを設定します:
    1. 副次 Value Axis を有効にします。
    2. 副次値軸の Line Format を設定します。
    3. 副次値軸の Number Format を設定します。
    4. 副次値軸の Min, Max, Major and Minor units を設定します。
  9. それから、最初のチャート系列を副次値軸に描画します。
  10. チャートの背面壁の塗りつぶし色を設定します。
  11. チャートのプロット領域の塗りつぶし色を設定します。
  12. 変更されたプレゼンテーションを書き出して PPTX ファイルに保存します。
// プレゼンテーションのインスタンス化
Presentation pres = new Presentation();

// 最初のスライドにアクセス
ISlide slide = pres.Slides[0];

// サンプルチャートを追加
IChart chart = slide.Shapes.AddChart(ChartType.LineWithMarkers, 50, 50, 500, 400);

// チャートタイトルの設定
chart.HasTitle = true;
chart.ChartTitle.AddTextFrameForOverriding("");
IPortion chartTitle = chart.ChartTitle.TextFrameForOverriding.Paragraphs[0].Portions[0];
chartTitle.Text = "Sample Chart";
chartTitle.PortionFormat.FillFormat.FillType = FillType.Solid;
chartTitle.PortionFormat.FillFormat.SolidFillColor.Color = Color.Gray;
chartTitle.PortionFormat.FontHeight = 20;
chartTitle.PortionFormat.FontBold = NullableBool.True;
chartTitle.PortionFormat.FontItalic = NullableBool.True;

// 値軸の主要グリッド線の書式設定
chart.Axes.VerticalAxis.MajorGridLinesFormat.Line.FillFormat.FillType = FillType.Solid;
chart.Axes.VerticalAxis.MajorGridLinesFormat.Line.FillFormat.SolidFillColor.Color = Color.Blue;
chart.Axes.VerticalAxis.MajorGridLinesFormat.Line.Width = 5;
chart.Axes.VerticalAxis.MajorGridLinesFormat.Line.DashStyle = LineDashStyle.DashDot;

// 値軸の副次グリッド線の書式設定
chart.Axes.VerticalAxis.MinorGridLinesFormat.Line.FillFormat.FillType = FillType.Solid;
chart.Axes.VerticalAxis.MinorGridLinesFormat.Line.FillFormat.SolidFillColor.Color = Color.Red;
chart.Axes.VerticalAxis.MinorGridLinesFormat.Line.Width = 3;

// 値軸の数値書式設定
chart.Axes.VerticalAxis.IsNumberFormatLinkedToSource = false;
chart.Axes.VerticalAxis.DisplayUnit = DisplayUnitType.Thousands;
chart.Axes.VerticalAxis.NumberFormat = "0.0%";

// チャートの最大・最小値の設定
chart.Axes.VerticalAxis.IsAutomaticMajorUnit = false;
chart.Axes.VerticalAxis.IsAutomaticMaxValue = false;
chart.Axes.VerticalAxis.IsAutomaticMinorUnit = false;
chart.Axes.VerticalAxis.IsAutomaticMinValue = false;

chart.Axes.VerticalAxis.MaxValue = 15f;
chart.Axes.VerticalAxis.MinValue = -2f;
chart.Axes.VerticalAxis.MinorUnit = 0.5f;
chart.Axes.VerticalAxis.MajorUnit = 2.0f;

// 値軸のテキストプロパティ設定
IChartPortionFormat txtVal = chart.Axes.VerticalAxis.TextFormat.PortionFormat;
txtVal.FontBold = NullableBool.True;
txtVal.FontHeight = 16;
txtVal.FontItalic = NullableBool.True;
txtVal.FillFormat.FillType = FillType.Solid; ;
txtVal.FillFormat.SolidFillColor.Color = Color.DarkGreen;
txtVal.LatinFont = new FontData("Times New Roman");

// 値軸のタイトル設定
chart.Axes.VerticalAxis.HasTitle = true;
chart.Axes.VerticalAxis.Title.AddTextFrameForOverriding("");
IPortion valtitle = chart.Axes.VerticalAxis.Title.TextFrameForOverriding.Paragraphs[0].Portions[0];
valtitle.Text = "Primary Axis";
valtitle.PortionFormat.FillFormat.FillType = FillType.Solid;
valtitle.PortionFormat.FillFormat.SolidFillColor.Color = Color.Gray;
valtitle.PortionFormat.FontHeight = 20;
valtitle.PortionFormat.FontBold = NullableBool.True;
valtitle.PortionFormat.FontItalic = NullableBool.True;

// 値軸のライン書式設定 : 現在は廃止
// chart.Axes.VerticalAxis.aVerticalAxis.l.AxisLine.Width = 10;
// chart.Axes.VerticalAxis.AxisLine.FillFormat.FillType = FillType.Solid;
// Chart.Axes.VerticalAxis.AxisLine.FillFormat.SolidFillColor.Color = Color.Red;

// カテゴリ軸の主要グリッド線の書式設定
chart.Axes.HorizontalAxis.MajorGridLinesFormat.Line.FillFormat.FillType = FillType.Solid;
chart.Axes.HorizontalAxis.MajorGridLinesFormat.Line.FillFormat.SolidFillColor.Color = Color.Green;
chart.Axes.HorizontalAxis.MajorGridLinesFormat.Line.Width = 5;

// カテゴリ軸の副次グリッド線の書式設定
chart.Axes.HorizontalAxis.MinorGridLinesFormat.Line.FillFormat.FillType = FillType.Solid;
chart.Axes.HorizontalAxis.MinorGridLinesFormat.Line.FillFormat.SolidFillColor.Color = Color.Yellow;
chart.Axes.HorizontalAxis.MinorGridLinesFormat.Line.Width = 3;

// カテゴリ軸のテキストプロパティ設定
IChartPortionFormat txtCat = chart.Axes.HorizontalAxis.TextFormat.PortionFormat;
txtCat.FontBold = NullableBool.True;
txtCat.FontHeight = 16;
txtCat.FontItalic = NullableBool.True;
txtCat.FillFormat.FillType = FillType.Solid; ;
txtCat.FillFormat.SolidFillColor.Color = Color.Blue;
txtCat.LatinFont = new FontData("Arial");

// カテゴリタイトルの設定
chart.Axes.HorizontalAxis.HasTitle = true;
chart.Axes.HorizontalAxis.Title.AddTextFrameForOverriding("");

IPortion catTitle = chart.Axes.HorizontalAxis.Title.TextFrameForOverriding.Paragraphs[0].Portions[0];
catTitle.Text = "Sample Category";
catTitle.PortionFormat.FillFormat.FillType = FillType.Solid;
catTitle.PortionFormat.FillFormat.SolidFillColor.Color = Color.Gray;
catTitle.PortionFormat.FontHeight = 20;
catTitle.PortionFormat.FontBold = NullableBool.True;
catTitle.PortionFormat.FontItalic = NullableBool.True;

// カテゴリ軸ラベル位置の設定
chart.Axes.HorizontalAxis.TickLabelPosition = TickLabelPositionType.Low;

// カテゴリ軸ラベルの回転角度設定
chart.Axes.HorizontalAxis.TickLabelRotationAngle = 45;

// 凡例のテキストプロパティ設定
IChartPortionFormat txtleg = chart.Legend.TextFormat.PortionFormat;
txtleg.FontBold = NullableBool.True;
txtleg.FontHeight = 16;
txtleg.FontItalic = NullableBool.True;
txtleg.FillFormat.FillType = FillType.Solid; ;
txtleg.FillFormat.SolidFillColor.Color = Color.DarkRed;

// 凡例がチャートと重ならないように表示設定

chart.Legend.Overlay = true;
            
// 二次値軸に最初の系列をプロット
// Chart.ChartData.Series[0].PlotOnSecondAxis = true;

// チャートのバックウォール色の設定
chart.BackWall.Thickness = 1;
chart.BackWall.Format.Fill.FillType = FillType.Solid;
chart.BackWall.Format.Fill.SolidFillColor.Color = Color.Orange;

chart.Floor.Format.Fill.FillType = FillType.Solid;
chart.Floor.Format.Fill.SolidFillColor.Color = Color.Red;
// プロット領域の色の設定
chart.PlotArea.Format.Fill.FillType = FillType.Solid;
chart.PlotArea.Format.Fill.SolidFillColor.Color = Color.LightCyan;

// プレゼンテーションを保存
pres.Save("FormattedChart_out.pptx", SaveFormat.Pptx);

チャートのフォントプロパティの設定

Aspose.Slides for .NET は、チャートのフォント関連プロパティの設定をサポートしています。以下の手順に従ってチャートのフォントプロパティを設定してください。

  • Presentation クラスオブジェクトをインスタンス化します。
  • スライドにチャートを追加します。
  • フォントの高さを設定します。
  • 変更されたプレゼンテーションを保存します。

以下にサンプル例を示します。

using (Presentation pres = new Presentation())
{               
    IChart chart = pres.Slides[0].Shapes.AddChart(ChartType.ClusteredColumn, 100, 100, 500, 400);
    chart.TextFormat.PortionFormat.FontHeight = 20;
    chart.ChartData.Series[0].Labels.DefaultDataLabelFormat.ShowValue = true;
    pres.Save("FontPropertiesForChart.pptx", SaveFormat.Pptx);
}

数値形式の設定

Aspose.Slides for .NET は、チャートデータ形式を管理するためのシンプルな API を提供します。

  1. Presentation クラスのインスタンスを作成します。
  2. インデックスでスライドの参照を取得します。
  3. デフォルトデータと任意のタイプのチャートを追加します(この例では ChartType.ClusteredColumn を使用します)。
  4. 可能なプリセット値から事前設定された数値形式を設定します。
  5. 各チャート系列のチャートデータセルを走査し、チャートデータの数値形式を設定します。
  6. プレゼンテーションを保存します。
  7. カスタム数値形式を設定します。
  8. 各チャート系列のチャートデータセルを走査し、異なる数値形式を設定します。
  9. プレゼンテーションを保存します。
// プレゼンテーションをインスタンス化// プレゼンテーションをインスタンス化
Presentation pres = new Presentation();

// 最初のプレゼンテーションスライドにアクセス
ISlide slide = pres.Slides[0];

// デフォルトのクラスター化された棒グラフを追加
IChart chart = slide.Shapes.AddChart(ChartType.ClusteredColumn, 50, 50, 500, 400);

// チャートのシリーズコレクションにアクセス
IChartSeriesCollection series = chart.ChartData.Series;

// プリセットの数値形式を設定
// すべてのチャートシリーズを走査
foreach (ChartSeries ser in series)
{
    // シリーズ内のすべてのデータセルを走査
    foreach (IChartDataPoint cell in ser.DataPoints)
    {
        // 数値形式を設定
        cell.Value.AsCell.PresetNumberFormat = 10; //0.00%
    }
}

// プレゼンテーションを保存
pres.Save("PresetNumberFormat_out.pptx", SaveFormat.Pptx);

使用できる事前設定数値形式とそのインデックスは以下のとおりです:

0 一般
1 0
2 0.00
3 #,##0
4 #,##0.00
5 $#,##0;$-#,##0
6 $#,##0;Red$-#,##0
7 $#,##0.00;$-#,##0.00
8 $#,##0.00;Red$-#,##0.00
9 0%
10 0.00%
11 0.00E+00
12 # ?/?
13 # /
14 m/d/yy
15 d-mmm-yy
16 d-mmm
17 mmm-yy
18 h:mm AM/PM
19 h:mm:ss AM/PM
20 h:mm
21 h:mm:ss
22 m/d/yy h:mm
37 #,##0;-#,##0
38 #,##0;Red-#,##0
39 #,##0.00;-#,##0.00
40 #,##0.00;Red-#,##0.00
41 _ * #,##0_ ;_ * “_ ;_ @_
42 _ $* #,##0_ ;_ $* “_ ;_ @_
43 _ * #,##0.00_ ;_ * “??_ ;_ @_
44 _ $* #,##0.00_ ;_ $* “??_ ;_ @_
45 mm:ss
46 h:mm:ss
47 mm:ss.0
48 ##0.0E+00
49 @

チャート領域の角丸境界線の設定

Aspose.Slides for .NET は、チャート領域の設定をサポートしています。IChart.HasRoundedCorners および Chart.HasRoundedCorners プロパティが Aspose.Slides に追加されました。

  1. Presentation クラスオブジェクトをインスタンス化します。
  2. スライドにチャートを追加します。
  3. チャートの塗りつぶしタイプと塗りつぶし色を設定します。
  4. 角丸プロパティを True に設定します。
  5. 変更されたプレゼンテーションを保存します。

以下にサンプル例を示します。

using (Presentation presentation = new Presentation())
{
	ISlide slide = presentation.Slides[0];
	IChart chart = slide.Shapes.AddChart(ChartType.ClusteredColumn, 20, 100, 600, 400);
	chart.LineFormat.FillFormat.FillType = FillType.Solid;
	chart.LineFormat.Style = LineStyle.Single;
	chart.HasRoundedCorners = true;

	presentation.Save("out.pptx", Aspose.Slides.Export.SaveFormat.Pptx);
}

FAQ

列/エリアの半透明塗りつぶしを設定し、枠線を不透明のままにできますか?

はい。塗りつぶしの透明度と輪郭は個別に設定できます。これにより、密集したビジュアライゼーションでのグリッドやデータの可読性が向上します。

ラベルが重なった場合、どう対処すればよいですか?

フォントサイズを小さくする、不要なラベル要素(例えばカテゴリ)を無効にする、ラベルのオフセット/位置を設定する、必要に応じて選択されたポイントのみラベルを表示する、または形式を「値+凡例」に切り替えるなどの方法があります。

系列に対してグラデーションやパターンの塗りつぶしを適用できますか?

はい。通常、単色とグラデーション/パターンの塗りつぶしの両方が利用可能です。実務では、グラデーションは控えめに使用し、グリッドやテキストとのコントラストを低下させる組み合わせは避けてください。