I am using LiveCharts to create a StackedColumns chart that gets populated from a Dictionary collection like this:
I have only found examples where the labels and the content of the columsn are input hardcoded.
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);
}
}
my problem is that the labels on the X axis don not correspond to the right column. How can I set the labels in the correct order according to the columns? I have tried all kind of solution but I can't figure it out. What am I missing?
The problem was that I was not adding the "zero" values to the StackedColumnsSeries so all the values where compressed to the left of the chart and the labels where wrong.
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);
}
}
}