www.gusucode.com > graphics 案例源码程序 matlab代码 > graphics/OffsetPieSliceWithGreatestContributionExample.m

    %% Offset Pie Slice with Greatest Contribution
% This example shows how to create a pie graph and automatically offset the
% pie slice with the greatest contribution.
%
% Set up a three-column array, |X|, so that each column contains yearly sales
% data for a specific product over a 5-year period.

% Copyright 2015 The MathWorks, Inc.


X = [19.3, 22.1, 51.6
     34.2, 70.3, 82.4
     61.4, 82.9, 90.8
     50.5, 54.9, 59.1
     29.4, 36.3, 47.0];
 
%%
% Calculate the total sales for each product over the 5-year period by
% taking the sum of each column. Store the results in
% |product_totals|. 

product_totals = sum(X);

%% 
% Use the |max| function to find the largest element in |product_totals| and
% return the index of this element, |ind|. 

[c,ind] = max(product_totals);

%%
% Use the |pie| function input argument, |explode|, to offset a pie slice.
% The |explode| argument is a vector of zero and nonzero values where the
% nonzero values indicate the slices to offset. Initialize |explode| as a
% three-element vector of zeros.

explode = zeros(1,3);

%%
% Use the index of the maximum element in |product_totals| to set the corresponding |explode|
% element to 1.
explode(ind) = 1;

%%
% Create a pie chart of the sales totals for each product and offset the
% pie slice for the product with the largest total sales.

figure
pie(product_totals,explode)
title('Sales Contributions of Three Products')