首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何按正确的顺序设置LiveCharts StackedColumns标签

如何按正确的顺序设置LiveCharts StackedColumns标签
EN

Stack Overflow用户
提问于 2019-01-15 23:39:50
回答 1查看 276关注 0票数 0

我正在使用LiveCharts创建一个从字典集合填充的StackedColumns图表,如下所示:

我只找到了列n的标签和内容是输入硬编码的示例。

代码语言:javascript
复制
AgvCtlSvcs.AgvErrorHistory[] agvErrorHistory;

AgvError[] agvErrors;

Dictionary<string, int> errorCodes = new Dictionary<string, int>();

Dictionary<string, AgvChartEntry> unsortedAgvCollection = new Dictionary<string, AgvChartEntry>();
Dictionary<string, StackedColumnSeries> stackedColumns = new Dictionary<string, StackedColumnSeries>();

List<string> errors = new List<string>();
List<string> errorsDisplay = new List<string>();
List<string> agvs = new List<string>();
List<string> stations = new List<string>();


foreach (Agv.AgvStatus agv in mw.AgvCtlSvcs.GetAgvStatus())
                {
                    List<AgvCtlSvcs.AgvErrorHistory> query = (from item in agvErrorHistory where agv.AgvID.ToString() == item.AGV select item).ToList();

                // Create a collection containing a record for each AGV and
                // for each record store home many errors of each type
                // occurred for that specific AGV
                foreach (AgvCtlSvcs.AgvErrorHistory item in query)
                {
                    // Add the record to the colletion for that AGV if not present
                    if (!unsortedAgvCollection.ContainsKey(item.AGV))
                    {
                        stackedColumns.Add(item.AGV, new StackedColumnSeries { Title = "AGV-" + item.AGV, DataLabels = true });
                        // add the error to the AGV record
                        unsortedAgvCollection.Add(item.AGV, new AgvChartEntry { AgvID = item.AGV, Error = new Dictionary<string, int>() });
                        // set 1 occurrency for that record (initialize it)
                            unsortedAgvCollection[item.AGV].Error.Add(item.ErrorDesc, 1);
                    }
                    else
                    {
                        // If this record already stored for that AGV, increment occurrency
                        if (unsortedAgvCollection[item.AGV].Error.ContainsKey(item.ErrorDesc))
                            {
                                unsortedAgvCollection[item.AGV].Error[item.ErrorDesc]++;
                            }
                            // set 1 occurrency for that record (initialize it)
                            else
                            {
                                unsortedAgvCollection[item.AGV].Error.Add(item.ErrorDesc, 1);
                            }
                        }
                        // create a record for that error in the errors code collection
                        if (!errors.Contains(item.ErrorCode))
                        {
                            errors.Add(item.ErrorCode);
                        }
                        // create a record for that error in the errors description collection
                        if (!errorsDisplay.Contains(item.ErrorDesc))
                        {
                            errorsDisplay.Add(item.ErrorDesc);
                        }
                    }
                }

                // Initialize the values of the columns to int
                foreach (KeyValuePair<string, StackedColumnSeries> item in stackedColumns)
                {
                    item.Value.Values = new ChartValues<int>();
                }

                // clear labels collection
                for (int i = AgvChartLabels.Count - 1; i >= 0; i--)
                {
                    AgvChartLabels.RemoveAt(i);
                }

                // for each type of error that occurred check for each AGV in the agvCollection
                // if that specific AGV had this error, if so add a column for that error with the
                // occurrency value

                foreach (KeyValuePair<string, AgvChartEntry> agv in unsortedAgvCollection)
                {
                    foreach (string error in errorsDisplay)
                    {
                        if (agv.Value.Error.ContainsKey(error))
                        {
                            stackedColumns[agv.Key].Values.Add(agv.Value.Error[error]);
                        }                        
                    }
                }
                foreach (var error in errorsDisplay)
                {
                    if (!AgvChartLabels.Contains(error))
                    {
                        AgvChartLabels.Add(error);
                    } 
                }

                // add to the series collection the columns (AGVs) that contains at least a value (error)
                foreach (KeyValuePair<string, StackedColumnSeries> item in stackedColumns)
                {
                    if (item.Value.Values.Count > 0)
                    {
                        AgvSeriesCollection.Add(item.Value);
                    }
                }

我的问题是X轴上的标签并不对应于右列。如何根据列设置标签的正确顺序?我试过所有的解决办法,但我想不出来。我遗漏了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-17 16:28:35

问题是我没有将“零”值添加到StackedColumnsSeries中,所以所有的值都被压缩到了图表的左边,而标签则是错误的。

代码语言:javascript
复制
foreach (KeyValuePair<string, AgvChartEntry> agv in unsortedAgvCollection)
{
    foreach (string error in errorsDisplay)
    {
         if (agv.Value.Error.ContainsKey(error))
         {
               stackedColumns[agv.Key].Values.Add(agv.Value.Error[error]);
         } 
         else  
         {
               //Must add the zero value it the entry does not exist
               stackedColumns[agv.Key].Values.Add(0); 
         }                     
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54202140

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档