How to create a stacked bar graph based on select table data
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I would like to be able to create a stacked bar graph a data table and using data points that meet a specific requirement.
Requirement: Create a stacked bar graph for data of TYPE: SET only X-Axis: Order Y-Axis: Time
I can create the bar chart if I have column vectors already but can't figure out how to create column vectors from the data. The data will always change so the program would have to reinterpret the data and plot according. I manually recreated what the stacked bar chart would look like in excel (attached).
Any help would be greatly appreciated.
0 commentaires
Réponses (1)
Mishaal Aleem
le 7 Juil 2017
Modifié(e) : Mishaal Aleem
le 8 Juil 2017
Based on your description and the provided attachment, the code below should help you achieve this goal.
I am assuming you read the data in from an Excel file, so this script makes use of xlsread, where 'data.xlsx' was assumed to be an Excel file with the data only (i.e., I deleted the graph and the extra cells for generating the graph from your attachment & read that in).
Based on the bar graph documentation, you need to create a matrix to input into the bar command, where the matrix rows correspond to 'employees', the matrix columns correspond to the 'orders' and the value at any matrix position is the 'time' for the given employee (row) and given order (column) combination.
Creating this matrix can be done by looping through all unique orders, and within that looping through all unique employees. For each combination of order+employee, find the time spent (which will be plotted). This will make use of the unique command to find the unique orders and employees, for loops to loop through them, and the find command to find the correct indices to read the time value.
[num,txt,raw] = xlsread('Setup Time.xls'); %read in the raw data
headers = txt(1,:); %extract the headers
%Find columns of the data we need to use
typeIndex = find(strcmp(headers,'TYPE'));
timeIndex = find(strcmp(headers,'TIME'));
orderIndex = find(strcmp(headers,'ORDER#'));
empIDIndex = find(strcmp(headers,'EMPL#'));
%Find the rows where the type is 'SET'
type = 'SET';
types = raw(2:end,typeIndex);
SETIndexes = find(strcmp(types,type));
%Extract the order, time, and employee information
orders = num(SETIndexes,orderIndex);
times = num(SETIndexes,timeIndex);
emps = num(SETIndexes,empIDIndex);
%Create an array that stores all the relevant information needed to build
%the bar graph matrix
fullArray = [orders emps times];
%Get all unique orders and employees
orders = unique(orders);
emps = unique(emps);
y = zeros(numel(orders),numel(emps)); %Pre-allocate bar graph matrix
for i = 1:numel(orders) %Loop through each unique order
%For the current order number (i.e. current loop iteration), extract
%the employee and time information (column 2 and 3 of our fullArray
partialArray = fullArray(fullArray(:,1)==orders(i),2:3);
for j = 1:numel(emps) %Loop through each unique employee
%For the current employee (i.e. current inner-loop iteration),
%extract time information (column 3 of our partialArray)
time = partialArray(partialArray(:,1)==emps(j),2);
%If no time index corresponds to this, this employee has no time
%associated with the order
if(isempty(time))
time = 0;
end
y(i,j) = time; %Append time to bar chart plot matrix
end
end
%Generate plot
bar(y,'stacked');
legend(num2str(emps));
set(gca,'XTickLabel',num2str(orders));
xlabel('Orders');
ylabel('Time');
2 commentaires
Mishaal Aleem
le 8 Juil 2017
I have updated the code in the answer assuming use of R2016a.
The previous solution made use of double quotes (") which were introduced in R2017a and the string function which was introduced in R2016b. You can learn more about both of those on the string documentation.
If using R2016a, you can simply omit the explicit string conversion, since using the single quoted input will imply a string. So, for example, in line 6
typeIndex = find(strcmp(headers,'TYPE')); %Line 6 from code above
the second input to strcmp, 'TYPE', is of type string. The find will still find the correct index in headers corresponding to this string because each element in the headers cell array is itself a string, e.g. headers{2} = 'ORDER#', where 'ORDER#'is a string.
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!