【问题标题】:How to set LiveCharts StackedColumns labels in the correct order如何以正确的顺序设置 LiveCharts StackedColumns 标签
【发布时间】:2019-01-15 15:39:50
【问题描述】:

我正在使用 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 轴上的标签与右列不对应。 如何根据列以正确的顺序设置标签? 我已经尝试了所有类型的解决方案,但我无法弄清楚。 我错过了什么?

【问题讨论】:

    标签: c# wpf livecharts


    【解决方案1】:

    问题是我没有将“零”值添加到 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); 
             }                     
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-12-22
      • 1970-01-01
      • 2019-10-05
      • 1970-01-01
      • 2020-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-16
      相关资源
      最近更新 更多