CIMChart series color - unable to set

363
4
10-27-2023 01:02 PM
Kevin_Andras
New Contributor III

In my Add-In, I am able to create a table and make a chart from the data.  I am unable to change the color of the line series.  I modified my code from the sample found at

https://pro.arcgis.com/en/pro-app/latest/sdk/api-reference/topic21212.html 

I had to change several things to make it work with a standalone table.

The chart draws fine, and I am able to change the LineSymbolProperties.Style (e.g. dashed, solid), but changing the Color does not make any difference - it always plots with the default line series color, pale blue.   

The entire function that I'm coding uses a MapTool, with which a user can define a line - points along that line are sampled from a raster (typically a DTM) and distance/raster values are added to the table.   I ultimately want the line series on the plot to match the graphic line on the map.

private async static void MakeChart(Table table, string field1, string field2, CIMRGBColor lineColor)
{

    string tableName = table.GetName();
    var stTable = MapView.Active.Map.GetStandaloneTablesAsFlattenedList().Where((l) => l.Name == tableName).FirstOrDefault() as StandaloneTable;

    CIMStandaloneTable cimTableDefinition = null;

    await QueuedTask.Run(() => {

        cimTableDefinition = stTable.GetDefinition() as CIMStandaloneTable;

    });

    string[] fieldString = new string[] { "distance", field1 };
    if (field2 != "")
    {
        fieldString = new string[] { "distance", field1, field2 };
    }

    var lineChart = new CIMChart
    {
        Name = "lineChart",
        GeneralProperties = new CIMChartGeneralProperties
        {
            Title = $"Profile chart for {field1} ",
        },
        Series = new CIMChartSeries[]
            {
                new CIMChartLineSeries {
                    UniqueName = "lineChartSeries",
                    Name = field1, 
                    Fields = new string[] { "distance" , field1,},
                // Specify aggregation type
                //FieldAggregation = new string[] { string.Empty, "SUM" },
                // Specify custom time bin of 6 months
                //TimeAggregationType = ChartTimeAggregationType.EqualIntervalsFromStartTime,
                //TimeIntervalSize = 1.0,
                //TimeIntervalUnits = esriTimeUnits.esriTimeUnitsDays,
                // Define custom line color
                ColorType = ChartColorType.CustomColor,  //SingleColor makes no difference
                LineSymbolProperties = new CIMChartLineSymbolProperties {
                    Style = ChartLineDashStyle.Solid,  //can change this
                    Color = new CIMRGBColor { R = 0, G = 150, B = 20 },  //changing this does not have any effect - plots blue line regardless
                }
            },
        }
    };

    // Add new chart to layer's existing list of charts (if any exist)
    var newChartsLine = new CIMChart[] { lineChart };
    // Add CIM chart to layer defintion 
    //vvar allChartsLine = (cimTableDefinition == null) ? newChartsLine : cimTableDefinition.Charts.Concat(newChartsLine);  //value cannot be null error
    cimTableDefinition.Charts = newChartsLine.ToArray();
    stTable.SetDefinition(cimTableDefinition);
}
4 Replies
Kevin_Andras
New Contributor III

