【Aspose.Cells】グラフの描画

グラフの描画をしており、以下のサイトを参考しています。

X座標の指定については、以下のような記載がありますが、
chart.NSeries[0].XValues = “{1,3,2.5,3.5}”;

XValues = xxxxxxx⇒ここは別の配列変数からのセットは可能でしょうか?
サンプルコード等を提示いただけると幸いです。

@kenable811
配列リストオブジェクトの直接設定は現在サポートされていません。簡単な変換で目標を達成することができます。以下のコードを参照してください。

public static string ConvertListToString(ArrayList list)
{
    StringBuilder builder = new StringBuilder();
    builder.Append("{");
    int count = list.Count;
    for (int i = 0; i < count; i++)
    {
        builder.Append(list[i].ToString());
        if (i < count - 1)
        {
            builder.Append(",");
        }                
    }
    builder.Append("}");
    return builder.ToString();
}

// Create an instance of Workbook
Workbook workbook = new Workbook();
// Access the first worksheet.
Worksheet worksheet = workbook.Worksheets[0];
// Put the sample values used in charts
worksheet.Cells["A2"].PutValue(1);
worksheet.Cells["A3"].PutValue(3);
worksheet.Cells["A4"].PutValue(2.5);
worksheet.Cells["A5"].PutValue(3.5);
worksheet.Cells["B1"].PutValue("Cats");
worksheet.Cells["C1"].PutValue("Dogs");
worksheet.Cells["D1"].PutValue("Fishes");
worksheet.Cells["B2"].PutValue(7);
worksheet.Cells["B3"].PutValue(6);
worksheet.Cells["B4"].PutValue(5);
worksheet.Cells["B5"].PutValue(4);
worksheet.Cells["C2"].PutValue(7);
worksheet.Cells["C3"].PutValue(5);
worksheet.Cells["C4"].PutValue(4);
worksheet.Cells["C5"].PutValue(3);
worksheet.Cells["D2"].PutValue(8);
worksheet.Cells["D3"].PutValue(7);
worksheet.Cells["D4"].PutValue(3);
worksheet.Cells["D5"].PutValue(2);
//Create Line Chart: X as Category Axis
int pieIdx = worksheet.Charts.Add(ChartType.LineWithDataMarkers, 6, 15, 20, 21);
// Retrieve the Chart object
Chart chart = worksheet.Charts[pieIdx];
// Add Series
chart.NSeries.Add("B2:D5", true);
// Set the category data
chart.NSeries.CategoryData = "=Sheet1!$A$2:$A$5";
// Set the first series mame
chart.NSeries[0].Name = "Cats";
// Set the second series mame
chart.NSeries[1].Name = "Dogs";
// Set the third series mame
chart.NSeries[2].Name = "Fishes";
// Set the Legend at the bottom of the chart area
chart.Legend.Position = LegendPositionType.Bottom;
// Fill the PlotArea area with nothing 
chart.PlotArea.Area.FillFormat.FillType = FillType.None;
// Create XY (Scatter) Chart: X as Value Axis
pieIdx = worksheet.Charts.Add(ChartType.ScatterConnectedByLinesWithDataMarker, 6, 6, 20, 12);
// Retrieve the Chart object
chart = worksheet.Charts[pieIdx];
// Add Series
chart.NSeries.Add("B2:D5", true);
ArrayList list = new ArrayList();
list.Add(1);
list.Add(3);
list.Add(2.5);
list.Add(3.5);
// Set X values for series 
chart.NSeries[0].XValues = ConvertListToString(list);
chart.NSeries[1].XValues = ConvertListToString(list);
chart.NSeries[2].XValues = ConvertListToString(list);

// Set the first series mame
chart.NSeries[0].Name = "Cats";
// Set the second series mame
chart.NSeries[1].Name = "Dogs";
// Set the third series mame
chart.NSeries[2].Name = "Fishes";
// Set the Legend at the bottom of the chart area
chart.Legend.Position = LegendPositionType.Bottom;
// Fill the PlotArea area with nothing 
chart.PlotArea.Area.FillFormat.FillType = FillType.None;
// Save the Excel file
workbook.Save(filePath + "XAxis.xlsx");
1 Like

ありがとうございます。

チャートのARGBカラー指定のサンプルコードはございますか?
以下のサイトを参考にしていますが、boder.colorの箇所を変更する必要があると思います。

// Change the Border color of the second data series.
chart.NSeries[1].Border.Color = Color.Green;

@kenable811
ARGBを通じてカラーオブジェクトに値すると思いますか。Color持参の方法を使っています。以下のコードを参照してください。

Color color = Color.FromArgb(argbvalue);
1 Like

@kenable811,

以下のコード例(更新)を参照してください。

// Change the Border color of the second data series.
chart.NSeries[1].Border.Color = Color.FromArgb(0, 255, 0);
1 Like

チャートのマーカー色は統一できますか?
またグラフ線の太さがポイント単位で変更できないのですが、サンプルコードがあればご提示願えますでしょうか?
image.png (2.0 KB)

@kenable811,

以下のサンプルコードを参照して、シリーズマーカーの色を設定する方法を理解してください。

chart.NSeries[1].Area.Formatting = FormattingType.Custom;
chart.NSeries[1].Marker.Area.ForegroundColor = System.Drawing.Color.Green;
chart.NSeries[1].Marker.Border.IsVisible = false;

次のサンプルコードを参照して、シリーズの線幅をポイント単位で設定する方法を理解してください。

chart.NSeries[1].Border.WeightPt = 0.75;

これが役に立つことを願っています。

@amjad.sahi
マーカーについて
グラフ内の点の色と、点の枠線の色、線の色が上記サンプルコードの場合、統一されません。
再度調査いただけますでしょうか?

image.png (21.3 KB)

@kenable811,

以下のファイルを圧縮して共有できますか?

  1. Aspose.Cellsが現在出力しているExcelファイルには、不要な結果を含むグラフが含まれています。
    2)必要な形式のグラフや、シリーズポイント、マーカー、枠線の色などを含むExcelファイルの例。MS Excelで必要なExcelファイルを手動で作成できます。

私たちはAsposeを通過する方法をチェックし、評価します。Cells APIが完了しました。

@amjad.sahi
上記問題は解決しました。ご助力ありがとうございました。

ただ、グラフの凡例の名称のみ非表示または削除したいのですが、可能でしょうか?
その凡例のグラフは残したままで、凡例の名称だけ削除したいです。
image.png (1.6 KB)

@kenable811,

グラフ内の凡例エントリまたは項目を非表示にしたり削除したりすることができます。 参考までに、次のコード行を参照してください。

chart.Legend.LegendEntries[0].IsDeleted = true;

これが少し役立つことを願っています。

1 Like