Main Content

matlab.graphics.chartcontainer.ChartContainer class

Package: matlab.graphics.chartcontainer

Base class for developing chart objects

Since R2019b

Description

matlab.graphics.chartcontainer.ChartContainer is a class for defining your own class of chart objects. When you define a subclass of this base class, you can use the ChartContainer properties and methods to build your chart objects. Specify this syntax on the first line of your class definition file to directly inherit from the ChartContainer class, where MyChart is the name of your object:

classdef MyChart < matlab.graphics.chartcontainer.ChartContainer

The matlab.graphics.chartcontainer.ChartContainer class is a handle class.

Class Attributes

Abstract
true
HandleCompatible
true

For information on class attributes, see Class Attributes.

Properties

expand all

Chart size and location, excluding the margins for decorations such as axis labels and tick marks. Specify this property as a vector of form [left bottom width height]. The values are in the units specified by the Units property.

  • left — Distance from the left edge of the parent container to the inner-left edge of the chart that excludes the margins. Typically, the parent container is a figure, panel, or tab.

  • bottom — Distance from the bottom edge of the parent container to the inner-bottom edge of the chart that excludes the margins.

  • width — Width of the chart, excluding the margins.

  • height — Height of the chart, excluding the margins.

Note

Setting this property has no effect when the parent of the chart is a TiledChartLayout.

Attributes:

GetAccess
public
SetAccess
public
Dependent
true
NonCopyable
true

Chart size and location, including the margins for decorations such as axis labels and tick marks. Specify this property as a vector of form [left bottom width height]. The values are in the units specified by the Units property.

  • left — Distance from the left edge of the parent container to the outer-left edge of the chart that includes the margins. Typically, the parent container is a figure, panel, or tab.

  • bottom — Distance from the bottom edge of the parent container to the outer-bottom edge of the chart that includes the margins.

  • width — Width of chart, including the margins.

  • height — Height of chart, including the margins.

Note

Setting this property has no effect when the parent of the chart is a TiledChartLayout.

Attributes:

GetAccess
public
SetAccess
public
Dependent
true
NonCopyable
true

Chart size and location, excluding the margins for decorations such as axis labels and tick marks. Specify this property as a vector of form [left bottom width height]. This property is equivalent to the InnerPosition property.

Note

Setting this property has no effect when the parent of the chart is a TiledChartLayout.

Attributes:

GetAccess
public
SetAccess
public
Dependent
true
NonCopyable
true

Position property to hold constant when adding, removing, or changing decorations, specified as one of the following values:

  • 'outerposition' — The OuterPosition property remains constant when you add, remove, or change decorations such as a title or an axis label. If any positional adjustments are needed, MATLAB® adjusts the InnerPosition property.

  • 'innerposition' — The InnerPosition property remains constant when you add, remove, or change decorations such as a title or an axis label. If any positional adjustments are needed, MATLAB adjusts the OuterPosition property.

Note

Setting this property has no effect when the parent container is a TiledChartLayout object.

Attributes:

GetAccess
public
SetAccess
public
Dependent
true
NonCopyable
true

Position units, specified as a value from the following table. To change the position of the chart in specific units, set the Units property before specifying the Position property. If you specify the Units and Position properties in a single command (using name-value pairs), be sure to specify Units before Position.

UnitsDescription
'normalized' (default)Normalized with respect to the parent container, which is typically the figure, panel, or tab. The lower left corner of the container maps to (0,0), and the upper right corner maps to (1,1).
'inches'Inches.
'centimeters'Centimeters.
'characters'

Based on the default font of the graphics root object:

  • Character width = width of letter x.

  • Character height = distance between the baselines of two lines of text.

'points'Typography points. One point equals 1/72 inch.
'pixels'

Distances in pixels are independent of your system resolution on Windows® and Macintosh systems:

  • On Windows systems, a pixel is 1/96th of an inch.

  • On Macintosh systems, a pixel is 1/72nd of an inch.

On Linux® systems, the size of a pixel is determined by your system resolution.

Attributes:

GetAccess
public
SetAccess
public
Dependent
true
NonCopyable
true

Layout options, specified as a TiledChartLayoutOptions object. This property specifies options when an instance of your chart is a child of a tiled chart layout. If the instance is not a child of a tiled chart layout (for example, it is a child of a figure or panel), then this property is empty and has no effect. Otherwise, you can position the chart within the layout by setting the Tile and TileSpan properties on the TiledChartLayoutOptions object.

For example, this code places chart object c into the third tile of a tiled chart layout.

c.Layout.Tile = 3;

To make the chart span multiple tiles, specify the TileSpan property as a two-element vector. For example, this chart spans 2 rows and 3 columns of tiles.

c.Layout.TileSpan = [2 3];

