극좌표 좌표축, 지리 좌표축 또는 여러 개의 좌표축을 사용하여 차트 개발하기
개발하려는 차트가 ChartContainer
기본 클래스의 서브클래스에 해당할 경우, getAxes
메서드는 단일 카테시안 axes 객체를 지원할 수 있습니다. 극좌표 좌표축, 지리 좌표축 또는 여러 개의 좌표축을 지원하려면 chart 객체에 저장된 TiledChartLayout
객체의 자식이 되도록 좌표축을 만들고 구성해야 합니다.
단일 polar axes 객체 또는 geographic axes 객체 만들기
단일 polar axes 객체 또는 geographic axes 객체를 차트에 포함시키려면 다음을 수행하십시오.
좌표축을 저장하는 프라이빗 속성을 정의합니다.
setup
메서드에서 다음을 수행합니다.
예를 들어, 다음은 polar axes 객체를 포함하는 기본적인 클래스입니다.
classdef SimplePolar < matlab.graphics.chartcontainer.ChartContainer properties(Access = private,Transient,NonCopyable) PolarAx matlab.graphics.axis.PolarAxes end methods(Access = protected) function setup(obj) % Get the layout and create the axes tcl = getLayout(obj); obj.PolarAx = polaraxes(tcl); % Other setup code % ... end function update(obj) % Update the chart % ... end end end
여러 axes 객체의 타일 형식 배열 만들기
여러 개의 좌표축을 타일 형식 배열로 표시하려면 다음을 수행하십시오.
axes 객체를 저장하는 프라이빗 속성을 여러 개 정의합니다. 여러 axes 객체로 구성된 배열을 저장하는 속성 하나를 정의할 수도 있습니다.
setup
메서드에서 다음을 수행합니다.
예를 들어, 다음은 두 개의 카테시안 좌표축을 포함하는 기본적인 클래스입니다.
classdef TwoAxesChart < matlab.graphics.chartcontainer.ChartContainer properties(Access = private,Transient,NonCopyable) Ax1 matlab.graphics.axis.Axes Ax2 matlab.graphics.axis.Axes end methods(Access = protected) function setup(obj) % Get the layout and set the grid size tcl = getLayout(obj); tcl.GridSize = [2 1]; % Create the axes obj.Ax1 = axes(tcl); obj.Ax2 = axes(tcl); % Move the second axes to the second tile obj.Ax2.Layout.Tile = 2; end function update(obj) % Update the chart % ... end end end
예제: 지리 좌표축 및 카테시안 좌표축을 포함하는 차트
이 예제에서는 두 개의 좌표축을 사용하여 지리 데이터와 범주형 데이터를 시각화하기 위해 차트의 클래스를 정의하는 방법을 보여줍니다. 왼쪽 좌표축은 여러 무선 통신 기지국의 위치를 보여주는 지도를 포함합니다. 오른쪽 좌표축은 범주별로 기지국의 분포를 보여줍니다.
아래에 있는 TowerChart
클래스 정의는 다음을 수행하는 방법을 보여줍니다.
테이블을 저장하는
TowerData
라는 퍼블릭 속성을 정의합니다.mustHaveRequiredVariables
라는 로컬 함수를 사용하여 테이블 내용의 유효성을 검사합니다.좌표축을 저장하는
MapAxes
및HistogramAxes
라는 2개의 프라이빗 속성을 정의합니다.TiledChartLayout
객체를 가져오고 레이아웃의 그리드 크기를 지정하고 좌표축의 위치를 지정하는setup
메서드를 구현합니다.
클래스를 정의하려면 다음 코드를 편집기에 복사하고 쓰기 가능한 폴더에 TowerChart.m
이라는 이름으로 저장하십시오.
classdef TowerChart < matlab.graphics.chartcontainer.ChartContainer properties TowerData (:,:) table {mustHaveRequiredVariables} = table([],... [],[],'VariableNames',{'STRUCTYPE','Latitude','Longitude'}) end properties (Access = private,Transient,NonCopyable) MapAxes matlab.graphics.axis.GeographicAxes HistogramAxes matlab.graphics.axis.Axes ScatterObject matlab.graphics.chart.primitive.Scatter HistogramObject matlab.graphics.chart.primitive.categorical.Histogram end methods (Access = protected) function setup(obj) % Configure layout and create axes tcl = getLayout(obj); tcl.GridSize = [1 2]; obj.MapAxes = geoaxes(tcl); obj.HistogramAxes = axes(tcl); % Move histogram axes to second tile obj.HistogramAxes.Layout.Tile = 2; % Create Scatter and Histogram objects obj.ScatterObject = geoscatter(obj.MapAxes,NaN,NaN,'.'); obj.HistogramObject = histogram(obj.HistogramAxes,categorical.empty,... 'Orientation','horizontal'); % Add titles to the axes title(obj.MapAxes,"Tower Locations") title(obj.HistogramAxes,"Tower Types") xlabel(obj.HistogramAxes,"Number of Towers") end function update(obj) % Update Scatter object obj.ScatterObject.LatitudeData = obj.TowerData.Latitude; obj.ScatterObject.LongitudeData = obj.TowerData.Longitude; % Get tower types from STRUCTYPE table variable towertypes = obj.TowerData.STRUCTYPE; % Check for empty towertypes before updating histogram if ~isempty(towertypes) obj.HistogramObject.Data = towertypes; obj.HistogramObject.Categories = categories(towertypes); else obj.HistogramObject.Data = categorical.empty; end end end end function mustHaveRequiredVariables(tbl) % Return error if table does not have required variables assert(all(ismember({'STRUCTYPE','Latitude','Longitude'},... tbl.Properties.VariableNames)),... 'MATLAB:TowerChart:InvalidTable',... 'Table must have STRUCTYPE, Latitude, and Longitude variables.'); end
클래스 파일을 저장한 후 cellularTowers.mat
에 저장된 테이블을 불러옵니다. 그런 다음 테이블을 이름-값 쌍의 인수로 TowerChart
생성자 메서드에 전달하여 차트의 인스턴스를 만듭니다.
load cellularTowers.mat TowerChart('TowerData',cellularTowers);