How to refresh a layer's chart list in the map's table of contents

678
5
Jump to solution
04-21-2020 09:37 AM
TimWhiteaker
Occasional Contributor II

I can add charts to a layer using code (thanks Stephen Rhea for the charting code). Each click of a button in my add-in adds a new chart to the first layer.  (I adapted Stephen's code to use a unique name for each chart.)

protected override void OnClick()
{
    var layer = MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>().FirstOrDefault();
    CreateLineChartExample(table);
}

After the first click, I see the first chart appear under the layer name in the Table of Contents.  However, although subsequent button clicks add new charts, only the first chart is visible in the Table of Contents.  If I right-click the layer and click to add a new chart, the other charts suddenly become visible.

How do I get the Table of Contents to refresh a layer's chart list?  This would be similar to the old IMxDocument.UpdateContents method.  I've tried MapView.Active.RedrawAsync(true); but it didn't update the TOC.

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
UmaHarano
Esri Regular Contributor

Hi Tim,

Here is some good news. With the 2.6 release of Pro coming up in the summer, this issue with displaying a chart for a standalone table is fixed. 

Thanks for reporting this!

Uma

View solution in original post

0 Kudos
5 Replies
UmaHarano
Esri Regular Contributor

Hi Tim,

I tried to do the same thing you are doing - I modified Stephen's code to use the layer name to create the chart.

I then iterated through each feature layer and applied the Chart method. The TOC was updated for me correctly. I had about 9 feature layers (from File GDBs).  I was on Pro 2.5.

To track down where the issue is happening, can you please give me more information? Some code snippets, info about the data you are using, etc might help us track this down.

Thank you!

Uma

0 Kudos
TimWhiteaker
Occasional Contributor II

Hi Uma,

For a simple test, I've got a simple standalone table, and my button is just generating a new chart for the table with each click. The table must be named test_data.  My add-in has a button with complete code below.

using System;
using System.Collections.Generic;
using System.Linq;
using ArcGIS.Core.CIM;
using ArcGIS.Desktop.Editing;
using ArcGIS.Desktop.Framework.Contracts;
using ArcGIS.Desktop.Framework.Threading.Tasks;
using ArcGIS.Desktop.Mapping;

namespace WRAP_Display
{
    internal class TestButton : Button
    {
        private async void CreateLineChartExample(StandaloneTable table)
        {
            var def = await QueuedTask.Run(() => { return table.GetDefinition(); });

            var chart = new CIMChart();

            var xAxis = new CIMChartAxis { Title = "X-Axis" };
            var yAxis = new CIMChartAxis { Title = "Y-Axis" };
            chart.Axes = new CIMChartAxis[] { xAxis, yAxis };

            var title = "Chart " + DateTime.Now.Ticks.ToString();
            chart.Name = title;
            chart.GeneralProperties = new CIMChartGeneralProperties { Title = title };

            var lineSeries = new CIMChartLineSeries
            {
                Fields = new string[] { "x", "y" },
                Name = "Line Chart Series1"
            };
            chart.Series = new CIMChartSeries[] { lineSeries };

            List<CIMChart> updated_charts = new List<CIMChart>();
            var charts = def.Charts;
            if (charts != null)
                updated_charts.AddRange(charts);
            updated_charts.Add(chart);
            def.Charts = updated_charts.ToArray();

            await QueuedTask.Run(() => { table.SetDefinition(def); });
        }

        protected override void OnClick()
        {
            StandaloneTable table = MapView.Active.Map.FindStandaloneTables("test_data").FirstOrDefault();
            CreateLineChartExample(table);
        }
    }
}

I placed sample data (a .gdb and a .mapx)  here:

sample_data.zip - Box 

The sample data is just a file geodatabase table with x and y fields, repeated below in CSV format in case you don't want to or can't get the zip.  If you import it, name it test_data.

x,y
1,4
2,2
3,4

I'm on current Windows 10, ArcGIS Pro 2.5.0, default Python environment, and no other add-ins except for this one that I am developing.  Nothing else in the add-in is listening to events or otherwise messing with the GUI unless invoked via button click.

Below is a link to a video illustrating the problem. I click the test button, and even though it creates a chart, no charts appear under the test_data table. When I right-click the table to make a new chart, suddenly the first chart appears. When I click the test button two more times, two additional charts are not visible. When I right-click the table to make a new chart again, the two additional charts are now visible.

2020-04-22_11-42-24 

I would like the chart to be visible under the table when the button finishes adding the chart to the table definition.

Also, my next question will be how to show the chart pane after the chart is added, though I haven't fully researched how to do that yet. I didn't find anything in searching the documentation, so I was going to poke around in debug mode a bit.

Thanks!

Tim

0 Kudos
UmaHarano
Esri Regular Contributor

Hi Tim,

Thank you for the detailed repro steps and the video. I see this issue too with standalone tables. If you reopen the Map, the chart shows up in the TOC.

I will get back to you soon.

Thanks

Uma

0 Kudos
UmaHarano
Esri Regular Contributor

Hi Tim,

Here is some good news. With the 2.6 release of Pro coming up in the summer, this issue with displaying a chart for a standalone table is fixed. 

Thanks for reporting this!

Uma

0 Kudos
TimWhiteaker
Occasional Contributor II

Thanks Uma!

0 Kudos