Category labels on charts

Hi,

Is it possible to put in custom text for the category markers on a chart. This would be for a bar chart where you can compare values by quarter and year side by side, e.g:

|
| _ _ _
| _ | | | | _ _ _ | | _
| | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | |
|----------|----------|----------|----------|
Q1 2005 Q2 2005 Year 2005 Year 2006

I have everything working, except having numbers running 1, 2, 3 etc instead of the text ‘Q1 2005’, ‘Q2 2005’, etc along the bottom.

Any help you can offer will be most appreciated,

Thanks,

a

Please use NSeries.CategoryData to set it. Please check the following sample code:

Excel excel = new Excel();
Cells cells = excel.Worksheets[0].Cells;
cells["A1"].PutValue(2);
cells["A2"].PutValue(5);
cells["A3"].PutValue(3);
cells["A4"].PutValue(6);
cells["B1"].PutValue(4);
cells["B2"].PutValue(3);
cells["B3"].PutValue(6);
cells["B4"].PutValue(7);

cells["C1"].PutValue("Q1");
cells["C2"].PutValue("Q2");
cells["C3"].PutValue("Y1");
cells["C4"].PutValue("Y2");

int chartIndex = excel.Worksheets[0].Charts.Add(ChartType.Column, 11, 0, 27, 13);

Chart chart = excel.Worksheets[0].Charts[chartIndex];
chart.NSeries.Add("A1:B4", true);

chart.NSeries.CategoryData = "C1:C4";

That’s great, not sure how I missed it in the documentation!

Thank you very much Laurence.

a

@animya,
Aspose.Cells has replaced Aspose.Excel that is discarded now. You can use Aspose.Cells to create charts and avail all the latest features available for charts in different versions of MS Excel. You can set the category labels using this product also as demonstrated in the following sample code:

// Instantiating a Workbook object
Workbook workbook = new Workbook();

// Adding a new worksheet to the Excel object
int sheetIndex = workbook.Worksheets.Add();

// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[sheetIndex];

// Adding sample values to cells
worksheet.Cells["A1"].PutValue(10);
worksheet.Cells["A2"].PutValue(100);
worksheet.Cells["A3"].PutValue(170);
worksheet.Cells["A4"].PutValue(200);
worksheet.Cells["B1"].PutValue(120);
worksheet.Cells["B2"].PutValue(320);
worksheet.Cells["B3"].PutValue(50);
worksheet.Cells["B4"].PutValue(40);

// Adding sample values to cells as category data
worksheet.Cells["C1"].PutValue("Q1");
worksheet.Cells["C2"].PutValue("Q2");
worksheet.Cells["C3"].PutValue("Y1");
worksheet.Cells["C4"].PutValue("Y2");

// Adding a chart to the worksheet
int chartIndex = worksheet.Charts.Add(Aspose.Cells.Charts.ChartType.Column, 5, 0, 15, 5);

// Accessing the instance of the newly added chart
Aspose.Cells.Charts.Chart chart = worksheet.Charts[chartIndex];

// Adding SeriesCollection (chart data source) to the chart ranging from "A1" cell to "B4"
chart.NSeries.Add("A1:B4", true);

// Setting the data source for the category data of SeriesCollection
chart.NSeries.CategoryData = "C1:C4";

// Saving the Excel file
workbook.Save("outputChart.xls");

For a more detailed description and working samples refer to the following articles:
How to Create Excel Pie Chart in C#
How to Convert Excel Chart to JPG in C# without Interop