We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Trend Chart

Hi,

Can anyone tell me how to draw a trend chart in excel using ASPOSE.

Mithun

Hi,


Please see the sample example on how to create a chart with a trendline.
Sample code:
Workbook excel = new Workbook();
Worksheet sheet = excel.Worksheets[0];
Cells cells = excel.Worksheets[0].Cells;
cells[“A1”].PutValue(“Daily Rainfall”);
cells[“B1”].PutValue(“Particulate”);
cells[“A2”].PutValue(1.9);
cells[“B2”].PutValue(137);
cells[“A3”].PutValue(3.6);
cells[“B3”].PutValue(128);
cells[“A4”].PutValue(4.1);
cells[“B4”].PutValue(122);
cells[“A5”].PutValue(4.3);
cells[“B5”].PutValue(117);
cells[“A6”].PutValue(5);
cells[“B6”].PutValue(114);
cells[“A7”].PutValue(5.4);
cells[“B7”].PutValue(114);
cells[“A8”].PutValue(5.7);
cells[“B8”].PutValue(112);
cells[“A9”].PutValue(5.9);
cells[“B9”].PutValue(110);
cells[“A10”].PutValue(7.3);
cells[“B10”].PutValue(104);
excel.Worksheets.Add(SheetType.Chart);
ChartCollection charts = excel.Worksheets[1].Charts;
int chartIndex = charts.Add(ChartType.LineWithDataMarkers, 1, 3, 25, 12);
Chart chart = charts[chartIndex];

chart.Legend.Position = LegendPositionType.Top;
chart.Title.Text = “Scatter Chart:Particulate Levels in Rainfall”;
chart.Title.TextFont.Color = Color.Black;
chart.Title.TextFont.IsBold = true;
chart.Title.TextFont.Size = 12;
chart.ValueAxis.MinValue = 50;
chart.ValueAxis.MaxValue = 200;
chart.NSeries.Add(“Sheet1!B2:B10”, true);
// chart.NSeries.CategoryData = “Sheet1!A2:A10”;
chart.NSeries[0].XValues = “Sheet1!A2:A10”;
SeriesCollection nseries = chart.NSeries;
for (int i = 0; i < nseries.Count; i++)
{
nseries[i].Name = “Test Series”;
nseries[i].MarkerStyle = ChartMarkerType.Circle;
nseries[i].MarkerBackgroundColor = Color.Red;
nseries[i].MarkerForegroundColor = Color.Blue;
nseries[i].MarkerSize = 8;
nseries[i].DataLabels.Position = LabelPositionType.Below;


}

chart.NSeries[0].TrendLines.Add(TrendlineType.Linear, “test_line”);
chart.NSeries[0].TrendLines[0].Weight = WeightType.HairLine;
chart.NSeries[0].TrendLines[0].Color = Color.Blue;
chart.NSeries[0].TrendLines[0].Backward = 0.4;




chart.CategoryAxis.Title.Text = cells[“A1”].Value.ToString();
chart.ValueAxis.Title.Text = cells[“B1”].Value.ToString();

excel.Save(“e:\test2\out_scatterchart_trendline.xls”);



Thank you.