Note

Tiled chart layouts are not supported for the axes returned by the getAxes method. Instead, you can place an instance of your chart into a tiled chart layout.

Attributes:

GetAccess
public
SetAccess
public

State of visibility, specified as 'on' or 'off', or as numeric or logical 1 (true) or 0 (false). A value of 'on' is equivalent to true, and 'off' is equivalent to false. Thus, you can use the value of this property as a logical value. The value is stored as an on/off logical value of type matlab.lang.OnOffSwitchState.

  • 'on' — Display the chart.

  • 'off' — Hide the chart without deleting it. You still can access the properties of chart when it is not visible.

Attributes:

GetAccess
public
SetAccess
public
Dependent
true
GetObservable
true
SetObservable
true

Parent container of the chart, specified as a Figure, Panel, Tab, or TiledChartLayout object.

Attributes:

GetAccess
public
SetAccess
public
Transient
true
Dependent
true
GetObservable
true
SetObservable
true
NonCopyable
true

Visibility of the chart object handle in its parent's list of children, specified as one of these values:

  • 'on' — Chart object handle is always visible.

  • 'off' — Chart object handle is invisible at all times. This option is useful for preventing unintended changes to the chart by another function. To temporarily hide the handle during the execution of that function, set the HandleVisibility to 'off'.

  • 'callback' — Chart object handle is visible from within callbacks or functions invoked by callbacks, but not from within functions invoked from the command line. This option blocks access to the object in the Command Window, but it allows callback functions to access it.

If the object is not listed in the Children property of the chart's parent object, then functions that get object handles by searching the object hierarchy or querying handle properties cannot return it. This includes get, findobj, gca, gcf, gco, newplot, cla, clf, and close.

Hidden object handles are still valid. Set the root ShowHiddenHandles property to 'on' to list all object handles, regardless of their HandleVisibility property setting.

Attributes:

GetAccess
public
SetAccess
public
Dependent
true
GetObservable
true
SetObservable
true

Methods

expand all

Examples

collapse all

Define a class called SmoothPlot that plots a set of data using a dotted blue line with a smoothed version of the line.

To define the class, create a file called SmoothPlot.m that contains the following class definition with these features:

  • XData and YData public properties that store the x- and y-coordinate data for the original line.

  • SmoothColor and SmoothWidth public properties that control the color and width of the smooth line.

  • OriginalLine and SmoothLine private properties that store the Line objects for original and smoothed data.

  • A setup method that initializes OriginalLine and SmoothLine.

  • An update method that updates the plot when the user changes the value of a property.

  • A createSmoothData method that calculates a smoothed version of YData.

classdef SmoothPlot < matlab.graphics.chartcontainer.ChartContainer
    properties
        XData (1,:) double = NaN
        YData (1,:) double = NaN
        SmoothColor (1,3) double {mustBeGreaterThanOrEqual(SmoothColor,0),...
            mustBeLessThanOrEqual(SmoothColor,1)} = [0.9290 0.6940 0.1250]
        SmoothWidth (1,1) double = 2
    end
    properties(Access = private,Transient,NonCopyable)
        OriginalLine (1,1) matlab.graphics.chart.primitive.Line
        SmoothLine (1,1) matlab.graphics.chart.primitive.Line
    end
    
    methods(Access = protected)
        function setup(obj)
            % Create the axes
            ax = getAxes(obj);
            
            % Create the original and smooth lines
            obj.OriginalLine = plot(ax,NaN,NaN,'LineStyle',':');
            hold(ax,'on')
            obj.SmoothLine = plot(ax,NaN,NaN);
            hold(ax,'off')
        end
        function update(obj)
            % Update line data
            obj.OriginalLine.XData = obj.XData;
            obj.OriginalLine.YData = obj.YData;
            obj.SmoothLine.XData = obj.XData;
            obj.SmoothLine.YData = createSmoothData(obj);
            
            % Update line color and width
            obj.SmoothLine.Color = obj.SmoothColor;
            obj.SmoothLine.LineWidth = obj.SmoothWidth;
        end
        function sm = createSmoothData(obj)
            % Calculate smoothed data
            v = ones(1,10)*0.1;
            sm = conv(obj.YData,v,'same');
        end
    end
end

Next, create a pair of x and y vectors. Plot x and y by calling the SmoothPlot constructor method, which is provided by the ChartContainer class. Specify the 'XData' and 'YData' name-value pair arguments and return the object as c.

x = 1:1:100;
y = 10*sin(x./5) + 8*sin(10.*x + 0.5);
c = SmoothPlot('XData',x,'YData',y);

Use c to change the color of the smooth line to red.

c.SmoothColor = [1 0 0];

More About

expand all

Version History

Introduced in R2019b

expand all