Hi Mark,
You’ll need a separate bar series per customer (if there is no data per customer for a specific month you can use empty data points). The following sample code shows how to achieve this using a randomly generated data table:
private static DataTable CreateDataTable()
{
DataRow row;
DataTable dataTable = new DataTable();
dataTable.Columns.Add("Customer", typeof(String));
dataTable.Columns.Add("Month", typeof(string));
dataTable.Columns.Add("Value", typeof(double));
DateTime timeStamp = DateTime.Now;
Random rand = new Random();
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 5; j++)
{
row = dataTable.NewRow();
row["Customer"] = "Customer" + j.ToString();
row["Month"] = i;
row["Value"] = (double)rand.Next(100);
dataTable.Rows.Add(row);
}
}
return dataTable;
}
private void Form1_Load(object sender, EventArgs e)
{
NChart chart = nChartControl1.Charts[0];
// get sample data table
DataTable data = CreateDataTable();
// find the unique months
Dictionary<string, NBarSeries> customerToSeries = new Dictionary<string, NBarSeries>();
List<string> months = new List<string>();
for (int i = 0; i < data.Rows.Count; i++)
{
string month = (string)data.Rows[i]["Month"].ToString();
if (!months.Contains(month))
{
months.Add(month);
}
}
months.Sort();
chart.Series.Clear();
// pass data to the chart
for (int i = 0; i < data.Rows.Count; i++)
{
DataRow row = data.Rows[i];
string customer = (string)row["Customer"];
string month = (string)row["Month"];
double value = (double)row["Value"];
NBarSeries series;
if (!customerToSeries.TryGetValue(customer, out series))
{
series = new NBarSeries();
series.MultiBarMode = MultiBarMode.Stacked;
// reserver space for months
for (int j = 0; j < months.Count; j++)
{
series.Values.Add(DBNull.Value);
}
customerToSeries.Add(customer, series);
chart.Series.Add(series);
}
int monthIndex = months.IndexOf(month);
series.Values[monthIndex] = value;
}
// add months as ordinal scale
NOrdinalScaleConfigurator ordinalScale = (NOrdinalScaleConfigurator)chart.Axis(StandardAxis.PrimaryX).ScaleConfigurator;
ordinalScale.MajorTickMode = MajorTickMode.CustomStep;
ordinalScale.CustomStep = 1;
ordinalScale.AutoLabels = false;
for (int i = 0; i < months.Count; i++)
{
ordinalScale.Labels.Add(months[i]);
}
// apply style sheet
NStyleSheet styleSheet = NStyleSheet.CreatePredefinedStyleSheet(PredefinedStyleSheet.Nevron);
styleSheet.Apply(nChartControl1.Document);
nChartControl1.Refresh();
}
Hope I helped - let me know if you have any questions or comments...
Best regards,
Bob