Profile Picture

Working with columns of data instead of rows

Posted By Mark Whitmore 15 Years Ago
Author
Message
Mark Whitmore
Posted 15 Years Ago
View Quick Profile
Forum Newbie

Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: Forum Members
Last Active: 15 Years Ago
Posts: 2, Visits: 1

Thanks Bob.  I had a feeling I'd have to convert from columns to rows.

Regards,

Mark



bob milanov
Posted 15 Years Ago
View Quick Profile
Supreme Being

Supreme Being (152 reputation)Supreme Being (152 reputation)Supreme Being (152 reputation)Supreme Being (152 reputation)Supreme Being (152 reputation)Supreme Being (152 reputation)Supreme Being (152 reputation)Supreme Being (152 reputation)Supreme Being (152 reputation)

Group: Forum Members
Last Active: 6 Months Ago
Posts: 153, Visits: 11

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



Mark Whitmore
Posted 15 Years Ago
View Quick Profile
Forum Newbie

Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: Forum Members
Last Active: 15 Years Ago
Posts: 2, Visits: 1

Hi,

I have data being passed from a database and I want to use that data in columns as opposed to rows

For example

Customer   Month_1_Value         Month_2_Value            Month_3_Value

Cust1        200                        210                           190

Cust2        160                        170                           180

Cust3        140                        150                           160

I am trying to display this as a stacked bar chart with the bars representing the months. The problem I have is that the chart data is generated dynamically and the number of customers varies dependant upon parameters passed. The only way I seem to be able to display it is with a separate stacked bar per customer.

Am I missing something simple?

Regards,

Mark





Similar Topics


Reading This Topic