Nevron Forum

Axis Label linked to Datatable

https://www.nevron.com/Forum/Topic7901.aspx

By Ronny Brouillard - Thursday, June 20, 2013

Hi everyone.

I'm trying to make a simple 2D Bar graph linked to a Datatable.

My datas look like this :
NAME - VALUE
OneName - 10
AnotherName - 5
AnotherAgain - 7

I succeded to create the values liked this :

Dim dataBindingManager As NDataBindingManager = chartControl.DataBindingManager
dataBindingManager.AddBinding(0, 0, "Values", MyDataTable, "VALUE")

But I would like to display on the X Axis the NAME field.

Any idea ?

Thanks !
By Nevron Support - Wednesday, June 26, 2013

Hi,

Currently it is not possible to data bind the axis labels, but there is a relatively simple workaround. You can bind the Labels data series of the Bar series and copy the strings to the axis labels (after they are read from the data table). The following example demonstrated this approach:


      void Form1_Load(object sender, EventArgs e)
      {
         dataTable = new DataTable("MyTable");
         dataTable.Columns.Add("Value", typeof(double));
         dataTable.Columns.Add("Name", typeof(string));
         dataTable.Rows.Add(new object[] { 24, "George" });
         dataTable.Rows.Add(new object[] { 19, "Larry" });
         dataTable.Rows.Add(new object[] { 31, "Billy" });
         dataTable.Rows.Add(new object[] { 29, "Stanley" });
         dataTable.Rows.Add(new object[] { 25, "Michael" });

         NChart chart = nChartControl1.Charts[0];
         NBarSeries series = new NBarSeries();
         series.DataLabelStyle.Format = "";
         chart.Series.Add(series);

         nChartControl1.DataBindingManager.AddBinding(0, 0, "Values", dataTable, "Value");
         nChartControl1.DataBindingManager.AddBinding(0, 0, "Labels", dataTable, "Name");

         dataTable.RowChanged += new DataRowChangeEventHandler(dataTable_RowChanged);

         UpdateAxisLabels();
      }

      void dataTable_RowChanged(object sender, DataRowChangeEventArgs e)
      {
         UpdateAxisLabels();
      }

      void UpdateAxisLabels()
      {
         NChart chart = nChartControl1.Charts[0];
         NBarSeries series = (NBarSeries)chart.Series[0];

         NOrdinalScaleConfigurator scaleX = (NOrdinalScaleConfigurator)chart.Axis(StandardAxis.PrimaryX).ScaleConfigurator;
         scaleX.AutoLabels = false;
         scaleX.Labels.Clear();
         scaleX.Labels.AddRange(series.Labels);

         nChartControl1.Refresh();
      }
By Ronny Brouillard - Monday, July 8, 2013

Hello, I'm back from holidays with this wonderful answer !
I get the point and used the BindingSource.PositionChanged event to make it work in my case.

Thank you for your support.