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

Free Support Forum - aspose.com

Charts for Windows Forms over Aspose.Cell

How can I create Charts from DataSet, DataView or DataTable over Aspose for Windows Forms in the programing language C#?

Hi,

Well, you may import data from DataTable / DataView using Cells.ImportDataTable() or Cells.ImportDataView method and then use Aspose.Cells.Charts API to create your desired charts based on the data filled in the Worksheet Cells for your need.

See some documents for your reference.

//Import data from DataTable or DataView:
http://www.aspose.com/documentation/.net-components/aspose.cells-for-.net/importing-data-to-worksheets.html


//Check the topics in the section/sub-sections:
http://www.aspose.com/documentation/.net-components/aspose.cells-for-.net/creating-charts.html


Thank you.

Hi Rudolf,

DataSet, DataView or DataTable are just the source of the data into the chart. They are many ways to create chart.
Please following the line below in order to "How to create chart" or can follow the code snippet.

Link:
http://www.aspose.com/documentation/.net-components/aspose.cells-for-.net/how-to-create-a-chart.html

Code snippet:
private Chart CreateChartFromDataTable(DataTable table)
{
//Create the chart
Chart chart = new Chart();
chart.Width = 600;
chart.Height = 450;
chart.SmoothingMode = SmoothingMode.Default;

Title title = new Title();
title.Text = "Chart from Datatable";
chart.Titles.Add(title);

ChartArea chartArea = chart.ChartArea;

//Specify the legend
chartArea.LegendBox.LegendPositionType = LegendPositionType.Bottom;
chartArea.LegendBox.LayoutType = LayoutType.Row;

//Specify formatting for the Y axes
chartArea.AxisY.IsMajorGridVisible = false;
chartArea.AxisY.DefaultLabel.Format = "G0";
chartArea.AxisY2.IsMajorGridVisible = false;
chartArea.AxisY2.DefaultLabel.Format = "C0";


//Create the Number of Sales series.
Series salesCountSeries = new Series();
salesCountSeries.Name = "Number of sales (LH)";
salesCountSeries.ChartType = ChartType.Bar;

//Load data from the data table and add the series to the chart.
salesCountSeries.DataPoints.DataBindXY(table.Rows, "Year", table.Rows, "Number of Sales");
chart.SeriesCollection.Add(salesCountSeries);


//Create the Total Sales Value series and add to the chart.
Series salesValueSeries = new Series();
salesValueSeries.Name = "Total sales, $m (RH)";
salesValueSeries.ChartType = ChartType.Curve;
salesValueSeries.IsPrimaryAxisY = false;
salesValueSeries.DefaultDataPoint.BorderWidth = 10;
salesValueSeries.DefaultDataPoint.Color = Color.Red;

//Load data from the data table and add the series to the chart.
salesValueSeries.DataPoints.DataBindXY(table.Rows, "Year", table.Rows, "Total Sales Value");
chart.SeriesCollection.Add(salesValueSeries);


//Format the X axis. Ideally this should be done before series are added.
Axis axisX = chartArea.AxisX;
axisX.IsMajorGridVisible = false;
axisX.DefaultLabel.Format = "G0";
axisX.MinorTickMark.IsVisible = false;
//Lets use manual settings for the X axis here.
axisX.IsAutoCalc = false;
axisX.Interval = 1;

//This is somewhat a hack to give the labels on the X axis the look that we want.
int minYear = Convert.ToInt32(table.Rows[0].ItemArray[0]);
chartArea.AxisX.Minimum = minYear - 1;
int maxYear = Convert.ToInt32(table.Rows[table.Rows.Count - 1].ItemArray[0]);
axisX.Maximum = maxYear + 1;
axisX.AxisLabels[0].Text = "";
axisX.AxisLabels[axisX.AxisLabels.Count - 1].Text = "";

return chart;
}

Thanks,