When I run the sample that I linked to above, it creates a blank chart telling me to select the variables.   I then can change the "Aggregation" type in the Chart Properties and it draws the chart...with a GREEN line or whatever I set it to.    So it works in that case, but for some reason the Color cannot be changed in my situation.  Here's the sample I ran - I had to wrap it in a Queued Task:  

   internal class buttonTest : Button
   {
       protected async override void OnClick()
       {

           // For more information on the chart CIM specification:
           // https://github.com/Esri/cim-spec/blob/main/docs/v2/CIMCharts.md

           // Define fields names used in chart parameters.
           const string dateField = "last_review";
           const string numericField = "price";

           var lyrsLine = MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>();
           var lyrLine = lyrsLine.First();

           CIMBaseLayer lyrDefLine = null;

           await QueuedTask.Run(() =>
           {


               //tableDefLine = stTable.GetDefinition();
               lyrDefLine = lyrLine.GetDefinition() as CIMBaseLayer;




               // Define line chart CIM properties
               var lineChart = new CIMChart
               {
                   Name = "lineChart",
                   GeneralProperties = new CIMChartGeneralProperties
                   {
                       Title = $"Line chart for {dateField} summarized by {numericField}",
                   },
                   Series = new CIMChartSeries[]
                   {
                   new CIMChartLineSeries {
                       UniqueName = "lineChartSeries",
                       Name = $"Sum({numericField})",
                       // Specify date field and numeric field
                       Fields = new string[] { dateField, numericField },
                       // Specify aggregation type
                       FieldAggregation = new string[] { string.Empty, "SUM" },
                       // Specify custom time bin of 6 months
                       TimeAggregationType = ChartTimeAggregationType.EqualIntervalsFromStartTime,
                       TimeIntervalSize = 1.0,
                       TimeIntervalUnits = esriTimeUnits.esriTimeUnitsWeeks,
                       // NOTE: When setting custom time binning, be sure to set CalculateAutomaticTimeInterval = false
                       CalculateAutomaticTimeInterval = false,
                       // Define custom line color
                       ColorType = ChartColorType.CustomColor,
                       LineSymbolProperties = new CIMChartLineSymbolProperties {
                           Style = ChartLineDashStyle.DashDot,
                           Color = new CIMRGBColor { R = 0, G = 150, B = 20 },
                       }
                   },
                   }
               };

               // Add new chart to layer's existing list of charts (if any exist)
               var newChartsLine = new CIMChart[] { lineChart };
               //var allChartsLine = (lyrDefLine == null) ? newChartsLine : lyrDefLine.Charts.Concat(newChartsLine);
               // Add CIM chart to layer defintion 
               lyrDefLine.Charts = newChartsLine.ToArray();
               lyrLine.SetDefinition(lyrDefLine);


           });
       }
   }

 

0 Kudos
ChristopherAllen
Esri Contributor

Hi @Kevin_Andras ,

Thanks for the question! We've found a bug where in some scenarios the line color does not display correctly when you set the color through the `LineSymbolProperties` property. We are working to fix this bug, but in the meantime as a workaround you can try setting the color through the `MarkerSymbolProperties ` property. Please see the code below for an example of how you would set the color in this manner:

 

  internal class AddLineChartButton : Button
  {
    protected override void OnClick()
    {

      QueuedTask.Run(() =>
      {
        var lyrs = MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>();
        var lyr = lyrs.First();
        var lyrDef = lyr.GetDefinition();
        var xField = "distance";

        var lineChart = new CIMChart
        {
          Name = "lineChart",
          GeneralProperties = new CIMChartGeneralProperties
          {
            Title = $"Chart for {xField} ",
          },
          Series = new CIMChartSeries[]
          {
            new CIMChartLineSeries
            {
              UniqueName = "lineChartSeries",
              Name = xField,
              Fields = new string[] { xField, "price" },
              FieldAggregation = new string[] { string.Empty, "SUM" },
              ColorType = ChartColorType.CustomColor,
              LineSymbolProperties = new CIMChartLineSymbolProperties
              {
                Style = ChartLineDashStyle.Solid,
              },
              // set line series color with MarkerSymbolProperties
              MarkerSymbolProperties = new CIMChartMarkerSymbolProperties
              {
                Color = new CIMRGBColor { R = 0, G = 150, B = 20 }
              }
            },
          }
        };
        var newChartsLine = new CIMChart[] { lineChart };
        lyrDef.Charts = newChartsLine.ToArray();
        lyr.SetDefinition(lyrDef);
      });
    }
  }

 

 Please let me know if this does not solve your problem.

Thanks again,

Chris

Kevin_Andras
New Contributor III

That works.  Thank you so much!

ChristopherAllen
Esri Contributor

Awesome, glad to hear! Thank you for following up.

0 Kudos