Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
405
Howto bind tooltip to series datapoint (when series is mapped to DataTable)
posted

I have a seemingly simple requirement. I want a tooltip to popup when the user hovers over a charted datapoint (2d column chart).

All the examples I've found (including feature brower) show tooltips set at the (hard coded) datapoint object. However, I'm adding the series to the chart in code behind by mapping to columns in  DataTable and setting tooltips for the individual datapoints doesn't work.

How do I bind tooltips to the column value? I would think this is a no brainer... but I'm stumped!

Thanks in advance for the help.

Parents Reply Children
  • 30692
    Suggested Answer
    Offline posted in reply to Jeff

    Jeff,

    It is a bit of a pain point in the current version of the API, but there are many valid ways to approach augmenting the tooltip, regardless. If you are just looking to apply some additional formatting to the value in the tooltip, you may want to set a DataPointTemplate for the series, http://help.infragistics.com/NetAdvantage/WPF/2010.2/CLR4.0/?page=xamChart_Using_a_DataTemplate_to_Style_DataPoints.html

    This will allow you to specify a value converter when you bind the Tooltip from the DataPointTemplate to the tooltip somewhere on that visual. see: http://community.infragistics.com/forums/p/40864/229969.aspx#229969

    You also have other options in terms of augmenting the data being bound to the chart in order to use a simple DataMapping to define the population of the tooltips see the two methods shown below:

     

    public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
    
                DataTable testTable = GetTestData();
                CreateSeriesLinq(testTable);
            }
    
            private void CreateSeriesExtraColumns(DataTable testTable)
            {
                int columnCount = testTable.Columns.Count;
                for (int i = 0; i < columnCount; i++)
                {
                    DataColumn newColumn = new DataColumn(
                        testTable.Columns[i].ColumnName + "ToolTip");
                    testTable.Columns.Add(newColumn);
                    foreach (DataRow row in testTable.Rows)
                    {
                        row[newColumn] = 
                            testTable.Columns[i].ColumnName + " = " + row[i];
                    }
                }
    
                for (int i = 0; i < columnCount; i++)
                {
                    Series seriesSales = new Series();
                    seriesSales.ChartType = ChartType.Column;
                    seriesSales.DataSource = testTable;
                    seriesSales.DataMapping = "Value=" + 
                        testTable.Columns[i].ColumnName + ";Tooltip=" + 
                        testTable.Columns[i + columnCount].ColumnName;
                    seriesSales.Label = testTable.Columns[i].ColumnName;
                    seriesSales.StrokeThickness = .5d;
                    theChart.Series.Add(seriesSales);
                }
            }
    
            private void CreateSeriesLinq(DataTable testTable)
            {
                int columnCount = testTable.Columns.Count;
                for (int i = 0; i < columnCount; i++)
                {
                    Series seriesSales = new Series();
                    seriesSales.ChartType = ChartType.Column;
    
                    int currIndex = i;
                    seriesSales.DataSource = 
                        from row in testTable.AsEnumerable()
                        select new 
                        { 
                            Value = row[testTable.Columns[currIndex]],
                            Tooltip = testTable.Columns[currIndex].ColumnName +
                            " = " + 
                            row[testTable.Columns[currIndex]] 
                        };
                    seriesSales.DataMapping = "Value=Value;Tooltip=Tooltip";
                    seriesSales.Label = testTable.Columns[i].ColumnName;
                    seriesSales.StrokeThickness = .5d;
                    theChart.Series.Add(seriesSales);
                }
            }
    
            private DataTable GetTestData()
            {
                DataTable testTable = new DataTable("TestTable");
    
                testTable.Columns.Add(new DataColumn("NumberCars"));
                testTable.Columns.Add(new DataColumn("NumberPlanes"));
    
                DataRow row = testTable.NewRow();
                row["NumberCars"] = 54;
                row["NumberPlanes"] = 26;
                testTable.Rows.Add(row);
    
                row = testTable.NewRow();
                row["NumberCars"] = 21;
                row["NumberPlanes"] = 6;
                testTable.Rows.Add(row);
    
                row = testTable.NewRow();
                row["NumberCars"] = 12;
                row["NumberPlanes"] = 31;
                testTable.Rows.Add(row);
    
                row = testTable.NewRow();
                row["NumberCars"] = 63;
                row["NumberPlanes"] = 1;
                testTable.Rows.Add(row);
    
                row = testTable.NewRow();
                row["NumberCars"] = 15;
                row["NumberPlanes"] = 11;
                testTable.Rows.Add(row);
    
                row = testTable.NewRow();
                row["NumberCars"] = 3;
                row["NumberPlanes"] = 52;
                testTable.Rows.Add(row);
    
                row = testTable.NewRow();
                row["NumberCars"] = 26;
                row["NumberPlanes"] = 54;
                testTable.Rows.Add(row);
    
                return testTable;
            }
        }
    

    There are other ways of approaching this also. If you have a way you would prefer the API worked, also, please go ahead and make a feature request and hopefully we will be able to accomodate soon. Hope this helps!

    -Graham