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

时间:2019-01-15 15:39:50

标签: c# wpf livecharts

我正在使用LiveCharts创建StackedColumns图表,该图表是从Dictionary集合中填充的,如下所示:

我只发现了示例,其中标签和列的内容是硬编码的。

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轴上的标签与右列不对应。 如何根据列以正确的顺序设置标签? 我已经尝试过各种解决方案,但无法解决。 我想念什么?

1 个答案:

答案 0 :(得分:0)

问题是我没有在StackedColumnsSeries中添加“零”值,因此所有值都压缩到图表的左侧,标签被压缩到错误的位置。

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); 
         }                     
    }
}