This is a migrated thread and some comments may be shown as answers.

Howto linking a Chart to a RadGridView

2 Answers 35 Views
GridView
This is a migrated thread and some comments may be shown as answers.
FMorales
Top achievements
Rank 1
FMorales asked on 03 Nov 2014, 04:12 PM
Hi,

I would like to generate a dynamic graphic chart linked to a normal radGridview, which would be automatically generated as a function of the value of certain columns.

Somebody have made this before? any example available?

I have been looking the radpivotGrid that has this feature natively but I have to use the radGridView.

Thanks

Francisco

2 Answers, 1 is accepted

Sort by
0
Accepted
Dess | Tech Support Engineer, Principal
Telerik team
answered on 05 Nov 2014, 04:19 PM
Hello Francisco,

Thank you for writing.

Currently, RadChartView supports integration only with RadPivotGrid out of the box. However, you can easily implement similar behavior between RadChartView and RadGridView. It is necessary to repopulate the RadChartView when the RadGridView selection is changed. Here is a sample code snippet demonstrating how to achieve the behavior illustrated in the attached gif file:
public Form1()
{
    InitializeComponent();
 
    this.radGridView1.SelectionMode = Telerik.WinControls.UI.GridViewSelectionMode.CellSelect;
    this.radGridView1.MultiSelect = true;
}
 
private void radGridView1_SelectionChanged(object sender, EventArgs e)
{
    if (this.radGridView1.SelectedCells.Count > 0)
    {
        PopulateChart(this.radGridView1.SelectedCells);
    }
}
 
private void PopulateChart(GridViewSelectedCellsCollection gridViewSelectedCellsCollection)
{
    List<string> orderIds = new List<string>();
    this.radChartView1.Series.Clear();
    this.radChartView1.Axes.Clear();
    this.radChartView1.ShowLegend = true;
 
    foreach (var cell in gridViewSelectedCellsCollection)
    {
        double cellValue;
        if (double.TryParse(cell.Value + "", out cellValue))
        {
            BarSeries barSeries;
            DataRowView rowView = cell.RowInfo.DataBoundItem as DataRowView;
            if (!orderIds.Contains(rowView.Row["OrderID"].ToString()))
            {
                orderIds.Add(rowView.Row["OrderID"].ToString());
                barSeries = new BarSeries();
                barSeries.Name = rowView.Row["OrderID"].ToString();
                barSeries.LegendTitle = barSeries.Name;
              
                this.radChartView1.Series.Add(barSeries);
            }
            else
            {
                barSeries = GetBarSeries(rowView.Row["OrderID"].ToString()) as BarSeries ;
            }
            barSeries.DataPoints.Add(new CategoricalDataPoint(cellValue, cell.ColumnInfo.Name)); 
        }
    }
 
    this.radChartView1.Invalidate();
}
 
private ChartSeries GetBarSeries(string p)
{
    foreach (ChartSeries s in this.radChartView1.Series)
    {
        if (s.Name == p)
        {
            return s;
        }
    }
 
    return null;
}

Note that this is just a sample solution and it may not cover all possible cases. Feel free to modify it on a way which suits your requirement best.

I hope this information helps. Should you have further questions, I would be glad to help.
 
Regards,
Desislava
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
FMorales
Top achievements
Rank 1
answered on 06 Nov 2014, 07:01 AM
Hi Desislava,

I have maked the link without problems and my result code is very similar to your proposal.

Thanks a lot, we are changing all our old Xceed grid used in a lot of applications by Telerik controls and ... I am happy to realize that your controls are simply ...fantastic.

Thanks a lot.
Tags
GridView
Asked by
FMorales
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
FMorales
Top achievements
Rank 1
Share this question
or